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));
297 /* launch() expects guestfsd to start. However, virt-rescue doesn't
298 * run guestfsd, so this will always fail with ECHILD when the
299 * appliance exits unexpectedly.
301 if (errno != ECHILD) {
302 fprintf (stderr, "%s: %s\n", program_name, guestfs_last_error (g));
312 /* The following was a nice idea, but in fact it doesn't work. This is
313 * because qemu has some (broken) pty emulation itself.
318 struct termios tsorig, tsnew;
321 fd_m = posix_openpt (O_RDWR);
323 perror ("posix_openpt");
336 fd_s = open (ptsname (fd_m), O_RDWR);
338 perror ("open ptsname");
352 r = tcgetattr (fd_s, &tsorig);
355 tcsetattr (fd_s, TCSANOW, &tsnew);
358 /* Close the master side of pty and set slave side as
359 * stdin/stdout/stderr.
366 if (dup (fd_s) == -1 || dup (fd_s) == -1 || dup (fd_s) == -1) {
374 perror ("warning: failed to setsid");
375 if (ioctl (0, TIOCSCTTY, 0) == -1)
376 perror ("warning: failed to TIOCSCTTY");
379 /* Run the appliance. This won't return until the user quits the
382 guestfs_set_error_handler (g, NULL, NULL);
383 r = guestfs_launch (g);
385 /* launch() expects guestfsd to start. However, virt-rescue doesn't
386 * run guestfsd, so this will always fail with ECHILD when the
387 * appliance exits unexpectedly.
389 if (errno != ECHILD) {
390 fprintf (stderr, "%s: %s\n", program_name, guestfs_last_error (g));
396 _exit (EXIT_SUCCESS);
399 /* Parent process continues ... */
401 /* Close slave side of pty. */
405 r = tcgetattr (fd_s, &tsorig);
408 tcsetattr (fd_s, TCSANOW, &tsnew);
410 /* Send input and output to master side of pty. */
411 r = multiplex (fd_m);
412 tcsetattr (fd_s, TCSANOW, &tsorig); /* Restore cooked mode. */
416 if (waitpid (pid, &r, 0) == -1) {
420 if (!WIFEXITED (r)) {
421 /* abnormal child exit */
422 fprintf (stderr, _("%s: unknown child exit status (%d)\n"),
427 exit (WEXITSTATUS (r)); /* normal exit, return child process's status */
430 /* Naive and simple multiplex function. */
434 int r, eof_stdin = 0;
436 char tobuf[BUFSIZ], frombuf[BUFSIZ]; /* to/from slave */
437 size_t tosize = 0, fromsize = 0;
440 long flags_0, flags_1, flags_fd_m;
442 flags_0 = fcntl (0, F_GETFL);
443 fcntl (0, F_SETFL, O_NONBLOCK | flags_0);
444 flags_1 = fcntl (0, F_GETFL);
445 fcntl (1, F_SETFL, O_NONBLOCK | flags_1);
446 flags_fd_m = fcntl (0, F_GETFL);
447 fcntl (fd_m, F_SETFL, O_NONBLOCK | flags_fd_m);
453 /* Still space in to-buffer? If so, we can read from the user. */
454 if (!eof_stdin && tosize < BUFSIZ)
456 /* Still space in from-buffer? If so, we can read from the slave. */
457 if (fromsize < BUFSIZ)
458 FD_SET (fd_m, &rfds);
459 /* Content in to-buffer? If so, we want to write to the slave. */
461 FD_SET (fd_m, &wfds);
462 /* Content in from-buffer? If so, we want to write to the user. */
466 r = select (fd_m+1, &rfds, &wfds, NULL, NULL);
468 if (errno == EAGAIN || errno == EINTR)
474 /* Input from user: Put it in the to-buffer. */
475 if (FD_ISSET (0, &rfds)) {
476 count = BUFSIZ - tosize;
477 n = read (0, &tobuf[tosize], count);
482 if (n == 0) { /* stdin was closed */
484 /* This is what telnetd does ... */
485 tobuf[tosize] = '\004';
491 /* Input from slave: Put it in the from-buffer. */
492 if (FD_ISSET (fd_m, &rfds)) {
493 count = BUFSIZ - fromsize;
494 n = read (fd_m, &frombuf[fromsize], count);
496 if (errno != EIO) /* EIO if slave process dies */
500 if (n == 0) /* slave closed the connection */
505 /* Can write to user. */
506 if (FD_ISSET (1, &wfds)) {
507 n = write (1, frombuf, fromsize);
512 memmove (frombuf, &frombuf[n], BUFSIZ - n);
516 /* Can write to slave. */
517 if (FD_ISSET (fd_m, &wfds)) {
518 n = write (fd_m, tobuf, tosize);
523 memmove (tobuf, &tobuf[n], BUFSIZ - n);
528 /* We end up here when slave has closed the connection. */
531 /* Restore blocking behaviour. */
532 fcntl (1, F_SETFL, flags_1);
534 /* Last chance to write out any remaining data in the buffers, but
535 * don't bother about errors.
537 ignore_value (write (1, frombuf, fromsize));