examples: Move and install shell examples
[libvirt/ericb.git] / examples / dommigrate / dommigrate.c
blob60cfb3fb8328d75ce76f9b3af6c64aac96d60787
1 /*
2 * dommigrate.c: This file is largely inspired from hellolibvirt and
3 * contains a trivial example that illustrate p2p domain
4 * migration with libvirt.
6 * Copyright (C) 2014 Cloudwatt
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library. If not, see
20 * <http://www.gnu.org/licenses/>.
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <libvirt/libvirt.h>
26 #include <libvirt/virterror.h>
29 static int
30 usage(char *prgn, int ret)
32 printf("Usage: %s <src uri> <dst uri> <domain name>\n", prgn);
33 return ret;
36 int
37 main(int argc, char *argv[])
39 char *src_uri, *dst_uri, *domname;
40 int ret = 0;
41 virConnectPtr conn = NULL;
42 virDomainPtr dom = NULL;
44 if (argc < 4) {
45 ret = usage(argv[0], 1);
46 goto out;
49 src_uri = argv[1];
50 dst_uri = argv[2];
51 domname = argv[3];
53 printf("Attempting to connect to the source hypervisor...\n");
54 conn = virConnectOpenAuth(src_uri, virConnectAuthPtrDefault, 0);
55 if (!conn) {
56 ret = 1;
57 fprintf(stderr, "No connection to the source hypervisor: %s.\n",
58 virGetLastErrorMessage());
59 goto out;
62 printf("Attempting to retrieve domain %s...\n", domname);
63 dom = virDomainLookupByName(conn, domname);
64 if (!dom) {
65 fprintf(stderr, "Failed to find domain %s.\n", domname);
66 goto cleanup;
69 printf("Attempting to migrate %s to %s...\n", domname, dst_uri);
70 if ((ret = virDomainMigrateToURI(dom, dst_uri,
71 VIR_MIGRATE_PEER2PEER,
72 NULL, 0)) != 0) {
73 fprintf(stderr, "Failed to migrate domain %s.\n", domname);
74 goto cleanup;
77 printf("Migration finished with success.\n");
79 cleanup:
80 if (dom != NULL)
81 virDomainFree(dom);
82 virConnectClose(conn);
84 out:
85 return ret;