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 const char *format = NULL;
119 const char *append = NULL;
123 g = guestfs_create ();
125 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
129 argv[0] = bad_cast (program_name);
132 c = getopt_long (argc, argv, options, long_options, &option_index);
136 case 0: /* options which are long only */
137 if (STREQ (long_options[option_index].name, "selinux")) {
138 guestfs_set_selinux (g, 1);
139 } else if (STREQ (long_options[option_index].name, "append")) {
141 } else if (STREQ (long_options[option_index].name, "network")) {
143 } else if (STREQ (long_options[option_index].name, "format")) {
144 if (!optarg || STREQ (optarg, ""))
149 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
150 program_name, long_options[option_index].name, option_index);
168 usage (EXIT_SUCCESS);
171 if (sscanf (optarg, "%u", &memsize) != 1) {
172 fprintf (stderr, _("%s: could not parse memory size '%s'\n"),
173 program_name, optarg);
195 usage (EXIT_SUCCESS);
198 usage (EXIT_FAILURE);
202 /* Old-style syntax? There were no -a or -d options in the old
203 * virt-rescue which is how we detect this.
206 while (optind < argc) {
207 if (strchr (argv[optind], '/') ||
208 access (argv[optind], F_OK) == 0) { /* simulate -a option */
209 drv = malloc (sizeof (struct drv));
215 drv->a.filename = argv[optind];
216 drv->a.format = NULL;
219 } else { /* simulate -d option */
220 drv = malloc (sizeof (struct drv));
226 drv->d.guest = argv[optind];
235 /* These are really constants, but they have to be variables for the
236 * options parsing code. Assert here that they have known-good
239 assert (inspector == 0);
240 assert (keys_from_stdin == 0);
241 assert (echo_keys == 0);
243 /* Must be no extra arguments on the command line. */
245 usage (EXIT_FAILURE);
247 /* User must have specified some drives. */
249 usage (EXIT_FAILURE);
251 /* Setting "direct mode" is required for the rescue appliance. */
252 guestfs_set_direct (g, 1);
254 /* Set other features. */
256 guestfs_set_memsize (g, memsize);
258 guestfs_set_network (g, 1);
260 /* Kernel command line must include guestfs_rescue=1 (see
261 * appliance/init) as well as other options.
263 append_full = xasprintf ("guestfs_rescue=1%s%s",
265 append ? append : "");
266 guestfs_set_append (g, append_full);
270 add_drives (drvs, 'a');
272 /* Free up data structures, no longer needed after this point. */
275 /* Run the appliance. This won't return until the user quits the
278 guestfs_set_error_handler (g, NULL, NULL);
281 /* launch() expects guestfsd to start. However, virt-rescue doesn't
282 * run guestfsd, so this will always fail with ECHILD when the
283 * appliance exits unexpectedly.
285 if (errno != ECHILD) {
286 fprintf (stderr, "%s: %s\n", program_name, guestfs_last_error (g));
296 /* The following was a nice idea, but in fact it doesn't work. This is
297 * because qemu has some (broken) pty emulation itself.
302 struct termios tsorig, tsnew;
305 fd_m = posix_openpt (O_RDWR);
307 perror ("posix_openpt");
320 fd_s = open (ptsname (fd_m), O_RDWR);
322 perror ("open ptsname");
336 r = tcgetattr (fd_s, &tsorig);
339 tcsetattr (fd_s, TCSANOW, &tsnew);
342 /* Close the master side of pty and set slave side as
343 * stdin/stdout/stderr.
350 if (dup (fd_s) == -1 || dup (fd_s) == -1 || dup (fd_s) == -1) {
358 perror ("warning: failed to setsid");
359 if (ioctl (0, TIOCSCTTY, 0) == -1)
360 perror ("warning: failed to TIOCSCTTY");
363 /* Run the appliance. This won't return until the user quits the
366 guestfs_set_error_handler (g, NULL, NULL);
367 r = guestfs_launch (g);
369 /* launch() expects guestfsd to start. However, virt-rescue doesn't
370 * run guestfsd, so this will always fail with ECHILD when the
371 * appliance exits unexpectedly.
373 if (errno != ECHILD) {
374 fprintf (stderr, "%s: %s\n", program_name, guestfs_last_error (g));
380 _exit (EXIT_SUCCESS);
383 /* Parent process continues ... */
385 /* Close slave side of pty. */
389 r = tcgetattr (fd_s, &tsorig);
392 tcsetattr (fd_s, TCSANOW, &tsnew);
394 /* Send input and output to master side of pty. */
395 r = multiplex (fd_m);
396 tcsetattr (fd_s, TCSANOW, &tsorig); /* Restore cooked mode. */
400 if (waitpid (pid, &r, 0) == -1) {
404 if (!WIFEXITED (r)) {
405 /* abnormal child exit */
406 fprintf (stderr, _("%s: unknown child exit status (%d)\n"),
411 exit (WEXITSTATUS (r)); /* normal exit, return child process's status */
414 /* Naive and simple multiplex function. */
418 int r, eof_stdin = 0;
420 char tobuf[BUFSIZ], frombuf[BUFSIZ]; /* to/from slave */
421 size_t tosize = 0, fromsize = 0;
424 long flags_0, flags_1, flags_fd_m;
426 flags_0 = fcntl (0, F_GETFL);
427 fcntl (0, F_SETFL, O_NONBLOCK | flags_0);
428 flags_1 = fcntl (0, F_GETFL);
429 fcntl (1, F_SETFL, O_NONBLOCK | flags_1);
430 flags_fd_m = fcntl (0, F_GETFL);
431 fcntl (fd_m, F_SETFL, O_NONBLOCK | flags_fd_m);
437 /* Still space in to-buffer? If so, we can read from the user. */
438 if (!eof_stdin && tosize < BUFSIZ)
440 /* Still space in from-buffer? If so, we can read from the slave. */
441 if (fromsize < BUFSIZ)
442 FD_SET (fd_m, &rfds);
443 /* Content in to-buffer? If so, we want to write to the slave. */
445 FD_SET (fd_m, &wfds);
446 /* Content in from-buffer? If so, we want to write to the user. */
450 r = select (fd_m+1, &rfds, &wfds, NULL, NULL);
452 if (errno == EAGAIN || errno == EINTR)
458 /* Input from user: Put it in the to-buffer. */
459 if (FD_ISSET (0, &rfds)) {
460 count = BUFSIZ - tosize;
461 n = read (0, &tobuf[tosize], count);
466 if (n == 0) { /* stdin was closed */
468 /* This is what telnetd does ... */
469 tobuf[tosize] = '\004';
475 /* Input from slave: Put it in the from-buffer. */
476 if (FD_ISSET (fd_m, &rfds)) {
477 count = BUFSIZ - fromsize;
478 n = read (fd_m, &frombuf[fromsize], count);
480 if (errno != EIO) /* EIO if slave process dies */
484 if (n == 0) /* slave closed the connection */
489 /* Can write to user. */
490 if (FD_ISSET (1, &wfds)) {
491 n = write (1, frombuf, fromsize);
496 memmove (frombuf, &frombuf[n], BUFSIZ - n);
500 /* Can write to slave. */
501 if (FD_ISSET (fd_m, &wfds)) {
502 n = write (fd_m, tobuf, tosize);
507 memmove (tobuf, &tobuf[n], BUFSIZ - n);
512 /* We end up here when slave has closed the connection. */
515 /* Restore blocking behaviour. */
516 fcntl (1, F_SETFL, flags_1);
518 /* Last chance to write out any remaining data in the buffers, but
519 * don't bother about errors.
521 ignore_value (write (1, frombuf, fromsize));