[libvirt] [libvirt-designer][PATCH v2 4/4] domain: Introduce interface support

Christophe Fergeau cfergeau at redhat.com
Wed Sep 12 09:46:03 UTC 2012


On Mon, Sep 10, 2012 at 03:58:28PM +0200, Michal Privoznik wrote:
> Let users add NICs to domains.
> ---
>  examples/virtxml.c                         |   78 ++++++++++++++++++++++++++
>  libvirt-designer/libvirt-designer-domain.c |   83 ++++++++++++++++++++++++++++
>  libvirt-designer/libvirt-designer-domain.h |    3 +
>  libvirt-designer/libvirt-designer.sym      |    1 +
>  4 files changed, 165 insertions(+), 0 deletions(-)
> 
> diff --git a/examples/virtxml.c b/examples/virtxml.c
> index 20e3f3c..df83f57 100644
> --- a/examples/virtxml.c
> +++ b/examples/virtxml.c
> @@ -35,6 +35,7 @@
>  #include <unistd.h>
>  
>  GList *disk_str_list = NULL;
> +GList *iface_str_list = NULL;
>  
>  #define print_error(...) \
>      print_error_impl(__FUNCTION__, __LINE__, __VA_ARGS__)
> @@ -215,6 +216,79 @@ add_disk_str(const gchar *option_name,
>      return TRUE;
>  }
>  
> +static void
> +add_iface(gpointer data,
> +          gpointer user_data)
> +{
> +    GVirDesignerDomain *domain = (GVirDesignerDomain *) user_data;
> +    char *network = (char *) data;
> +    char *param = NULL;
> +    GVirConfigDomainInterface *iface = NULL;
> +    GError *error = NULL;
> +
> +    param = strchr(network, ',');
> +    if (param) {
> +        *param = '\0';
> +        param++;
> +    }

glib provides a g_strsplit (and plenty of useful functions in
http://developer.gnome.org/glib/stable/glib-String-Utility-Functions.html )

> +
> +    iface = gvir_designer_domain_add_interface_network(domain, network, &error);
> +    if (error) {
> +        print_error("%s", error->message);
> +        exit(EXIT_FAILURE);

An alternative here can be g_error

> +    }
> +
> +    while (param && *param) {
> +        char *key = param;
> +        char *val;
> +        GVirConfigDomainInterfaceLinkState link;
> +
> +        /* move to next token */
> +        param = strchr(param, ',');
> +        if (param) {
> +            *param = '\0';
> +            param++;
> +        }
> +
> +        /* parse token */
> +        val = strchr(key, '=');
> +        if (!val) {
> +            print_error("Invalid format: %s", key);
> +            exit(EXIT_FAILURE);
> +        }
> +
> +        *val = '\0';
> +        val++;
> +
> +        if (!strcmp(key, "mac")) {
> +            gvir_config_domain_interface_set_mac(iface, val);
> +        } else if (!strcmp(key, "link")) {
> +            if (!strcmp(val, "up")) {
> +                link = GVIR_CONFIG_DOMAIN_INTERFACE_LINK_STATE_UP;
> +            } else if (!strcmp(val, "down")) {
> +                link = GVIR_CONFIG_DOMAIN_INTERFACE_LINK_STATE_DOWN;
> +            } else {
> +                print_error("Unknown value: %s", val);
> +                exit(EXIT_FAILURE);
> +            }

If you find yourself doing similar parsing in several places, a function
similar to  gvir_config_genum_get_value can be really useful
( http://libvirt.org/git/?p=libvirt-glib.git;a=blob;f=libvirt-gconfig/libvirt-gconfig-helpers.c;h=87b13752158a376f5f39b3f51ad98020fc93f8a9;hb=HEAD#l264
)

Christophe
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20120912/30e046e5/attachment-0001.sig>


More information about the libvir-list mailing list