2 * Copyright (C) 2010-2011 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.
33 #include "xvasprintf.h"
38 /* Currently open libguestfs handle. */
44 int keys_from_stdin = 0;
46 const char *libvirt_uri = NULL;
50 bad_cast (char const *s)
55 static void __attribute__((noreturn))
58 if (status != EXIT_SUCCESS)
59 fprintf (stderr, _("Try `%s --help' for more information.\n"),
63 _("%s: Run a rescue shell on a virtual machine\n"
64 "Copyright (C) 2009-2010 Red Hat Inc.\n"
66 " %s [--options] -d domname\n"
67 " %s [--options] -a disk.img [-a disk.img ...]\n"
69 " -a|--add image Add image\n"
70 " --append kernelopts Append kernel options\n"
71 " -c|--connect uri Specify libvirt URI for -d option\n"
72 " -d|--domain guest Add disks from libvirt guest\n"
73 " --format[=raw|..] Force disk format for -a option\n"
74 " --help Display brief help\n"
75 " -m|--memsize MB Set memory size in megabytes\n"
76 " --network Enable network\n"
77 " -r|--ro Access read-only\n"
78 " --selinux Enable SELinux\n"
79 " -v|--verbose Verbose messages\n"
80 " -V|--version Display version and exit\n"
81 " -w|--rw Mount read-write\n"
82 " -x Trace libguestfs API calls\n"
83 "For more information, see the manpage %s(1).\n"),
84 program_name, program_name, program_name,
91 main (int argc, char *argv[])
93 /* Set global program name that is not polluted with libtool artifacts. */
94 set_program_name (argv[0]);
96 setlocale (LC_ALL, "");
97 bindtextdomain (PACKAGE, LOCALEBASEDIR);
102 enum { HELP_OPTION = CHAR_MAX + 1 };
104 static const char *options = "a:c:d:m:rvVx";
105 static const struct option long_options[] = {
106 { "add", 1, 0, 'a' },
107 { "append", 1, 0, 0 },
108 { "connect", 1, 0, 'c' },
109 { "domain", 1, 0, 'd' },
110 { "format", 2, 0, 0 },
111 { "help", 0, 0, HELP_OPTION },
112 { "memsize", 1, 0, 'm' },
113 { "network", 0, 0, 0 },
116 { "selinux", 0, 0, 0 },
117 { "verbose", 0, 0, 'v' },
118 { "version", 0, 0, 'V' },
121 struct drv *drvs = NULL;
123 const char *format = NULL;
127 const char *append = NULL;
131 g = guestfs_create ();
133 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
137 argv[0] = bad_cast (program_name);
140 c = getopt_long (argc, argv, options, long_options, &option_index);
144 case 0: /* options which are long only */
145 if (STREQ (long_options[option_index].name, "selinux")) {
146 guestfs_set_selinux (g, 1);
147 } else if (STREQ (long_options[option_index].name, "append")) {
149 } else if (STREQ (long_options[option_index].name, "network")) {
151 } else if (STREQ (long_options[option_index].name, "format")) {
152 if (!optarg || STREQ (optarg, ""))
157 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
158 program_name, long_options[option_index].name, option_index);
176 usage (EXIT_SUCCESS);
179 if (sscanf (optarg, "%u", &memsize) != 1) {
180 fprintf (stderr, _("%s: could not parse memory size '%s'\n"),
181 program_name, optarg);
207 usage (EXIT_SUCCESS);
210 usage (EXIT_FAILURE);
214 /* Old-style syntax? There were no -a or -d options in the old
215 * virt-rescue which is how we detect this.
218 while (optind < argc) {
219 if (strchr (argv[optind], '/') ||
220 access (argv[optind], F_OK) == 0) { /* simulate -a option */
221 drv = malloc (sizeof (struct drv));
227 drv->a.filename = argv[optind];
228 drv->a.format = NULL;
231 } else { /* simulate -d option */
232 drv = malloc (sizeof (struct drv));
238 drv->d.guest = argv[optind];
247 /* These are really constants, but they have to be variables for the
248 * options parsing code. Assert here that they have known-good
251 assert (inspector == 0);
252 assert (keys_from_stdin == 0);
253 assert (echo_keys == 0);
256 /* Must be no extra arguments on the command line. */
258 usage (EXIT_FAILURE);
260 /* User must have specified some drives. */
262 usage (EXIT_FAILURE);
264 /* Setting "direct mode" is required for the rescue appliance. */
265 guestfs_set_direct (g, 1);
267 /* Set other features. */
269 guestfs_set_memsize (g, memsize);
271 guestfs_set_network (g, 1);
273 /* Kernel command line must include guestfs_rescue=1 (see
274 * appliance/init) as well as other options.
276 append_full = xasprintf ("guestfs_rescue=1%s%s",
278 append ? append : "");
279 guestfs_set_append (g, append_full);
283 add_drives (drvs, 'a');
285 /* Free up data structures, no longer needed after this point. */
288 /* Run the appliance. This won't return until the user quits the
291 guestfs_set_error_handler (g, NULL, NULL);
294 /* launch() expects guestfsd to start. However, virt-rescue doesn't
295 * run guestfsd, so this will always fail with ECHILD when the
296 * appliance exits unexpectedly.
298 if (errno != ECHILD) {
299 fprintf (stderr, "%s: %s\n", program_name, guestfs_last_error (g));
309 /* The following was a nice idea, but in fact it doesn't work. This is
310 * because qemu has some (broken) pty emulation itself.
315 struct termios tsorig, tsnew;
318 fd_m = posix_openpt (O_RDWR);
320 perror ("posix_openpt");
333 fd_s = open (ptsname (fd_m), O_RDWR);
335 perror ("open ptsname");
349 r = tcgetattr (fd_s, &tsorig);
352 tcsetattr (fd_s, TCSANOW, &tsnew);
355 /* Close the master side of pty and set slave side as
356 * stdin/stdout/stderr.
363 if (dup (fd_s) == -1 || dup (fd_s) == -1 || dup (fd_s) == -1) {
371 perror ("warning: failed to setsid");
372 if (ioctl (0, TIOCSCTTY, 0) == -1)
373 perror ("warning: failed to TIOCSCTTY");
376 /* Run the appliance. This won't return until the user quits the
379 guestfs_set_error_handler (g, NULL, NULL);
380 r = guestfs_launch (g);
382 /* launch() expects guestfsd to start. However, virt-rescue doesn't
383 * run guestfsd, so this will always fail with ECHILD when the
384 * appliance exits unexpectedly.
386 if (errno != ECHILD) {
387 fprintf (stderr, "%s: %s\n", program_name, guestfs_last_error (g));
393 _exit (EXIT_SUCCESS);
396 /* Parent process continues ... */
398 /* Close slave side of pty. */
402 r = tcgetattr (fd_s, &tsorig);
405 tcsetattr (fd_s, TCSANOW, &tsnew);
407 /* Send input and output to master side of pty. */
408 r = multiplex (fd_m);
409 tcsetattr (fd_s, TCSANOW, &tsorig); /* Restore cooked mode. */
413 if (waitpid (pid, &r, 0) == -1) {
417 if (!WIFEXITED (r)) {
418 /* abnormal child exit */
419 fprintf (stderr, _("%s: unknown child exit status (%d)\n"),
424 exit (WEXITSTATUS (r)); /* normal exit, return child process's status */
427 /* Naive and simple multiplex function. */
431 int r, eof_stdin = 0;
433 char tobuf[BUFSIZ], frombuf[BUFSIZ]; /* to/from slave */
434 size_t tosize = 0, fromsize = 0;
437 long flags_0, flags_1, flags_fd_m;
439 flags_0 = fcntl (0, F_GETFL);
440 fcntl (0, F_SETFL, O_NONBLOCK | flags_0);
441 flags_1 = fcntl (0, F_GETFL);
442 fcntl (1, F_SETFL, O_NONBLOCK | flags_1);
443 flags_fd_m = fcntl (0, F_GETFL);
444 fcntl (fd_m, F_SETFL, O_NONBLOCK | flags_fd_m);
450 /* Still space in to-buffer? If so, we can read from the user. */
451 if (!eof_stdin && tosize < BUFSIZ)
453 /* Still space in from-buffer? If so, we can read from the slave. */
454 if (fromsize < BUFSIZ)
455 FD_SET (fd_m, &rfds);
456 /* Content in to-buffer? If so, we want to write to the slave. */
458 FD_SET (fd_m, &wfds);
459 /* Content in from-buffer? If so, we want to write to the user. */
463 r = select (fd_m+1, &rfds, &wfds, NULL, NULL);
465 if (errno == EAGAIN || errno == EINTR)
471 /* Input from user: Put it in the to-buffer. */
472 if (FD_ISSET (0, &rfds)) {
473 count = BUFSIZ - tosize;
474 n = read (0, &tobuf[tosize], count);
479 if (n == 0) { /* stdin was closed */
481 /* This is what telnetd does ... */
482 tobuf[tosize] = '\004';
488 /* Input from slave: Put it in the from-buffer. */
489 if (FD_ISSET (fd_m, &rfds)) {
490 count = BUFSIZ - fromsize;
491 n = read (fd_m, &frombuf[fromsize], count);
493 if (errno != EIO) /* EIO if slave process dies */
497 if (n == 0) /* slave closed the connection */
502 /* Can write to user. */
503 if (FD_ISSET (1, &wfds)) {
504 n = write (1, frombuf, fromsize);
509 memmove (frombuf, &frombuf[n], BUFSIZ - n);
513 /* Can write to slave. */
514 if (FD_ISSET (fd_m, &wfds)) {
515 n = write (fd_m, tobuf, tosize);
520 memmove (tobuf, &tobuf[n], BUFSIZ - n);
525 /* We end up here when slave has closed the connection. */
528 /* Restore blocking behaviour. */
529 fcntl (1, F_SETFL, flags_1);
531 /* Last chance to write out any remaining data in the buffers, but
532 * don't bother about errors.
534 ignore_value (write (1, frombuf, fromsize));