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 ... */
56 struct guestfs___add_libvirt_dom_argv {
58 #define GUESTFS___ADD_LIBVIRT_DOM_READONLY_BITMASK (UINT64_C(1)<<0)
60 #define GUESTFS___ADD_LIBVIRT_DOM_IFACE_BITMASK (UINT64_C(1)<<1)
62 #define GUESTFS___ADD_LIBVIRT_DOM_LIVE_BITMASK (UINT64_C(1)<<2)
66 static int guestfs___add_libvirt_dom (guestfs_h *g, virDomainPtr dom, const struct guestfs___add_libvirt_dom_argv *optargs);
69 guestfs__add_domain (guestfs_h *g, const char *domain_name,
70 const struct guestfs_add_domain_argv *optargs)
73 virConnectPtr conn = NULL;
74 virDomainPtr dom = NULL;
76 const char *libvirturi;
80 struct guestfs___add_libvirt_dom_argv optargs2 = { .bitmask = 0 };
82 libvirturi = optargs->bitmask & GUESTFS_ADD_DOMAIN_LIBVIRTURI_BITMASK
83 ? optargs->libvirturi : NULL;
84 readonly = optargs->bitmask & GUESTFS_ADD_DOMAIN_READONLY_BITMASK
85 ? optargs->readonly : 0;
86 iface = optargs->bitmask & GUESTFS_ADD_DOMAIN_IFACE_BITMASK
87 ? optargs->iface : NULL;
88 live = optargs->bitmask & GUESTFS_ADD_DOMAIN_LIVE_BITMASK
91 if (live && readonly) {
92 error (g, _("you cannot set both live and readonly flags"));
96 /* Connect to libvirt, find the domain. */
97 conn = virConnectOpenReadOnly (libvirturi);
99 err = virGetLastError ();
100 error (g, _("could not connect to libvirt (code %d, domain %d): %s"),
101 err->code, err->domain, err->message);
105 dom = virDomainLookupByName (conn, domain_name);
107 err = virGetLastError ();
108 error (g, _("no libvirt domain called '%s': %s"),
109 domain_name, err->message);
114 optargs2.bitmask |= GUESTFS___ADD_LIBVIRT_DOM_READONLY_BITMASK;
115 optargs2.readonly = readonly;
118 optargs2.bitmask |= GUESTFS___ADD_LIBVIRT_DOM_IFACE_BITMASK;
119 optargs2.iface = iface;
122 optargs2.bitmask |= GUESTFS___ADD_LIBVIRT_DOM_LIVE_BITMASK;
123 optargs2.live = live;
126 r = guestfs___add_libvirt_dom (g, dom, &optargs2);
129 if (dom) virDomainFree (dom);
130 if (conn) virConnectClose (conn);
135 /* This function is also used in virt-df to avoid having all that
136 * stupid XPath code repeated. This is something that libvirt should
139 * The callback function 'f' is called once for each disk.
141 * Returns number of disks, or -1 if there was an error.
144 guestfs___for_each_disk (guestfs_h *g,
146 int (*f) (guestfs_h *g,
147 const char *filename, const char *format,
151 int i, nr_added = 0, r = -1;
153 xmlDocPtr doc = NULL;
154 xmlXPathContextPtr xpathCtx = NULL;
155 xmlXPathObjectPtr xpathObj = NULL;
159 xml = virDomainGetXMLDesc (dom, 0);
162 err = virGetLastError ();
163 error (g, _("error reading libvirt XML information: %s"),
168 /* Now the horrible task of parsing out the fields we need from the XML.
169 * http://www.xmlsoft.org/examples/xpath1.c
171 doc = xmlParseMemory (xml, strlen (xml));
173 error (g, _("unable to parse XML information returned by libvirt"));
177 xpathCtx = xmlXPathNewContext (doc);
178 if (xpathCtx == NULL) {
179 error (g, _("unable to create new XPath context"));
183 /* This gives us a set of all the <disk> nodes. */
184 xpathObj = xmlXPathEvalExpression (BAD_CAST "//devices/disk", xpathCtx);
185 if (xpathObj == NULL) {
186 error (g, _("unable to evaluate XPath expression"));
190 xmlNodeSetPtr nodes = xpathObj->nodesetval;
191 for (i = 0; i < nodes->nodeNr; ++i) {
192 xmlXPathObjectPtr xptype;
194 /* Change the context to the current <disk> node.
195 * DV advises to reset this before each search since older versions of
196 * libxml2 might overwrite it.
198 xpathCtx->node = nodes->nodeTab[i];
200 /* Filename can be in <source dev=..> or <source file=..> attribute.
201 * Check the <disk type=..> attribute first to find out which one.
203 xptype = xmlXPathEvalExpression (BAD_CAST "./@type", xpathCtx);
204 if (xptype == NULL ||
205 xptype->nodesetval == NULL ||
206 xptype->nodesetval->nodeNr == 0) {
207 xmlXPathFreeObject (xptype);
208 continue; /* no type attribute, skip it */
210 assert (xptype->nodesetval->nodeTab[0]);
211 assert (xptype->nodesetval->nodeTab[0]->type == XML_ATTRIBUTE_NODE);
212 xmlAttrPtr attr = (xmlAttrPtr) xptype->nodesetval->nodeTab[0];
213 char *type = (char *) xmlNodeListGetString (doc, attr->children, 1);
214 xmlXPathFreeObject (xptype);
216 xmlXPathObjectPtr xpfilename;
218 if (STREQ (type, "file")) { /* type = "file" so look at source/@file */
221 xpathCtx->node = nodes->nodeTab[i];
222 xpfilename = xmlXPathEvalExpression (BAD_CAST "./source/@file", xpathCtx);
223 if (xpfilename == NULL ||
224 xpfilename->nodesetval == NULL ||
225 xpfilename->nodesetval->nodeNr == 0) {
226 xmlXPathFreeObject (xpfilename);
227 continue; /* disk filename not found, skip this */
229 } else if (STREQ (type, "block")) { /* type = "block", use source/@dev */
232 xpathCtx->node = nodes->nodeTab[i];
233 xpfilename = xmlXPathEvalExpression (BAD_CAST "./source/@dev", xpathCtx);
234 if (xpfilename == NULL ||
235 xpfilename->nodesetval == NULL ||
236 xpfilename->nodesetval->nodeNr == 0) {
237 xmlXPathFreeObject (xpfilename);
238 continue; /* disk filename not found, skip this */
242 continue; /* type <> "file" or "block", skip it */
245 assert (xpfilename->nodesetval->nodeTab[0]);
246 assert (xpfilename->nodesetval->nodeTab[0]->type == XML_ATTRIBUTE_NODE);
247 attr = (xmlAttrPtr) xpfilename->nodesetval->nodeTab[0];
248 char *filename = (char *) xmlNodeListGetString (doc, attr->children, 1);
250 /* Get the disk format (may not be set). */
251 xmlXPathObjectPtr xpformat;
253 xpathCtx->node = nodes->nodeTab[i];
254 xpformat = xmlXPathEvalExpression (BAD_CAST "./driver/@type", xpathCtx);
256 if (xpformat != NULL &&
257 xpformat->nodesetval &&
258 xpformat->nodesetval->nodeNr > 0) {
259 assert (xpformat->nodesetval->nodeTab[0]);
260 assert (xpformat->nodesetval->nodeTab[0]->type == XML_ATTRIBUTE_NODE);
261 attr = (xmlAttrPtr) xpformat->nodesetval->nodeTab[0];
262 format = (char *) xmlNodeListGetString (doc, attr->children, 1);
267 t = f (g, filename, format, data);
273 xmlXPathFreeObject (xpfilename);
274 xmlXPathFreeObject (xpformat);
283 error (g, _("libvirt domain has no disks"));
292 if (xpathObj) xmlXPathFreeObject (xpathObj);
293 if (xpathCtx) xmlXPathFreeContext (xpathCtx);
294 if (doc) xmlFreeDoc (doc);
299 /* This was proposed as an external API, but it's not quite baked yet. */
301 static int add_disk (guestfs_h *g, const char *filename, const char *format, void *optargs_vp);
302 static int connect_live (guestfs_h *g, virDomainPtr dom);
305 guestfs___add_libvirt_dom (guestfs_h *g, virDomainPtr dom,
306 const struct guestfs___add_libvirt_dom_argv *optargs)
315 optargs->bitmask & GUESTFS___ADD_LIBVIRT_DOM_READONLY_BITMASK
316 ? optargs->readonly : 0;
318 optargs->bitmask & GUESTFS___ADD_LIBVIRT_DOM_IFACE_BITMASK
319 ? optargs->iface : NULL;
321 optargs->bitmask & GUESTFS___ADD_LIBVIRT_DOM_LIVE_BITMASK
324 if (live && readonly) {
325 error (g, _("you cannot set both live and readonly flags"));
334 if (virDomainGetInfo (dom, &info) == -1) {
335 err = virGetLastError ();
336 error (g, _("error getting domain info: %s"), err->message);
339 vm_running = info.state != VIR_DOMAIN_SHUTOFF;
342 /* If the caller specified the 'live' flag, then they want us to
343 * try to connect to guestfsd if the domain is running. Note
344 * that live readonly connections are not possible.
347 return connect_live (g, dom);
349 /* Dangerous to modify the disks of a running VM. */
350 error (g, _("error: domain is a live virtual machine.\n"
351 "Writing to the disks of a running virtual machine can cause disk corruption.\n"
352 "Either use read-only access, or if the guest is running the guestfsd daemon\n"
353 "specify live access. In most libguestfs tools these options are --ro or\n"
354 "--live respectively. Consult the documentation for further information."));
360 struct guestfs_add_drive_opts_argv optargs2 = { .bitmask = 0 };
362 optargs2.bitmask |= GUESTFS_ADD_DRIVE_OPTS_READONLY_BITMASK;
363 optargs2.readonly = readonly;
366 optargs2.bitmask |= GUESTFS_ADD_DRIVE_OPTS_IFACE_BITMASK;
367 optargs2.iface = iface;
370 /* Checkpoint the command line around the operation so that either
371 * all disks are added or none are added.
373 cmdline_pos = guestfs___checkpoint_cmdline (g);
374 r = guestfs___for_each_disk (g, dom, add_disk, &optargs2);
376 guestfs___rollback_cmdline (g, cmdline_pos);
382 add_disk (guestfs_h *g, const char *filename, const char *format,
385 struct guestfs_add_drive_opts_argv *optargs = optargs_vp;
388 optargs->bitmask |= GUESTFS_ADD_DRIVE_OPTS_FORMAT_BITMASK;
389 optargs->format = format;
391 optargs->bitmask &= ~GUESTFS_ADD_DRIVE_OPTS_FORMAT_BITMASK;
393 return guestfs__add_drive_opts (g, filename, optargs);
397 connect_live (guestfs_h *g, virDomainPtr dom)
401 xmlDocPtr doc = NULL;
402 xmlXPathContextPtr xpathCtx = NULL;
403 xmlXPathObjectPtr xpathObj = NULL;
406 char *attach_method = NULL;
409 xml = virDomainGetXMLDesc (dom, 0);
412 err = virGetLastError ();
413 error (g, _("error reading libvirt XML information: %s"),
418 /* Parse XML to document. */
419 doc = xmlParseMemory (xml, strlen (xml));
421 error (g, _("unable to parse XML information returned by libvirt"));
425 xpathCtx = xmlXPathNewContext (doc);
426 if (xpathCtx == NULL) {
427 error (g, _("unable to create new XPath context"));
431 /* This gives us a set of all the <channel> nodes related to the
432 * guestfsd virtio-serial channel.
434 xpathObj = xmlXPathEvalExpression (BAD_CAST
435 "//devices/channel[@type=\"unix\" and "
436 "./source/@mode=\"bind\" and "
437 "./source/@path and "
438 "./target/@type=\"virtio\" and "
439 "./target/@name=\"org.libguestfs.channel.0\"]",
441 if (xpathObj == NULL) {
442 error (g, _("unable to evaluate XPath expression"));
446 xmlNodeSetPtr nodes = xpathObj->nodesetval;
447 for (i = 0; i < nodes->nodeNr; ++i) {
448 xmlXPathObjectPtr xppath;
450 /* See note in function above. */
451 xpathCtx->node = nodes->nodeTab[i];
453 /* The path is in <source path=..> attribute. */
454 xppath = xmlXPathEvalExpression (BAD_CAST "./source/@path", xpathCtx);
455 if (xppath == NULL ||
456 xppath->nodesetval == NULL ||
457 xppath->nodesetval->nodeNr == 0) {
458 xmlXPathFreeObject (xppath);
459 continue; /* no type attribute, skip it */
461 assert (xppath->nodesetval->nodeTab[0]);
462 assert (xppath->nodesetval->nodeTab[0]->type == XML_ATTRIBUTE_NODE);
463 xmlAttrPtr attr = (xmlAttrPtr) xppath->nodesetval->nodeTab[0];
464 path = (char *) xmlNodeListGetString (doc, attr->children, 1);
465 xmlXPathFreeObject (xppath);
470 error (g, _("this guest has no libvirt <channel> definition for guestfsd\n"
471 "See ATTACHING TO RUNNING DAEMONS in guestfs(3) for further information."));
476 attach_method = safe_malloc (g, strlen (path) + 5 + 1);
477 strcpy (attach_method, "unix:");
478 strcat (attach_method, path);
479 r = guestfs_set_attach_method (g, attach_method);
483 free (attach_method);
485 if (xpathObj) xmlXPathFreeObject (xpathObj);
486 if (xpathCtx) xmlXPathFreeContext (xpathCtx);
487 if (doc) xmlFreeDoc (doc);
492 #else /* no libvirt or libxml2 at compile time */
494 #define NOT_IMPL(r) \
495 error (g, _("add-domain API not available since this version of libguestfs was compiled without libvirt or libxml2")); \
499 guestfs__add_domain (guestfs_h *g, const char *dom,
500 const struct guestfs_add_domain_argv *optargs)
505 #endif /* no libvirt or libxml2 at compile time */