In some cases telling OS version is redundant as ISO image with specified OS is passed some arguments later as disk. Don't require --os then. --- examples/virtxml.c | 110 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 105 insertions(+), 5 deletions(-) diff --git a/examples/virtxml.c b/examples/virtxml.c index b0c3a77..4ec9cd8 100644 --- a/examples/virtxml.c +++ b/examples/virtxml.c @@ -293,6 +293,98 @@ add_iface_str(const gchar *option_name, return TRUE; } +static OsinfoOs * +find_os(const gchar *os_str) +{ + OsinfoDb *db = get_default_osinfo_db(); + OsinfoOs *ret = NULL; + + if (!db) + return NULL; + + ret = osinfo_db_get_os(db, os_str); + + return ret; +} + +static OsinfoOs * +find_os_by_short_id(const char *short_id) +{ + OsinfoDb *db = get_default_osinfo_db(); + OsinfoOs *ret = NULL; + OsinfoOsList *oses = NULL; + GList *list, *list_iterator; + + if (!db) + return NULL; + + oses = osinfo_db_get_os_list(db); + + if (!oses) + goto cleanup; + + list = osinfo_list_get_elements(OSINFO_LIST(oses)); + for (list_iterator = list; list_iterator; list_iterator = list_iterator->next) { + const char *id = osinfo_entity_get_param_value(list_iterator->data, + OSINFO_PRODUCT_PROP_SHORT_ID); + + if (id && g_str_equal(id, short_id)) { + ret = OSINFO_OS(list_iterator->data); + g_object_ref(ret); + break; + } + } + + g_object_unref(oses); + +cleanup: + return ret; +} + +static OsinfoOs * +guess_os_from_disk(GList *disk_list) +{ + OsinfoOs *ret = NULL; + GList *tmp = g_list_first(disk_list); + OsinfoLoader *loader = osinfo_loader_new(); + OsinfoDb *db; + + osinfo_loader_process_default_path(loader, NULL); + db = osinfo_loader_get_db(loader); + + while (tmp) { + char *path = (char *) tmp->data; + char *sep = strchr(path, ','); + OsinfoMedia *media = NULL; + OsinfoMedia *matched_media = NULL; + + if (sep) { + path = g_strndup(path, sep-path); + } + + media = osinfo_media_create_from_location(path, NULL, NULL); + if (!media) { + tmp = g_list_next(tmp); + continue; + } + + ret = osinfo_db_guess_os_from_media(db, media, &matched_media); + + if (sep) + g_free(path); + + if (ret) { + g_object_ref(ret); + break; + } + + tmp = g_list_next(tmp); + } + + g_clear_object(&loader); + return ret; +} + #define CHECK_ERROR \ if (error) { \ print_error("%s", error->message); \ @@ -347,10 +439,6 @@ main(int argc, char *argv[]) g_print ("option parsing failed: %s\n", error->message); return EXIT_FAILURE; } - if (!os_str) { - print_error("Operating system was not specified"); - return EXIT_FAILURE; - } if (!platform_str) { print_error("Platform was not specified"); return EXIT_FAILURE; @@ -363,9 +451,21 @@ main(int argc, char *argv[]) caps = gvir_connection_get_capabilities(conn, &error); CHECK_ERROR; - os = osinfo_os_new(os_str); platform = osinfo_platform_new(platform_str); + if (os_str) { + os = find_os(os_str); + if (!os) + os = find_os_by_short_id(os_str); + } else { + os = guess_os_from_disk(disk_str_list); + } + + if (!os) { + print_error("Operating system could not be find or guessed"); + goto cleanup; + } + domain = gvir_designer_domain_new(os, platform, caps); gvir_designer_domain_setup_machine(domain, &error); -- 1.7.8.6 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list