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. */
41 int keys_from_stdin = 0;
43 const char *libvirt_uri = NULL;
47 bad_cast (char const *s)
52 static void __attribute__((noreturn))
55 if (status != EXIT_SUCCESS)
56 fprintf (stderr, _("Try `%s --help' for more information.\n"),
60 _("%s: Run a rescue shell on a virtual machine\n"
61 "Copyright (C) 2009-2010 Red Hat Inc.\n"
63 " %s [--options] -d domname\n"
64 " %s [--options] -a disk.img [-a disk.img ...]\n"
66 " -a|--add image Add image\n"
67 " --append kernelopts Append kernel options\n"
68 " -c|--connect uri Specify libvirt URI for -d option\n"
69 " -d|--domain guest Add disks from libvirt guest\n"
70 " --format[=raw|..] Force disk format for -a option\n"
71 " --help Display brief help\n"
72 " -m|--memsize MB Set memory size in megabytes\n"
73 " --network Enable network\n"
74 " -r|--ro Access read-only\n"
75 " --selinux Enable SELinux\n"
76 " -v|--verbose Verbose messages\n"
77 " -V|--version Display version and exit\n"
78 " -w|--rw Mount read-write\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 },
111 { "selinux", 0, 0, 0 },
112 { "verbose", 0, 0, 'v' },
113 { "version", 0, 0, 'V' },
116 struct drv *drvs = NULL;
118 const char *format = NULL;
122 const char *append = NULL;
126 g = guestfs_create ();
128 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
132 argv[0] = bad_cast (program_name);
135 c = getopt_long (argc, argv, options, long_options, &option_index);
139 case 0: /* options which are long only */
140 if (STREQ (long_options[option_index].name, "selinux")) {
141 guestfs_set_selinux (g, 1);
142 } else if (STREQ (long_options[option_index].name, "append")) {
144 } else if (STREQ (long_options[option_index].name, "network")) {
146 } else if (STREQ (long_options[option_index].name, "format")) {
147 if (!optarg || STREQ (optarg, ""))
152 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
153 program_name, long_options[option_index].name, option_index);
171 usage (EXIT_SUCCESS);
174 if (sscanf (optarg, "%u", &memsize) != 1) {
175 fprintf (stderr, _("%s: could not parse memory size '%s'\n"),
176 program_name, optarg);
202 usage (EXIT_SUCCESS);
205 usage (EXIT_FAILURE);
209 /* Old-style syntax? There were no -a or -d options in the old
210 * virt-rescue which is how we detect this.
213 while (optind < argc) {
214 if (strchr (argv[optind], '/') ||
215 access (argv[optind], F_OK) == 0) { /* simulate -a option */
216 drv = malloc (sizeof (struct drv));
222 drv->a.filename = argv[optind];
223 drv->a.format = NULL;
226 } else { /* simulate -d option */
227 drv = malloc (sizeof (struct drv));
233 drv->d.guest = argv[optind];
242 /* These are really constants, but they have to be variables for the
243 * options parsing code. Assert here that they have known-good
246 assert (inspector == 0);
247 assert (keys_from_stdin == 0);
248 assert (echo_keys == 0);
250 /* Must be no extra arguments on the command line. */
252 usage (EXIT_FAILURE);
254 /* User must have specified some drives. */
256 usage (EXIT_FAILURE);
258 /* Setting "direct mode" is required for the rescue appliance. */
259 guestfs_set_direct (g, 1);
261 /* Set other features. */
263 guestfs_set_memsize (g, memsize);
265 guestfs_set_network (g, 1);
267 /* Kernel command line must include guestfs_rescue=1 (see
268 * appliance/init) as well as other options.
270 append_full = xasprintf ("guestfs_rescue=1%s%s",
272 append ? append : "");
273 guestfs_set_append (g, append_full);
277 add_drives (drvs, 'a');
279 /* Free up data structures, no longer needed after this point. */
282 /* Run the appliance. This won't return until the user quits the
285 guestfs_set_error_handler (g, NULL, NULL);
288 /* launch() expects guestfsd to start. However, virt-rescue doesn't
289 * run guestfsd, so this will always fail with ECHILD when the
290 * appliance exits unexpectedly.
292 if (errno != ECHILD) {
293 fprintf (stderr, "%s: %s\n", program_name, guestfs_last_error (g));
303 /* The following was a nice idea, but in fact it doesn't work. This is
304 * because qemu has some (broken) pty emulation itself.
309 struct termios tsorig, tsnew;
312 fd_m = posix_openpt (O_RDWR);
314 perror ("posix_openpt");
327 fd_s = open (ptsname (fd_m), O_RDWR);
329 perror ("open ptsname");
343 r = tcgetattr (fd_s, &tsorig);
346 tcsetattr (fd_s, TCSANOW, &tsnew);
349 /* Close the master side of pty and set slave side as
350 * stdin/stdout/stderr.
357 if (dup (fd_s) == -1 || dup (fd_s) == -1 || dup (fd_s) == -1) {
365 perror ("warning: failed to setsid");
366 if (ioctl (0, TIOCSCTTY, 0) == -1)
367 perror ("warning: failed to TIOCSCTTY");
370 /* Run the appliance. This won't return until the user quits the
373 guestfs_set_error_handler (g, NULL, NULL);
374 r = guestfs_launch (g);
376 /* launch() expects guestfsd to start. However, virt-rescue doesn't
377 * run guestfsd, so this will always fail with ECHILD when the
378 * appliance exits unexpectedly.
380 if (errno != ECHILD) {
381 fprintf (stderr, "%s: %s\n", program_name, guestfs_last_error (g));
387 _exit (EXIT_SUCCESS);
390 /* Parent process continues ... */
392 /* Close slave side of pty. */
396 r = tcgetattr (fd_s, &tsorig);
399 tcsetattr (fd_s, TCSANOW, &tsnew);
401 /* Send input and output to master side of pty. */
402 r = multiplex (fd_m);
403 tcsetattr (fd_s, TCSANOW, &tsorig); /* Restore cooked mode. */
407 if (waitpid (pid, &r, 0) == -1) {
411 if (!WIFEXITED (r)) {
412 /* abnormal child exit */
413 fprintf (stderr, _("%s: unknown child exit status (%d)\n"),
418 exit (WEXITSTATUS (r)); /* normal exit, return child process's status */
421 /* Naive and simple multiplex function. */
425 int r, eof_stdin = 0;
427 char tobuf[BUFSIZ], frombuf[BUFSIZ]; /* to/from slave */
428 size_t tosize = 0, fromsize = 0;
431 long flags_0, flags_1, flags_fd_m;
433 flags_0 = fcntl (0, F_GETFL);
434 fcntl (0, F_SETFL, O_NONBLOCK | flags_0);
435 flags_1 = fcntl (0, F_GETFL);
436 fcntl (1, F_SETFL, O_NONBLOCK | flags_1);
437 flags_fd_m = fcntl (0, F_GETFL);
438 fcntl (fd_m, F_SETFL, O_NONBLOCK | flags_fd_m);
444 /* Still space in to-buffer? If so, we can read from the user. */
445 if (!eof_stdin && tosize < BUFSIZ)
447 /* Still space in from-buffer? If so, we can read from the slave. */
448 if (fromsize < BUFSIZ)
449 FD_SET (fd_m, &rfds);
450 /* Content in to-buffer? If so, we want to write to the slave. */
452 FD_SET (fd_m, &wfds);
453 /* Content in from-buffer? If so, we want to write to the user. */
457 r = select (fd_m+1, &rfds, &wfds, NULL, NULL);
459 if (errno == EAGAIN || errno == EINTR)
465 /* Input from user: Put it in the to-buffer. */
466 if (FD_ISSET (0, &rfds)) {
467 count = BUFSIZ - tosize;
468 n = read (0, &tobuf[tosize], count);
473 if (n == 0) { /* stdin was closed */
475 /* This is what telnetd does ... */
476 tobuf[tosize] = '\004';
482 /* Input from slave: Put it in the from-buffer. */
483 if (FD_ISSET (fd_m, &rfds)) {
484 count = BUFSIZ - fromsize;
485 n = read (fd_m, &frombuf[fromsize], count);
487 if (errno != EIO) /* EIO if slave process dies */
491 if (n == 0) /* slave closed the connection */
496 /* Can write to user. */
497 if (FD_ISSET (1, &wfds)) {
498 n = write (1, frombuf, fromsize);
503 memmove (frombuf, &frombuf[n], BUFSIZ - n);
507 /* Can write to slave. */
508 if (FD_ISSET (fd_m, &wfds)) {
509 n = write (fd_m, tobuf, tosize);
514 memmove (tobuf, &tobuf[n], BUFSIZ - n);
519 /* We end up here when slave has closed the connection. */
522 /* Restore blocking behaviour. */
523 fcntl (1, F_SETFL, flags_1);
525 /* Last chance to write out any remaining data in the buffers, but
526 * don't bother about errors.
528 ignore_value (write (1, frombuf, fromsize));