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
31 #ifdef HAVE_LIBREADLINE
32 #include <readline/readline.h>
33 #include <readline/history.h>
46 static void mount_mps (struct mp *mp);
47 static void interactive (void);
48 static void shell_script (void);
49 static void script (int prompt);
50 static void cmdline (char *argv[], int optind, int argc);
51 static int issue_command (const char *cmd, char *argv[]);
52 static void initialize_readline (void);
53 static void cleanup_readline (void);
54 static void add_history_line (const char *);
56 /* Currently open libguestfs handle. */
65 launch (guestfs_h *_g)
70 if (guestfs_launch (g) == -1)
72 if (guestfs_wait_ready (g) == -1)
83 "guestfish: guest filesystem shell\n"
84 "guestfish lets you edit virtual machine filesystems\n"
85 "Copyright (C) 2009 Red Hat Inc.\n"
87 " guestfish [--options] cmd [: cmd : cmd ...]\n"
88 "or for interactive use:\n"
90 "or from a shell script:\n"
96 " -h|--cmd-help List available commands\n"
97 " -h|--cmd-help cmd Display detailed help on 'cmd'\n"
98 " -a|--add image Add image\n"
99 " -m|--mount dev[:mnt] Mount dev on mnt (if omitted, /)\n"
100 " -n|--no-sync Don't autosync\n"
101 " -r|--ro Mount read-only\n"
102 " -v|--verbose Verbose messages\n"
103 "For more information, see the manpage guestfish(1).\n");
107 main (int argc, char *argv[])
109 static const char *options = "a:h::m:nrv?";
110 static struct option long_options[] = {
111 { "add", 1, 0, 'a' },
112 { "cmd-help", 2, 0, 'h' },
113 { "help", 0, 0, '?' },
114 { "mount", 1, 0, 'm' },
115 { "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.
142 * BUT if LIBGUESTFS_PATH environment variable is already set by
143 * the user, then don't override it.
145 if (getenv ("LIBGUESTFS_PATH") == NULL &&
147 (argv[0][0] != '/' || strstr (argv[0], "/.libs/lt-") != NULL))
148 guestfs_set_path (g, ".:" GUESTFS_DEFAULT_PATH);
151 c = getopt_long (argc, argv, options, long_options, NULL);
156 if (access (optarg, R_OK) != 0) {
160 if (guestfs_add_drive (g, optarg) == -1)
166 display_command (optarg);
167 else if (argv[optind] && argv[optind][0] != '-')
168 display_command (argv[optind++]);
174 mp = malloc (sizeof (struct mp));
179 p = strchr (optarg, ':');
182 mp->mountpoint = p+1;
184 mp->mountpoint = "/";
191 guestfs_set_autosync (g, 0);
200 guestfs_set_verbose (g, verbose);
208 fprintf (stderr, "guestfish: unexpected command line option 0x%x\n", c);
213 /* If we've got mountpoints, we must launch the guest and mount them. */
215 if (launch (g) == -1) exit (1);
219 /* Interactive, shell script, or command(s) on the command line? */
220 if (optind >= argc) {
227 cmdline (argv, optind, argc);
235 pod2text (const char *heading, const char *str)
239 fp = popen ("pod2text", "w");
241 /* pod2text failed, maybe not found, so let's just print the
242 * source instead, since that's better than doing nothing.
244 printf ("%s\n\n%s\n", heading, str);
247 fputs ("=head1 ", fp);
254 /* List is built in reverse order, so mount them in reverse order. */
256 mount_mps (struct mp *mp)
261 mount_mps (mp->next);
263 r = guestfs_mount (g, mp->device, mp->mountpoint);
265 r = guestfs_mount_ro (g, mp->device, mp->mountpoint);
283 #define FISH "><fs> "
285 static char *line_read = NULL;
290 #ifdef HAVE_LIBREADLINE
298 line_read = readline (prompt ? FISH : "");
300 if (line_read && *line_read)
301 add_history_line (line_read);
306 #endif /* HAVE_LIBREADLINE */
308 static char buf[8192];
311 if (prompt) printf (FISH);
312 line_read = fgets (buf, sizeof buf, stdin);
315 len = strlen (line_read);
316 if (len > 0 && buf[len-1] == '\n') buf[len-1] = '\0';
333 "Welcome to guestfish, the libguestfs filesystem interactive shell for\n"
334 "editing virtual machine filesystems.\n"
336 "Type: 'help' for help with commands\n"
337 " 'quit' to quit the shell\n"
341 buf = rl_gets (prompt);
347 /* Skip any initial whitespace before the command. */
348 while (*buf && isspace (*buf))
351 /* Get the command (cannot be quoted). */
352 len = strcspn (buf, " \t");
354 if (len == 0) continue;
358 if (buf[len] == '\0') {
365 p += strspn (p, " \t");
367 /* Get the parameters. */
368 while (*p && i < sizeof argv / sizeof argv[0]) {
369 /* Parameters which start with quotes or square brackets
370 * are treated specially. Bare parameters are delimited
375 len = strcspn (p, "\"");
376 if (p[len] == '\0') {
377 fprintf (stderr, "guestfish: unterminated double quote\n");
378 if (!prompt) exit (1);
381 if (p[len+1] && (p[len+1] != ' ' && p[len+1] != '\t')) {
382 fprintf (stderr, "guestfish: command arguments not separated by whitespace\n");
383 if (!prompt) exit (1);
387 pend = p[len+1] ? &p[len+2] : &p[len+1];
388 } else if (*p == '\'') {
390 len = strcspn (p, "'");
391 if (p[len] == '\0') {
392 fprintf (stderr, "guestfish: unterminated single quote\n");
393 if (!prompt) exit (1);
396 if (p[len+1] && (p[len+1] != ' ' && p[len+1] != '\t')) {
397 fprintf (stderr, "guestfish: command arguments not separated by whitespace\n");
398 if (!prompt) exit (1);
402 pend = p[len+1] ? &p[len+2] : &p[len+1];
404 } else if (*p == '[') {
408 while (*pend && c != 0) {
409 if (*pend == '[') c++;
410 else if (*pend == ']') c--;
414 fprintf (stderr, "guestfish: unterminated \"[...]\" sequence\n");
415 if (!prompt) exit (1);
418 if (*pend && (*pend != ' ' && *pend != '\t')) {
419 fprintf (stderr, "guestfish: command arguments not separated by whitespace\n");
420 if (!prompt) exit (1);
425 } else if (*p != ' ' && *p != '\t') {
426 len = strcspn (p, " \t");
433 fprintf (stderr, "guestfish: internal error parsing string at '%s'\n",
442 p += strspn (p, " \t");
445 if (i == sizeof argv / sizeof argv[0]) {
446 fprintf (stderr, "guestfish: too many arguments\n");
447 if (!prompt) exit (1);
454 if (issue_command (cmd, argv) == -1) {
455 if (!prompt) exit (1);
460 if (prompt) printf ("\n");
464 cmdline (char *argv[], int optind, int argc)
469 if (optind >= argc) return;
471 cmd = argv[optind++];
472 if (strcmp (cmd, ":") == 0) {
473 fprintf (stderr, "guestfish: empty command on command line\n");
476 params = &argv[optind];
478 /* Search for end of command list or ":" ... */
479 while (optind < argc && strcmp (argv[optind], ":") != 0)
482 if (optind == argc) {
483 if (issue_command (cmd, params) == -1) exit (1);
486 if (issue_command (cmd, params) == -1) exit (1);
487 cmdline (argv, optind+1, argc);
492 issue_command (const char *cmd, char *argv[])
496 for (argc = 0; argv[argc] != NULL; ++argc)
499 if (strcasecmp (cmd, "help") == 0) {
503 display_command (argv[0]);
506 else if (strcasecmp (cmd, "quit") == 0 ||
507 strcasecmp (cmd, "exit") == 0 ||
508 strcasecmp (cmd, "q") == 0) {
512 else if (strcasecmp (cmd, "alloc") == 0 ||
513 strcasecmp (cmd, "allocate") == 0)
514 return do_alloc (cmd, argc, argv);
515 else if (strcasecmp (cmd, "edit") == 0 ||
516 strcasecmp (cmd, "vi") == 0 ||
517 strcasecmp (cmd, "emacs") == 0)
518 return do_edit (cmd, argc, argv);
520 return run_action (cmd, argc, argv);
524 list_builtin_commands (void)
526 /* help and quit should appear at the top */
527 printf ("%-20s %s\n",
528 "help", "display a list of commands or help on a command");
529 printf ("%-20s %s\n",
530 "quit", "quit guestfish");
532 printf ("%-20s %s\n",
533 "alloc", "allocate an image");
534 printf ("%-20s %s\n",
535 "edit", "edit a file in the image");
537 /* actions are printed after this (see list_commands) */
541 display_builtin_command (const char *cmd)
543 /* help for actions is auto-generated, see display_command */
545 if (strcasecmp (cmd, "alloc") == 0 ||
546 strcasecmp (cmd, "allocate") == 0)
547 printf ("alloc - allocate an image\n"
548 " alloc <filename> <size>\n"
550 " This creates an empty (zeroed) file of the given size,\n"
551 " and then adds so it can be further examined.\n"
553 " For more advanced image creation, see qemu-img utility.\n"
555 " Size can be specified (where <nn> means a number):\n"
556 " <nn> number of kilobytes\n"
557 " eg: 1440 standard 3.5\" floppy\n"
558 " <nn>K or <nn>KB number of kilobytes\n"
559 " <nn>M or <nn>MB number of megabytes\n"
560 " <nn>G or <nn>GB number of gigabytes\n"
561 " <nn>sects number of 512 byte sectors\n");
562 else if (strcasecmp (cmd, "edit") == 0 ||
563 strcasecmp (cmd, "vi") == 0 ||
564 strcasecmp (cmd, "emacs") == 0)
565 printf ("edit - edit a file in the image\n"
568 " This is used to edit a file.\n"
570 " It is the equivalent of (and is implemented by)\n"
571 " running \"cat\", editing locally, and then \"write-file\".\n"
573 " Normally it uses $EDITOR, but if you use the aliases\n"
574 " \"vi\" or \"emacs\" you will get those editors.\n"
576 " NOTE: This will not work reliably for large files\n"
577 " (> 2 MB) or binary files containing \\0 bytes.\n");
578 else if (strcasecmp (cmd, "help") == 0)
579 printf ("help - display a list of commands or help on a command\n"
582 else if (strcasecmp (cmd, "quit") == 0 ||
583 strcasecmp (cmd, "exit") == 0 ||
584 strcasecmp (cmd, "q") == 0)
585 printf ("quit - quit guestfish\n"
588 fprintf (stderr, "%s: command not known, use -h to list all commands\n",
593 free_strings (char **argv)
597 for (argc = 0; argv[argc] != NULL; ++argc)
603 print_strings (char * const * const argv)
607 for (argc = 0; argv[argc] != NULL; ++argc)
608 printf ("%s\n", argv[argc]);
612 print_table (char * const * const argv)
616 for (i = 0; argv[i] != NULL; i += 2)
617 printf ("%s: %s\n", argv[i], argv[i+1]);
621 is_true (const char *str)
624 strcasecmp (str, "0") != 0 &&
625 strcasecmp (str, "f") != 0 &&
626 strcasecmp (str, "false") != 0 &&
627 strcasecmp (str, "n") != 0 &&
628 strcasecmp (str, "no") != 0;
631 /* XXX We could improve list parsing. */
633 parse_string_list (const char *str)
636 const char *p, *pend;
640 for (i = 0; str[i]; ++i)
641 if (str[i] == ' ') argc++;
643 argv = malloc (sizeof (char *) * (argc+1));
644 if (argv == NULL) { perror ("malloc"); exit (1); }
649 pend = strchrnul (p, ' ');
650 argv[i] = strndup (p, pend-p);
652 p = *pend == ' ' ? pend+1 : pend;
659 #ifdef HAVE_LIBREADLINE
660 static char histfile[1024];
661 static int nr_history_lines = 0;
665 initialize_readline (void)
667 #ifdef HAVE_LIBREADLINE
670 home = getenv ("HOME");
672 snprintf (histfile, sizeof histfile, "%s/.guestfish", home);
674 (void) read_history (histfile);
677 rl_readline_name = "guestfish";
678 rl_attempted_completion_function = do_completion;
683 cleanup_readline (void)
685 #ifdef HAVE_LIBREADLINE
688 if (histfile[0] != '\0') {
689 fd = open (histfile, O_WRONLY|O_CREAT, 0644);
696 (void) append_history (nr_history_lines, histfile);
702 add_history_line (const char *line)
704 #ifdef HAVE_LIBREADLINE