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);
100 enum { HELP_OPTION = CHAR_MAX + 1 };
102 static const char *options = "a:c:d:m:rvVx";
103 static const struct option long_options[] = {
104 { "add", 1, 0, 'a' },
105 { "append", 1, 0, 0 },
106 { "connect", 1, 0, 'c' },
107 { "domain", 1, 0, 'd' },
108 { "format", 2, 0, 0 },
109 { "help", 0, 0, HELP_OPTION },
110 { "memsize", 1, 0, 'm' },
111 { "network", 0, 0, 0 },
114 { "selinux", 0, 0, 0 },
115 { "verbose", 0, 0, 'v' },
116 { "version", 0, 0, 'V' },
119 struct drv *drvs = NULL;
121 const char *format = NULL;
125 const char *append = NULL;
129 g = guestfs_create ();
131 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
135 argv[0] = bad_cast (program_name);
138 c = getopt_long (argc, argv, options, long_options, &option_index);
142 case 0: /* options which are long only */
143 if (STREQ (long_options[option_index].name, "selinux")) {
144 guestfs_set_selinux (g, 1);
145 } else if (STREQ (long_options[option_index].name, "append")) {
147 } else if (STREQ (long_options[option_index].name, "network")) {
149 } else if (STREQ (long_options[option_index].name, "format")) {
150 if (!optarg || STREQ (optarg, ""))
155 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
156 program_name, long_options[option_index].name, option_index);
174 usage (EXIT_SUCCESS);
177 if (sscanf (optarg, "%u", &memsize) != 1) {
178 fprintf (stderr, _("%s: could not parse memory size '%s'\n"),
179 program_name, optarg);
205 usage (EXIT_SUCCESS);
208 usage (EXIT_FAILURE);
212 /* Old-style syntax? There were no -a or -d options in the old
213 * virt-rescue which is how we detect this.
216 while (optind < argc) {
217 if (strchr (argv[optind], '/') ||
218 access (argv[optind], F_OK) == 0) { /* simulate -a option */
219 drv = malloc (sizeof (struct drv));
225 drv->a.filename = argv[optind];
226 drv->a.format = NULL;
229 } else { /* simulate -d option */
230 drv = malloc (sizeof (struct drv));
236 drv->d.guest = argv[optind];
245 /* These are really constants, but they have to be variables for the
246 * options parsing code. Assert here that they have known-good
249 assert (inspector == 0);
250 assert (keys_from_stdin == 0);
251 assert (echo_keys == 0);
254 /* Must be no extra arguments on the command line. */
256 usage (EXIT_FAILURE);
258 /* User must have specified some drives. */
260 usage (EXIT_FAILURE);
262 /* Setting "direct mode" is required for the rescue appliance. */
263 guestfs_set_direct (g, 1);
265 /* Set other features. */
267 guestfs_set_memsize (g, memsize);
269 guestfs_set_network (g, 1);
271 /* Kernel command line must include guestfs_rescue=1 (see
272 * appliance/init) as well as other options.
274 append_full = xasprintf ("guestfs_rescue=1%s%s",
276 append ? append : "");
277 guestfs_set_append (g, append_full);
281 add_drives (drvs, 'a');
283 /* Free up data structures, no longer needed after this point. */
286 /* Run the appliance. This won't return until the user quits the
289 guestfs_set_error_handler (g, NULL, NULL);
292 /* launch() expects guestfsd to start. However, virt-rescue doesn't
293 * run guestfsd, so this will always fail with ECHILD when the
294 * appliance exits unexpectedly.
296 if (errno != ECHILD) {
297 fprintf (stderr, "%s: %s\n", program_name, guestfs_last_error (g));
307 /* The following was a nice idea, but in fact it doesn't work. This is
308 * because qemu has some (broken) pty emulation itself.
313 struct termios tsorig, tsnew;
316 fd_m = posix_openpt (O_RDWR);
318 perror ("posix_openpt");
331 fd_s = open (ptsname (fd_m), O_RDWR);
333 perror ("open ptsname");
347 r = tcgetattr (fd_s, &tsorig);
350 tcsetattr (fd_s, TCSANOW, &tsnew);
353 /* Close the master side of pty and set slave side as
354 * stdin/stdout/stderr.
361 if (dup (fd_s) == -1 || dup (fd_s) == -1 || dup (fd_s) == -1) {
369 perror ("warning: failed to setsid");
370 if (ioctl (0, TIOCSCTTY, 0) == -1)
371 perror ("warning: failed to TIOCSCTTY");
374 /* Run the appliance. This won't return until the user quits the
377 guestfs_set_error_handler (g, NULL, NULL);
378 r = guestfs_launch (g);
380 /* launch() expects guestfsd to start. However, virt-rescue doesn't
381 * run guestfsd, so this will always fail with ECHILD when the
382 * appliance exits unexpectedly.
384 if (errno != ECHILD) {
385 fprintf (stderr, "%s: %s\n", program_name, guestfs_last_error (g));
391 _exit (EXIT_SUCCESS);
394 /* Parent process continues ... */
396 /* Close slave side of pty. */
400 r = tcgetattr (fd_s, &tsorig);
403 tcsetattr (fd_s, TCSANOW, &tsnew);
405 /* Send input and output to master side of pty. */
406 r = multiplex (fd_m);
407 tcsetattr (fd_s, TCSANOW, &tsorig); /* Restore cooked mode. */
411 if (waitpid (pid, &r, 0) == -1) {
415 if (!WIFEXITED (r)) {
416 /* abnormal child exit */
417 fprintf (stderr, _("%s: unknown child exit status (%d)\n"),
422 exit (WEXITSTATUS (r)); /* normal exit, return child process's status */
425 /* Naive and simple multiplex function. */
429 int r, eof_stdin = 0;
431 char tobuf[BUFSIZ], frombuf[BUFSIZ]; /* to/from slave */
432 size_t tosize = 0, fromsize = 0;
435 long flags_0, flags_1, flags_fd_m;
437 flags_0 = fcntl (0, F_GETFL);
438 fcntl (0, F_SETFL, O_NONBLOCK | flags_0);
439 flags_1 = fcntl (0, F_GETFL);
440 fcntl (1, F_SETFL, O_NONBLOCK | flags_1);
441 flags_fd_m = fcntl (0, F_GETFL);
442 fcntl (fd_m, F_SETFL, O_NONBLOCK | flags_fd_m);
448 /* Still space in to-buffer? If so, we can read from the user. */
449 if (!eof_stdin && tosize < BUFSIZ)
451 /* Still space in from-buffer? If so, we can read from the slave. */
452 if (fromsize < BUFSIZ)
453 FD_SET (fd_m, &rfds);
454 /* Content in to-buffer? If so, we want to write to the slave. */
456 FD_SET (fd_m, &wfds);
457 /* Content in from-buffer? If so, we want to write to the user. */
461 r = select (fd_m+1, &rfds, &wfds, NULL, NULL);
463 if (errno == EAGAIN || errno == EINTR)
469 /* Input from user: Put it in the to-buffer. */
470 if (FD_ISSET (0, &rfds)) {
471 count = BUFSIZ - tosize;
472 n = read (0, &tobuf[tosize], count);
477 if (n == 0) { /* stdin was closed */
479 /* This is what telnetd does ... */
480 tobuf[tosize] = '\004';
486 /* Input from slave: Put it in the from-buffer. */
487 if (FD_ISSET (fd_m, &rfds)) {
488 count = BUFSIZ - fromsize;
489 n = read (fd_m, &frombuf[fromsize], count);
491 if (errno != EIO) /* EIO if slave process dies */
495 if (n == 0) /* slave closed the connection */
500 /* Can write to user. */
501 if (FD_ISSET (1, &wfds)) {
502 n = write (1, frombuf, fromsize);
507 memmove (frombuf, &frombuf[n], BUFSIZ - n);
511 /* Can write to slave. */
512 if (FD_ISSET (fd_m, &wfds)) {
513 n = write (fd_m, tobuf, tosize);
518 memmove (tobuf, &tobuf[n], BUFSIZ - n);
523 /* We end up here when slave has closed the connection. */
526 /* Restore blocking behaviour. */
527 fcntl (1, F_SETFL, flags_1);
529 /* Last chance to write out any remaining data in the buffers, but
530 * don't bother about errors.
532 ignore_value (write (1, frombuf, fromsize));