Update FSF address.
[libguestfs.git] / rescue / virt-rescue.c
1 /* virt-rescue
2  * Copyright (C) 2010-2011 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <inttypes.h>
25 #include <unistd.h>
26 #include <getopt.h>
27 #include <errno.h>
28 #include <locale.h>
29 #include <assert.h>
30 #include <libintl.h>
31
32 #include "ignore-value.h"
33 #include "progname.h"
34 #include "xvasprintf.h"
35
36 #include "guestfs.h"
37 #include "options.h"
38
39 static void do_suggestion (struct drv *drvs);
40
41 /* Currently open libguestfs handle. */
42 guestfs_h *g;
43
44 int read_only = 0;
45 int live = 0;
46 int verbose = 0;
47 int keys_from_stdin = 0;
48 int echo_keys = 0;
49 const char *libvirt_uri = NULL;
50 int inspector = 0;
51
52 static inline char *
53 bad_cast (char const *s)
54 {
55   return (char *) s;
56 }
57
58 static void __attribute__((noreturn))
59 usage (int status)
60 {
61   if (status != EXIT_SUCCESS)
62     fprintf (stderr, _("Try `%s --help' for more information.\n"),
63              program_name);
64   else {
65     fprintf (stdout,
66            _("%s: Run a rescue shell on a virtual machine\n"
67              "Copyright (C) 2009-2010 Red Hat Inc.\n"
68              "Usage:\n"
69              "  %s [--options] -d domname\n"
70              "  %s [--options] -a disk.img [-a disk.img ...]\n"
71              "Options:\n"
72              "  -a|--add image       Add image\n"
73              "  --append kernelopts  Append kernel options\n"
74              "  -c|--connect uri     Specify libvirt URI for -d option\n"
75              "  -d|--domain guest    Add disks from libvirt guest\n"
76              "  --format[=raw|..]    Force disk format for -a option\n"
77              "  --help               Display brief help\n"
78              "  -m|--memsize MB      Set memory size in megabytes\n"
79              "  --network            Enable network\n"
80              "  -r|--ro              Access read-only\n"
81              "  --selinux            Enable SELinux\n"
82              "  --smp N              Enable SMP with N >= 2 virtual CPUs\n"
83              "  --suggest            Suggest mount commands for this guest\n"
84              "  -v|--verbose         Verbose messages\n"
85              "  -V|--version         Display version and exit\n"
86              "  -w|--rw              Mount read-write\n"
87              "  -x                   Trace libguestfs API calls\n"
88              "For more information, see the manpage %s(1).\n"),
89              program_name, program_name, program_name,
90              program_name);
91   }
92   exit (status);
93 }
94
95 int
96 main (int argc, char *argv[])
97 {
98   /* Set global program name that is not polluted with libtool artifacts.  */
99   set_program_name (argv[0]);
100
101   setlocale (LC_ALL, "");
102   bindtextdomain (PACKAGE, LOCALEBASEDIR);
103   textdomain (PACKAGE);
104
105   parse_config ();
106
107   enum { HELP_OPTION = CHAR_MAX + 1 };
108
109   static const char *options = "a:c:d:m:rvVx";
110   static const struct option long_options[] = {
111     { "add", 1, 0, 'a' },
112     { "append", 1, 0, 0 },
113     { "connect", 1, 0, 'c' },
114     { "domain", 1, 0, 'd' },
115     { "format", 2, 0, 0 },
116     { "help", 0, 0, HELP_OPTION },
117     { "memsize", 1, 0, 'm' },
118     { "network", 0, 0, 0 },
119     { "ro", 0, 0, 'r' },
120     { "rw", 0, 0, 'w' },
121     { "selinux", 0, 0, 0 },
122     { "smp", 1, 0, 0 },
123     { "suggest", 0, 0, 0 },
124     { "verbose", 0, 0, 'v' },
125     { "version", 0, 0, 'V' },
126     { 0, 0, 0, 0 }
127   };
128   struct drv *drvs = NULL;
129   struct drv *drv;
130   const char *format = NULL;
131   int c;
132   int option_index;
133   int network = 0;
134   const char *append = NULL;
135   char *append_full;
136   int memsize = 0;
137   int smp = 0;
138   int suggest = 0;
139
140   g = guestfs_create ();
141   if (g == NULL) {
142     fprintf (stderr, _("guestfs_create: failed to create handle\n"));
143     exit (EXIT_FAILURE);
144   }
145
146   argv[0] = bad_cast (program_name);
147
148   for (;;) {
149     c = getopt_long (argc, argv, options, long_options, &option_index);
150     if (c == -1) break;
151
152     switch (c) {
153     case 0:                     /* options which are long only */
154       if (STREQ (long_options[option_index].name, "selinux")) {
155         guestfs_set_selinux (g, 1);
156       } else if (STREQ (long_options[option_index].name, "append")) {
157         append = optarg;
158       } else if (STREQ (long_options[option_index].name, "network")) {
159         network = 1;
160       } else if (STREQ (long_options[option_index].name, "format")) {
161         if (!optarg || STREQ (optarg, ""))
162           format = NULL;
163         else
164           format = optarg;
165       } else if (STREQ (long_options[option_index].name, "smp")) {
166         if (sscanf (optarg, "%u", &smp) != 1) {
167           fprintf (stderr, _("%s: could not parse --smp parameter '%s'\n"),
168                    program_name, optarg);
169           exit (EXIT_FAILURE);
170         }
171         if (smp < 1) {
172           fprintf (stderr, _("%s: --smp parameter '%s' should be >= 1\n"),
173                    program_name, optarg);
174           exit (EXIT_FAILURE);
175         }
176       } else if (STREQ (long_options[option_index].name, "suggest")) {
177         suggest = 1;
178       } else {
179         fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
180                  program_name, long_options[option_index].name, option_index);
181         exit (EXIT_FAILURE);
182       }
183       break;
184
185     case 'a':
186       OPTION_a;
187       break;
188
189     case 'c':
190       OPTION_c;
191       break;
192
193     case 'd':
194       OPTION_d;
195       break;
196
197     case 'h':
198       usage (EXIT_SUCCESS);
199
200     case 'm':
201       if (sscanf (optarg, "%u", &memsize) != 1) {
202         fprintf (stderr, _("%s: could not parse memory size '%s'\n"),
203                  program_name, optarg);
204         exit (EXIT_FAILURE);
205       }
206       break;
207
208     case 'r':
209       OPTION_r;
210       break;
211
212     case 'v':
213       OPTION_v;
214       break;
215
216     case 'V':
217       OPTION_V;
218       break;
219
220     case 'w':
221       OPTION_w;
222       break;
223
224     case 'x':
225       OPTION_x;
226       break;
227
228     case HELP_OPTION:
229       usage (EXIT_SUCCESS);
230
231     default:
232       usage (EXIT_FAILURE);
233     }
234   }
235
236   /* Old-style syntax?  There were no -a or -d options in the old
237    * virt-rescue which is how we detect this.
238    */
239   if (drvs == NULL) {
240     while (optind < argc) {
241       if (strchr (argv[optind], '/') ||
242           access (argv[optind], F_OK) == 0) { /* simulate -a option */
243         drv = malloc (sizeof (struct drv));
244         if (!drv) {
245           perror ("malloc");
246           exit (EXIT_FAILURE);
247         }
248         drv->type = drv_a;
249         drv->a.filename = argv[optind];
250         drv->a.format = NULL;
251         drv->next = drvs;
252         drvs = drv;
253       } else {                  /* simulate -d option */
254         drv = malloc (sizeof (struct drv));
255         if (!drv) {
256           perror ("malloc");
257           exit (EXIT_FAILURE);
258         }
259         drv->type = drv_d;
260         drv->d.guest = argv[optind];
261         drv->next = drvs;
262         drvs = drv;
263       }
264
265       optind++;
266     }
267   }
268
269   /* --suggest flag */
270   if (suggest) {
271     do_suggestion (drvs);
272     exit (EXIT_SUCCESS);
273   }
274
275   /* These are really constants, but they have to be variables for the
276    * options parsing code.  Assert here that they have known-good
277    * values.
278    */
279   assert (inspector == 0);
280   assert (keys_from_stdin == 0);
281   assert (echo_keys == 0);
282   assert (live == 0);
283
284   /* Must be no extra arguments on the command line. */
285   if (optind != argc)
286     usage (EXIT_FAILURE);
287
288   /* User must have specified some drives. */
289   if (drvs == NULL)
290     usage (EXIT_FAILURE);
291
292   /* Setting "direct mode" is required for the rescue appliance. */
293   guestfs_set_direct (g, 1);
294
295   /* Set other features. */
296   if (memsize > 0)
297     guestfs_set_memsize (g, memsize);
298   if (network)
299     guestfs_set_network (g, 1);
300   if (smp >= 1)
301     guestfs_set_smp (g, smp);
302
303   /* Kernel command line must include guestfs_rescue=1 (see
304    * appliance/init) as well as other options.
305    */
306   append_full = xasprintf ("guestfs_rescue=1%s%s",
307                            append ? " " : "",
308                            append ? append : "");
309   guestfs_set_append (g, append_full);
310   free (append_full);
311
312   /* Add drives. */
313   add_drives (drvs, 'a');
314
315   /* Free up data structures, no longer needed after this point. */
316   free_drives (drvs);
317
318   /* Run the appliance.  This won't return until the user quits the
319    * appliance.
320    */
321   guestfs_set_error_handler (g, NULL, NULL);
322
323   /* We expect launch to fail, so ignore the return value. */
324   ignore_value (guestfs_launch (g));
325
326   guestfs_close (g);
327
328   exit (EXIT_SUCCESS);
329 }
330
331 static void suggest_filesystems (void);
332
333 static int
334 compare_keys_len (const void *p1, const void *p2)
335 {
336   const char *key1 = * (char * const *) p1;
337   const char *key2 = * (char * const *) p2;
338   return strlen (key1) - strlen (key2);
339 }
340
341 static size_t
342 count_strings (char *const *argv)
343 {
344   size_t i;
345
346   for (i = 0; argv[i]; ++i)
347     ;
348   return i;
349 }
350
351 /* virt-rescue --suggest flag does a kind of inspection on the
352  * drives and suggests mount commands that you should use.
353  */
354 static void
355 do_suggestion (struct drv *drvs)
356 {
357   char **roots;
358   size_t i, j;
359   char *type, *distro, *product_name;
360   int major, minor;
361   char **mps;
362
363   /* For inspection, force add_drives to add the drives read-only. */
364   read_only = 1;
365
366   /* Add drives. */
367   add_drives (drvs, 'a');
368
369   /* Free up data structures, no longer needed after this point. */
370   free_drives (drvs);
371
372   printf (_("Inspecting the virtual machine or disk image ...\n\n"));
373   fflush (stdout);
374
375   if (guestfs_launch (g) == -1)
376     exit (EXIT_FAILURE);
377
378   /* Don't use inspect_mount, since for virt-rescue we should allow
379    * arbitrary disks and disks with more than one OS on them.  Let's
380    * do this using the basic API instead.
381    */
382   roots = guestfs_inspect_os (g);
383   if (roots == NULL)
384     exit (EXIT_FAILURE);
385
386   if (roots[0] == NULL) {
387     suggest_filesystems ();
388     return;
389   }
390
391   printf (_("This disk contains one or more operating systems.  You can use these mount\n"
392             "commands in virt-rescue (at the ><rescue> prompt) to mount the filesystems.\n\n"));
393
394   for (i = 0; roots[i] != NULL; ++i) {
395     type = guestfs_inspect_get_type (g, roots[i]);
396     distro = guestfs_inspect_get_distro (g, roots[i]);
397     product_name = guestfs_inspect_get_product_name (g, roots[i]);
398     major = guestfs_inspect_get_major_version (g, roots[i]);
399     minor = guestfs_inspect_get_minor_version (g, roots[i]);
400
401     printf (_("# %s is the root of a %s operating system\n"
402               "# type: %s, distro: %s, version: %d.%d\n"
403               "# %s\n\n"),
404             roots[i], type ? : "unknown",
405             type ? : "unknown", distro ? : "unknown", major, minor,
406             product_name ? : "");
407
408     mps = guestfs_inspect_get_mountpoints (g, roots[i]);
409     if (mps == NULL)
410       exit (EXIT_FAILURE);
411
412     /* Sort by key length, shortest key first, so that we end up
413      * mounting the filesystems in the correct order.
414      */
415     qsort (mps, count_strings (mps) / 2, 2 * sizeof (char *),
416            compare_keys_len);
417
418     for (j = 0; mps[j] != NULL; j += 2) {
419       printf ("mount %s /sysroot%s\n", mps[j+1], mps[j]);
420       free (mps[j]);
421       free (mps[j+1]);
422     }
423
424     /* If it's Linux, print the bind-mounts. */
425     if (type && STREQ (type, "linux")) {
426       printf ("mount --bind /dev /sysroot/dev\n");
427       printf ("mount --bind /dev/pts /sysroot/dev/pts\n");
428       printf ("mount --bind /proc /sysroot/proc\n");
429       printf ("mount --bind /sys /sysroot/sys\n");
430     }
431
432     printf ("\n");
433
434     free (type);
435     free (distro);
436     free (product_name);
437     free (roots[i]);
438   }
439
440   free (roots);
441 }
442
443 /* Inspection failed, so it doesn't contain any OS that we recognise.
444  * However there might still be filesystems so print some suggestions
445  * for those.
446  */
447 static void
448 suggest_filesystems (void)
449 {
450   char **fses;
451   size_t i;
452
453   fses = guestfs_list_filesystems (g);
454   if (fses == NULL)
455     exit (EXIT_FAILURE);
456
457   if (fses[0] == NULL) {
458     printf (_("This disk contains no filesystems that we recognize.\n\n"
459               "However you can still use virt-rescue on the disk image, to try to mount\n"
460               "filesystems that are not recognized by libguestfs, or to create partitions,\n"
461               "logical volumes and filesystems on a blank disk.\n"));
462     return;
463   }
464
465   printf (_("This disk contains one or more filesystems, but we don't recognize any\n"
466             "operating system.  You can use these mount commands in virt-rescue (at the\n"
467             "><rescue> prompt) to mount these filesystems.\n\n"));
468
469   for (i = 0; fses[i] != NULL; i += 2) {
470     printf (_("# %s has type '%s'\n"), fses[i], fses[i+1]);
471
472     if (STRNEQ (fses[i+1], "swap") && STRNEQ (fses[i+1], "unknown"))
473       printf ("mount %s /sysroot\n", fses[i]);
474
475     printf ("\n");
476
477     free (fses[i]);
478     free (fses[i+1]);
479   }
480   free (fses);
481 }
482
483
484 /* The following was a nice idea, but in fact it doesn't work.  This is
485  * because qemu has some (broken) pty emulation itself.
486  */
487 #if 0
488   int fd_m, fd_s, r;
489   pid_t pid;
490   struct termios tsorig, tsnew;
491
492   /* Set up pty. */
493   fd_m = posix_openpt (O_RDWR);
494   if (fd_m == -1) {
495     perror ("posix_openpt");
496     exit (EXIT_FAILURE);
497   }
498   r = grantpt (fd_m);
499   if (r == -1) {
500     perror ("grantpt");
501     exit (EXIT_FAILURE);
502   }
503   r = unlockpt (fd_m);
504   if (r == -1) {
505     perror ("unlockpt");
506     exit (EXIT_FAILURE);
507   }
508   fd_s = open (ptsname (fd_m), O_RDWR);
509   if (fd_s == -1) {
510     perror ("open ptsname");
511     exit (EXIT_FAILURE);
512   }
513
514   pid = fork ();
515   if (pid == -1) {
516     perror ("fork");
517     exit (EXIT_FAILURE);
518   }
519   if (pid == 0) {
520     /* Child process. */
521
522 #if 1
523     /* Set raw mode. */
524     r = tcgetattr (fd_s, &tsorig);
525     tsnew = tsorig;
526     cfmakeraw (&tsnew);
527     tcsetattr (fd_s, TCSANOW, &tsnew);
528 #endif
529
530     /* Close the master side of pty and set slave side as
531      * stdin/stdout/stderr.
532      */
533     close (fd_m);
534
535     close (0);
536     close (1);
537     close (2);
538     if (dup (fd_s) == -1 || dup (fd_s) == -1 || dup (fd_s) == -1) {
539       perror ("dup");
540       exit (EXIT_FAILURE);
541     }
542     close (fd_s);
543
544 #if 1
545     if (setsid () == -1)
546       perror ("warning: failed to setsid");
547     if (ioctl (0, TIOCSCTTY, 0) == -1)
548       perror ("warning: failed to TIOCSCTTY");
549 #endif
550
551     /* Run the appliance.  This won't return until the user quits the
552      * appliance.
553      */
554     guestfs_set_error_handler (g, NULL, NULL);
555     r = guestfs_launch (g);
556
557     /* launch() expects guestfsd to start. However, virt-rescue doesn't
558      * run guestfsd, so this will always fail with ECHILD when the
559      * appliance exits unexpectedly.
560      */
561     if (errno != ECHILD) {
562       fprintf (stderr, "%s: %s\n", program_name, guestfs_last_error (g));
563       guestfs_close (g);
564       exit (EXIT_FAILURE);
565     }
566
567     guestfs_close (g);
568     _exit (EXIT_SUCCESS);
569   }
570
571   /* Parent process continues ... */
572
573   /* Close slave side of pty. */
574   close (fd_s);
575
576   /* Set raw mode. */
577   r = tcgetattr (fd_s, &tsorig);
578   tsnew = tsorig;
579   cfmakeraw (&tsnew);
580   tcsetattr (fd_s, TCSANOW, &tsnew);
581
582   /* Send input and output to master side of pty. */
583   r = multiplex (fd_m);
584   tcsetattr (fd_s, TCSANOW, &tsorig); /* Restore cooked mode. */
585   if (r == -1)
586     exit (EXIT_FAILURE);
587
588   if (waitpid (pid, &r, 0) == -1) {
589     perror ("waitpid");
590     exit (EXIT_FAILURE);
591   }
592   if (!WIFEXITED (r)) {
593     /* abnormal child exit */
594     fprintf (stderr, _("%s: unknown child exit status (%d)\n"),
595              program_name, r);
596     exit (EXIT_FAILURE);
597   }
598   else
599     exit (WEXITSTATUS (r)); /* normal exit, return child process's status */
600 }
601
602 /* Naive and simple multiplex function. */
603 static int
604 multiplex (int fd_m)
605 {
606   int r, eof_stdin = 0;
607   fd_set rfds, wfds;
608   char tobuf[BUFSIZ], frombuf[BUFSIZ]; /* to/from slave */
609   size_t tosize = 0, fromsize = 0;
610   ssize_t n;
611   size_t count;
612   long flags_0, flags_1, flags_fd_m;
613
614   flags_0 = fcntl (0, F_GETFL);
615   fcntl (0, F_SETFL, O_NONBLOCK | flags_0);
616   flags_1 = fcntl (0, F_GETFL);
617   fcntl (1, F_SETFL, O_NONBLOCK | flags_1);
618   flags_fd_m = fcntl (0, F_GETFL);
619   fcntl (fd_m, F_SETFL, O_NONBLOCK | flags_fd_m);
620
621   for (;;) {
622     FD_ZERO (&rfds);
623     FD_ZERO (&wfds);
624
625     /* Still space in to-buffer?  If so, we can read from the user. */
626     if (!eof_stdin && tosize < BUFSIZ)
627       FD_SET (0, &rfds);
628     /* Still space in from-buffer?  If so, we can read from the slave. */
629     if (fromsize < BUFSIZ)
630       FD_SET (fd_m, &rfds);
631     /* Content in to-buffer?  If so, we want to write to the slave. */
632     if (tosize > 0)
633       FD_SET (fd_m, &wfds);
634     /* Content in from-buffer?  If so, we want to write to the user. */
635     if (fromsize > 0)
636       FD_SET (1, &wfds);
637
638     r = select (fd_m+1, &rfds, &wfds, NULL, NULL);
639     if (r == -1) {
640       if (errno == EAGAIN || errno == EINTR)
641         continue;
642       perror ("select");
643       return -1;
644     }
645
646     /* Input from user: Put it in the to-buffer. */
647     if (FD_ISSET (0, &rfds)) {
648       count = BUFSIZ - tosize;
649       n = read (0, &tobuf[tosize], count);
650       if (n == -1) {
651         perror ("read");
652         return -1;
653       }
654       if (n == 0) { /* stdin was closed */
655         eof_stdin = 1;
656         /* This is what telnetd does ... */
657         tobuf[tosize] = '\004';
658         tosize += 1;
659       } else
660         tosize += n;
661     }
662
663     /* Input from slave: Put it in the from-buffer. */
664     if (FD_ISSET (fd_m, &rfds)) {
665       count = BUFSIZ - fromsize;
666       n = read (fd_m, &frombuf[fromsize], count);
667       if (n == -1) {
668         if (errno != EIO) /* EIO if slave process dies */
669           perror ("read");
670         break;
671       }
672       if (n == 0) /* slave closed the connection */
673         break;
674       fromsize += n;
675     }
676
677     /* Can write to user. */
678     if (FD_ISSET (1, &wfds)) {
679       n = write (1, frombuf, fromsize);
680       if (n == -1) {
681         perror ("write");
682         return -1;
683       }
684       memmove (frombuf, &frombuf[n], BUFSIZ - n);
685       fromsize -= n;
686     }
687
688     /* Can write to slave. */
689     if (FD_ISSET (fd_m, &wfds)) {
690       n = write (fd_m, tobuf, tosize);
691       if (n == -1) {
692         perror ("write");
693         return -1;
694       }
695       memmove (tobuf, &tobuf[n], BUFSIZ - n);
696       tosize -= n;
697     }
698   } /* for (;;) */
699
700   /* We end up here when slave has closed the connection. */
701   close (fd_m);
702
703   /* Restore blocking behaviour. */
704   fcntl (1, F_SETFL, flags_1);
705
706   /* Last chance to write out any remaining data in the buffers, but
707    * don't bother about errors.
708    */
709   ignore_value (write (1, frombuf, fromsize));
710
711   return 0;
712 }
713 #endif