[libvirt-designer][PATCH 3/4] virtxml: Detect OS from given ISO

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 |   76 ++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 71 insertions(+), 5 deletions(-)

diff --git a/examples/virtxml.c b/examples/virtxml.c
index 468bc2a..131c462 100644
--- a/examples/virtxml.c
+++ b/examples/virtxml.c
@@ -253,6 +253,67 @@ add_iface(gpointer data,
     }
 }
 
+static OsinfoOs *
+guess_os_from_disk(GList *disk_str_list)
+{
+    OsinfoOs *ret = NULL;
+    GList *tmp = g_list_first(disk_str_list);
+    OsinfoLoader *loader = osinfo_loader_new();
+    GError *error = NULL;
+    OsinfoDb *db;
+
+    osinfo_loader_process_default_path(loader, &error);
+    if (error) {
+        print_error("Error loading libosinfo data: %s", error->message);
+        goto cleanup;
+    }
+
+    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, &error);
+        if (error) {
+            /* don't report error but silently continue to next disk */
+            print_error("%s", error->message);
+            g_error_free(error);
+            error = NULL;
+            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);
+    }
+
+cleanup:
+    g_clear_object(&loader);
+    return ret;
+}
+
+static OsinfoPlatform *
+guess_platform_from_connect(virConnectPtr conn)
+{
+    return NULL;
+}
+
 #define CHECK_ERROR \
     if (error) {                            \
         print_error("%s", error->message);  \
@@ -371,10 +432,6 @@ main(int argc, char *argv[])
         }
     }
 
-    if (!os_str) {
-        print_error("Operating system was not specified");
-        exit(EXIT_FAILURE);
-    }
     if (!platform_str) {
         print_error("Platform was not specified");
         exit(EXIT_FAILURE);
@@ -391,10 +448,19 @@ main(int argc, char *argv[])
         goto cleanup;
     }
 
-    os = osinfo_os_new(os_str);
     platform = osinfo_platform_new(platform_str);
     caps = gvir_config_capabilities_new_from_xml(caps_str, NULL);
 
+    if (os_str) {
+        os = osinfo_os_new(os_str);
+    } else {
+        os = guess_os_from_disk(disk_str_list);
+        if (!os) {
+            print_error("Operating system was not specified and could not be guessed");
+            exit(EXIT_FAILURE);
+        }
+    }
+
     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


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]