summaryrefslogtreecommitdiffstats
path: root/fish/virt.c
blob: d915d22093797e75d2a7f57730e6a734ffad1615 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* guestfish - the filesystem interactive shell
 * Copyright (C) 2010 Red Hat Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include <libvirt/libvirt.h>
#include <libvirt/virterror.h>

#include <libxml/xpath.h>
#include <libxml/parser.h>
#include <libxml/tree.h>

#include "fish.h"

/* Implements the guts of the '-d' option.
 *
 * Note that we have to observe the '--ro' flag in two respects: by
 * adding the drives read-only if the flag is set, and by restricting
 * guests to shut down ones unless '--ro' is set.
 *
 * Returns the number of drives added (> 0), or -1 for failure.
 */
int
add_libvirt_drives (const char *guest)
{
  static int initialized = 0;
  if (!initialized) {
    initialized = 1;

    if (virInitialize () == -1)
      return -1;

    xmlInitParser ();
    LIBXML_TEST_VERSION;
  }

  int r = -1, nr_added = 0, i;
  virErrorPtr err;
  virConnectPtr conn = NULL;
  virDomainPtr dom = NULL;
  xmlDocPtr doc = NULL;
  xmlXPathContextPtr xpathCtx = NULL;
  xmlXPathObjectPtr xpathObj = NULL;
  char *xml = NULL;

  /* Connect to libvirt, find the domain. */
  conn = virConnectOpenReadOnly (libvirt_uri);
  if (!conn) {
    err = virGetLastError ();
    fprintf (stderr, _("guestfish: could not connect to libvirt (code %d, domain %d): %s\n"),
             err->code, err->domain, err->message);
    goto cleanup;
  }

  dom = virDomainLookupByName (conn, guest);
  if (!dom) {
    err = virConnGetLastError (conn);
    fprintf (stderr, _("guestfish: no libvirt domain called '%s': %s\n"),
             guest, err->message);
    goto cleanup;
  }
  if (!read_only) {
    virDomainInfo info;
    if (virDomainGetInfo (dom, &info) == -1) {
      err = virConnGetLastError (conn);
      fprintf (stderr, _("guestfish: error getting domain info about '%s': %s\n"),
               guest, err->message);
      goto cleanup;
    }
    if (info.state != VIR_DOMAIN_SHUTOFF) {
      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"),
               guest);
      goto cleanup;
    }
  }

  /* Domain XML. */
  xml = virDomainGetXMLDesc (dom, 0);

  if (!xml) {
    err = virConnGetLastError (conn);
    fprintf (stderr, _("guestfish: error reading libvirt XML information about '%s': %s\n"),
             guest, err->message);
    goto cleanup;
  }

  /* Now the horrible task of parsing out the fields we need from the XML.
   * http://www.xmlsoft.org/examples/xpath1.c
   */
  doc = xmlParseMemory (xml, strlen (xml));
  if (doc == NULL) {
    fprintf (stderr, _("guestfish: unable to parse XML information returned by libvirt\n"));
    goto cleanup;
  }

  xpathCtx = xmlXPathNewContext (doc);
  if (xpathCtx == NULL) {
    fprintf (stderr, _("guestfish: unable to create new XPath context\n"));
    goto cleanup;
  }

  /* This gives us a set of all the <disk> nodes. */
  xpathObj = xmlXPathEvalExpression (BAD_CAST "//devices/disk", xpathCtx);
  if (xpathObj == NULL) {
    fprintf (stderr, _("guestfish: unable to evaluate XPath expression\n"));
    goto cleanup;
  }

  xmlNodeSetPtr nodes = xpathObj->nodesetval;
  for (i = 0; i < nodes->nodeNr; ++i) {
    xmlXPathObjectPtr xpfilename;
    xmlXPathObjectPtr xpformat;

    /* Change the context to the current <disk> node.
     * DV advises to reset this before each search since older versions of
     * libxml2 might overwrite it.
     */
    xpathCtx->node = nodes->nodeTab[i];

    /* Filename can be in <source dev=..> or <source file=..> attribute. */
    xpfilename = xmlXPathEvalExpression (BAD_CAST "./source/@dev", xpathCtx);
    if (xpfilename == NULL ||
        xpfilename->nodesetval == NULL ||
        xpfilename->nodesetval->nodeNr == 0) {
      xmlXPathFreeObject (xpfilename);
      xpathCtx->node = nodes->nodeTab[i];
      xpfilename = xmlXPathEvalExpression (BAD_CAST "./source/@file", xpathCtx);
      if (xpfilename == NULL ||
          xpfilename->nodesetval == NULL ||
          xpfilename->nodesetval->nodeNr == 0) {
        xmlXPathFreeObject (xpfilename);
        continue;               /* disk filename not found, skip this */
      }
    }

    assert (xpfilename->nodesetval->nodeTab[0]);
    assert (xpfilename->nodesetval->nodeTab[0]->type == XML_ATTRIBUTE_NODE);
    xmlAttrPtr attr = (xmlAttrPtr) xpfilename->nodesetval->nodeTab[0];
    char *filename = (char *) xmlNodeListGetString (doc, attr->children, 1);

    /* Get the disk format (may not be set). */
    xpathCtx->node = nodes->nodeTab[i];
    xpformat = xmlXPathEvalExpression (BAD_CAST "./driver/@type", xpathCtx);
    char *format = NULL;
    if (xpformat != NULL &&
        xpformat->nodesetval &&
        xpformat->nodesetval->nodeNr > 0) {
      assert (xpformat->nodesetval->nodeTab[0]);
      assert (xpformat->nodesetval->nodeTab[0]->type == XML_ATTRIBUTE_NODE);
      attr = (xmlAttrPtr) xpformat->nodesetval->nodeTab[0];
      format = (char *) xmlNodeListGetString (doc, attr->children, 1);
    }

    /* Add the disk, with optional format. */
    struct guestfs_add_drive_opts_argv optargs = { .bitmask = 0 };
    if (read_only) {
      optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_READONLY_BITMASK;
      optargs.readonly = read_only;
    }
    if (format) {
      optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_FORMAT_BITMASK;
      optargs.format = format;
    }

    int t = guestfs_add_drive_opts_argv (g, filename, &optargs);

    xmlFree (filename);
    xmlFree (format);
    xmlXPathFreeObject (xpfilename);
    xmlXPathFreeObject (xpformat);

    if (t == -1)
      goto cleanup;

    nr_added++;
  }

  if (nr_added == 0) {
    fprintf (stderr, _("guestfish: libvirt domain '%s' has no disks\n"),
             guest);
    goto cleanup;
  }

  /* Successful. */
  r = nr_added;

cleanup:
  free (xml);
  if (xpathObj) xmlXPathFreeObject (xpathObj);
  if (xpathCtx) xmlXPathFreeContext (xpathCtx);
  if (doc) xmlFreeDoc (doc);
  if (dom) virDomainFree (dom);
  if (conn) virConnectClose (conn);

  return r;
}