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 | 23 +++++++ libvirt-designer/libvirt-designer-domain.c | 91 ++++++++++++++++++++++++++++ libvirt-designer/libvirt-designer-domain.h | 10 +++ libvirt-designer/libvirt-designer.sym | 2 + 4 files changed, 126 insertions(+), 0 deletions(-) diff --git a/examples/virtxml.c b/examples/virtxml.c index 623ff60..c582d0b 100644 --- a/examples/virtxml.c +++ b/examples/virtxml.c @@ -491,6 +491,8 @@ main(int argc, char *argv[]) static char *platform_str = NULL; static char *arch_str = NULL; static char *connect_uri = NULL; + static char *resources_str = NULL; + GVirDesignerDomainResources resources; GOptionContext *context; static GOptionEntry entries[] = @@ -511,6 +513,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_str, + "Set minimal or recommended values for cpu count and RAM amount", "{minimal|recommended}"}, {NULL} }; @@ -567,6 +571,25 @@ main(int argc, char *argv[]) CHECK_ERROR; } + if (resources_str) { + if (g_str_equal(resources_str, "minimal") || + g_str_equal(resources_str, "min")) + resources = GVIR_DESIGNER_DOMAIN_RESOURCES_MINIMAL; + else if (g_str_equal(resources_str, "recommended") || + g_str_equal(resources_str, "rec")) + resources = GVIR_DESIGNER_DOMAIN_RESOURCES_RECOMMENDED; + else { + print_error("Unknown value '%s' for resources", resources_str); + goto cleanup; + } + 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..1f734de 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 instance + * @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, + GVirDesignerDomainResources 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 (req ==GVIR_DESIGNER_DOMAIN_RESOURCES_MINIMAL) { + gvir_designer_domain_get_resources(res_list_min, design_arch, + &n_cpus, &ram); + } else if (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: '%d'", 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..7c837e0 100644 --- a/libvirt-designer/libvirt-designer-domain.h +++ b/libvirt-designer/libvirt-designer-domain.h @@ -39,6 +39,11 @@ 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)) +typedef enum { + GVIR_DESIGNER_DOMAIN_RESOURCES_MINIMAL, + GVIR_DESIGNER_DOMAIN_RESOURCES_RECOMMENDED, +} GVirDesignerDomainResources; + typedef struct _GVirDesignerDomain GVirDesignerDomain; typedef struct _GVirDesignerDomainPrivate GVirDesignerDomainPrivate; typedef struct _GVirDesignerDomainClass GVirDesignerDomainClass; @@ -104,6 +109,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, + GVirDesignerDomainResources 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..79db09f 100644 --- a/libvirt-designer/libvirt-designer.sym +++ b/libvirt-designer/libvirt-designer.sym @@ -13,6 +13,8 @@ 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_resources_get_type; 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