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 " --smp N Enable SMP with N >= 2 virtual CPUs\n"
81 " -v|--verbose Verbose messages\n"
82 " -V|--version Display version and exit\n"
83 " -w|--rw Mount read-write\n"
84 " -x Trace libguestfs API calls\n"
85 "For more information, see the manpage %s(1).\n"),
86 program_name, program_name, program_name,
93 main (int argc, char *argv[])
95 /* Set global program name that is not polluted with libtool artifacts. */
96 set_program_name (argv[0]);
98 setlocale (LC_ALL, "");
99 bindtextdomain (PACKAGE, LOCALEBASEDIR);
100 textdomain (PACKAGE);
104 enum { HELP_OPTION = CHAR_MAX + 1 };
106 static const char *options = "a:c:d:m:rvVx";
107 static const struct option long_options[] = {
108 { "add", 1, 0, 'a' },
109 { "append", 1, 0, 0 },
110 { "connect", 1, 0, 'c' },
111 { "domain", 1, 0, 'd' },
112 { "format", 2, 0, 0 },
113 { "help", 0, 0, HELP_OPTION },
114 { "memsize", 1, 0, 'm' },
115 { "network", 0, 0, 0 },
118 { "selinux", 0, 0, 0 },
120 { "verbose", 0, 0, 'v' },
121 { "version", 0, 0, 'V' },
124 struct drv *drvs = NULL;
126 const char *format = NULL;
130 const char *append = NULL;
135 g = guestfs_create ();
137 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
141 argv[0] = bad_cast (program_name);
144 c = getopt_long (argc, argv, options, long_options, &option_index);
148 case 0: /* options which are long only */
149 if (STREQ (long_options[option_index].name, "selinux")) {
150 guestfs_set_selinux (g, 1);
151 } else if (STREQ (long_options[option_index].name, "append")) {
153 } else if (STREQ (long_options[option_index].name, "network")) {
155 } else if (STREQ (long_options[option_index].name, "format")) {
156 if (!optarg || STREQ (optarg, ""))
160 } else if (STREQ (long_options[option_index].name, "smp")) {
161 if (sscanf (optarg, "%u", &smp) != 1) {
162 fprintf (stderr, _("%s: could not parse --smp parameter '%s'\n"),
163 program_name, optarg);
167 fprintf (stderr, _("%s: --smp parameter '%s' should be >= 1\n"),
168 program_name, optarg);
172 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
173 program_name, long_options[option_index].name, option_index);
191 usage (EXIT_SUCCESS);
194 if (sscanf (optarg, "%u", &memsize) != 1) {
195 fprintf (stderr, _("%s: could not parse memory size '%s'\n"),
196 program_name, optarg);
222 usage (EXIT_SUCCESS);
225 usage (EXIT_FAILURE);
229 /* Old-style syntax? There were no -a or -d options in the old
230 * virt-rescue which is how we detect this.
233 while (optind < argc) {
234 if (strchr (argv[optind], '/') ||
235 access (argv[optind], F_OK) == 0) { /* simulate -a option */
236 drv = malloc (sizeof (struct drv));
242 drv->a.filename = argv[optind];
243 drv->a.format = NULL;
246 } else { /* simulate -d option */
247 drv = malloc (sizeof (struct drv));
253 drv->d.guest = argv[optind];
262 /* These are really constants, but they have to be variables for the
263 * options parsing code. Assert here that they have known-good
266 assert (inspector == 0);
267 assert (keys_from_stdin == 0);
268 assert (echo_keys == 0);
271 /* Must be no extra arguments on the command line. */
273 usage (EXIT_FAILURE);
275 /* User must have specified some drives. */
277 usage (EXIT_FAILURE);
279 /* Setting "direct mode" is required for the rescue appliance. */
280 guestfs_set_direct (g, 1);
282 /* Set other features. */
284 guestfs_set_memsize (g, memsize);
286 guestfs_set_network (g, 1);
288 guestfs_set_smp (g, smp);
290 /* Kernel command line must include guestfs_rescue=1 (see
291 * appliance/init) as well as other options.
293 append_full = xasprintf ("guestfs_rescue=1%s%s",
295 append ? append : "");
296 guestfs_set_append (g, append_full);
300 add_drives (drvs, 'a');
302 /* Free up data structures, no longer needed after this point. */
305 /* Run the appliance. This won't return until the user quits the
308 guestfs_set_error_handler (g, NULL, NULL);
310 /* We expect launch to fail, so ignore the return value. */
311 ignore_value (guestfs_launch (g));
318 /* The following was a nice idea, but in fact it doesn't work. This is
319 * because qemu has some (broken) pty emulation itself.
324 struct termios tsorig, tsnew;
327 fd_m = posix_openpt (O_RDWR);
329 perror ("posix_openpt");
342 fd_s = open (ptsname (fd_m), O_RDWR);
344 perror ("open ptsname");
358 r = tcgetattr (fd_s, &tsorig);
361 tcsetattr (fd_s, TCSANOW, &tsnew);
364 /* Close the master side of pty and set slave side as
365 * stdin/stdout/stderr.
372 if (dup (fd_s) == -1 || dup (fd_s) == -1 || dup (fd_s) == -1) {
380 perror ("warning: failed to setsid");
381 if (ioctl (0, TIOCSCTTY, 0) == -1)
382 perror ("warning: failed to TIOCSCTTY");
385 /* Run the appliance. This won't return until the user quits the
388 guestfs_set_error_handler (g, NULL, NULL);
389 r = guestfs_launch (g);
391 /* launch() expects guestfsd to start. However, virt-rescue doesn't
392 * run guestfsd, so this will always fail with ECHILD when the
393 * appliance exits unexpectedly.
395 if (errno != ECHILD) {
396 fprintf (stderr, "%s: %s\n", program_name, guestfs_last_error (g));
402 _exit (EXIT_SUCCESS);
405 /* Parent process continues ... */
407 /* Close slave side of pty. */
411 r = tcgetattr (fd_s, &tsorig);
414 tcsetattr (fd_s, TCSANOW, &tsnew);
416 /* Send input and output to master side of pty. */
417 r = multiplex (fd_m);
418 tcsetattr (fd_s, TCSANOW, &tsorig); /* Restore cooked mode. */
422 if (waitpid (pid, &r, 0) == -1) {
426 if (!WIFEXITED (r)) {
427 /* abnormal child exit */
428 fprintf (stderr, _("%s: unknown child exit status (%d)\n"),
433 exit (WEXITSTATUS (r)); /* normal exit, return child process's status */
436 /* Naive and simple multiplex function. */
440 int r, eof_stdin = 0;
442 char tobuf[BUFSIZ], frombuf[BUFSIZ]; /* to/from slave */
443 size_t tosize = 0, fromsize = 0;
446 long flags_0, flags_1, flags_fd_m;
448 flags_0 = fcntl (0, F_GETFL);
449 fcntl (0, F_SETFL, O_NONBLOCK | flags_0);
450 flags_1 = fcntl (0, F_GETFL);
451 fcntl (1, F_SETFL, O_NONBLOCK | flags_1);
452 flags_fd_m = fcntl (0, F_GETFL);
453 fcntl (fd_m, F_SETFL, O_NONBLOCK | flags_fd_m);
459 /* Still space in to-buffer? If so, we can read from the user. */
460 if (!eof_stdin && tosize < BUFSIZ)
462 /* Still space in from-buffer? If so, we can read from the slave. */
463 if (fromsize < BUFSIZ)
464 FD_SET (fd_m, &rfds);
465 /* Content in to-buffer? If so, we want to write to the slave. */
467 FD_SET (fd_m, &wfds);
468 /* Content in from-buffer? If so, we want to write to the user. */
472 r = select (fd_m+1, &rfds, &wfds, NULL, NULL);
474 if (errno == EAGAIN || errno == EINTR)
480 /* Input from user: Put it in the to-buffer. */
481 if (FD_ISSET (0, &rfds)) {
482 count = BUFSIZ - tosize;
483 n = read (0, &tobuf[tosize], count);
488 if (n == 0) { /* stdin was closed */
490 /* This is what telnetd does ... */
491 tobuf[tosize] = '\004';
497 /* Input from slave: Put it in the from-buffer. */
498 if (FD_ISSET (fd_m, &rfds)) {
499 count = BUFSIZ - fromsize;
500 n = read (fd_m, &frombuf[fromsize], count);
502 if (errno != EIO) /* EIO if slave process dies */
506 if (n == 0) /* slave closed the connection */
511 /* Can write to user. */
512 if (FD_ISSET (1, &wfds)) {
513 n = write (1, frombuf, fromsize);
518 memmove (frombuf, &frombuf[n], BUFSIZ - n);
522 /* Can write to slave. */
523 if (FD_ISSET (fd_m, &wfds)) {
524 n = write (fd_m, tobuf, tosize);
529 memmove (tobuf, &tobuf[n], BUFSIZ - n);
534 /* We end up here when slave has closed the connection. */
537 /* Restore blocking behaviour. */
538 fcntl (1, F_SETFL, flags_1);
540 /* Last chance to write out any remaining data in the buffers, but
541 * don't bother about errors.
543 ignore_value (write (1, frombuf, fromsize));