Version 1.5.23.
[libguestfs.git] / fish / virt.c
1 /* guestfish - the filesystem interactive shell
2  * Copyright (C) 2010 Red Hat Inc.
3  *
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.
8  *
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.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <assert.h>
25
26 #include <libvirt/libvirt.h>
27 #include <libvirt/virterror.h>
28
29 #include <libxml/xpath.h>
30 #include <libxml/parser.h>
31 #include <libxml/tree.h>
32
33 #include "fish.h"
34
35 /* Implements the guts of the '-d' option.
36  *
37  * Note that we have to observe the '--ro' flag in two respects: by
38  * adding the drives read-only if the flag is set, and by restricting
39  * guests to shut down ones unless '--ro' is set.
40  *
41  * Returns the number of drives added (> 0), or -1 for failure.
42  */
43 int
44 add_libvirt_drives (const char *guest)
45 {
46   static int initialized = 0;
47   if (!initialized) {
48     initialized = 1;
49
50     if (virInitialize () == -1)
51       return -1;
52
53     xmlInitParser ();
54     LIBXML_TEST_VERSION;
55   }
56
57   int r = -1, nr_added = 0, i;
58   virErrorPtr err;
59   virConnectPtr conn = NULL;
60   virDomainPtr dom = NULL;
61   xmlDocPtr doc = NULL;
62   xmlXPathContextPtr xpathCtx = NULL;
63   xmlXPathObjectPtr xpathObj = NULL;
64   char *xml = NULL;
65
66   /* Connect to libvirt, find the domain. */
67   conn = virConnectOpenReadOnly (libvirt_uri);
68   if (!conn) {
69     err = virGetLastError ();
70     fprintf (stderr, _("guestfish: could not connect to libvirt (code %d, domain %d): %s\n"),
71              err->code, err->domain, err->message);
72     goto cleanup;
73   }
74
75   dom = virDomainLookupByName (conn, guest);
76   if (!dom) {
77     err = virConnGetLastError (conn);
78     fprintf (stderr, _("guestfish: no libvirt domain called '%s': %s\n"),
79              guest, err->message);
80     goto cleanup;
81   }
82   if (!read_only) {
83     virDomainInfo info;
84     if (virDomainGetInfo (dom, &info) == -1) {
85       err = virConnGetLastError (conn);
86       fprintf (stderr, _("guestfish: error getting domain info about '%s': %s\n"),
87                guest, err->message);
88       goto cleanup;
89     }
90     if (info.state != VIR_DOMAIN_SHUTOFF) {
91       fprintf (stderr, _("guestfish: error: '%s' is a live virtual machine.\nYou must use '--ro' because write access to a running virtual machine can\ncause disk corruption.\n"),
92                guest);
93       goto cleanup;
94     }
95   }
96
97   /* Domain XML. */
98   xml = virDomainGetXMLDesc (dom, 0);
99
100   if (!xml) {
101     err = virConnGetLastError (conn);
102     fprintf (stderr, _("guestfish: error reading libvirt XML information about '%s': %s\n"),
103              guest, err->message);
104     goto cleanup;
105   }
106
107   /* Now the horrible task of parsing out the fields we need from the XML.
108    * http://www.xmlsoft.org/examples/xpath1.c
109    */
110   doc = xmlParseMemory (xml, strlen (xml));
111   if (doc == NULL) {
112     fprintf (stderr, _("guestfish: unable to parse XML information returned by libvirt\n"));
113     goto cleanup;
114   }
115
116   xpathCtx = xmlXPathNewContext (doc);
117   if (xpathCtx == NULL) {
118     fprintf (stderr, _("guestfish: unable to create new XPath context\n"));
119     goto cleanup;
120   }
121
122   /* This gives us a set of all the <disk> nodes. */
123   xpathObj = xmlXPathEvalExpression (BAD_CAST "//devices/disk", xpathCtx);
124   if (xpathObj == NULL) {
125     fprintf (stderr, _("guestfish: unable to evaluate XPath expression\n"));
126     goto cleanup;
127   }
128
129   xmlNodeSetPtr nodes = xpathObj->nodesetval;
130   for (i = 0; i < nodes->nodeNr; ++i) {
131     xmlXPathObjectPtr xpfilename;
132     xmlXPathObjectPtr xpformat;
133
134     /* Change the context to the current <disk> node.
135      * DV advises to reset this before each search since older versions of
136      * libxml2 might overwrite it.
137      */
138     xpathCtx->node = nodes->nodeTab[i];
139
140     /* Filename can be in <source dev=..> or <source file=..> attribute. */
141     xpfilename = xmlXPathEvalExpression (BAD_CAST "./source/@dev", xpathCtx);
142     if (xpfilename == NULL ||
143         xpfilename->nodesetval == NULL ||
144         xpfilename->nodesetval->nodeNr == 0) {
145       xmlXPathFreeObject (xpfilename);
146       xpathCtx->node = nodes->nodeTab[i];
147       xpfilename = xmlXPathEvalExpression (BAD_CAST "./source/@file", xpathCtx);
148       if (xpfilename == NULL ||
149           xpfilename->nodesetval == NULL ||
150           xpfilename->nodesetval->nodeNr == 0) {
151         xmlXPathFreeObject (xpfilename);
152         continue;               /* disk filename not found, skip this */
153       }
154     }
155
156     assert (xpfilename->nodesetval->nodeTab[0]);
157     assert (xpfilename->nodesetval->nodeTab[0]->type == XML_ATTRIBUTE_NODE);
158     xmlAttrPtr attr = (xmlAttrPtr) xpfilename->nodesetval->nodeTab[0];
159     char *filename = (char *) xmlNodeListGetString (doc, attr->children, 1);
160
161     /* Get the disk format (may not be set). */
162     xpathCtx->node = nodes->nodeTab[i];
163     xpformat = xmlXPathEvalExpression (BAD_CAST "./driver/@type", xpathCtx);
164     char *format = NULL;
165     if (xpformat != NULL &&
166         xpformat->nodesetval &&
167         xpformat->nodesetval->nodeNr > 0) {
168       assert (xpformat->nodesetval->nodeTab[0]);
169       assert (xpformat->nodesetval->nodeTab[0]->type == XML_ATTRIBUTE_NODE);
170       attr = (xmlAttrPtr) xpformat->nodesetval->nodeTab[0];
171       format = (char *) xmlNodeListGetString (doc, attr->children, 1);
172     }
173
174     /* Add the disk, with optional format. */
175     struct guestfs_add_drive_opts_argv optargs = { .bitmask = 0 };
176     if (read_only) {
177       optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_READONLY_BITMASK;
178       optargs.readonly = read_only;
179     }
180     if (format) {
181       optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_FORMAT_BITMASK;
182       optargs.format = format;
183     }
184
185     int t = guestfs_add_drive_opts_argv (g, filename, &optargs);
186
187     xmlFree (filename);
188     xmlFree (format);
189     xmlXPathFreeObject (xpfilename);
190     xmlXPathFreeObject (xpformat);
191
192     if (t == -1)
193       goto cleanup;
194
195     nr_added++;
196   }
197
198   if (nr_added == 0) {
199     fprintf (stderr, _("guestfish: libvirt domain '%s' has no disks\n"),
200              guest);
201     goto cleanup;
202   }
203
204   /* Successful. */
205   r = nr_added;
206
207 cleanup:
208   free (xml);
209   if (xpathObj) xmlXPathFreeObject (xpathObj);
210   if (xpathCtx) xmlXPathFreeContext (xpathCtx);
211   if (doc) xmlFreeDoc (doc);
212   if (dom) virDomainFree (dom);
213   if (conn) virConnectClose (conn);
214
215   return r;
216 }