File: guestfs-plugin.c

package info (click to toggle)
nbdkit 1.46.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,504 kB
  • sloc: ansic: 63,658; sh: 18,717; makefile: 6,814; python: 1,848; cpp: 1,143; perl: 504; ml: 504; tcl: 62
file content (609 lines) | stat: -rw-r--r-- 15,361 bytes parent folder | download
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
/* nbdkit
 * Copyright Red Hat
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of Red Hat nor the names of its contributors may be
 * used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>

#include <guestfs.h>

#include <nbdkit-plugin.h>

#include "cleanup.h"

/* Configuration. */
static const char *libvirt_uri = NULL; /* libvirt URI */
static const char *export = NULL;  /* export device or file */
static const char *format = NULL;  /* format parameter */
static int trace = 0, debug = 0;

/* disk and domain options.  (NB: list stored in reverse order) */
struct drive {
  struct drive *next;
  enum { drv_disk, drv_domain } type;
  const char *value;
  const char *format;
};
struct drive *all_drives = NULL;

/* mount options.  (NB: list stored in reverse order)  */
struct mount {
  struct mount *next;
  enum { mount_inspect, mount_fs } type;
  const char *dev;
  const char *mp;
};
struct mount *all_mounts = NULL;

static int
plugin_guestfs_config (const char *key, const char *value)
{
  if (strcmp (key, "debug") == 0) {
    debug = nbdkit_parse_bool (value);
    if (debug == -1) {
      nbdkit_error ("could not parse 'debug' option, expecting an integer");
      return -1;
    }
  }
  else if (strcmp (key, "trace") == 0) {
    trace = nbdkit_parse_bool (value);
    if (trace == -1) {
      nbdkit_error ("could not parse 'trace' option, expecting an integer");
      return -1;
    }
  }
  else if (strcmp (key, "connect") == 0) {
    libvirt_uri = value;
  }
  else if (strcmp (key, "export") == 0) {
    export = value;
  }
  else if (strcmp (key, "format") == 0) {
    if (strcmp (value, "") != 0)
      format = value;
    else
      format = NULL;
  }
  else if (strcmp (key, "disk") == 0) {
    struct drive *d;

    d = malloc (sizeof *d);
    if (!d) {
      nbdkit_error ("malloc: %m");
      return -1;
    }
    d->type = drv_disk;
    d->value = value;
    d->format = format;
    d->next = all_drives;
    all_drives = d;
  }
  else if (strcmp (key, "domain") == 0) {
    struct drive *d;

    d = malloc (sizeof *d);
    if (!d) {
      nbdkit_error ("malloc: %m");
      return -1;
    }
    d->type = drv_domain;
    d->value = value;
    d->next = all_drives;
    all_drives = d;
  }
  else if (strcmp (key, "mount") == 0) {
    struct mount *m;
    char *p;

    m = malloc (sizeof *m);
    if (!m) {
      nbdkit_error ("malloc: %m");
      return -1;
    }

    if (strcmp (value, "inspect") == 0) {
      m->type = mount_inspect;
      m->dev = m->mp = NULL;
    }
    /* strchr in C23 correctly returns 'const char *' here (because
     * 'value' is also const).  However we happen to know that this
     * string may be modified.  Allocating a new string isn't
     * convenient here, but we should probably fix this eventually
     * instead of the bad cast to 'char *' XXX
     */
    else if ((p = (char *) strchr (value, ':')) != NULL) {
      *p = '\0';
      m->type = mount_fs;
      m->dev = value;
      m->mp = p+1;
    }
    else {
      m->type = mount_fs;
      m->dev = value;
      m->mp = "/";
    }
    m->next = all_mounts;
    all_mounts = m;
  }
  else {
    nbdkit_error ("unknown parameter '%s'", key);
    return -1;
  }

  return 0;
}

static int
plugin_guestfs_config_complete (void)
{
  if (export == NULL) {
    nbdkit_error ("the 'export' parameter is required");
    return -1;
  }

  if (all_drives == NULL) {
    nbdkit_error ("at least one 'disk' or 'domain' parameter is required");
    return -1;
  }

  return 0;
}

#define plugin_guestfs_config_help                                  \
  "connect=<URI>       (optional) libvirt connection URI\n"         \
  "debug=on                       enable libguestfs debugging\n"    \
  "domain=<DOMAIN>                libvirt domain name\n"            \
  "disk=<DISK>                    disk name\n"                      \
  "export=DEVICE|FILE  (required) export device or file\n"          \
  "format=<FORMAT>                specify the format of next disk\n" \
  "mount=inspect|MOUNT            mount filesystems\n"              \
  "trace=on                       enable libguestfs call tracing"

/* Free up the structures used to store the command line arguments.
 * Note the static strings don't need to be freed.
 */
static void
plugin_guestfs_unload (void)
{
  struct drive *d, *d_next;
  struct mount *m, *m_next;

  for (d = all_drives; d != NULL; d = d_next) {
    d_next = d->next;
    free (d);
  }

  for (m = all_mounts; m != NULL; m = m_next) {
    m_next = m->next;
    free (m);
  }
}

/* For libguestfs errors. */
#define GERROR(g,fs,...)                                                \
  do {                                                                  \
    nbdkit_error (fs ": %s", ##__VA_ARGS__,                             \
                  guestfs_last_error ((g)));                            \
  } while (0)

/* The per-connection handle. */
struct handle {
  guestfs_h *g;
  bool is_device;
  uint64_t exportsize;
};

static int set_up_logging (guestfs_h *g);
static int add_disks (guestfs_h *g, int readonly, struct drive *);
static int mount_filesystems (guestfs_h *g, int readonly, struct mount *);

/* Create the per-connection handle. */
static void *
plugin_guestfs_open (int readonly)
{
  struct handle *h;
  int64_t r;

  h = malloc (sizeof *h);
  if (h == NULL) {
    nbdkit_error ("malloc: %m");
    return NULL;
  }

  h->g = guestfs_create_flags (GUESTFS_CREATE_NO_ENVIRONMENT);
  if (!h->g) {
    nbdkit_error ("creating libguestfs handle: %m");
    goto err1;
  }

  guestfs_set_error_handler (h->g, NULL, NULL);

  if (trace)
    guestfs_set_trace (h->g, 1);

  if (debug)
    guestfs_set_verbose (h->g, 1);

  if (guestfs_parse_environment (h->g) == -1) {
    GERROR (h->g, "guestfs_parse_environment");
    goto err2;
  }

  if (set_up_logging (h->g) == -1)
    goto err2;

  if (add_disks (h->g, readonly, all_drives) == -1)
    goto err2;

  if (guestfs_launch (h->g) == -1) {
    GERROR (h->g, "guestfs_launch");
    goto err2;
  }

  if (mount_filesystems (h->g, readonly, all_mounts) == -1)
    goto err2;

  /* Exported thing. */
  if (strncmp (export, "/dev/", 5) == 0) {
    h->is_device = true;
    r = guestfs_blockdev_getsize64 (h->g, export);
    if (r == -1) {
      GERROR (h->g, "%s: guestfs_blockdev_getsize64", export);
      goto err2;
    }
    h->exportsize = (uint64_t) r;
  }
  else {
    h->is_device = false;
    r = guestfs_filesize (h->g, export);
    if (r == -1) {
      GERROR (h->g, "%s: guestfs_filesize", export);
      goto err2;
    }
    h->exportsize = (uint64_t) r;
  }

  nbdkit_debug ("guestfs: export %s, size = %" PRIu64 " bytes",
                export, h->exportsize);

  return h;

 err2:
  guestfs_close (h->g);
 err1:
  free (h);
  return NULL;
}

static void
log_to_nbdkit (guestfs_h *g,
               void *opaque,
               uint64_t event,
               int eh,
               int flags,
               const char *buf, size_t buf_len,
               const uint64_t *array, size_t array_len)
{
  CLEANUP_FREE char *sbuf = NULL;

  /* Note the buffer may not be \0 terminated.  Hence this. */
  sbuf = strndup (buf, buf_len);
  if (sbuf)
    nbdkit_debug ("%s", sbuf);
}

static int
set_up_logging (guestfs_h *g)
{
  int eh;
  const uint64_t EVENTS =
    GUESTFS_EVENT_APPLIANCE | GUESTFS_EVENT_LIBRARY | GUESTFS_EVENT_TRACE;

  eh = guestfs_set_event_callback (g, log_to_nbdkit, EVENTS, 0, NULL);
  if (eh == -1) {
    GERROR (g, "guestfs_set_event_callback");
    return -1;
  }

  return 0;
}

static int
add_disks (guestfs_h *g, int readonly, struct drive *drives)
{
  struct guestfs_add_domain_argv domain_optargs;
  struct guestfs_add_drive_opts_argv drive_optargs;

  if (drives == NULL)
    return 0;

  /* Because the 'drives' list is stored in reverse order, use some
   * creative recursion to add the drives in the intended order.
   */
  if (add_disks (g, readonly, drives->next) == -1)
    return -1;

  switch (drives->type) {
  case drv_domain:
    domain_optargs.bitmask =
      GUESTFS_ADD_DOMAIN_READONLY_BITMASK |
      GUESTFS_ADD_DOMAIN_ALLOWUUID_BITMASK;
    domain_optargs.readonly = readonly;
    domain_optargs.allowuuid = 1;
    if (libvirt_uri) {
      domain_optargs.bitmask |= GUESTFS_ADD_DOMAIN_LIBVIRTURI_BITMASK;
      domain_optargs.libvirturi = libvirt_uri;
    }
    if (guestfs_add_domain_argv (g, drives->value, &domain_optargs) == -1) {
      GERROR (g, "domain %s", drives->value);
      return -1;
    }
    break;

  case drv_disk:
    drive_optargs.bitmask = GUESTFS_ADD_DRIVE_OPTS_READONLY_BITMASK;
    drive_optargs.readonly = readonly;
    if (drives->format) {
      drive_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_FORMAT_BITMASK;
      drive_optargs.format = drives->format;
    }
    if (guestfs_add_drive_opts_argv (g, drives->value, &drive_optargs) == -1) {
      GERROR (g, "disk %s", drives->value);
      return -1;
    }
    break;

  default:
    abort ();
  }

  return 0;
}

static int
inspect_and_mount (guestfs_h *g, const char *options)
{
  char **roots = NULL;
  const char *root;
  char **mountpoints = NULL;
  size_t i, mounted;
  int ret = -1;

  roots = guestfs_inspect_os (g);
  if (roots == NULL) {
    GERROR (g, "mount=inspect: guestfs_inspect_os");
    goto out;
  }

  if (roots[0] == NULL) {
    nbdkit_error ("no operating system was found inside this disk image");
    goto out;
  }

  if (roots[1] != NULL) {
    nbdkit_error ("multiple operating system were found.  "
                  "The plugin doesn't support that.");
    goto out;
  }
  root = roots[0];

  mountpoints = guestfs_inspect_get_mountpoints (g, root);
  if (mountpoints == NULL) {
    GERROR (g, "mount=inspect: guestfs_inspect_get_mountpoints");
    goto out;
  }

  /* Ignore errors as long as at least one filesystem is mountable. */
  mounted = 0;
  for (i = 0; mountpoints[i] != NULL; i += 2) {
    if (guestfs_mount_options (g, options,
                               mountpoints[i+1], mountpoints[i]) == 0)
      mounted++;
  }
  if (mounted == 0) {
    nbdkit_error ("mount=inspect: could not mount any filesystems");
    goto out;
  }

  ret = 0;
 out:
  if (roots) {
    for (i = 0; roots[i] != NULL; ++i)
      free (roots[i]);
    free (roots);
  }
  if (mountpoints) {
    for (i = 0; mountpoints[i] != NULL; ++i)
      free (mountpoints[i]);
    free (mountpoints);
  }

  return ret;
}

static int
mount_filesystems (guestfs_h *g, int readonly, struct mount *mounts)
{
  const char *options = readonly ? "ro" : "";

  if (mounts == NULL)
    return 0;

  /* Because the 'mounts' list is stored in reverse order, use some
   * creative recursion to add the drives in the intended order.
   */
  if (mount_filesystems (g, readonly, mounts->next) == -1)
    return -1;

  switch (mounts->type) {
  case mount_fs:
    if (guestfs_mount_options (g, options, mounts->dev, mounts->mp) == -1) {
      GERROR (g, "mount [%s] %s:%s", options, mounts->dev, mounts->mp);
      return -1;
    }
    break;

  case mount_inspect:
    /* Ask libguestfs to inspect for operating systems. */
    if (inspect_and_mount (g, options) == -1)
      return -1;
    break;
  }

  return 0;
}

/* Free up the per-connection handle. */
static void
plugin_guestfs_close (void *handle)
{
  struct handle *h = handle;

  if (guestfs_shutdown (h->g) == -1) {
    GERROR (h->g, "shutdown failure: there may be unwritten data");
    /* ... but we can't do anything about it, see note in nbdkit-plugin(3) */
  }
  guestfs_close (h->g);
  free (h);
}

#define THREAD_MODEL NBDKIT_THREAD_MODEL_SERIALIZE_CONNECTIONS

/* Get the file size. */
static int64_t
plugin_guestfs_get_size (void *handle)
{
  struct handle *h = handle;

  return h->exportsize;
}

/* Read data. */
static int
plugin_guestfs_pread (void *handle, void *buf, uint32_t count, uint64_t offset)
{
  struct handle *h = handle;
  char *(*pr) (guestfs_h *, const char *, int, int64_t, size_t *);
  size_t size;

  if (h->is_device)
    pr = guestfs_pread_device;
  else
    pr = guestfs_pread;

  while (count > 0) {
    CLEANUP_FREE char *data = pr (h->g, export, count, offset, &size);
    if (!data) {
      GERROR (h->g, "%s: pread", export);
      errno = guestfs_last_errno (h->g) ? : EIO;
      return -1;
    }

    memcpy (buf, data, size);

    buf += size;
    offset += size;
    count -= size;
  }

  return 0;
}

/* Write data. */
static int
plugin_guestfs_pwrite (void *handle, const void *buf,
                       uint32_t count, uint64_t offset)
{
  struct handle *h = handle;
  int (*pw) (guestfs_h *, const char *, const char *, size_t, int64_t);
  int r;

  if (h->is_device)
    pw = guestfs_pwrite_device;
  else
    pw = guestfs_pwrite;

  while (count > 0) {
    r = pw (h->g, export, buf, count, offset);
    if (r == -1) {
      GERROR (h->g, "%s: pwrite", export);
      errno = guestfs_last_errno (h->g) ? : EIO;
      return -1;
    }

    buf += r;
    offset += r;
    count -= r;
  }

  return 0;
}

/* Sync. */
static int
plugin_guestfs_flush (void *handle)
{
  struct handle *h = handle;

  if (guestfs_sync (h->g) == -1) {
    GERROR (h->g, "guestfs_sync");
    errno = guestfs_last_errno (h->g) ? : EIO;
    return -1;
  }

  return 0;
}

static struct nbdkit_plugin plugin = {
  .name              = "guestfs",
  .longname          = "nbdkit guestfs plugin",
  .version           = PACKAGE_VERSION,
  .config            = plugin_guestfs_config,
  .config_complete   = plugin_guestfs_config_complete,
  .config_help       = plugin_guestfs_config_help,
  .unload            = plugin_guestfs_unload,
  .open              = plugin_guestfs_open,
  .close             = plugin_guestfs_close,
  .get_size          = plugin_guestfs_get_size,
  .pread             = plugin_guestfs_pread,
  .pwrite            = plugin_guestfs_pwrite,
  .flush             = plugin_guestfs_flush,
};

NBDKIT_REGISTER_PLUGIN (plugin)