2 * Copyright (C) 2010 Red Hat Inc.
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.
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.
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.
31 #include "xvasprintf.h"
36 /* Currently open libguestfs handle. */
42 int keys_from_stdin = 0;
44 const char *libvirt_uri = NULL;
48 bad_cast (char const *s)
53 static void __attribute__((noreturn))
56 if (status != EXIT_SUCCESS)
57 fprintf (stderr, _("Try `%s --help' for more information.\n"),
61 _("%s: Run a rescue shell on a virtual machine\n"
62 "Copyright (C) 2009-2010 Red Hat Inc.\n"
64 " %s [--options] -d domname\n"
65 " %s [--options] -a disk.img [-a disk.img ...]\n"
67 " -a|--add image Add image\n"
68 " --append kernelopts Append kernel options\n"
69 " -c|--connect uri Specify libvirt URI for -d option\n"
70 " -d|--domain guest Add disks from libvirt guest\n"
71 " --format[=raw|..] Force disk format for -a option\n"
72 " --help Display brief help\n"
73 " -m|--memsize MB Set memory size in megabytes\n"
74 " --network Enable network\n"
75 " -r|--ro Access read-only\n"
76 " --selinux Enable SELinux\n"
77 " -v|--verbose Verbose messages\n"
78 " -V|--version Display version and exit\n"
79 " -w|--rw Mount read-write\n"
80 " -x Trace libguestfs API calls\n"
81 "For more information, see the manpage %s(1).\n"),
82 program_name, program_name, program_name,
89 main (int argc, char *argv[])
91 /* Set global program name that is not polluted with libtool artifacts. */
92 set_program_name (argv[0]);
94 setlocale (LC_ALL, "");
95 bindtextdomain (PACKAGE, LOCALEBASEDIR);
98 enum { HELP_OPTION = CHAR_MAX + 1 };
100 static const char *options = "a:c:d:m:rvVx";
101 static const struct option long_options[] = {
102 { "add", 1, 0, 'a' },
103 { "append", 1, 0, 0 },
104 { "connect", 1, 0, 'c' },
105 { "domain", 1, 0, 'd' },
106 { "format", 2, 0, 0 },
107 { "help", 0, 0, HELP_OPTION },
108 { "memsize", 1, 0, 'm' },
109 { "network", 0, 0, 0 },
112 { "selinux", 0, 0, 0 },
113 { "verbose", 0, 0, 'v' },
114 { "version", 0, 0, 'V' },
117 struct drv *drvs = NULL;
119 const char *format = NULL;
123 const char *append = NULL;
127 g = guestfs_create ();
129 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
133 argv[0] = bad_cast (program_name);
136 c = getopt_long (argc, argv, options, long_options, &option_index);
140 case 0: /* options which are long only */
141 if (STREQ (long_options[option_index].name, "selinux")) {
142 guestfs_set_selinux (g, 1);
143 } else if (STREQ (long_options[option_index].name, "append")) {
145 } else if (STREQ (long_options[option_index].name, "network")) {
147 } else if (STREQ (long_options[option_index].name, "format")) {
148 if (!optarg || STREQ (optarg, ""))
153 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
154 program_name, long_options[option_index].name, option_index);
172 usage (EXIT_SUCCESS);
175 if (sscanf (optarg, "%u", &memsize) != 1) {
176 fprintf (stderr, _("%s: could not parse memory size '%s'\n"),
177 program_name, optarg);
203 usage (EXIT_SUCCESS);
206 usage (EXIT_FAILURE);
210 /* Old-style syntax? There were no -a or -d options in the old
211 * virt-rescue which is how we detect this.
214 while (optind < argc) {
215 if (strchr (argv[optind], '/') ||
216 access (argv[optind], F_OK) == 0) { /* simulate -a option */
217 drv = malloc (sizeof (struct drv));
223 drv->a.filename = argv[optind];
224 drv->a.format = NULL;
227 } else { /* simulate -d option */
228 drv = malloc (sizeof (struct drv));
234 drv->d.guest = argv[optind];
243 /* These are really constants, but they have to be variables for the
244 * options parsing code. Assert here that they have known-good
247 assert (inspector == 0);
248 assert (keys_from_stdin == 0);
249 assert (echo_keys == 0);
252 /* Must be no extra arguments on the command line. */
254 usage (EXIT_FAILURE);
256 /* User must have specified some drives. */
258 usage (EXIT_FAILURE);
260 /* Setting "direct mode" is required for the rescue appliance. */
261 guestfs_set_direct (g, 1);
263 /* Set other features. */
265 guestfs_set_memsize (g, memsize);
267 guestfs_set_network (g, 1);
269 /* Kernel command line must include guestfs_rescue=1 (see
270 * appliance/init) as well as other options.
272 append_full = xasprintf ("guestfs_rescue=1%s%s",
274 append ? append : "");
275 guestfs_set_append (g, append_full);
279 add_drives (drvs, 'a');
281 /* Free up data structures, no longer needed after this point. */
284 /* Run the appliance. This won't return until the user quits the
287 guestfs_set_error_handler (g, NULL, NULL);
290 /* launch() expects guestfsd to start. However, virt-rescue doesn't
291 * run guestfsd, so this will always fail with ECHILD when the
292 * appliance exits unexpectedly.
294 if (errno != ECHILD) {
295 fprintf (stderr, "%s: %s\n", program_name, guestfs_last_error (g));
305 /* The following was a nice idea, but in fact it doesn't work. This is
306 * because qemu has some (broken) pty emulation itself.
311 struct termios tsorig, tsnew;
314 fd_m = posix_openpt (O_RDWR);
316 perror ("posix_openpt");
329 fd_s = open (ptsname (fd_m), O_RDWR);
331 perror ("open ptsname");
345 r = tcgetattr (fd_s, &tsorig);
348 tcsetattr (fd_s, TCSANOW, &tsnew);
351 /* Close the master side of pty and set slave side as
352 * stdin/stdout/stderr.
359 if (dup (fd_s) == -1 || dup (fd_s) == -1 || dup (fd_s) == -1) {
367 perror ("warning: failed to setsid");
368 if (ioctl (0, TIOCSCTTY, 0) == -1)
369 perror ("warning: failed to TIOCSCTTY");
372 /* Run the appliance. This won't return until the user quits the
375 guestfs_set_error_handler (g, NULL, NULL);
376 r = guestfs_launch (g);
378 /* launch() expects guestfsd to start. However, virt-rescue doesn't
379 * run guestfsd, so this will always fail with ECHILD when the
380 * appliance exits unexpectedly.
382 if (errno != ECHILD) {
383 fprintf (stderr, "%s: %s\n", program_name, guestfs_last_error (g));
389 _exit (EXIT_SUCCESS);
392 /* Parent process continues ... */
394 /* Close slave side of pty. */
398 r = tcgetattr (fd_s, &tsorig);
401 tcsetattr (fd_s, TCSANOW, &tsnew);
403 /* Send input and output to master side of pty. */
404 r = multiplex (fd_m);
405 tcsetattr (fd_s, TCSANOW, &tsorig); /* Restore cooked mode. */
409 if (waitpid (pid, &r, 0) == -1) {
413 if (!WIFEXITED (r)) {
414 /* abnormal child exit */
415 fprintf (stderr, _("%s: unknown child exit status (%d)\n"),
420 exit (WEXITSTATUS (r)); /* normal exit, return child process's status */
423 /* Naive and simple multiplex function. */
427 int r, eof_stdin = 0;
429 char tobuf[BUFSIZ], frombuf[BUFSIZ]; /* to/from slave */
430 size_t tosize = 0, fromsize = 0;
433 long flags_0, flags_1, flags_fd_m;
435 flags_0 = fcntl (0, F_GETFL);
436 fcntl (0, F_SETFL, O_NONBLOCK | flags_0);
437 flags_1 = fcntl (0, F_GETFL);
438 fcntl (1, F_SETFL, O_NONBLOCK | flags_1);
439 flags_fd_m = fcntl (0, F_GETFL);
440 fcntl (fd_m, F_SETFL, O_NONBLOCK | flags_fd_m);
446 /* Still space in to-buffer? If so, we can read from the user. */
447 if (!eof_stdin && tosize < BUFSIZ)
449 /* Still space in from-buffer? If so, we can read from the slave. */
450 if (fromsize < BUFSIZ)
451 FD_SET (fd_m, &rfds);
452 /* Content in to-buffer? If so, we want to write to the slave. */
454 FD_SET (fd_m, &wfds);
455 /* Content in from-buffer? If so, we want to write to the user. */
459 r = select (fd_m+1, &rfds, &wfds, NULL, NULL);
461 if (errno == EAGAIN || errno == EINTR)
467 /* Input from user: Put it in the to-buffer. */
468 if (FD_ISSET (0, &rfds)) {
469 count = BUFSIZ - tosize;
470 n = read (0, &tobuf[tosize], count);
475 if (n == 0) { /* stdin was closed */
477 /* This is what telnetd does ... */
478 tobuf[tosize] = '\004';
484 /* Input from slave: Put it in the from-buffer. */
485 if (FD_ISSET (fd_m, &rfds)) {
486 count = BUFSIZ - fromsize;
487 n = read (fd_m, &frombuf[fromsize], count);
489 if (errno != EIO) /* EIO if slave process dies */
493 if (n == 0) /* slave closed the connection */
498 /* Can write to user. */
499 if (FD_ISSET (1, &wfds)) {
500 n = write (1, frombuf, fromsize);
505 memmove (frombuf, &frombuf[n], BUFSIZ - n);
509 /* Can write to slave. */
510 if (FD_ISSET (fd_m, &wfds)) {
511 n = write (fd_m, tobuf, tosize);
516 memmove (tobuf, &tobuf[n], BUFSIZ - n);
521 /* We end up here when slave has closed the connection. */
524 /* Restore blocking behaviour. */
525 fcntl (1, F_SETFL, flags_1);
527 /* Last chance to write out any remaining data in the buffers, but
528 * don't bother about errors.
530 ignore_value (write (1, frombuf, fromsize));