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.
32 #include "ignore-value.h"
34 #include "xvasprintf.h"
39 /* Currently open libguestfs handle. */
45 int keys_from_stdin = 0;
47 const char *libvirt_uri = NULL;
51 bad_cast (char const *s)
56 static void __attribute__((noreturn))
59 if (status != EXIT_SUCCESS)
60 fprintf (stderr, _("Try `%s --help' for more information.\n"),
64 _("%s: Run a rescue shell on a virtual machine\n"
65 "Copyright (C) 2009-2010 Red Hat Inc.\n"
67 " %s [--options] -d domname\n"
68 " %s [--options] -a disk.img [-a disk.img ...]\n"
70 " -a|--add image Add image\n"
71 " --append kernelopts Append kernel options\n"
72 " -c|--connect uri Specify libvirt URI for -d option\n"
73 " -d|--domain guest Add disks from libvirt guest\n"
74 " --format[=raw|..] Force disk format for -a option\n"
75 " --help Display brief help\n"
76 " -m|--memsize MB Set memory size in megabytes\n"
77 " --network Enable network\n"
78 " -r|--ro Access read-only\n"
79 " --selinux Enable SELinux\n"
80 " -v|--verbose Verbose messages\n"
81 " -V|--version Display version and exit\n"
82 " -w|--rw Mount read-write\n"
83 " -x Trace libguestfs API calls\n"
84 "For more information, see the manpage %s(1).\n"),
85 program_name, program_name, program_name,
92 main (int argc, char *argv[])
94 /* Set global program name that is not polluted with libtool artifacts. */
95 set_program_name (argv[0]);
97 setlocale (LC_ALL, "");
98 bindtextdomain (PACKAGE, LOCALEBASEDIR);
103 enum { HELP_OPTION = CHAR_MAX + 1 };
105 static const char *options = "a:c:d:m:rvVx";
106 static const struct option long_options[] = {
107 { "add", 1, 0, 'a' },
108 { "append", 1, 0, 0 },
109 { "connect", 1, 0, 'c' },
110 { "domain", 1, 0, 'd' },
111 { "format", 2, 0, 0 },
112 { "help", 0, 0, HELP_OPTION },
113 { "memsize", 1, 0, 'm' },
114 { "network", 0, 0, 0 },
117 { "selinux", 0, 0, 0 },
118 { "verbose", 0, 0, 'v' },
119 { "version", 0, 0, 'V' },
122 struct drv *drvs = NULL;
124 const char *format = NULL;
128 const char *append = NULL;
132 g = guestfs_create ();
134 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
138 argv[0] = bad_cast (program_name);
141 c = getopt_long (argc, argv, options, long_options, &option_index);
145 case 0: /* options which are long only */
146 if (STREQ (long_options[option_index].name, "selinux")) {
147 guestfs_set_selinux (g, 1);
148 } else if (STREQ (long_options[option_index].name, "append")) {
150 } else if (STREQ (long_options[option_index].name, "network")) {
152 } else if (STREQ (long_options[option_index].name, "format")) {
153 if (!optarg || STREQ (optarg, ""))
158 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
159 program_name, long_options[option_index].name, option_index);
177 usage (EXIT_SUCCESS);
180 if (sscanf (optarg, "%u", &memsize) != 1) {
181 fprintf (stderr, _("%s: could not parse memory size '%s'\n"),
182 program_name, optarg);
208 usage (EXIT_SUCCESS);
211 usage (EXIT_FAILURE);
215 /* Old-style syntax? There were no -a or -d options in the old
216 * virt-rescue which is how we detect this.
219 while (optind < argc) {
220 if (strchr (argv[optind], '/') ||
221 access (argv[optind], F_OK) == 0) { /* simulate -a option */
222 drv = malloc (sizeof (struct drv));
228 drv->a.filename = argv[optind];
229 drv->a.format = NULL;
232 } else { /* simulate -d option */
233 drv = malloc (sizeof (struct drv));
239 drv->d.guest = argv[optind];
248 /* These are really constants, but they have to be variables for the
249 * options parsing code. Assert here that they have known-good
252 assert (inspector == 0);
253 assert (keys_from_stdin == 0);
254 assert (echo_keys == 0);
257 /* Must be no extra arguments on the command line. */
259 usage (EXIT_FAILURE);
261 /* User must have specified some drives. */
263 usage (EXIT_FAILURE);
265 /* Setting "direct mode" is required for the rescue appliance. */
266 guestfs_set_direct (g, 1);
268 /* Set other features. */
270 guestfs_set_memsize (g, memsize);
272 guestfs_set_network (g, 1);
274 /* Kernel command line must include guestfs_rescue=1 (see
275 * appliance/init) as well as other options.
277 append_full = xasprintf ("guestfs_rescue=1%s%s",
279 append ? append : "");
280 guestfs_set_append (g, append_full);
284 add_drives (drvs, 'a');
286 /* Free up data structures, no longer needed after this point. */
289 /* Run the appliance. This won't return until the user quits the
292 guestfs_set_error_handler (g, NULL, NULL);
294 /* We expect launch to fail, so ignore the return value. */
295 ignore_value (guestfs_launch (g));
302 /* The following was a nice idea, but in fact it doesn't work. This is
303 * because qemu has some (broken) pty emulation itself.
308 struct termios tsorig, tsnew;
311 fd_m = posix_openpt (O_RDWR);
313 perror ("posix_openpt");
326 fd_s = open (ptsname (fd_m), O_RDWR);
328 perror ("open ptsname");
342 r = tcgetattr (fd_s, &tsorig);
345 tcsetattr (fd_s, TCSANOW, &tsnew);
348 /* Close the master side of pty and set slave side as
349 * stdin/stdout/stderr.
356 if (dup (fd_s) == -1 || dup (fd_s) == -1 || dup (fd_s) == -1) {
364 perror ("warning: failed to setsid");
365 if (ioctl (0, TIOCSCTTY, 0) == -1)
366 perror ("warning: failed to TIOCSCTTY");
369 /* Run the appliance. This won't return until the user quits the
372 guestfs_set_error_handler (g, NULL, NULL);
373 r = guestfs_launch (g);
375 /* launch() expects guestfsd to start. However, virt-rescue doesn't
376 * run guestfsd, so this will always fail with ECHILD when the
377 * appliance exits unexpectedly.
379 if (errno != ECHILD) {
380 fprintf (stderr, "%s: %s\n", program_name, guestfs_last_error (g));
386 _exit (EXIT_SUCCESS);
389 /* Parent process continues ... */
391 /* Close slave side of pty. */
395 r = tcgetattr (fd_s, &tsorig);
398 tcsetattr (fd_s, TCSANOW, &tsnew);
400 /* Send input and output to master side of pty. */
401 r = multiplex (fd_m);
402 tcsetattr (fd_s, TCSANOW, &tsorig); /* Restore cooked mode. */
406 if (waitpid (pid, &r, 0) == -1) {
410 if (!WIFEXITED (r)) {
411 /* abnormal child exit */
412 fprintf (stderr, _("%s: unknown child exit status (%d)\n"),
417 exit (WEXITSTATUS (r)); /* normal exit, return child process's status */
420 /* Naive and simple multiplex function. */
424 int r, eof_stdin = 0;
426 char tobuf[BUFSIZ], frombuf[BUFSIZ]; /* to/from slave */
427 size_t tosize = 0, fromsize = 0;
430 long flags_0, flags_1, flags_fd_m;
432 flags_0 = fcntl (0, F_GETFL);
433 fcntl (0, F_SETFL, O_NONBLOCK | flags_0);
434 flags_1 = fcntl (0, F_GETFL);
435 fcntl (1, F_SETFL, O_NONBLOCK | flags_1);
436 flags_fd_m = fcntl (0, F_GETFL);
437 fcntl (fd_m, F_SETFL, O_NONBLOCK | flags_fd_m);
443 /* Still space in to-buffer? If so, we can read from the user. */
444 if (!eof_stdin && tosize < BUFSIZ)
446 /* Still space in from-buffer? If so, we can read from the slave. */
447 if (fromsize < BUFSIZ)
448 FD_SET (fd_m, &rfds);
449 /* Content in to-buffer? If so, we want to write to the slave. */
451 FD_SET (fd_m, &wfds);
452 /* Content in from-buffer? If so, we want to write to the user. */
456 r = select (fd_m+1, &rfds, &wfds, NULL, NULL);
458 if (errno == EAGAIN || errno == EINTR)
464 /* Input from user: Put it in the to-buffer. */
465 if (FD_ISSET (0, &rfds)) {
466 count = BUFSIZ - tosize;
467 n = read (0, &tobuf[tosize], count);
472 if (n == 0) { /* stdin was closed */
474 /* This is what telnetd does ... */
475 tobuf[tosize] = '\004';
481 /* Input from slave: Put it in the from-buffer. */
482 if (FD_ISSET (fd_m, &rfds)) {
483 count = BUFSIZ - fromsize;
484 n = read (fd_m, &frombuf[fromsize], count);
486 if (errno != EIO) /* EIO if slave process dies */
490 if (n == 0) /* slave closed the connection */
495 /* Can write to user. */
496 if (FD_ISSET (1, &wfds)) {
497 n = write (1, frombuf, fromsize);
502 memmove (frombuf, &frombuf[n], BUFSIZ - n);
506 /* Can write to slave. */
507 if (FD_ISSET (fd_m, &wfds)) {
508 n = write (fd_m, tobuf, tosize);
513 memmove (tobuf, &tobuf[n], BUFSIZ - n);
518 /* We end up here when slave has closed the connection. */
521 /* Restore blocking behaviour. */
522 fcntl (1, F_SETFL, flags_1);
524 /* Last chance to write out any remaining data in the buffers, but
525 * don't bother about errors.
527 ignore_value (write (1, frombuf, fromsize));