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 " -V|--version Display version and exit\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:nrv?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' },
118 { "verbose", 0, 0, 'v' },
119 { "version", 0, 0, 'V' },
122 struct mp *mps = NULL;
127 initialize_readline ();
129 /* guestfs_create is meant to be a lightweight operation, so
130 * it's OK to do it early here.
132 g = guestfs_create ();
134 fprintf (stderr, "guestfs_create: failed to create handle\n");
138 guestfs_set_autosync (g, 1);
140 /* If developing, add . to the path. Note that libtools interferes
141 * with this because uninstalled guestfish is a shell script that runs
142 * the real program with an absolute path. Detect that too.
144 * BUT if LIBGUESTFS_PATH environment variable is already set by
145 * the user, then don't override it.
147 if (getenv ("LIBGUESTFS_PATH") == NULL &&
149 (argv[0][0] != '/' || strstr (argv[0], "/.libs/lt-") != NULL))
150 guestfs_set_path (g, ".:" GUESTFS_DEFAULT_PATH);
153 c = getopt_long (argc, argv, options, long_options, NULL);
158 if (access (optarg, R_OK) != 0) {
162 if (guestfs_add_drive (g, optarg) == -1)
168 display_command (optarg);
169 else if (argv[optind] && argv[optind][0] != '-')
170 display_command (argv[optind++]);
176 mp = malloc (sizeof (struct mp));
181 p = strchr (optarg, ':');
184 mp->mountpoint = p+1;
186 mp->mountpoint = "/";
193 guestfs_set_autosync (g, 0);
202 guestfs_set_verbose (g, verbose);
206 printf ("guestfish %s\n", PACKAGE_VERSION);
214 fprintf (stderr, "guestfish: unexpected command line option 0x%x\n", c);
219 /* If we've got mountpoints, we must launch the guest and mount them. */
221 if (launch (g) == -1) exit (1);
225 /* Interactive, shell script, or command(s) on the command line? */
226 if (optind >= argc) {
233 cmdline (argv, optind, argc);
241 pod2text (const char *heading, const char *str)
245 fp = popen ("pod2text", "w");
247 /* pod2text failed, maybe not found, so let's just print the
248 * source instead, since that's better than doing nothing.
250 printf ("%s\n\n%s\n", heading, str);
253 fputs ("=head1 ", fp);
260 /* List is built in reverse order, so mount them in reverse order. */
262 mount_mps (struct mp *mp)
267 mount_mps (mp->next);
269 r = guestfs_mount (g, mp->device, mp->mountpoint);
271 r = guestfs_mount_ro (g, mp->device, mp->mountpoint);
289 #define FISH "><fs> "
291 static char *line_read = NULL;
296 #ifdef HAVE_LIBREADLINE
304 line_read = readline (prompt ? FISH : "");
306 if (line_read && *line_read)
307 add_history_line (line_read);
312 #endif /* HAVE_LIBREADLINE */
314 static char buf[8192];
317 if (prompt) printf (FISH);
318 line_read = fgets (buf, sizeof buf, stdin);
321 len = strlen (line_read);
322 if (len > 0 && buf[len-1] == '\n') buf[len-1] = '\0';
339 "Welcome to guestfish, the libguestfs filesystem interactive shell for\n"
340 "editing virtual machine filesystems.\n"
342 "Type: 'help' for help with commands\n"
343 " 'quit' to quit the shell\n"
347 buf = rl_gets (prompt);
353 /* Skip any initial whitespace before the command. */
354 while (*buf && isspace (*buf))
359 /* If the next character is '#' then this is a comment. */
360 if (*buf == '#') continue;
362 /* Get the command (cannot be quoted). */
363 len = strcspn (buf, " \t");
365 if (len == 0) continue;
369 if (buf[len] == '\0') {
376 p += strspn (p, " \t");
378 /* Get the parameters. */
379 while (*p && i < sizeof argv / sizeof argv[0]) {
380 /* Parameters which start with quotes or square brackets
381 * are treated specially. Bare parameters are delimited
386 len = strcspn (p, "\"");
387 if (p[len] == '\0') {
388 fprintf (stderr, "guestfish: unterminated double quote\n");
389 if (!prompt) exit (1);
392 if (p[len+1] && (p[len+1] != ' ' && p[len+1] != '\t')) {
393 fprintf (stderr, "guestfish: command arguments not separated by whitespace\n");
394 if (!prompt) exit (1);
398 pend = p[len+1] ? &p[len+2] : &p[len+1];
399 } else if (*p == '\'') {
401 len = strcspn (p, "'");
402 if (p[len] == '\0') {
403 fprintf (stderr, "guestfish: unterminated single quote\n");
404 if (!prompt) exit (1);
407 if (p[len+1] && (p[len+1] != ' ' && p[len+1] != '\t')) {
408 fprintf (stderr, "guestfish: command arguments not separated by whitespace\n");
409 if (!prompt) exit (1);
413 pend = p[len+1] ? &p[len+2] : &p[len+1];
415 } else if (*p == '[') {
419 while (*pend && c != 0) {
420 if (*pend == '[') c++;
421 else if (*pend == ']') c--;
425 fprintf (stderr, "guestfish: unterminated \"[...]\" sequence\n");
426 if (!prompt) exit (1);
429 if (*pend && (*pend != ' ' && *pend != '\t')) {
430 fprintf (stderr, "guestfish: command arguments not separated by whitespace\n");
431 if (!prompt) exit (1);
436 } else if (*p != ' ' && *p != '\t') {
437 len = strcspn (p, " \t");
444 fprintf (stderr, "guestfish: internal error parsing string at '%s'\n",
453 p += strspn (p, " \t");
456 if (i == sizeof argv / sizeof argv[0]) {
457 fprintf (stderr, "guestfish: too many arguments\n");
458 if (!prompt) exit (1);
465 if (issue_command (cmd, argv) == -1) {
466 if (!prompt) exit (1);
471 if (prompt) printf ("\n");
475 cmdline (char *argv[], int optind, int argc)
480 if (optind >= argc) return;
482 cmd = argv[optind++];
483 if (strcmp (cmd, ":") == 0) {
484 fprintf (stderr, "guestfish: empty command on command line\n");
487 params = &argv[optind];
489 /* Search for end of command list or ":" ... */
490 while (optind < argc && strcmp (argv[optind], ":") != 0)
493 if (optind == argc) {
494 if (issue_command (cmd, params) == -1) exit (1);
497 if (issue_command (cmd, params) == -1) exit (1);
498 cmdline (argv, optind+1, argc);
503 issue_command (const char *cmd, char *argv[])
507 for (argc = 0; argv[argc] != NULL; ++argc)
510 if (strcasecmp (cmd, "help") == 0) {
514 display_command (argv[0]);
517 else if (strcasecmp (cmd, "quit") == 0 ||
518 strcasecmp (cmd, "exit") == 0 ||
519 strcasecmp (cmd, "q") == 0) {
523 else if (strcasecmp (cmd, "alloc") == 0 ||
524 strcasecmp (cmd, "allocate") == 0)
525 return do_alloc (cmd, argc, argv);
526 else if (strcasecmp (cmd, "echo") == 0)
527 return do_echo (cmd, argc, argv);
528 else if (strcasecmp (cmd, "edit") == 0 ||
529 strcasecmp (cmd, "vi") == 0 ||
530 strcasecmp (cmd, "emacs") == 0)
531 return do_edit (cmd, argc, argv);
533 return run_action (cmd, argc, argv);
537 list_builtin_commands (void)
539 /* help and quit should appear at the top */
540 printf ("%-20s %s\n",
541 "help", "display a list of commands or help on a command");
542 printf ("%-20s %s\n",
543 "quit", "quit guestfish");
545 printf ("%-20s %s\n",
546 "alloc", "allocate an image");
547 printf ("%-20s %s\n",
548 "echo", "display a line of text");
549 printf ("%-20s %s\n",
550 "edit", "edit a file in the image");
552 /* actions are printed after this (see list_commands) */
556 display_builtin_command (const char *cmd)
558 /* help for actions is auto-generated, see display_command */
560 if (strcasecmp (cmd, "alloc") == 0 ||
561 strcasecmp (cmd, "allocate") == 0)
562 printf ("alloc - allocate an image\n"
563 " alloc <filename> <size>\n"
565 " This creates an empty (zeroed) file of the given size,\n"
566 " and then adds so it can be further examined.\n"
568 " For more advanced image creation, see qemu-img utility.\n"
570 " Size can be specified (where <nn> means a number):\n"
571 " <nn> number of kilobytes\n"
572 " eg: 1440 standard 3.5\" floppy\n"
573 " <nn>K or <nn>KB number of kilobytes\n"
574 " <nn>M or <nn>MB number of megabytes\n"
575 " <nn>G or <nn>GB number of gigabytes\n"
576 " <nn>sects number of 512 byte sectors\n");
577 else if (strcasecmp (cmd, "echo") == 0)
578 printf ("echo - display a line of text\n"
579 " echo [<params> ...]\n"
581 " This echos the parameters to the terminal.\n");
582 else if (strcasecmp (cmd, "edit") == 0 ||
583 strcasecmp (cmd, "vi") == 0 ||
584 strcasecmp (cmd, "emacs") == 0)
585 printf ("edit - edit a file in the image\n"
588 " This is used to edit a file.\n"
590 " It is the equivalent of (and is implemented by)\n"
591 " running \"cat\", editing locally, and then \"write-file\".\n"
593 " Normally it uses $EDITOR, but if you use the aliases\n"
594 " \"vi\" or \"emacs\" you will get those editors.\n"
596 " NOTE: This will not work reliably for large files\n"
597 " (> 2 MB) or binary files containing \\0 bytes.\n");
598 else if (strcasecmp (cmd, "help") == 0)
599 printf ("help - display a list of commands or help on a command\n"
602 else if (strcasecmp (cmd, "quit") == 0 ||
603 strcasecmp (cmd, "exit") == 0 ||
604 strcasecmp (cmd, "q") == 0)
605 printf ("quit - quit guestfish\n"
608 fprintf (stderr, "%s: command not known, use -h to list all commands\n",
613 free_strings (char **argv)
617 for (argc = 0; argv[argc] != NULL; ++argc)
623 print_strings (char * const * const argv)
627 for (argc = 0; argv[argc] != NULL; ++argc)
628 printf ("%s\n", argv[argc]);
632 print_table (char * const * const argv)
636 for (i = 0; argv[i] != NULL; i += 2)
637 printf ("%s: %s\n", argv[i], argv[i+1]);
641 is_true (const char *str)
644 strcasecmp (str, "0") != 0 &&
645 strcasecmp (str, "f") != 0 &&
646 strcasecmp (str, "false") != 0 &&
647 strcasecmp (str, "n") != 0 &&
648 strcasecmp (str, "no") != 0;
651 /* XXX We could improve list parsing. */
653 parse_string_list (const char *str)
656 const char *p, *pend;
660 for (i = 0; str[i]; ++i)
661 if (str[i] == ' ') argc++;
663 argv = malloc (sizeof (char *) * (argc+1));
664 if (argv == NULL) { perror ("malloc"); exit (1); }
669 pend = strchrnul (p, ' ');
670 argv[i] = strndup (p, pend-p);
672 p = *pend == ' ' ? pend+1 : pend;
679 #ifdef HAVE_LIBREADLINE
680 static char histfile[1024];
681 static int nr_history_lines = 0;
685 initialize_readline (void)
687 #ifdef HAVE_LIBREADLINE
690 home = getenv ("HOME");
692 snprintf (histfile, sizeof histfile, "%s/.guestfish", home);
694 (void) read_history (histfile);
697 rl_readline_name = "guestfish";
698 rl_attempted_completion_function = do_completion;
703 cleanup_readline (void)
705 #ifdef HAVE_LIBREADLINE
708 if (histfile[0] != '\0') {
709 fd = open (histfile, O_WRONLY|O_CREAT, 0644);
716 (void) append_history (nr_history_lines, histfile);
722 add_history_line (const char *line)
724 #ifdef HAVE_LIBREADLINE