1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
/* libguestfs
* Copyright (C) 2010-2020 Red Hat Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <error.h>
#include <libvirt/libvirt.h>
#include <libvirt/virterror.h>
#include "guestfs.h"
#include "guestfs-utils.h"
static void
make_test_xml (FILE *fp, const char *cwd)
{
fprintf (fp,
"<?xml version=\"1.0\"?>\n"
"<node>\n"
" <domain type='test'>\n"
" <name>guest</name>\n"
" <os>\n"
" <type>hvm</type>\n"
" <boot dev='hd'/>\n"
" </os>\n"
" <memory>524288</memory>\n"
" <devices>\n"
" <disk type='file'>\n"
" <source file='%s/test-add-libvirt-dom-1.img'/>\n"
" <target dev='hda'/>\n"
" </disk>\n"
" <disk type='file'>\n"
" <driver name='qemu' type='raw'/>\n"
" <source file='%s/test-add-libvirt-dom-2.img'/>\n"
" <target dev='hdb'/>\n"
" </disk>\n"
" <disk type='file'>\n"
" <driver name='qemu' type='qcow2'/>\n"
" <source file='%s/test-add-libvirt-dom-3.img'/>\n"
" <target dev='hdc'/>\n"
" </disk>\n"
" </devices>\n"
" </domain>\n"
"</node>",
cwd, cwd, cwd);
}
int
main (int argc, char *argv[])
{
guestfs_h *g;
virConnectPtr conn;
virDomainPtr dom;
virErrorPtr err;
int r;
char *backend;
char cwd[1024];
FILE *fp;
char libvirt_uri[sizeof cwd + 64];
if (getcwd (cwd, sizeof cwd) == NULL)
error (EXIT_FAILURE, errno, "getcwd");
/* Create the guestfs handle. */
g = guestfs_create ();
if (g == NULL)
error (EXIT_FAILURE, errno, "guestfs_create");
backend = guestfs_get_backend (g);
if (STREQ (backend, "uml")) {
free (backend);
error (77, 0, "test skipped because UML backend does not support qcow2");
}
free (backend);
/* Create the libvirt XML and test images in the current
* directory.
*/
fp = fopen ("test-add-libvirt-dom.xml", "w");
if (fp == NULL)
error (EXIT_FAILURE, errno, "fopen: %s", "test-add-libvirt-dom.xml");
make_test_xml (fp, cwd);
fclose (fp);
if (guestfs_disk_create (g, "test-add-libvirt-dom-1.img", "raw",
1024*1024, -1) == -1)
exit (EXIT_FAILURE);
if (guestfs_disk_create (g, "test-add-libvirt-dom-2.img", "raw",
1024*1024, -1) == -1)
exit (EXIT_FAILURE);
if (guestfs_disk_create (g, "test-add-libvirt-dom-3.img", "qcow2",
1024*1024, -1) == -1)
exit (EXIT_FAILURE);
/* Create the libvirt connection. */
snprintf (libvirt_uri, sizeof libvirt_uri,
"test://%s/test-add-libvirt-dom.xml", cwd);
conn = virConnectOpenReadOnly (libvirt_uri);
if (!conn) {
err = virGetLastError ();
error (EXIT_FAILURE, 0,
"could not connect to libvirt (code %d, domain %d): %s",
err->code, err->domain, err->message);
}
dom = virDomainLookupByName (conn, "guest");
if (!dom) {
err = virGetLastError ();
error (EXIT_FAILURE, 0,
"no libvirt domain called '%s': %s", "guest", err->message);
}
r = guestfs_add_libvirt_dom (g, dom,
GUESTFS_ADD_LIBVIRT_DOM_READONLY, 1,
-1);
if (r == -1)
exit (EXIT_FAILURE);
if (r != 3)
error (EXIT_FAILURE, 0,
"incorrect number of disks added (%d, expected 3)", r);
guestfs_close (g);
virDomainFree (dom);
virConnectClose (conn);
unlink ("test-add-libvirt-dom.xml");
unlink ("test-add-libvirt-dom-1.img");
unlink ("test-add-libvirt-dom-2.img");
unlink ("test-add-libvirt-dom-3.img");
exit (EXIT_SUCCESS);
}
|