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 " -x Trace libguestfs API calls\n"
80 "For more information, see the manpage %s(1).\n"),
81 program_name, program_name, program_name,
88 main (int argc, char *argv[])
90 /* Set global program name that is not polluted with libtool artifacts. */
91 set_program_name (argv[0]);
93 setlocale (LC_ALL, "");
94 bindtextdomain (PACKAGE, LOCALEBASEDIR);
97 enum { HELP_OPTION = CHAR_MAX + 1 };
99 static const char *options = "a:c:d:m:rvVx";
100 static const struct option long_options[] = {
101 { "add", 1, 0, 'a' },
102 { "append", 1, 0, 0 },
103 { "connect", 1, 0, 'c' },
104 { "domain", 1, 0, 'd' },
105 { "format", 2, 0, 0 },
106 { "help", 0, 0, HELP_OPTION },
107 { "memsize", 1, 0, 'm' },
108 { "network", 0, 0, 0 },
110 { "selinux", 0, 0, 0 },
111 { "verbose", 0, 0, 'v' },
112 { "version", 0, 0, 'V' },
115 struct drv *drvs = NULL;
117 const char *format = NULL;
121 const char *append = NULL;
125 g = guestfs_create ();
127 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
131 argv[0] = bad_cast (program_name);
134 c = getopt_long (argc, argv, options, long_options, &option_index);
138 case 0: /* options which are long only */
139 if (STREQ (long_options[option_index].name, "selinux")) {
140 guestfs_set_selinux (g, 1);
141 } else if (STREQ (long_options[option_index].name, "append")) {
143 } else if (STREQ (long_options[option_index].name, "network")) {
145 } else if (STREQ (long_options[option_index].name, "format")) {
146 if (!optarg || STREQ (optarg, ""))
151 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
152 program_name, long_options[option_index].name, option_index);
170 usage (EXIT_SUCCESS);
173 if (sscanf (optarg, "%u", &memsize) != 1) {
174 fprintf (stderr, _("%s: could not parse memory size '%s'\n"),
175 program_name, optarg);
197 usage (EXIT_SUCCESS);
200 usage (EXIT_FAILURE);
204 /* Old-style syntax? There were no -a or -d options in the old
205 * virt-rescue which is how we detect this.
208 while (optind < argc) {
209 if (strchr (argv[optind], '/') ||
210 access (argv[optind], F_OK) == 0) { /* simulate -a option */
211 drv = malloc (sizeof (struct drv));
217 drv->a.filename = argv[optind];
218 drv->a.format = NULL;
221 } else { /* simulate -d option */
222 drv = malloc (sizeof (struct drv));
228 drv->d.guest = argv[optind];
237 /* These are really constants, but they have to be variables for the
238 * options parsing code. Assert here that they have known-good
241 assert (inspector == 0);
242 assert (keys_from_stdin == 0);
243 assert (echo_keys == 0);
246 /* Must be no extra arguments on the command line. */
248 usage (EXIT_FAILURE);
250 /* User must have specified some drives. */
252 usage (EXIT_FAILURE);
254 /* Setting "direct mode" is required for the rescue appliance. */
255 guestfs_set_direct (g, 1);
257 /* Set other features. */
259 guestfs_set_memsize (g, memsize);
261 guestfs_set_network (g, 1);
263 /* Kernel command line must include guestfs_rescue=1 (see
264 * appliance/init) as well as other options.
266 append_full = xasprintf ("guestfs_rescue=1%s%s",
268 append ? append : "");
269 guestfs_set_append (g, append_full);
273 add_drives (drvs, 'a');
275 /* Free up data structures, no longer needed after this point. */
278 /* Run the appliance. This won't return until the user quits the
281 guestfs_set_error_handler (g, NULL, NULL);
284 /* launch() expects guestfsd to start. However, virt-rescue doesn't
285 * run guestfsd, so this will always fail with ECHILD when the
286 * appliance exits unexpectedly.
288 if (errno != ECHILD) {
289 fprintf (stderr, "%s: %s\n", program_name, guestfs_last_error (g));
299 /* The following was a nice idea, but in fact it doesn't work. This is
300 * because qemu has some (broken) pty emulation itself.
305 struct termios tsorig, tsnew;
308 fd_m = posix_openpt (O_RDWR);
310 perror ("posix_openpt");
323 fd_s = open (ptsname (fd_m), O_RDWR);
325 perror ("open ptsname");
339 r = tcgetattr (fd_s, &tsorig);
342 tcsetattr (fd_s, TCSANOW, &tsnew);
345 /* Close the master side of pty and set slave side as
346 * stdin/stdout/stderr.
353 if (dup (fd_s) == -1 || dup (fd_s) == -1 || dup (fd_s) == -1) {
361 perror ("warning: failed to setsid");
362 if (ioctl (0, TIOCSCTTY, 0) == -1)
363 perror ("warning: failed to TIOCSCTTY");
366 /* Run the appliance. This won't return until the user quits the
369 guestfs_set_error_handler (g, NULL, NULL);
370 r = guestfs_launch (g);
372 /* launch() expects guestfsd to start. However, virt-rescue doesn't
373 * run guestfsd, so this will always fail with ECHILD when the
374 * appliance exits unexpectedly.
376 if (errno != ECHILD) {
377 fprintf (stderr, "%s: %s\n", program_name, guestfs_last_error (g));
383 _exit (EXIT_SUCCESS);
386 /* Parent process continues ... */
388 /* Close slave side of pty. */
392 r = tcgetattr (fd_s, &tsorig);
395 tcsetattr (fd_s, TCSANOW, &tsnew);
397 /* Send input and output to master side of pty. */
398 r = multiplex (fd_m);
399 tcsetattr (fd_s, TCSANOW, &tsorig); /* Restore cooked mode. */
403 if (waitpid (pid, &r, 0) == -1) {
407 if (!WIFEXITED (r)) {
408 /* abnormal child exit */
409 fprintf (stderr, _("%s: unknown child exit status (%d)\n"),
414 exit (WEXITSTATUS (r)); /* normal exit, return child process's status */
417 /* Naive and simple multiplex function. */
421 int r, eof_stdin = 0;
423 char tobuf[BUFSIZ], frombuf[BUFSIZ]; /* to/from slave */
424 size_t tosize = 0, fromsize = 0;
427 long flags_0, flags_1, flags_fd_m;
429 flags_0 = fcntl (0, F_GETFL);
430 fcntl (0, F_SETFL, O_NONBLOCK | flags_0);
431 flags_1 = fcntl (0, F_GETFL);
432 fcntl (1, F_SETFL, O_NONBLOCK | flags_1);
433 flags_fd_m = fcntl (0, F_GETFL);
434 fcntl (fd_m, F_SETFL, O_NONBLOCK | flags_fd_m);
440 /* Still space in to-buffer? If so, we can read from the user. */
441 if (!eof_stdin && tosize < BUFSIZ)
443 /* Still space in from-buffer? If so, we can read from the slave. */
444 if (fromsize < BUFSIZ)
445 FD_SET (fd_m, &rfds);
446 /* Content in to-buffer? If so, we want to write to the slave. */
448 FD_SET (fd_m, &wfds);
449 /* Content in from-buffer? If so, we want to write to the user. */
453 r = select (fd_m+1, &rfds, &wfds, NULL, NULL);
455 if (errno == EAGAIN || errno == EINTR)
461 /* Input from user: Put it in the to-buffer. */
462 if (FD_ISSET (0, &rfds)) {
463 count = BUFSIZ - tosize;
464 n = read (0, &tobuf[tosize], count);
469 if (n == 0) { /* stdin was closed */
471 /* This is what telnetd does ... */
472 tobuf[tosize] = '\004';
478 /* Input from slave: Put it in the from-buffer. */
479 if (FD_ISSET (fd_m, &rfds)) {
480 count = BUFSIZ - fromsize;
481 n = read (fd_m, &frombuf[fromsize], count);
483 if (errno != EIO) /* EIO if slave process dies */
487 if (n == 0) /* slave closed the connection */
492 /* Can write to user. */
493 if (FD_ISSET (1, &wfds)) {
494 n = write (1, frombuf, fromsize);
499 memmove (frombuf, &frombuf[n], BUFSIZ - n);
503 /* Can write to slave. */
504 if (FD_ISSET (fd_m, &wfds)) {
505 n = write (fd_m, tobuf, tosize);
510 memmove (tobuf, &tobuf[n], BUFSIZ - n);
515 /* We end up here when slave has closed the connection. */
518 /* Restore blocking behaviour. */
519 fcntl (1, F_SETFL, flags_1);
521 /* Last chance to write out any remaining data in the buffers, but
522 * don't bother about errors.
524 ignore_value (write (1, frombuf, fromsize));