2 * Copyright (C) 2010 Red Hat Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library 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 GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <libvirt/libvirt.h>
27 #include <libvirt/virterror.h>
31 #include <libxml/xpath.h>
32 #include <libxml/parser.h>
33 #include <libxml/tree.h>
37 #include "guestfs-internal.h"
38 #include "guestfs-internal-actions.h"
39 #include "guestfs_protocol.h"
41 #if defined(HAVE_LIBVIRT) && defined(HAVE_LIBXML2)
43 static void init_libxml2 (void) __attribute__((constructor));
48 /* I am told that you don't really need to call virInitialize ... */
54 struct guestfs___add_libvirt_dom_argv {
56 #define GUESTFS___ADD_LIBVIRT_DOM_READONLY_BITMASK (UINT64_C(1)<<0)
58 #define GUESTFS___ADD_LIBVIRT_DOM_IFACE_BITMASK (UINT64_C(1)<<1)
62 static int guestfs___add_libvirt_dom (guestfs_h *g, virDomainPtr dom, const struct guestfs___add_libvirt_dom_argv *optargs);
65 guestfs__add_domain (guestfs_h *g, const char *domain_name,
66 const struct guestfs_add_domain_argv *optargs)
69 virConnectPtr conn = NULL;
70 virDomainPtr dom = NULL;
72 const char *libvirturi;
75 struct guestfs___add_libvirt_dom_argv optargs2 = { .bitmask = 0 };
77 libvirturi = optargs->bitmask & GUESTFS_ADD_DOMAIN_LIBVIRTURI_BITMASK
78 ? optargs->libvirturi : NULL;
79 readonly = optargs->bitmask & GUESTFS_ADD_DOMAIN_READONLY_BITMASK
80 ? optargs->readonly : 0;
81 iface = optargs->bitmask & GUESTFS_ADD_DOMAIN_IFACE_BITMASK
82 ? optargs->iface : NULL;
84 /* Connect to libvirt, find the domain. */
85 conn = virConnectOpenReadOnly (libvirturi);
87 err = virGetLastError ();
88 error (g, _("could not connect to libvirt (code %d, domain %d): %s"),
89 err->code, err->domain, err->message);
93 dom = virDomainLookupByName (conn, domain_name);
95 err = virGetLastError ();
96 error (g, _("no libvirt domain called '%s': %s"),
97 domain_name, err->message);
102 optargs2.bitmask |= GUESTFS___ADD_LIBVIRT_DOM_READONLY_BITMASK;
103 optargs2.readonly = readonly;
106 optargs2.bitmask |= GUESTFS___ADD_LIBVIRT_DOM_IFACE_BITMASK;
107 optargs2.iface = iface;
110 r = guestfs___add_libvirt_dom (g, dom, &optargs2);
113 if (dom) virDomainFree (dom);
114 if (conn) virConnectClose (conn);
119 /* This was proposed as an external API, but it's not quite baked yet. */
121 guestfs___add_libvirt_dom (guestfs_h *g, virDomainPtr dom,
122 const struct guestfs___add_libvirt_dom_argv *optargs)
124 int r = -1, nr_added = 0, i;
126 xmlDocPtr doc = NULL;
127 xmlXPathContextPtr xpathCtx = NULL;
128 xmlXPathObjectPtr xpathObj = NULL;
134 cmdline_pos = guestfs___checkpoint_cmdline (g);
136 readonly = optargs->bitmask & GUESTFS___ADD_LIBVIRT_DOM_READONLY_BITMASK
137 ? optargs->readonly : 0;
138 iface = optargs->bitmask & GUESTFS___ADD_LIBVIRT_DOM_IFACE_BITMASK
139 ? optargs->iface : NULL;
143 if (virDomainGetInfo (dom, &info) == -1) {
144 err = virGetLastError ();
145 error (g, _("error getting domain info: %s"), err->message);
148 if (info.state != VIR_DOMAIN_SHUTOFF) {
149 error (g, _("error: domain is a live virtual machine.\nYou must use readonly access because write access to a running virtual machine\ncan cause disk corruption."));
155 xml = virDomainGetXMLDesc (dom, 0);
158 err = virGetLastError ();
159 error (g, _("error reading libvirt XML information: %s"),
164 /* Now the horrible task of parsing out the fields we need from the XML.
165 * http://www.xmlsoft.org/examples/xpath1.c
167 doc = xmlParseMemory (xml, strlen (xml));
169 error (g, _("unable to parse XML information returned by libvirt"));
173 xpathCtx = xmlXPathNewContext (doc);
174 if (xpathCtx == NULL) {
175 error (g, _("unable to create new XPath context"));
179 /* This gives us a set of all the <disk> nodes. */
180 xpathObj = xmlXPathEvalExpression (BAD_CAST "//devices/disk", xpathCtx);
181 if (xpathObj == NULL) {
182 error (g, _("unable to evaluate XPath expression"));
186 xmlNodeSetPtr nodes = xpathObj->nodesetval;
187 for (i = 0; i < nodes->nodeNr; ++i) {
188 xmlXPathObjectPtr xptype;
190 /* Change the context to the current <disk> node.
191 * DV advises to reset this before each search since older versions of
192 * libxml2 might overwrite it.
194 xpathCtx->node = nodes->nodeTab[i];
196 /* Filename can be in <source dev=..> or <source file=..> attribute.
197 * Check the <disk type=..> attribute first to find out which one.
199 xptype = xmlXPathEvalExpression (BAD_CAST "./@type", xpathCtx);
200 if (xptype == NULL ||
201 xptype->nodesetval == NULL ||
202 xptype->nodesetval->nodeNr == 0) {
203 xmlXPathFreeObject (xptype);
204 continue; /* no type attribute, skip it */
206 assert (xptype->nodesetval->nodeTab[0]);
207 assert (xptype->nodesetval->nodeTab[0]->type == XML_ATTRIBUTE_NODE);
208 xmlAttrPtr attr = (xmlAttrPtr) xptype->nodesetval->nodeTab[0];
209 char *type = (char *) xmlNodeListGetString (doc, attr->children, 1);
211 xmlXPathObjectPtr xpfilename;
213 if (STREQ (type, "file")) { /* type = "file" so look at source/@file */
216 xpathCtx->node = nodes->nodeTab[i];
217 xpfilename = xmlXPathEvalExpression (BAD_CAST "./source/@file", xpathCtx);
218 if (xpfilename == NULL ||
219 xpfilename->nodesetval == NULL ||
220 xpfilename->nodesetval->nodeNr == 0) {
221 xmlXPathFreeObject (xpfilename);
222 continue; /* disk filename not found, skip this */
224 } else if (STREQ (type, "block")) { /* type = "block", use source/@dev */
227 xpathCtx->node = nodes->nodeTab[i];
228 xpfilename = xmlXPathEvalExpression (BAD_CAST "./source/@dev", xpathCtx);
229 if (xpfilename == NULL ||
230 xpfilename->nodesetval == NULL ||
231 xpfilename->nodesetval->nodeNr == 0) {
232 xmlXPathFreeObject (xpfilename);
233 continue; /* disk filename not found, skip this */
237 continue; /* type <> "file" or "block", skip it */
240 assert (xpfilename->nodesetval->nodeTab[0]);
241 assert (xpfilename->nodesetval->nodeTab[0]->type == XML_ATTRIBUTE_NODE);
242 attr = (xmlAttrPtr) xpfilename->nodesetval->nodeTab[0];
243 char *filename = (char *) xmlNodeListGetString (doc, attr->children, 1);
245 /* Get the disk format (may not be set). */
246 xmlXPathObjectPtr xpformat;
248 xpathCtx->node = nodes->nodeTab[i];
249 xpformat = xmlXPathEvalExpression (BAD_CAST "./driver/@type", xpathCtx);
251 if (xpformat != NULL &&
252 xpformat->nodesetval &&
253 xpformat->nodesetval->nodeNr > 0) {
254 assert (xpformat->nodesetval->nodeTab[0]);
255 assert (xpformat->nodesetval->nodeTab[0]->type == XML_ATTRIBUTE_NODE);
256 attr = (xmlAttrPtr) xpformat->nodesetval->nodeTab[0];
257 format = (char *) xmlNodeListGetString (doc, attr->children, 1);
260 /* Add the disk, with optional format. */
261 struct guestfs_add_drive_opts_argv optargs2 = { .bitmask = 0 };
263 optargs2.bitmask |= GUESTFS_ADD_DRIVE_OPTS_READONLY_BITMASK;
264 optargs2.readonly = readonly;
267 optargs2.bitmask |= GUESTFS_ADD_DRIVE_OPTS_FORMAT_BITMASK;
268 optargs2.format = format;
271 optargs2.bitmask |= GUESTFS_ADD_DRIVE_OPTS_IFACE_BITMASK;
272 optargs2.iface = iface;
275 int t = guestfs__add_drive_opts (g, filename, &optargs2);
279 xmlXPathFreeObject (xpfilename);
280 xmlXPathFreeObject (xpformat);
289 error (g, _("libvirt domain has no disks"));
297 if (r == -1) guestfs___rollback_cmdline (g, cmdline_pos);
299 if (xpathObj) xmlXPathFreeObject (xpathObj);
300 if (xpathCtx) xmlXPathFreeContext (xpathCtx);
301 if (doc) xmlFreeDoc (doc);
306 #else /* no libvirt or libxml2 at compile time */
308 #define NOT_IMPL(r) \
309 error (g, _("add-domain API not available since this version of libguestfs was compiled without libvirt or libxml2")); \
313 guestfs__add_domain (guestfs_h *g, const char *dom,
314 const struct guestfs_add_domain_argv *optargs)
319 #endif /* no libvirt or libxml2 at compile time */