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.
30 #include "xvasprintf.h"
35 /* Currently open libguestfs handle. */
40 int keys_from_stdin = 0;
42 const char *libvirt_uri = NULL;
46 bad_cast (char const *s)
51 static void __attribute__((noreturn))
54 if (status != EXIT_SUCCESS)
55 fprintf (stderr, _("Try `%s --help' for more information.\n"),
59 _("%s: Run a rescue shell on a virtual machine\n"
60 "Copyright (C) 2009-2010 Red Hat Inc.\n"
62 " %s [--options] -d domname\n"
63 " %s [--options] -a disk.img [-a disk.img ...]\n"
65 " -a|--add image Add image\n"
66 " --append kernelopts Append kernel options\n"
67 " -c|--connect uri Specify libvirt URI for -d option\n"
68 " -d|--domain guest Add disks from libvirt guest\n"
69 " --format[=raw|..] Force disk format for -a option\n"
70 " --help Display brief help\n"
71 " -m|--memsize MB Set memory size in megabytes\n"
72 " --network Enable network\n"
73 " -r|--ro Access read-only\n"
74 " --selinux Enable SELinux\n"
75 " -v|--verbose Verbose messages\n"
76 " -V|--version Display version and exit\n"
77 " -x Trace libguestfs API calls\n"
78 "For more information, see the manpage %s(1).\n"),
79 program_name, program_name, program_name,
86 main (int argc, char *argv[])
88 /* Set global program name that is not polluted with libtool artifacts. */
89 set_program_name (argv[0]);
91 setlocale (LC_ALL, "");
92 bindtextdomain (PACKAGE, LOCALEBASEDIR);
95 enum { HELP_OPTION = CHAR_MAX + 1 };
97 static const char *options = "a:c:d:m:rvVx";
98 static const struct option long_options[] = {
100 { "append", 1, 0, 0 },
101 { "connect", 1, 0, 'c' },
102 { "domain", 1, 0, 'd' },
103 { "format", 2, 0, 0 },
104 { "help", 0, 0, HELP_OPTION },
105 { "memsize", 1, 0, 'm' },
106 { "network", 0, 0, 0 },
108 { "selinux", 0, 0, 0 },
109 { "verbose", 0, 0, 'v' },
110 { "version", 0, 0, 'V' },
113 struct drv *drvs = NULL;
115 char *p, *file = NULL;
116 const char *format = NULL;
120 const char *append = NULL;
124 g = guestfs_create ();
126 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
130 argv[0] = bad_cast (program_name);
133 c = getopt_long (argc, argv, options, long_options, &option_index);
137 case 0: /* options which are long only */
138 if (STREQ (long_options[option_index].name, "selinux")) {
139 guestfs_set_selinux (g, 1);
140 } else if (STREQ (long_options[option_index].name, "append")) {
142 } else if (STREQ (long_options[option_index].name, "network")) {
144 } else if (STREQ (long_options[option_index].name, "format")) {
145 if (!optarg || STREQ (optarg, ""))
150 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
151 program_name, long_options[option_index].name, option_index);
169 usage (EXIT_SUCCESS);
172 if (sscanf (optarg, "%u", &memsize) != 1) {
173 fprintf (stderr, _("%s: could not parse memory size '%s'\n"),
174 program_name, optarg);
196 usage (EXIT_SUCCESS);
199 usage (EXIT_FAILURE);
203 /* Old-style syntax? There were no -a or -d options in the old
204 * virt-rescue which is how we detect this.
207 while (optind < argc) {
208 if (strchr (argv[optind], '/') ||
209 access (argv[optind], F_OK) == 0) { /* simulate -a option */
210 drv = malloc (sizeof (struct drv));
216 drv->a.filename = argv[optind];
217 drv->a.format = NULL;
220 } else { /* simulate -d option */
221 drv = malloc (sizeof (struct drv));
227 drv->d.guest = argv[optind];
236 /* These are really constants, but they have to be variables for the
237 * options parsing code. Assert here that they have known-good
240 assert (inspector == 0);
241 assert (keys_from_stdin == 0);
242 assert (echo_keys == 0);
244 /* Must be no extra arguments on the command line. */
246 usage (EXIT_FAILURE);
248 /* User must have specified some drives. */
250 usage (EXIT_FAILURE);
252 /* Setting "direct mode" is required for the rescue appliance. */
253 guestfs_set_direct (g, 1);
255 /* Set other features. */
257 guestfs_set_memsize (g, memsize);
259 guestfs_set_network (g, 1);
261 /* Kernel command line must include guestfs_rescue=1 (see
262 * appliance/init) as well as other options.
264 append_full = xasprintf ("guestfs_rescue=1%s%s",
266 append ? append : "");
267 guestfs_set_append (g, append_full);
271 add_drives (drvs, 'a');
273 /* Free up data structures, no longer needed after this point. */
276 /* Run the appliance. This won't return until the user quits the
279 guestfs_set_error_handler (g, NULL, NULL);
282 /* launch() expects guestfsd to start. However, virt-rescue doesn't
283 * run guestfsd, so this will always fail with ECHILD when the
284 * appliance exits unexpectedly.
286 if (errno != ECHILD) {
287 fprintf (stderr, "%s: %s\n", program_name, guestfs_last_error (g));
297 /* The following was a nice idea, but in fact it doesn't work. This is
298 * because qemu has some (broken) pty emulation itself.
303 struct termios tsorig, tsnew;
306 fd_m = posix_openpt (O_RDWR);
308 perror ("posix_openpt");
321 fd_s = open (ptsname (fd_m), O_RDWR);
323 perror ("open ptsname");
337 r = tcgetattr (fd_s, &tsorig);
340 tcsetattr (fd_s, TCSANOW, &tsnew);
343 /* Close the master side of pty and set slave side as
344 * stdin/stdout/stderr.
351 if (dup (fd_s) == -1 || dup (fd_s) == -1 || dup (fd_s) == -1) {
359 perror ("warning: failed to setsid");
360 if (ioctl (0, TIOCSCTTY, 0) == -1)
361 perror ("warning: failed to TIOCSCTTY");
364 /* Run the appliance. This won't return until the user quits the
367 guestfs_set_error_handler (g, NULL, NULL);
368 r = guestfs_launch (g);
370 /* launch() expects guestfsd to start. However, virt-rescue doesn't
371 * run guestfsd, so this will always fail with ECHILD when the
372 * appliance exits unexpectedly.
374 if (errno != ECHILD) {
375 fprintf (stderr, "%s: %s\n", program_name, guestfs_last_error (g));
381 _exit (EXIT_SUCCESS);
384 /* Parent process continues ... */
386 /* Close slave side of pty. */
390 r = tcgetattr (fd_s, &tsorig);
393 tcsetattr (fd_s, TCSANOW, &tsnew);
395 /* Send input and output to master side of pty. */
396 r = multiplex (fd_m);
397 tcsetattr (fd_s, TCSANOW, &tsorig); /* Restore cooked mode. */
401 if (waitpid (pid, &r, 0) == -1) {
405 if (!WIFEXITED (r)) {
406 /* abnormal child exit */
407 fprintf (stderr, _("%s: unknown child exit status (%d)\n"),
412 exit (WEXITSTATUS (r)); /* normal exit, return child process's status */
415 /* Naive and simple multiplex function. */
419 int r, eof_stdin = 0;
421 char tobuf[BUFSIZ], frombuf[BUFSIZ]; /* to/from slave */
422 size_t tosize = 0, fromsize = 0;
425 long flags_0, flags_1, flags_fd_m;
427 flags_0 = fcntl (0, F_GETFL);
428 fcntl (0, F_SETFL, O_NONBLOCK | flags_0);
429 flags_1 = fcntl (0, F_GETFL);
430 fcntl (1, F_SETFL, O_NONBLOCK | flags_1);
431 flags_fd_m = fcntl (0, F_GETFL);
432 fcntl (fd_m, F_SETFL, O_NONBLOCK | flags_fd_m);
438 /* Still space in to-buffer? If so, we can read from the user. */
439 if (!eof_stdin && tosize < BUFSIZ)
441 /* Still space in from-buffer? If so, we can read from the slave. */
442 if (fromsize < BUFSIZ)
443 FD_SET (fd_m, &rfds);
444 /* Content in to-buffer? If so, we want to write to the slave. */
446 FD_SET (fd_m, &wfds);
447 /* Content in from-buffer? If so, we want to write to the user. */
451 r = select (fd_m+1, &rfds, &wfds, NULL, NULL);
453 if (errno == EAGAIN || errno == EINTR)
459 /* Input from user: Put it in the to-buffer. */
460 if (FD_ISSET (0, &rfds)) {
461 count = BUFSIZ - tosize;
462 n = read (0, &tobuf[tosize], count);
467 if (n == 0) { /* stdin was closed */
469 /* This is what telnetd does ... */
470 tobuf[tosize] = '\004';
476 /* Input from slave: Put it in the from-buffer. */
477 if (FD_ISSET (fd_m, &rfds)) {
478 count = BUFSIZ - fromsize;
479 n = read (fd_m, &frombuf[fromsize], count);
481 if (errno != EIO) /* EIO if slave process dies */
485 if (n == 0) /* slave closed the connection */
490 /* Can write to user. */
491 if (FD_ISSET (1, &wfds)) {
492 n = write (1, frombuf, fromsize);
497 memmove (frombuf, &frombuf[n], BUFSIZ - n);
501 /* Can write to slave. */
502 if (FD_ISSET (fd_m, &wfds)) {
503 n = write (fd_m, tobuf, tosize);
508 memmove (tobuf, &tobuf[n], BUFSIZ - n);
513 /* We end up here when slave has closed the connection. */
516 /* Restore blocking behaviour. */
517 fcntl (1, F_SETFL, flags_1);
519 /* Last chance to write out any remaining data in the buffers, but
520 * don't bother about errors.
522 ignore_value (write (1, frombuf, fromsize));