1 /* guestfish - the filesystem interactive shell
2 * Copyright (C) 2009 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.
21 #define _GNU_SOURCE // for strchrnul
32 #ifdef HAVE_LIBREADLINE
33 #include <readline/readline.h>
34 #include <readline/history.h>
47 static void mount_mps (struct mp *mp);
48 static void interactive (void);
49 static void shell_script (void);
50 static void script (int prompt);
51 static void cmdline (char *argv[], int optind, int argc);
52 static int issue_command (const char *cmd, char *argv[]);
53 static int parse_size (const char *str, off_t *size_rtn);
54 static void initialize_readline (void);
55 static void cleanup_readline (void);
56 static void add_history_line (const char *);
58 /* Currently open libguestfs handle. */
66 launch (guestfs_h *_g)
71 if (guestfs_launch (g) == -1)
73 if (guestfs_wait_ready (g) == -1)
84 "guestfish: guest filesystem shell\n"
85 "guestfish lets you edit virtual machine filesystems\n"
86 "Copyright (C) 2009 Red Hat Inc.\n"
88 " guestfish [--options] cmd [: cmd : cmd ...]\n"
89 "or for interactive use:\n"
91 "or from a shell script:\n"
97 " -h|--cmd-help List available commands\n"
98 " -h|--cmd-help cmd Display detailed help on 'cmd'\n"
99 " -a|--add image Add image\n"
100 " -m|--mount dev[:mnt] Mount dev on mnt (if omitted, /)\n"
101 " -n|--no-sync Don't autosync\n"
102 /*" --ro|-r All mounts are read-only\n"*/
103 " -v|--verbose Verbose messages\n"
104 "For more information, see the manpage guestfish(1).\n");
108 main (int argc, char *argv[])
110 static const char *options = "a:h::m:v?";
111 static struct option long_options[] = {
112 { "add", 1, 0, 'a' },
113 { "cmd-help", 2, 0, 'h' },
114 { "help", 0, 0, '?' },
115 { "mount", 1, 0, 'm' },
116 { "no-sync", 0, 0, 'n' },
117 { "verbose", 0, 0, 'v' },
120 struct mp *mps = NULL;
125 initialize_readline ();
127 /* guestfs_create is meant to be a lightweight operation, so
128 * it's OK to do it early here.
130 g = guestfs_create ();
132 fprintf (stderr, "guestfs_create: failed to create handle\n");
136 guestfs_set_autosync (g, 1);
138 /* If developing, add . to the path. Note that libtools interferes
139 * with this because uninstalled guestfish is a shell script that runs
140 * the real program with an absolute path. Detect that too.
143 (argv[0][0] != '/' || strstr (argv[0], "/.libs/lt-") != NULL))
144 guestfs_set_path (g, ".:" GUESTFS_DEFAULT_PATH);
147 c = getopt_long (argc, argv, options, long_options, NULL);
152 if (access (optarg, R_OK) != 0) {
156 if (guestfs_add_drive (g, optarg) == -1)
162 display_command (optarg);
163 else if (argv[optind] && argv[optind][0] != '-')
164 display_command (argv[optind++]);
170 mp = malloc (sizeof (struct mp));
175 p = strchr (optarg, ':');
178 mp->mountpoint = p+1;
180 mp->mountpoint = "/";
187 guestfs_set_autosync (g, 0);
192 guestfs_set_verbose (g, verbose);
200 fprintf (stderr, "guestfish: unexpected command line option 0x%x\n", c);
205 /* If we've got mountpoints, we must launch the guest and mount them. */
207 if (launch (g) == -1) exit (1);
211 /* Interactive, shell script, or command(s) on the command line? */
212 if (optind >= argc) {
219 cmdline (argv, optind, argc);
227 pod2text (const char *heading, const char *str)
231 fp = popen ("pod2text", "w");
233 /* pod2text failed, maybe not found, so let's just print the
234 * source instead, since that's better than doing nothing.
236 printf ("%s\n\n%s\n", heading, str);
239 fputs ("=head1 ", fp);
246 /* List is built in reverse order, so mount them in reverse order. */
248 mount_mps (struct mp *mp)
251 mount_mps (mp->next);
252 if (guestfs_mount (g, mp->device, mp->mountpoint) == -1)
269 #define FISH "><fs> "
271 static char *line_read = NULL;
276 #ifdef HAVE_LIBREADLINE
283 line_read = readline (prompt ? FISH : "");
285 if (prompt && line_read && *line_read)
286 add_history_line (line_read);
288 #else /* !HAVE_LIBREADLINE */
290 static char buf[8192];
293 if (prompt) printf (FISH);
294 line_read = fgets (buf, sizeof buf, stdin);
297 len = strlen (line_read);
298 if (len > 0 && buf[len-1] == '\n') buf[len-1] = '\0';
301 #endif /* !HAVE_LIBREADLINE */
316 "Welcome to guestfish, the libguestfs filesystem interactive shell for\n"
317 "editing virtual machine filesystems.\n"
319 "Type: 'help' for help with commands\n"
320 " 'quit' to quit the shell\n"
324 buf = rl_gets (prompt);
330 /* Split the buffer up at whitespace. */
331 cmd = strtok (buf, " \t");
336 while (i < sizeof argv / sizeof argv[0] &&
337 (argv[i] = strtok (NULL, " \t")) != NULL)
339 if (i == sizeof argv / sizeof argv[0]) {
340 fprintf (stderr, "guestfish: too many arguments in command\n");
344 if (issue_command (cmd, argv) == -1) {
345 if (!prompt) exit (1);
348 if (prompt) printf ("\n");
352 cmdline (char *argv[], int optind, int argc)
357 if (optind >= argc) return;
359 cmd = argv[optind++];
360 if (strcmp (cmd, ":") == 0) {
361 fprintf (stderr, "guestfish: empty command on command line\n");
364 params = &argv[optind];
366 /* Search for end of command list or ":" ... */
367 while (optind < argc && strcmp (argv[optind], ":") != 0)
370 if (optind == argc) {
371 if (issue_command (cmd, params) == -1) exit (1);
374 if (issue_command (cmd, params) == -1) exit (1);
375 cmdline (argv, optind+1, argc);
380 issue_command (const char *cmd, char *argv[])
384 for (argc = 0; argv[argc] != NULL; ++argc)
387 if (strcasecmp (cmd, "help") == 0) {
391 display_command (argv[0]);
394 else if (strcasecmp (cmd, "quit") == 0 ||
395 strcasecmp (cmd, "exit") == 0 ||
396 strcasecmp (cmd, "q") == 0) {
400 else if (strcasecmp (cmd, "alloc") == 0 ||
401 strcasecmp (cmd, "allocate") == 0) {
403 fprintf (stderr, "use 'alloc file size' to create an image\n");
410 if (parse_size (argv[1], &size) == -1)
414 fprintf (stderr, "can't allocate or add disks after launching\n");
418 fd = open (argv[0], O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_TRUNC, 0666);
424 if (posix_fallocate (fd, 0, size) == -1) {
425 perror ("fallocate");
431 if (close (fd) == -1) {
437 if (guestfs_add_drive (g, argv[0]) == -1) {
446 return run_action (cmd, argc, argv);
450 list_builtin_commands (void)
452 /* help and quit should appear at the top */
453 printf ("%-20s %s\n",
454 "help", "display a list of commands or help on a command");
455 printf ("%-20s %s\n",
456 "quit", "quit guestfish");
458 printf ("%-20s %s\n",
459 "alloc", "allocate an image");
461 /* actions are printed after this (see list_commands) */
465 display_builtin_command (const char *cmd)
467 /* help for actions is auto-generated, see display_command */
469 if (strcasecmp (cmd, "alloc") == 0)
470 printf ("alloc - allocate an image\n"
471 " alloc <filename> <size>\n"
473 " This creates an empty (zeroed) file of the given size,\n"
474 " and then adds so it can be further examined.\n"
476 " For more advanced image creation, see qemu-img utility.\n"
478 " Size can be specified (where <nn> means a number):\n"
479 " <nn> number of kilobytes\n"
480 " eg: 1440 standard 3.5\" floppy\n"
481 " <nn>K or <nn>KB number of kilobytes\n"
482 " <nn>M or <nn>MB number of megabytes\n"
483 " <nn>G or <nn>GB number of gigabytes\n"
484 " <nn>sects number of 512 byte sectors\n");
485 else if (strcasecmp (cmd, "help") == 0)
486 printf ("help - display a list of commands or help on a command\n"
489 else if (strcasecmp (cmd, "quit") == 0)
490 printf ("quit - quit guestfish\n"
493 fprintf (stderr, "%s: command not known, use -h to list all commands\n",
497 /* Parse size parameter of alloc command. */
499 parse_size (const char *str, off_t *size_rtn)
504 /* Note that the parsing here is looser than what is specified in the
505 * help, but we may tighten it up in future so beware.
507 if (sscanf (str, "%"SCNu64"%c", &size, &type) == 2) {
509 case 'k': case 'K': size *= 1024; break;
510 case 'm': case 'M': size *= 1024 * 1024; break;
511 case 'g': case 'G': size *= 1024 * 1024 * 1024; break;
512 case 's': size *= 512; break;
514 fprintf (stderr, "could not parse size specification '%s'\n", str);
518 else if (sscanf (str, "%"SCNu64, &size) == 1)
521 fprintf (stderr, "could not parse size specification '%s'\n", str);
525 /* XXX 32 bit file offsets, if anyone uses them? GCC should give
526 * a warning here anyhow.
534 free_strings (char **argv)
538 for (argc = 0; argv[argc] != NULL; ++argc)
544 print_strings (char * const * const argv)
548 for (argc = 0; argv[argc] != NULL; ++argc)
549 printf ("%s\n", argv[argc]);
553 is_true (const char *str)
556 strcasecmp (str, "0") != 0 &&
557 strcasecmp (str, "f") != 0 &&
558 strcasecmp (str, "false") != 0 &&
559 strcasecmp (str, "n") != 0 &&
560 strcasecmp (str, "no") != 0;
563 /* This is quite inadequate for real use. For example, there is no way
564 * to specify an empty list. We need to use a real parser to allow
565 * quoting, empty lists, etc.
568 parse_string_list (const char *str)
571 const char *p, *pend;
575 for (i = 0; str[i]; ++i)
576 if (str[i] == ':') argc++;
578 argv = malloc (sizeof (char *) * (argc+1));
579 if (argv == NULL) { perror ("malloc"); exit (1); }
584 pend = strchrnul (p, ':');
585 argv[i] = strndup (p, pend-p);
587 p = *pend == ':' ? pend+1 : pend;
594 #ifdef HAVE_LIBREADLINE
595 static char histfile[1024];
596 static int nr_history_lines = 0;
600 initialize_readline (void)
602 #ifdef HAVE_LIBREADLINE
605 home = getenv ("HOME");
607 snprintf (histfile, sizeof histfile, "%s/.guestfish", home);
609 (void) read_history (histfile);
612 rl_readline_name = "guestfish";
613 rl_attempted_completion_function = do_completion;
618 cleanup_readline (void)
620 #ifdef HAVE_LIBREADLINE
623 if (histfile[0] != '\0') {
624 fd = open (histfile, O_WRONLY|O_CREAT, 0644);
631 (void) append_history (nr_history_lines, histfile);
637 add_history_line (const char *line)
639 #ifdef HAVE_LIBREADLINE