2 * Copyright (C) 2010 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include <libvirt/libvirt.h>
29 #include <libvirt/virterror.h>
34 make_test_xml (FILE *fp, const char *cwd)
37 "<?xml version=\"1.0\"?>\n"
39 " <domain type='test'>\n"
40 " <name>guest</name>\n"
45 " <memory>524288</memory>\n"
47 " <disk type='file'>\n"
48 " <source file='%s/test1.img'/>\n"
49 " <target dev='hda'/>\n"
51 " <disk type='file'>\n"
52 " <driver name='qemu' type='raw'/>\n"
53 " <source file='%s/test2.img'/>\n"
54 " <target dev='hdb'/>\n"
56 " <disk type='file'>\n"
57 " <driver name='qemu' type='qcow2'/>\n"
58 " <source file='%s/test3.img'/>\n"
59 " <target dev='hdc'/>\n"
68 main (int argc, char *argv[])
78 char libvirt_uri[1024];
82 /* Create the libvirt XML and test images in the current directory. */
83 fp = fopen ("test.xml", "w");
88 make_test_xml (fp, cwd);
91 fp = fopen ("test1.img", "w");
98 fp = fopen ("test2.img", "w");
100 perror ("test2.img");
105 fp = fopen ("test3.img", "w");
107 perror ("test3.img");
112 /* Create the guestfs handle. */
113 g = guestfs_create ();
115 fprintf (stderr, "failed to create handle\n");
119 /* Create the libvirt connection. */
120 snprintf (libvirt_uri, sizeof libvirt_uri, "test://%s/test.xml", cwd);
121 conn = virConnectOpenReadOnly (libvirt_uri);
123 err = virGetLastError ();
124 fprintf (stderr, "could not connect to libvirt (code %d, domain %d): %s\n",
125 err->code, err->domain, err->message);
129 dom = virDomainLookupByName (conn, "guest");
131 err = virGetLastError ();
133 "no libvirt domain called '%s': %s\n", "guest", err->message);
137 r = guestfs_add_libvirt_dom (g, dom,
138 GUESTFS_ADD_LIBVIRT_DOM_READONLY, 1,
146 unlink ("test1.img");
147 unlink ("test2.img");
148 unlink ("test3.img");