[libvirt-designer][PATCH v2 4/4] Implement resources setting

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

 



Users can choose between minimum and recommended values
for VCPU count and amount of RAM. Moreover, recommended
values should inherit defaults from the minimum.
---
 examples/virtxml.c                         |   12 ++++
 libvirt-designer/libvirt-designer-domain.c |   91 ++++++++++++++++++++++++++++
 libvirt-designer/libvirt-designer-domain.h |    8 +++
 libvirt-designer/libvirt-designer.sym      |    1 +
 4 files changed, 112 insertions(+), 0 deletions(-)

diff --git a/examples/virtxml.c b/examples/virtxml.c
index cfe25ff..2908667 100644
--- a/examples/virtxml.c
+++ b/examples/virtxml.c
@@ -496,6 +496,7 @@ main(int argc, char *argv[])
     static char *platform_str = NULL;
     static char *arch_str = NULL;
     static char *connect_uri = NULL;
+    static char *resources = NULL;
     GOptionContext *context;
 
     static GOptionEntry entries[] =
@@ -516,6 +517,8 @@ main(int argc, char *argv[])
             "add disk to domain with PATH being source and FORMAT its format", "PATH[,FORMAT]"},
         {"interface", 'i', 0, G_OPTION_ARG_CALLBACK, add_iface_str,
             "add interface with NETWORK source. Possible ARGs: mac, link={up,down}", "NETWORK[,ARG=VAL]"},
+        {"resources", 'r', 0, G_OPTION_ARG_STRING, &resources,
+            "Set minimal or recommended values for cpu count and RAM amount", "{minimal|recommended}"},
         {NULL}
     };
 
@@ -572,6 +575,15 @@ main(int argc, char *argv[])
         CHECK_ERROR;
     }
 
+    if (resources) {
+        gvir_designer_domain_setup_resources(domain, resources, &error);
+        CHECK_ERROR;
+    } else {
+        gvir_designer_domain_setup_resources(domain,
+                                             GVIR_DESIGNER_DOMAIN_RESOURCES_RECOMMENDED,
+                                             NULL);
+    }
+
     g_list_foreach(disk_str_list, add_disk, domain);
 
     g_list_foreach(iface_str_list, add_iface, domain);
diff --git a/libvirt-designer/libvirt-designer-domain.c b/libvirt-designer/libvirt-designer-domain.c
index 194fdf5..de4582c 100644
--- a/libvirt-designer/libvirt-designer-domain.c
+++ b/libvirt-designer/libvirt-designer-domain.c
@@ -1033,3 +1033,94 @@ gvir_designer_domain_add_interface_network(GVirDesignerDomain *design,
 
     return ret;
 }
+
+
+static void
+gvir_designer_domain_get_resources(OsinfoResourcesList *res_list,
+                                   const gchar *design_arch,
+                                   gint *design_n_cpus,
+                                   gint64 *design_ram)
+{
+    GList *elem_list, *elem_iterator;
+
+    if (!res_list)
+        return;
+
+    elem_list = osinfo_list_get_elements(OSINFO_LIST(res_list));
+    for (elem_iterator = elem_list; elem_iterator; elem_iterator = elem_iterator->next) {
+        OsinfoResources *res = OSINFO_RESOURCES(elem_iterator->data);
+        const char *arch = osinfo_resources_get_architecture(res);
+        gint n_cpus = -1;
+        gint64 ram = -1;
+
+        if (g_str_equal(arch, "all") ||
+            g_str_equal(arch, design_arch)) {
+            n_cpus = osinfo_resources_get_n_cpus(res);
+            ram =  osinfo_resources_get_ram(res);
+            if (n_cpus > 0) {
+                *design_n_cpus = n_cpus;
+            }
+            if (ram > 0) {
+                /* libosinfo returns RAM in B, libvirt-gconfig takes it in KB */
+                *design_ram = ram / 1024;
+            }
+            break;
+        }
+    }
+}
+
+
+/**
+ * gvir_designer_domain_setup_resources:
+ * @design: (transfer none): the domain designer intance
+ * @req: (transfer none): requirements to set
+ *
+ * Set minimal or recommended resources on @design.
+ *
+ * Returns: (transfer none): TRUE when successfully set, FALSE otherwise.
+ */
+gboolean gvir_designer_domain_setup_resources(GVirDesignerDomain *design,
+                                              const gchar *req,
+                                              GError **error)
+{
+    g_return_val_if_fail(GVIR_DESIGNER_IS_DOMAIN(design), FALSE);
+    gboolean ret = FALSE;
+    OsinfoResourcesList *res_list_min = NULL, *res_list_rec = NULL;
+    GVirConfigDomainOs *os = gvir_config_domain_get_os(design->priv->config);
+    const gchar *design_arch = os ? gvir_config_domain_os_get_arch(os) : "";
+    gint n_cpus = -1;
+    gint64 ram = -1;
+
+    /* If user request recommended settings those may just override
+     * only a few settings when compared to minimal. So we must implement
+     * inheritance here. */
+    res_list_min = osinfo_os_get_minimum_resources(design->priv->os);
+    res_list_rec = osinfo_os_get_recommended_resources(design->priv->os);
+    if (g_str_equal(req, GVIR_DESIGNER_DOMAIN_RESOURCES_MINIMAL)) {
+        gvir_designer_domain_get_resources(res_list_min, design_arch,
+                                           &n_cpus, &ram);
+    } else if (g_str_equal(req, GVIR_DESIGNER_DOMAIN_RESOURCES_RECOMMENDED)) {
+        gvir_designer_domain_get_resources(res_list_min, design_arch,
+                                           &n_cpus, &ram);
+        gvir_designer_domain_get_resources(res_list_rec, design_arch,
+                                           &n_cpus, &ram);
+    } else {
+        g_set_error(error, GVIR_DESIGNER_DOMAIN_ERROR, 0,
+                    "Unknown resources argument: '%s'", req);
+        goto cleanup;
+    }
+
+    if ((n_cpus <= 0) && (ram <= 0)) {
+        g_set_error(error, GVIR_DESIGNER_DOMAIN_ERROR, 0,
+                    "Unable to find resources in libosinfo database");
+        goto cleanup;
+    }
+
+    if (n_cpus > 0)
+        gvir_config_domain_set_vcpus(design->priv->config, n_cpus);
+    if (ram > 0)
+        gvir_config_domain_set_memory(design->priv->config, ram);
+
+cleanup:
+    return ret;
+}
diff --git a/libvirt-designer/libvirt-designer-domain.h b/libvirt-designer/libvirt-designer-domain.h
index 5097393..28a1d06 100644
--- a/libvirt-designer/libvirt-designer-domain.h
+++ b/libvirt-designer/libvirt-designer-domain.h
@@ -39,6 +39,9 @@ G_BEGIN_DECLS
 #define GVIR_DESIGNER_IS_DOMAIN_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GVIR_DESIGNER_TYPE_DOMAIN))
 #define GVIR_DESIGNER_DOMAIN_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GVIR_DESIGNER_TYPE_DOMAIN, GVirDesignerDomainClass))
 
+#define GVIR_DESIGNER_DOMAIN_RESOURCES_MINIMAL "minimal"
+#define GVIR_DESIGNER_DOMAIN_RESOURCES_RECOMMENDED "recommended"
+
 typedef struct _GVirDesignerDomain GVirDesignerDomain;
 typedef struct _GVirDesignerDomainPrivate GVirDesignerDomainPrivate;
 typedef struct _GVirDesignerDomainClass GVirDesignerDomainClass;
@@ -104,6 +107,11 @@ GVirConfigDomainDisk *gvir_designer_domain_add_disk_device(GVirDesignerDomain *d
 GVirConfigDomainInterface *gvir_designer_domain_add_interface_network(GVirDesignerDomain *design,
                                                                       const char *network,
                                                                       GError **error);
+
+gboolean gvir_designer_domain_setup_resources(GVirDesignerDomain *design,
+                                              const gchar *req,
+                                              GError **error);
+
 G_END_DECLS
 
 #endif /* __LIBVIRT_DESIGNER_DOMAIN_H__ */
diff --git a/libvirt-designer/libvirt-designer.sym b/libvirt-designer/libvirt-designer.sym
index 317a07b..0dde9a6 100644
--- a/libvirt-designer/libvirt-designer.sym
+++ b/libvirt-designer/libvirt-designer.sym
@@ -13,6 +13,7 @@ LIBVIRT_DESIGNER_0.0.1 {
 	gvir_designer_domain_add_disk_file;
 	gvir_designer_domain_add_disk_device;
 	gvir_designer_domain_add_interface_network;
+	gvir_designer_domain_setup_resources;
 
 	gvir_designer_domain_setup_machine;
 	gvir_designer_domain_setup_machine_full;
-- 
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]