fish: Allow guestfish -N help for listing prepared disk image help.
[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 static int add_drives_from_node_set (xmlDocPtr doc, xmlNodeSetPtr nodes);
36
37 /* Implements the guts of the '-d' option.
38  *
39  * Note that we have to observe the '--ro' flag in two respects: by
40  * adding the drives read-only if the flag is set, and by restricting
41  * guests to shut down ones unless '--ro' is set.
42  *
43  * Returns the number of drives added (> 0), or -1 for failure.
44  */
45 int
46 add_libvirt_drives (const char *guest)
47 {
48   static int initialized = 0;
49   if (!initialized) {
50     initialized = 1;
51
52     if (virInitialize () == -1)
53       return -1;
54
55     xmlInitParser ();
56     LIBXML_TEST_VERSION;
57   }
58
59   int r = -1, nr_added = 0;
60   virErrorPtr err;
61   virConnectPtr conn = NULL;
62   virDomainPtr dom = NULL;
63   xmlDocPtr doc = NULL;
64   xmlXPathContextPtr xpathCtx = NULL;
65   xmlXPathObjectPtr xpathObj = NULL;
66   char *xml = NULL;
67
68   /* Connect to libvirt, find the domain. */
69   conn = virConnectOpenReadOnly (libvirt_uri);
70   if (!conn) {
71     err = virGetLastError ();
72     fprintf (stderr, _("guestfish: could not connect to libvirt (code %d, domain %d): %s\n"),
73              err->code, err->domain, err->message);
74     goto cleanup;
75   }
76
77   dom = virDomainLookupByName (conn, guest);
78   if (!dom) {
79     err = virConnGetLastError (conn);
80     fprintf (stderr, _("guestfish: no libvirt domain called '%s': %s\n"),
81              guest, err->message);
82     goto cleanup;
83   }
84   if (!read_only) {
85     virDomainInfo info;
86     if (virDomainGetInfo (dom, &info) == -1) {
87       err = virConnGetLastError (conn);
88       fprintf (stderr, _("guestfish: error getting domain info about '%s': %s\n"),
89                guest, err->message);
90       goto cleanup;
91     }
92     if (info.state != VIR_DOMAIN_SHUTOFF) {
93       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"),
94                guest);
95       goto cleanup;
96     }
97   }
98
99   /* Domain XML. */
100   xml = virDomainGetXMLDesc (dom, 0);
101
102   if (!xml) {
103     err = virConnGetLastError (conn);
104     fprintf (stderr, _("guestfish: error reading libvirt XML information about '%s': %s\n"),
105              guest, err->message);
106     goto cleanup;
107   }
108
109   /* Now the horrible task of parsing out the fields we need from the XML.
110    * http://www.xmlsoft.org/examples/xpath1.c
111    */
112   doc = xmlParseMemory (xml, strlen (xml));
113   if (doc == NULL) {
114     fprintf (stderr, _("guestfish: unable to parse XML information returned by libvirt\n"));
115     goto cleanup;
116   }
117
118   xpathCtx = xmlXPathNewContext (doc);
119   if (xpathCtx == NULL) {
120     fprintf (stderr, _("guestfish: unable to create new XPath context\n"));
121     goto cleanup;
122   }
123
124   xpathObj = xmlXPathEvalExpression (BAD_CAST "//devices/disk/source/@dev",
125                                      xpathCtx);
126   if (xpathObj == NULL) {
127     fprintf (stderr, _("guestfish: unable to evaluate XPath expression\n"));
128     goto cleanup;
129   }
130
131   nr_added += add_drives_from_node_set (doc, xpathObj->nodesetval);
132
133   xmlXPathFreeObject (xpathObj); xpathObj = NULL;
134
135   xpathObj = xmlXPathEvalExpression (BAD_CAST "//devices/disk/source/@file",
136                                      xpathCtx);
137   if (xpathObj == NULL) {
138     fprintf (stderr, _("guestfish: unable to evaluate XPath expression\n"));
139     goto cleanup;
140   }
141
142   nr_added += add_drives_from_node_set (doc, xpathObj->nodesetval);
143
144   if (nr_added == 0) {
145     fprintf (stderr, _("guestfish: libvirt domain '%s' has no disks\n"),
146              guest);
147     goto cleanup;
148   }
149
150   /* Successful. */
151   r = nr_added;
152
153 cleanup:
154   free (xml);
155   if (xpathObj) xmlXPathFreeObject (xpathObj);
156   if (xpathCtx) xmlXPathFreeContext (xpathCtx);
157   if (doc) xmlFreeDoc (doc);
158   if (dom) virDomainFree (dom);
159   if (conn) virConnectClose (conn);
160
161   return r;
162 }
163
164 static int
165 add_drives_from_node_set (xmlDocPtr doc, xmlNodeSetPtr nodes)
166 {
167   if (!nodes)
168     return 0;
169
170   int i;
171
172   for (i = 0; i < nodes->nodeNr; ++i) {
173     assert (nodes->nodeTab[i]);
174     assert (nodes->nodeTab[i]->type == XML_ATTRIBUTE_NODE);
175     xmlAttrPtr attr = (xmlAttrPtr) nodes->nodeTab[i];
176
177     char *device = (char *) xmlNodeListGetString (doc, attr->children, 1);
178
179     int r;
180     if (!read_only)
181       r = guestfs_add_drive (g, device);
182     else
183       r = guestfs_add_drive_ro (g, device);
184     if (r == -1)
185       exit (EXIT_FAILURE);
186
187     xmlFree (device);
188   }
189
190   return nodes->nodeNr;
191 }