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 */
317 "Welcome to guestfish, the libguestfs filesystem interactive shell for\n"
318 "editing virtual machine filesystems.\n"
320 "Type: 'help' for help with commands\n"
321 " 'quit' to quit the shell\n"
325 buf = rl_gets (prompt);
331 /* Skip any initial whitespace before the command. */
332 while (*buf && isspace (*buf))
335 /* Get the command (cannot be quoted). */
336 len = strcspn (buf, " \t");
338 if (len == 0) continue;
342 if (buf[len] == '\0') {
349 p += strspn (p, " \t");
351 /* Get the parameters. */
352 while (*p && i < sizeof argv / sizeof argv[0]) {
353 /* Parameters which start with quotes or square brackets
354 * are treated specially. Bare parameters are delimited
359 len = strcspn (p, "\"");
360 if (p[len] == '\0') {
361 fprintf (stderr, "guestfish: unterminated double quote\n");
362 if (!prompt) exit (1);
365 if (p[len+1] && (p[len+1] != ' ' && p[len+1] != '\t')) {
366 fprintf (stderr, "guestfish: command arguments not separated by whitespace\n");
367 if (!prompt) exit (1);
372 } else if (*p == '\'') {
374 len = strcspn (p, "'");
375 if (p[len] == '\0') {
376 fprintf (stderr, "guestfish: unterminated single quote\n");
377 if (!prompt) exit (1);
380 if (p[len+1] && (p[len+1] != ' ' && p[len+1] != '\t')) {
381 fprintf (stderr, "guestfish: command arguments not separated by whitespace\n");
382 if (!prompt) exit (1);
388 } else if (*p == '[') {
392 while (*pend && c != 0) {
393 if (*pend == '[') c++;
394 else if (*pend == ']') c--;
398 fprintf (stderr, "guestfish: unterminated \"[...]\" sequence\n");
399 if (!prompt) exit (1);
402 if (*pend && (*pend != ' ' && *pend != '\t')) {
403 fprintf (stderr, "guestfish: command arguments not separated by whitespace\n");
404 if (!prompt) exit (1);
409 } else if (*p != ' ' && *p != '\t') {
410 len = strcspn (p, " \t");
417 fprintf (stderr, "guestfish: internal error parsing string at '%s'\n",
426 p += strspn (p, " \t");
429 if (i == sizeof argv / sizeof argv[0]) {
430 fprintf (stderr, "guestfish: too many arguments\n");
431 if (!prompt) exit (1);
438 if (issue_command (cmd, argv) == -1) {
439 if (!prompt) exit (1);
444 if (prompt) printf ("\n");
448 cmdline (char *argv[], int optind, int argc)
453 if (optind >= argc) return;
455 cmd = argv[optind++];
456 if (strcmp (cmd, ":") == 0) {
457 fprintf (stderr, "guestfish: empty command on command line\n");
460 params = &argv[optind];
462 /* Search for end of command list or ":" ... */
463 while (optind < argc && strcmp (argv[optind], ":") != 0)
466 if (optind == argc) {
467 if (issue_command (cmd, params) == -1) exit (1);
470 if (issue_command (cmd, params) == -1) exit (1);
471 cmdline (argv, optind+1, argc);
476 issue_command (const char *cmd, char *argv[])
480 for (argc = 0; argv[argc] != NULL; ++argc)
483 if (strcasecmp (cmd, "help") == 0) {
487 display_command (argv[0]);
490 else if (strcasecmp (cmd, "quit") == 0 ||
491 strcasecmp (cmd, "exit") == 0 ||
492 strcasecmp (cmd, "q") == 0) {
496 else if (strcasecmp (cmd, "alloc") == 0 ||
497 strcasecmp (cmd, "allocate") == 0) {
499 fprintf (stderr, "use 'alloc file size' to create an image\n");
506 if (parse_size (argv[1], &size) == -1)
510 fprintf (stderr, "can't allocate or add disks after launching\n");
514 fd = open (argv[0], O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_TRUNC, 0666);
520 if (posix_fallocate (fd, 0, size) == -1) {
521 perror ("fallocate");
527 if (close (fd) == -1) {
533 if (guestfs_add_drive (g, argv[0]) == -1) {
542 return run_action (cmd, argc, argv);
546 list_builtin_commands (void)
548 /* help and quit should appear at the top */
549 printf ("%-20s %s\n",
550 "help", "display a list of commands or help on a command");
551 printf ("%-20s %s\n",
552 "quit", "quit guestfish");
554 printf ("%-20s %s\n",
555 "alloc", "allocate an image");
557 /* actions are printed after this (see list_commands) */
561 display_builtin_command (const char *cmd)
563 /* help for actions is auto-generated, see display_command */
565 if (strcasecmp (cmd, "alloc") == 0)
566 printf ("alloc - allocate an image\n"
567 " alloc <filename> <size>\n"
569 " This creates an empty (zeroed) file of the given size,\n"
570 " and then adds so it can be further examined.\n"
572 " For more advanced image creation, see qemu-img utility.\n"
574 " Size can be specified (where <nn> means a number):\n"
575 " <nn> number of kilobytes\n"
576 " eg: 1440 standard 3.5\" floppy\n"
577 " <nn>K or <nn>KB number of kilobytes\n"
578 " <nn>M or <nn>MB number of megabytes\n"
579 " <nn>G or <nn>GB number of gigabytes\n"
580 " <nn>sects number of 512 byte sectors\n");
581 else if (strcasecmp (cmd, "help") == 0)
582 printf ("help - display a list of commands or help on a command\n"
585 else if (strcasecmp (cmd, "quit") == 0)
586 printf ("quit - quit guestfish\n"
589 fprintf (stderr, "%s: command not known, use -h to list all commands\n",
593 /* Parse size parameter of alloc command. */
595 parse_size (const char *str, off_t *size_rtn)
600 /* Note that the parsing here is looser than what is specified in the
601 * help, but we may tighten it up in future so beware.
603 if (sscanf (str, "%"SCNu64"%c", &size, &type) == 2) {
605 case 'k': case 'K': size *= 1024; break;
606 case 'm': case 'M': size *= 1024 * 1024; break;
607 case 'g': case 'G': size *= 1024 * 1024 * 1024; break;
608 case 's': size *= 512; break;
610 fprintf (stderr, "could not parse size specification '%s'\n", str);
614 else if (sscanf (str, "%"SCNu64, &size) == 1)
617 fprintf (stderr, "could not parse size specification '%s'\n", str);
621 /* XXX 32 bit file offsets, if anyone uses them? GCC should give
622 * a warning here anyhow.
630 free_strings (char **argv)
634 for (argc = 0; argv[argc] != NULL; ++argc)
640 print_strings (char * const * const argv)
644 for (argc = 0; argv[argc] != NULL; ++argc)
645 printf ("%s\n", argv[argc]);
649 is_true (const char *str)
652 strcasecmp (str, "0") != 0 &&
653 strcasecmp (str, "f") != 0 &&
654 strcasecmp (str, "false") != 0 &&
655 strcasecmp (str, "n") != 0 &&
656 strcasecmp (str, "no") != 0;
659 /* XXX We could improve list parsing. */
661 parse_string_list (const char *str)
664 const char *p, *pend;
668 for (i = 0; str[i]; ++i)
669 if (str[i] == ' ') argc++;
671 argv = malloc (sizeof (char *) * (argc+1));
672 if (argv == NULL) { perror ("malloc"); exit (1); }
677 pend = strchrnul (p, ' ');
678 argv[i] = strndup (p, pend-p);
680 p = *pend == ' ' ? pend+1 : pend;
687 #ifdef HAVE_LIBREADLINE
688 static char histfile[1024];
689 static int nr_history_lines = 0;
693 initialize_readline (void)
695 #ifdef HAVE_LIBREADLINE
698 home = getenv ("HOME");
700 snprintf (histfile, sizeof histfile, "%s/.guestfish", home);
702 (void) read_history (histfile);
705 rl_readline_name = "guestfish";
706 rl_attempted_completion_function = do_completion;
711 cleanup_readline (void)
713 #ifdef HAVE_LIBREADLINE
716 if (histfile[0] != '\0') {
717 fd = open (histfile, O_WRONLY|O_CREAT, 0644);
724 (void) append_history (nr_history_lines, histfile);
730 add_history_line (const char *line)
732 #ifdef HAVE_LIBREADLINE