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>
36 #define GUESTFS_PRIVATE_FOR_EACH_DISK 1
39 #include "guestfs-internal.h"
40 #include "guestfs-internal-actions.h"
41 #include "guestfs_protocol.h"
43 #if defined(HAVE_LIBVIRT) && defined(HAVE_LIBXML2)
45 static void init_libxml2 (void) __attribute__((constructor));
50 /* I am told that you don't really need to call virInitialize ... */
57 ignore_errors (void *ignore, virErrorPtr ignore2)
62 struct guestfs___add_libvirt_dom_argv {
64 #define GUESTFS___ADD_LIBVIRT_DOM_READONLY_BITMASK (UINT64_C(1)<<0)
66 #define GUESTFS___ADD_LIBVIRT_DOM_IFACE_BITMASK (UINT64_C(1)<<1)
68 #define GUESTFS___ADD_LIBVIRT_DOM_LIVE_BITMASK (UINT64_C(1)<<2)
72 static int guestfs___add_libvirt_dom (guestfs_h *g, virDomainPtr dom, const struct guestfs___add_libvirt_dom_argv *optargs);
75 guestfs__add_domain (guestfs_h *g, const char *domain_name,
76 const struct guestfs_add_domain_argv *optargs)
79 virConnectPtr conn = NULL;
80 virDomainPtr dom = NULL;
82 const char *libvirturi;
86 struct guestfs___add_libvirt_dom_argv optargs2 = { .bitmask = 0 };
88 libvirturi = optargs->bitmask & GUESTFS_ADD_DOMAIN_LIBVIRTURI_BITMASK
89 ? optargs->libvirturi : NULL;
90 readonly = optargs->bitmask & GUESTFS_ADD_DOMAIN_READONLY_BITMASK
91 ? optargs->readonly : 0;
92 iface = optargs->bitmask & GUESTFS_ADD_DOMAIN_IFACE_BITMASK
93 ? optargs->iface : NULL;
94 live = optargs->bitmask & GUESTFS_ADD_DOMAIN_LIVE_BITMASK
97 if (live && readonly) {
98 error (g, _("you cannot set both live and readonly flags"));
102 /* Connect to libvirt, find the domain. */
103 conn = virConnectOpenReadOnly (libvirturi);
105 err = virGetLastError ();
106 error (g, _("could not connect to libvirt (code %d, domain %d): %s"),
107 err->code, err->domain, err->message);
111 /* Suppress default behaviour of printing errors to stderr. Note
112 * you can't set this to NULL to ignore errors; setting it to NULL
113 * restores the default error handler ...
115 virConnSetErrorFunc (conn, NULL, ignore_errors);
117 dom = virDomainLookupByName (conn, domain_name);
119 err = virGetLastError ();
120 error (g, _("no libvirt domain called '%s': %s"),
121 domain_name, err->message);
126 optargs2.bitmask |= GUESTFS___ADD_LIBVIRT_DOM_READONLY_BITMASK;
127 optargs2.readonly = readonly;
130 optargs2.bitmask |= GUESTFS___ADD_LIBVIRT_DOM_IFACE_BITMASK;
131 optargs2.iface = iface;
134 optargs2.bitmask |= GUESTFS___ADD_LIBVIRT_DOM_LIVE_BITMASK;
135 optargs2.live = live;
138 r = guestfs___add_libvirt_dom (g, dom, &optargs2);
141 if (dom) virDomainFree (dom);
142 if (conn) virConnectClose (conn);
147 /* This function is also used in virt-df to avoid having all that
148 * stupid XPath code repeated. This is something that libvirt should
151 * The callback function 'f' is called once for each disk.
153 * Returns number of disks, or -1 if there was an error.
156 guestfs___for_each_disk (guestfs_h *g,
158 int (*f) (guestfs_h *g,
159 const char *filename, const char *format,
163 int i, nr_added = 0, r = -1;
165 xmlDocPtr doc = NULL;
166 xmlXPathContextPtr xpathCtx = NULL;
167 xmlXPathObjectPtr xpathObj = NULL;
171 xml = virDomainGetXMLDesc (dom, 0);
174 err = virGetLastError ();
175 error (g, _("error reading libvirt XML information: %s"),
180 /* Now the horrible task of parsing out the fields we need from the XML.
181 * http://www.xmlsoft.org/examples/xpath1.c
183 doc = xmlParseMemory (xml, strlen (xml));
185 error (g, _("unable to parse XML information returned by libvirt"));
189 xpathCtx = xmlXPathNewContext (doc);
190 if (xpathCtx == NULL) {
191 error (g, _("unable to create new XPath context"));
195 /* This gives us a set of all the <disk> nodes. */
196 xpathObj = xmlXPathEvalExpression (BAD_CAST "//devices/disk", xpathCtx);
197 if (xpathObj == NULL) {
198 error (g, _("unable to evaluate XPath expression"));
202 xmlNodeSetPtr nodes = xpathObj->nodesetval;
203 for (i = 0; i < nodes->nodeNr; ++i) {
204 xmlXPathObjectPtr xptype;
206 /* Change the context to the current <disk> node.
207 * DV advises to reset this before each search since older versions of
208 * libxml2 might overwrite it.
210 xpathCtx->node = nodes->nodeTab[i];
212 /* Filename can be in <source dev=..> or <source file=..> attribute.
213 * Check the <disk type=..> attribute first to find out which one.
215 xptype = xmlXPathEvalExpression (BAD_CAST "./@type", xpathCtx);
216 if (xptype == NULL ||
217 xptype->nodesetval == NULL ||
218 xptype->nodesetval->nodeNr == 0) {
219 xmlXPathFreeObject (xptype);
220 continue; /* no type attribute, skip it */
222 assert (xptype->nodesetval->nodeTab[0]);
223 assert (xptype->nodesetval->nodeTab[0]->type == XML_ATTRIBUTE_NODE);
224 xmlAttrPtr attr = (xmlAttrPtr) xptype->nodesetval->nodeTab[0];
225 char *type = (char *) xmlNodeListGetString (doc, attr->children, 1);
226 xmlXPathFreeObject (xptype);
228 xmlXPathObjectPtr xpfilename;
230 if (STREQ (type, "file")) { /* type = "file" so look at source/@file */
233 xpathCtx->node = nodes->nodeTab[i];
234 xpfilename = xmlXPathEvalExpression (BAD_CAST "./source/@file", xpathCtx);
235 if (xpfilename == NULL ||
236 xpfilename->nodesetval == NULL ||
237 xpfilename->nodesetval->nodeNr == 0) {
238 xmlXPathFreeObject (xpfilename);
239 continue; /* disk filename not found, skip this */
241 } else if (STREQ (type, "block")) { /* type = "block", use source/@dev */
244 xpathCtx->node = nodes->nodeTab[i];
245 xpfilename = xmlXPathEvalExpression (BAD_CAST "./source/@dev", xpathCtx);
246 if (xpfilename == NULL ||
247 xpfilename->nodesetval == NULL ||
248 xpfilename->nodesetval->nodeNr == 0) {
249 xmlXPathFreeObject (xpfilename);
250 continue; /* disk filename not found, skip this */
254 continue; /* type <> "file" or "block", skip it */
257 assert (xpfilename->nodesetval->nodeTab[0]);
258 assert (xpfilename->nodesetval->nodeTab[0]->type == XML_ATTRIBUTE_NODE);
259 attr = (xmlAttrPtr) xpfilename->nodesetval->nodeTab[0];
260 char *filename = (char *) xmlNodeListGetString (doc, attr->children, 1);
262 /* Get the disk format (may not be set). */
263 xmlXPathObjectPtr xpformat;
265 xpathCtx->node = nodes->nodeTab[i];
266 xpformat = xmlXPathEvalExpression (BAD_CAST "./driver/@type", xpathCtx);
268 if (xpformat != NULL &&
269 xpformat->nodesetval &&
270 xpformat->nodesetval->nodeNr > 0) {
271 assert (xpformat->nodesetval->nodeTab[0]);
272 assert (xpformat->nodesetval->nodeTab[0]->type == XML_ATTRIBUTE_NODE);
273 attr = (xmlAttrPtr) xpformat->nodesetval->nodeTab[0];
274 format = (char *) xmlNodeListGetString (doc, attr->children, 1);
279 t = f (g, filename, format, data);
285 xmlXPathFreeObject (xpfilename);
286 xmlXPathFreeObject (xpformat);
295 error (g, _("libvirt domain has no disks"));
304 if (xpathObj) xmlXPathFreeObject (xpathObj);
305 if (xpathCtx) xmlXPathFreeContext (xpathCtx);
306 if (doc) xmlFreeDoc (doc);
311 /* This was proposed as an external API, but it's not quite baked yet. */
313 static int add_disk (guestfs_h *g, const char *filename, const char *format, void *optargs_vp);
314 static int connect_live (guestfs_h *g, virDomainPtr dom);
317 guestfs___add_libvirt_dom (guestfs_h *g, virDomainPtr dom,
318 const struct guestfs___add_libvirt_dom_argv *optargs)
327 optargs->bitmask & GUESTFS___ADD_LIBVIRT_DOM_READONLY_BITMASK
328 ? optargs->readonly : 0;
330 optargs->bitmask & GUESTFS___ADD_LIBVIRT_DOM_IFACE_BITMASK
331 ? optargs->iface : NULL;
333 optargs->bitmask & GUESTFS___ADD_LIBVIRT_DOM_LIVE_BITMASK
336 if (live && readonly) {
337 error (g, _("you cannot set both live and readonly flags"));
346 if (virDomainGetInfo (dom, &info) == -1) {
347 err = virGetLastError ();
348 error (g, _("error getting domain info: %s"), err->message);
351 vm_running = info.state != VIR_DOMAIN_SHUTOFF;
354 /* If the caller specified the 'live' flag, then they want us to
355 * try to connect to guestfsd if the domain is running. Note
356 * that live readonly connections are not possible.
359 return connect_live (g, dom);
361 /* Dangerous to modify the disks of a running VM. */
362 error (g, _("error: domain is a live virtual machine.\n"
363 "Writing to the disks of a running virtual machine can cause disk corruption.\n"
364 "Either use read-only access, or if the guest is running the guestfsd daemon\n"
365 "specify live access. In most libguestfs tools these options are --ro or\n"
366 "--live respectively. Consult the documentation for further information."));
372 struct guestfs_add_drive_opts_argv optargs2 = { .bitmask = 0 };
374 optargs2.bitmask |= GUESTFS_ADD_DRIVE_OPTS_READONLY_BITMASK;
375 optargs2.readonly = readonly;
378 optargs2.bitmask |= GUESTFS_ADD_DRIVE_OPTS_IFACE_BITMASK;
379 optargs2.iface = iface;
382 /* Checkpoint the command line around the operation so that either
383 * all disks are added or none are added.
385 cmdline_pos = guestfs___checkpoint_cmdline (g);
386 r = guestfs___for_each_disk (g, dom, add_disk, &optargs2);
388 guestfs___rollback_cmdline (g, cmdline_pos);
394 add_disk (guestfs_h *g, const char *filename, const char *format,
397 struct guestfs_add_drive_opts_argv *optargs = optargs_vp;
400 optargs->bitmask |= GUESTFS_ADD_DRIVE_OPTS_FORMAT_BITMASK;
401 optargs->format = format;
403 optargs->bitmask &= ~GUESTFS_ADD_DRIVE_OPTS_FORMAT_BITMASK;
405 return guestfs__add_drive_opts (g, filename, optargs);
409 connect_live (guestfs_h *g, virDomainPtr dom)
413 xmlDocPtr doc = NULL;
414 xmlXPathContextPtr xpathCtx = NULL;
415 xmlXPathObjectPtr xpathObj = NULL;
418 char *attach_method = NULL;
421 xml = virDomainGetXMLDesc (dom, 0);
424 err = virGetLastError ();
425 error (g, _("error reading libvirt XML information: %s"),
430 /* Parse XML to document. */
431 doc = xmlParseMemory (xml, strlen (xml));
433 error (g, _("unable to parse XML information returned by libvirt"));
437 xpathCtx = xmlXPathNewContext (doc);
438 if (xpathCtx == NULL) {
439 error (g, _("unable to create new XPath context"));
443 /* This gives us a set of all the <channel> nodes related to the
444 * guestfsd virtio-serial channel.
446 xpathObj = xmlXPathEvalExpression (BAD_CAST
447 "//devices/channel[@type=\"unix\" and "
448 "./source/@mode=\"bind\" and "
449 "./source/@path and "
450 "./target/@type=\"virtio\" and "
451 "./target/@name=\"org.libguestfs.channel.0\"]",
453 if (xpathObj == NULL) {
454 error (g, _("unable to evaluate XPath expression"));
458 xmlNodeSetPtr nodes = xpathObj->nodesetval;
459 for (i = 0; i < nodes->nodeNr; ++i) {
460 xmlXPathObjectPtr xppath;
462 /* See note in function above. */
463 xpathCtx->node = nodes->nodeTab[i];
465 /* The path is in <source path=..> attribute. */
466 xppath = xmlXPathEvalExpression (BAD_CAST "./source/@path", xpathCtx);
467 if (xppath == NULL ||
468 xppath->nodesetval == NULL ||
469 xppath->nodesetval->nodeNr == 0) {
470 xmlXPathFreeObject (xppath);
471 continue; /* no type attribute, skip it */
473 assert (xppath->nodesetval->nodeTab[0]);
474 assert (xppath->nodesetval->nodeTab[0]->type == XML_ATTRIBUTE_NODE);
475 xmlAttrPtr attr = (xmlAttrPtr) xppath->nodesetval->nodeTab[0];
476 path = (char *) xmlNodeListGetString (doc, attr->children, 1);
477 xmlXPathFreeObject (xppath);
482 error (g, _("this guest has no libvirt <channel> definition for guestfsd\n"
483 "See ATTACHING TO RUNNING DAEMONS in guestfs(3) for further information."));
488 attach_method = safe_malloc (g, strlen (path) + 5 + 1);
489 strcpy (attach_method, "unix:");
490 strcat (attach_method, path);
491 r = guestfs_set_attach_method (g, attach_method);
495 free (attach_method);
497 if (xpathObj) xmlXPathFreeObject (xpathObj);
498 if (xpathCtx) xmlXPathFreeContext (xpathCtx);
499 if (doc) xmlFreeDoc (doc);
504 #else /* no libvirt or libxml2 at compile time */
506 #define NOT_IMPL(r) \
507 error (g, _("add-domain API not available since this version of libguestfs was compiled without libvirt or libxml2")); \
511 guestfs__add_domain (guestfs_h *g, const char *dom,
512 const struct guestfs_add_domain_argv *optargs)
517 #endif /* no libvirt or libxml2 at compile time */