summaryrefslogtreecommitdiffstats
path: root/rescue/virt-rescue.c
blob: efde9837acc005027503a618b27d29c916590265 (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
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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
/* virt-rescue
 * Copyright (C) 2010-2011 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <unistd.h>
#include <getopt.h>
#include <errno.h>
#include <locale.h>
#include <assert.h>
#include <libintl.h>

#include "ignore-value.h"
#include "progname.h"
#include "xvasprintf.h"

#include "guestfs.h"
#include "options.h"

static void do_suggestion (struct drv *drvs);

/* Currently open libguestfs handle. */
guestfs_h *g;

int read_only = 0;
int live = 0;
int verbose = 0;
int keys_from_stdin = 0;
int echo_keys = 0;
const char *libvirt_uri = NULL;
int inspector = 0;

static inline char *
bad_cast (char const *s)
{
  return (char *) s;
}

static void __attribute__((noreturn))
usage (int status)
{
  if (status != EXIT_SUCCESS)
    fprintf (stderr, _("Try `%s --help' for more information.\n"),
             program_name);
  else {
    fprintf (stdout,
           _("%s: Run a rescue shell on a virtual machine\n"
             "Copyright (C) 2009-2010 Red Hat Inc.\n"
             "Usage:\n"
             "  %s [--options] -d domname\n"
             "  %s [--options] -a disk.img [-a disk.img ...]\n"
             "Options:\n"
             "  -a|--add image       Add image\n"
             "  --append kernelopts  Append kernel options\n"
             "  -c|--connect uri     Specify libvirt URI for -d option\n"
             "  -d|--domain guest    Add disks from libvirt guest\n"
             "  --format[=raw|..]    Force disk format for -a option\n"
             "  --help               Display brief help\n"
             "  -m|--memsize MB      Set memory size in megabytes\n"
             "  --network            Enable network\n"
             "  -r|--ro              Access read-only\n"
             "  --selinux            Enable SELinux\n"
             "  --smp N              Enable SMP with N >= 2 virtual CPUs\n"
             "  --suggest            Suggest mount commands for this guest\n"
             "  -v|--verbose         Verbose messages\n"
             "  -V|--version         Display version and exit\n"
             "  -w|--rw              Mount read-write\n"
             "  -x                   Trace libguestfs API calls\n"
             "For more information, see the manpage %s(1).\n"),
             program_name, program_name, program_name,
             program_name);
  }
  exit (status);
}

int
main (int argc, char *argv[])
{
  /* Set global program name that is not polluted with libtool artifacts.  */
  set_program_name (argv[0]);

  setlocale (LC_ALL, "");
  bindtextdomain (PACKAGE, LOCALEBASEDIR);
  textdomain (PACKAGE);

  parse_config ();

  enum { HELP_OPTION = CHAR_MAX + 1 };

  static const char *options = "a:c:d:m:rvVx";
  static const struct option long_options[] = {
    { "add", 1, 0, 'a' },
    { "append", 1, 0, 0 },
    { "connect", 1, 0, 'c' },
    { "domain", 1, 0, 'd' },
    { "format", 2, 0, 0 },
    { "help", 0, 0, HELP_OPTION },
    { "memsize", 1, 0, 'm' },
    { "network", 0, 0, 0 },
    { "ro", 0, 0, 'r' },
    { "rw", 0, 0, 'w' },
    { "selinux", 0, 0, 0 },
    { "smp", 1, 0, 0 },
    { "suggest", 0, 0, 0 },
    { "verbose", 0, 0, 'v' },
    { "version", 0, 0, 'V' },
    { 0, 0, 0, 0 }
  };
  struct drv *drvs = NULL;
  struct drv *drv;
  const char *format = NULL;
  int c;
  int option_index;
  int network = 0;
  const char *append = NULL;
  char *append_full;
  int memsize = 0;
  int smp = 0;
  int suggest = 0;

  g = guestfs_create ();
  if (g == NULL) {
    fprintf (stderr, _("guestfs_create: failed to create handle\n"));
    exit (EXIT_FAILURE);
  }

  argv[0] = bad_cast (program_name);

  for (;;) {
    c = getopt_long (argc, argv, options, long_options, &option_index);
    if (c == -1) break;

    switch (c) {
    case 0:			/* options which are long only */
      if (STREQ (long_options[option_index].name, "selinux")) {
        guestfs_set_selinux (g, 1);
      } else if (STREQ (long_options[option_index].name, "append")) {
        append = optarg;
      } else if (STREQ (long_options[option_index].name, "network")) {
        network = 1;
      } else if (STREQ (long_options[option_index].name, "format")) {
        if (!optarg || STREQ (optarg, ""))
          format = NULL;
        else
          format = optarg;
      } else if (STREQ (long_options[option_index].name, "smp")) {
        if (sscanf (optarg, "%u", &smp) != 1) {
          fprintf (stderr, _("%s: could not parse --smp parameter '%s'\n"),
                   program_name, optarg);
          exit (EXIT_FAILURE);
        }
        if (smp < 1) {
          fprintf (stderr, _("%s: --smp parameter '%s' should be >= 1\n"),
                   program_name, optarg);
          exit (EXIT_FAILURE);
        }
      } else if (STREQ (long_options[option_index].name, "suggest")) {
        suggest = 1;
      } else {
        fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
                 program_name, long_options[option_index].name, option_index);
        exit (EXIT_FAILURE);
      }
      break;

    case 'a':
      OPTION_a;
      break;

    case 'c':
      OPTION_c;
      break;

    case 'd':
      OPTION_d;
      break;

    case 'h':
      usage (EXIT_SUCCESS);

    case 'm':
      if (sscanf (optarg, "%u", &memsize) != 1) {
        fprintf (stderr, _("%s: could not parse memory size '%s'\n"),
                 program_name, optarg);
        exit (EXIT_FAILURE);
      }
      break;

    case 'r':
      OPTION_r;
      break;

    case 'v':
      OPTION_v;
      break;

    case 'V':
      OPTION_V;
      break;

    case 'w':
      OPTION_w;
      break;

    case 'x':
      OPTION_x;
      break;

    case HELP_OPTION:
      usage (EXIT_SUCCESS);

    default:
      usage (EXIT_FAILURE);
    }
  }

  /* Old-style syntax?  There were no -a or -d options in the old
   * virt-rescue which is how we detect this.
   */
  if (drvs == NULL) {
    while (optind < argc) {
      if (strchr (argv[optind], '/') ||
          access (argv[optind], F_OK) == 0) { /* simulate -a option */
        drv = calloc (1, sizeof (struct drv));
        if (!drv) {
          perror ("malloc");
          exit (EXIT_FAILURE);
        }
        drv->type = drv_a;
        drv->a.filename = argv[optind];
        drv->next = drvs;
        drvs = drv;
      } else {                  /* simulate -d option */
        drv = calloc (1, sizeof (struct drv));
        if (!drv) {
          perror ("malloc");
          exit (EXIT_FAILURE);
        }
        drv->type = drv_d;
        drv->d.guest = argv[optind];
        drv->next = drvs;
        drvs = drv;
      }

      optind++;
    }
  }

  /* --suggest flag */
  if (suggest) {
    do_suggestion (drvs);
    exit (EXIT_SUCCESS);
  }

  /* These are really constants, but they have to be variables for the
   * options parsing code.  Assert here that they have known-good
   * values.
   */
  assert (inspector == 0);
  assert (keys_from_stdin == 0);
  assert (echo_keys == 0);
  assert (live == 0);

  /* Must be no extra arguments on the command line. */
  if (optind != argc)
    usage (EXIT_FAILURE);

  /* User must have specified some drives. */
  if (drvs == NULL)
    usage (EXIT_FAILURE);

  /* Setting "direct mode" is required for the rescue appliance. */
  guestfs_set_direct (g, 1);

  /* Set other features. */
  if (memsize > 0)
    guestfs_set_memsize (g, memsize);
  if (network)
    guestfs_set_network (g, 1);
  if (smp >= 1)
    guestfs_set_smp (g, smp);

  /* Kernel command line must include guestfs_rescue=1 (see
   * appliance/init) as well as other options.
   */
  append_full = xasprintf ("guestfs_rescue=1%s%s",
                           append ? " " : "",
                           append ? append : "");
  guestfs_set_append (g, append_full);
  free (append_full);

  /* Add drives. */
  add_drives (drvs, 'a');

  /* Free up data structures, no longer needed after this point. */
  free_drives (drvs);

  /* Run the appliance.  This won't return until the user quits the
   * appliance.
   */
  guestfs_set_error_handler (g, NULL, NULL);

  /* We expect launch to fail, so ignore the return value. */
  ignore_value (guestfs_launch (g));

  guestfs_close (g);

  exit (EXIT_SUCCESS);
}

static void suggest_filesystems (void);

static int
compare_keys_len (const void *p1, const void *p2)
{
  const char *key1 = * (char * const *) p1;
  const char *key2 = * (char * const *) p2;
  return strlen (key1) - strlen (key2);
}

static size_t
count_strings (char *const *argv)
{
  size_t i;

  for (i = 0; argv[i]; ++i)
    ;
  return i;
}

/* virt-rescue --suggest flag does a kind of inspection on the
 * drives and suggests mount commands that you should use.
 */
static void
do_suggestion (struct drv *drvs)
{
  char **roots;
  size_t i, j;
  char *type, *distro, *product_name;
  int major, minor;
  char **mps;

  /* For inspection, force add_drives to add the drives read-only. */
  read_only = 1;

  /* Add drives. */
  add_drives (drvs, 'a');

  /* Free up data structures, no longer needed after this point. */
  free_drives (drvs);

  printf (_("Inspecting the virtual machine or disk image ...\n\n"));
  fflush (stdout);

  if (guestfs_launch (g) == -1)
    exit (EXIT_FAILURE);

  /* Don't use inspect_mount, since for virt-rescue we should allow
   * arbitrary disks and disks with more than one OS on them.  Let's
   * do this using the basic API instead.
   */
  roots = guestfs_inspect_os (g);
  if (roots == NULL)
    exit (EXIT_FAILURE);

  if (roots[0] == NULL) {
    suggest_filesystems ();
    return;
  }

  printf (_("This disk contains one or more operating systems.  You can use these mount\n"
            "commands in virt-rescue (at the ><rescue> prompt) to mount the filesystems.\n\n"));

  for (i = 0; roots[i] != NULL; ++i) {
    type = guestfs_inspect_get_type (g, roots[i]);
    distro = guestfs_inspect_get_distro (g, roots[i]);
    product_name = guestfs_inspect_get_product_name (g, roots[i]);
    major = guestfs_inspect_get_major_version (g, roots[i]);
    minor = guestfs_inspect_get_minor_version (g, roots[i]);

    printf (_("# %s is the root of a %s operating system\n"
              "# type: %s, distro: %s, version: %d.%d\n"
              "# %s\n\n"),
            roots[i], type ? : "unknown",
            type ? : "unknown", distro ? : "unknown", major, minor,
            product_name ? : "");

    mps = guestfs_inspect_get_mountpoints (g, roots[i]);
    if (mps == NULL)
      exit (EXIT_FAILURE);

    /* Sort by key length, shortest key first, so that we end up
     * mounting the filesystems in the correct order.
     */
    qsort (mps, count_strings (mps) / 2, 2 * sizeof (char *),
           compare_keys_len);

    for (j = 0; mps[j] != NULL; j += 2) {
      printf ("mount %s /sysroot%s\n", mps[j+1], mps[j]);
      free (mps[j]);
      free (mps[j+1]);
    }

    /* If it's Linux, print the bind-mounts. */
    if (type && STREQ (type, "linux")) {
      printf ("mount --bind /dev /sysroot/dev\n");
      printf ("mount --bind /dev/pts /sysroot/dev/pts\n");
      printf ("mount --bind /proc /sysroot/proc\n");
      printf ("mount --bind /sys /sysroot/sys\n");
    }

    printf ("\n");

    free (type);
    free (distro);
    free (product_name);
    free (roots[i]);
  }

  free (roots);
}

/* Inspection failed, so it doesn't contain any OS that we recognise.
 * However there might still be filesystems so print some suggestions
 * for those.
 */
static void
suggest_filesystems (void)
{
  char **fses;
  size_t i;

  fses = guestfs_list_filesystems (g);
  if (fses == NULL)
    exit (EXIT_FAILURE);

  if (fses[0] == NULL) {
    printf (_("This disk contains no filesystems that we recognize.\n\n"
              "However you can still use virt-rescue on the disk image, to try to mount\n"
              "filesystems that are not recognized by libguestfs, or to create partitions,\n"
              "logical volumes and filesystems on a blank disk.\n"));
    return;
  }

  printf (_("This disk contains one or more filesystems, but we don't recognize any\n"
            "operating system.  You can use these mount commands in virt-rescue (at the\n"
            "><rescue> prompt) to mount these filesystems.\n\n"));

  for (i = 0; fses[i] != NULL; i += 2) {
    printf (_("# %s has type '%s'\n"), fses[i], fses[i+1]);

    if (STRNEQ (fses[i+1], "swap") && STRNEQ (fses[i+1], "unknown"))
      printf ("mount %s /sysroot\n", fses[i]);

    printf ("\n");

    free (fses[i]);
    free (fses[i+1]);
  }
  free (fses);
}


/* The following was a nice idea, but in fact it doesn't work.  This is
 * because qemu has some (broken) pty emulation itself.
 */
#if 0
  int fd_m, fd_s, r;
  pid_t pid;
  struct termios tsorig, tsnew;

  /* Set up pty. */
  fd_m = posix_openpt (O_RDWR);
  if (fd_m == -1) {
    perror ("posix_openpt");
    exit (EXIT_FAILURE);
  }
  r = grantpt (fd_m);
  if (r == -1) {
    perror ("grantpt");
    exit (EXIT_FAILURE);
  }
  r = unlockpt (fd_m);
  if (r == -1) {
    perror ("unlockpt");
    exit (EXIT_FAILURE);
  }
  fd_s = open (ptsname (fd_m), O_RDWR);
  if (fd_s == -1) {
    perror ("open ptsname");
    exit (EXIT_FAILURE);
  }

  pid = fork ();
  if (pid == -1) {
    perror ("fork");
    exit (EXIT_FAILURE);
  }
  if (pid == 0) {
    /* Child process. */

#if 1
    /* Set raw mode. */
    r = tcgetattr (fd_s, &tsorig);
    tsnew = tsorig;
    cfmakeraw (&tsnew);
    tcsetattr (fd_s, TCSANOW, &tsnew);
#endif

    /* Close the master side of pty and set slave side as
     * stdin/stdout/stderr.
     */
    close (fd_m);

    close (0);
    close (1);
    close (2);
    if (dup (fd_s) == -1 || dup (fd_s) == -1 || dup (fd_s) == -1) {
      perror ("dup");
      exit (EXIT_FAILURE);
    }
    close (fd_s);

#if 1
    if (setsid () == -1)
      perror ("warning: failed to setsid");
    if (ioctl (0, TIOCSCTTY, 0) == -1)
      perror ("warning: failed to TIOCSCTTY");
#endif

    /* Run the appliance.  This won't return until the user quits the
     * appliance.
     */
    guestfs_set_error_handler (g, NULL, NULL);
    r = guestfs_launch (g);

    /* launch() expects guestfsd to start. However, virt-rescue doesn't
     * run guestfsd, so this will always fail with ECHILD when the
     * appliance exits unexpectedly.
     */
    if (errno != ECHILD) {
      fprintf (stderr, "%s: %s\n", program_name, guestfs_last_error (g));
      guestfs_close (g);
      exit (EXIT_FAILURE);
    }

    guestfs_close (g);
    _exit (EXIT_SUCCESS);
  }

  /* Parent process continues ... */

  /* Close slave side of pty. */
  close (fd_s);

  /* Set raw mode. */
  r = tcgetattr (fd_s, &tsorig);
  tsnew = tsorig;
  cfmakeraw (&tsnew);
  tcsetattr (fd_s, TCSANOW, &tsnew);

  /* Send input and output to master side of pty. */
  r = multiplex (fd_m);
  tcsetattr (fd_s, TCSANOW, &tsorig); /* Restore cooked mode. */
  if (r == -1)
    exit (EXIT_FAILURE);

  if (waitpid (pid, &r, 0) == -1) {
    perror ("waitpid");
    exit (EXIT_FAILURE);
  }
  if (!WIFEXITED (r)) {
    /* abnormal child exit */
    fprintf (stderr, _("%s: unknown child exit status (%d)\n"),
             program_name, r);
    exit (EXIT_FAILURE);
  }
  else
    exit (WEXITSTATUS (r)); /* normal exit, return child process's status */
}

/* Naive and simple multiplex function. */
static int
multiplex (int fd_m)
{
  int r, eof_stdin = 0;
  fd_set rfds, wfds;
  char tobuf[BUFSIZ], frombuf[BUFSIZ]; /* to/from slave */
  size_t tosize = 0, fromsize = 0;
  ssize_t n;
  size_t count;
  long flags_0, flags_1, flags_fd_m;

  flags_0 = fcntl (0, F_GETFL);
  fcntl (0, F_SETFL, O_NONBLOCK | flags_0);
  flags_1 = fcntl (0, F_GETFL);
  fcntl (1, F_SETFL, O_NONBLOCK | flags_1);
  flags_fd_m = fcntl (0, F_GETFL);
  fcntl (fd_m, F_SETFL, O_NONBLOCK | flags_fd_m);

  for (;;) {
    FD_ZERO (&rfds);
    FD_ZERO (&wfds);

    /* Still space in to-buffer?  If so, we can read from the user. */
    if (!eof_stdin && tosize < BUFSIZ)
      FD_SET (0, &rfds);
    /* Still space in from-buffer?  If so, we can read from the slave. */
    if (fromsize < BUFSIZ)
      FD_SET (fd_m, &rfds);
    /* Content in to-buffer?  If so, we want to write to the slave. */
    if (tosize > 0)
      FD_SET (fd_m, &wfds);
    /* Content in from-buffer?  If so, we want to write to the user. */
    if (fromsize > 0)
      FD_SET (1, &wfds);

    r = select (fd_m+1, &rfds, &wfds, NULL, NULL);
    if (r == -1) {
      if (errno == EAGAIN || errno == EINTR)
        continue;
      perror ("select");
      return -1;
    }

    /* Input from user: Put it in the to-buffer. */
    if (FD_ISSET (0, &rfds)) {
      count = BUFSIZ - tosize;
      n = read (0, &tobuf[tosize], count);
      if (n == -1) {
        perror ("read");
        return -1;
      }
      if (n == 0) { /* stdin was closed */
        eof_stdin = 1;
        /* This is what telnetd does ... */
        tobuf[tosize] = '\004';
        tosize += 1;
      } else
        tosize += n;
    }

    /* Input from slave: Put it in the from-buffer. */
    if (FD_ISSET (fd_m, &rfds)) {
      count = BUFSIZ - fromsize;
      n = read (fd_m, &frombuf[fromsize], count);
      if (n == -1) {
        if (errno != EIO) /* EIO if slave process dies */
          perror ("read");
        break;
      }
      if (n == 0) /* slave closed the connection */
        break;
      fromsize += n;
    }

    /* Can write to user. */
    if (FD_ISSET (1, &wfds)) {
      n = write (1, frombuf, fromsize);
      if (n == -1) {
        perror ("write");
        return -1;
      }
      memmove (frombuf, &frombuf[n], BUFSIZ - n);
      fromsize -= n;
    }

    /* Can write to slave. */
    if (FD_ISSET (fd_m, &wfds)) {
      n = write (fd_m, tobuf, tosize);
      if (n == -1) {
        perror ("write");
        return -1;
      }
      memmove (tobuf, &tobuf[n], BUFSIZ - n);
      tosize -= n;
    }
  } /* for (;;) */

  /* We end up here when slave has closed the connection. */
  close (fd_m);

  /* Restore blocking behaviour. */
  fcntl (1, F_SETFL, flags_1);

  /* Last chance to write out any remaining data in the buffers, but
   * don't bother about errors.
   */
  ignore_value (write (1, frombuf, fromsize));

  return 0;
}
#endif