[libvirt] [PATCH 1/2] Add a new example to illustrate domain migration

Daniel P. Berrange berrange at redhat.com
Wed Apr 23 10:06:18 UTC 2014


On Sun, Apr 13, 2014 at 01:52:47PM +0200, Sahid Orentino Ferdjaoui wrote:
> This commit adds a new example to illustrate peer to
> peer domain migration with virDomainMigrateToURI.
> 

> diff --git a/examples/dommigrate/Makefile.am b/examples/dommigrate/Makefile.am
> new file mode 100644
> index 0000000..43b55fc
> --- /dev/null
> +++ b/examples/dommigrate/Makefile.am
> @@ -0,0 +1,26 @@
> +## Copyright (C) 2014 Cloudwatt
> +## Copyright (C) 2005-2013 Red Hat, Inc.
> +##
> +## This library is free software; you can redistribute it and/or
> +## modify it under the terms of the GNU Lesser General Public
> +## License as published by the Free Software Foundation; either
> +## version 2.1 of the License, or (at your option) any later version.
> +##
> +## This library is distributed in the hope that it will be useful,
> +## but WITHOUT ANY WARRANTY; without even the implied warranty of
> +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +## Lesser General Public License for more details.
> +##
> +## You should have received a copy of the GNU Lesser General Public
> +## License along with this library.  If not, see
> +## <http://www.gnu.org/licenses/>.

IMHO  Makefile.am rules aren't really copyright-able material so
we don't include copyright headers in them....

> +INCLUDES = \
> +	-I$(top_builddir)/include -I$(top_srcdir)/include	\
> +	-I$(top_builddir)/gnulib/lib -I$(top_srcdir)/gnulib/lib	\
> +	-I$(top_srcdir)/src -I$(top_srcdir)/src/util		\
> +	-I$(top_srcdir)
> +noinst_PROGRAMS = dommigrate
> +dommigrate_CFLAGS = $(WARN_CFLAGS)
> +dommigrate_SOURCES = dommigrate.c
> +dommigrate_LDADD = $(top_builddir)/src/libvirt.la
> diff --git a/examples/dommigrate/dommigrate.c b/examples/dommigrate/dommigrate.c
> new file mode 100644
> index 0000000..a8f951e
> --- /dev/null
> +++ b/examples/dommigrate/dommigrate.c
> @@ -0,0 +1,78 @@
> +/* This file is largely inspired from hellolibvirt and contains a trivial
> +   example that illustrate p2p domain migration with libvirt. */

But you should add the copyright header to this example code.

> +
> +#include <config.h>

You want to leave out config.h in the example programs, because we intend
that the example code can be compiler outside the libvirt source tree.

> +
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <libvirt/libvirt.h>
> +#include <libvirt/virterror.h>
> +
> +#include "virstring.h"

Likewise don't use virstring.h header, or the functions it contains - the
examples should restrict themselves to the main POSIX <string.h> function

It seems you only include this so you can use virStrToLong_i to parse the
domain ID number. I suggest just making the example use the domain name
instead of ID.

> +
> +static int
> +usage(char *prgn, int ret)
> +{
> +    printf("Usage: %s <src_uri> <dst_uri> <domain>\n", prgn);
> +    return ret;
> +}
> +
> +int
> +main(int argc, char *argv[])
> +{
> +    char *src_uri, *dst_uri;
> +    int ret = 0, id;
> +    virConnectPtr conn = NULL;
> +    virDomainPtr dom = NULL;
> +
> +    if (argc < 4) {
> +        ret = usage(argv[0], 1);
> +        goto out;
> +    }
> +
> +    src_uri = argv[1];
> +    dst_uri = argv[2];
> +    virStrToLong_i(argv[3], NULL, 10, &id);
> +
> +    printf("Attempting to connect to the source hypervisor\n");
> +    conn = virConnectOpenAuth(src_uri, virConnectAuthPtrDefault, 0);
> +    if (!conn) {
> +        ret = 1;
> +        fprintf(stderr, "No connection to the source hypervisor: %s\n",
> +                virGetLastErrorMessage());
> +        goto out;
> +    }
> +    src_uri = virConnectGetURI(conn);
> +    if (!src_uri) {
> +        ret = 1;
> +        fprintf(stderr, "Failed to get uri for the source connection: %s\n",
> +                virGetLastErrorMessage());
> +        goto disconnect;
> +    }

You don't do anything with 'src_uri' here, so I think you could
probably just remove these few lines.
> +
> +    printf("Attempting to retrieve domain id: %d\n", id);
> +    dom = virDomainLookupByID(conn, id);
> +    if (!dom) {
> +        fprintf(stderr, "Failed to find domain %d\n", id);
> +        goto disconnect;
> +    }
> +
> +    printf("Attempting to migrate to: %s\n", dst_uri);
> +    if ((ret = virDomainMigrateToURI(dom, dst_uri,
> +                                     VIR_MIGRATE_PEER2PEER,
> +                                     NULL, 0)) != 0) {
> +        fprintf(stderr, "Failed to migrate domain %d\n", id);
> +        goto disconnect;
> +    }
> +
> +    printf("Migration finished\n");
> +
> + disconnect:

Can you rename 'disconnect' to 'cleanup' since that's our more usual
naming convention

> +    if (dom != NULL)
> +        virDomainFree(dom);
> +    if (conn != NULL)
> +        virConnectClose(conn);
> +
> + out:
> +    return ret;
> +}


Basically looks like a good example to have though.

Regards,
Daniel
-- 
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|




More information about the libvir-list mailing list