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
42 static void mount_mps (struct mp *mp);
43 static void interactive (void);
44 static void shell_script (void);
45 static void script (int prompt);
46 static void cmdline (char *argv[], int optind, int argc);
47 static int issue_command (const char *cmd, char *argv[]);
48 static int parse_size (const char *str, off_t *size_rtn);
50 /* Currently open libguestfs handle. */
58 launch (guestfs_h *_g)
63 if (guestfs_launch (g) == -1)
65 if (guestfs_wait_ready (g) == -1)
76 "guestfish: guest filesystem shell\n"
77 "guestfish lets you edit virtual machine filesystems\n"
78 "Copyright (C) 2009 Red Hat Inc.\n"
80 " guestfish [--options] cmd [: cmd : cmd ...]\n"
81 "or for interactive use:\n"
83 "or from a shell script:\n"
89 " -h|--cmd-help List available commands\n"
90 " -h|--cmd-help cmd Display detailed help on 'cmd'\n"
91 " -a|--add image Add image\n"
92 " -m|--mount dev[:mnt] Mount dev on mnt (if omitted, /)\n"
93 " -n|--no-sync Don't autosync\n"
94 /*" --ro|-r All mounts are read-only\n"*/
95 " -v|--verbose Verbose messages\n"
96 "For more information, see the manpage guestfish(1).\n");
100 main (int argc, char *argv[])
102 static const char *options = "a:h::m:v?";
103 static struct option long_options[] = {
104 { "add", 1, 0, 'a' },
105 { "cmd-help", 2, 0, 'h' },
106 { "help", 0, 0, '?' },
107 { "mount", 1, 0, 'm' },
108 { "no-sync", 0, 0, 'n' },
109 { "verbose", 0, 0, 'v' },
112 struct mp *mps = NULL;
117 /* guestfs_create is meant to be a lightweight operation, so
118 * it's OK to do it early here.
120 g = guestfs_create ();
122 fprintf (stderr, "guestfs_create: failed to create handle\n");
126 guestfs_set_autosync (g, 1);
128 /* If developing, add . to the path. Note that libtools interferes
129 * with this because uninstalled guestfish is a shell script that runs
130 * the real program with an absolute path. Detect that too.
133 (argv[0][0] != '/' || strstr (argv[0], "/.libs/lt-") != NULL))
134 guestfs_set_path (g, ".:" GUESTFS_DEFAULT_PATH);
137 c = getopt_long (argc, argv, options, long_options, NULL);
142 if (access (optarg, R_OK) != 0) {
146 if (guestfs_add_drive (g, optarg) == -1)
152 display_command (optarg);
153 else if (argv[optind] && argv[optind][0] != '-')
154 display_command (argv[optind++]);
160 mp = malloc (sizeof (struct mp));
165 p = strchr (optarg, ':');
168 mp->mountpoint = p+1;
170 mp->mountpoint = "/";
177 guestfs_set_autosync (g, 0);
182 guestfs_set_verbose (g, verbose);
190 fprintf (stderr, "guestfish: unexpected command line option 0x%x\n", c);
195 /* If we've got mountpoints, we must launch the guest and mount them. */
197 if (launch (g) == -1) exit (1);
201 /* Interactive, shell script, or command(s) on the command line? */
202 if (optind >= argc) {
209 cmdline (argv, optind, argc);
215 pod2text (const char *heading, const char *str)
219 fp = popen ("pod2text", "w");
221 /* pod2text failed, maybe not found, so let's just print the
222 * source instead, since that's better than doing nothing.
224 printf ("%s\n\n%s\n", heading, str);
227 fputs ("=head1 ", fp);
234 /* List is built in reverse order, so mount them in reverse order. */
236 mount_mps (struct mp *mp)
239 mount_mps (mp->next);
240 if (guestfs_mount (g, mp->device, mp->mountpoint) == -1)
267 "Welcome to guestfish, the libguestfs filesystem interactive shell for\n"
268 "editing virtual machine filesystems.\n"
270 "Type: 'help' for help with commands\n"
271 " 'quit' to quit the shell\n"
275 if (prompt) printf ("><fs> ");
276 if (fgets (buf, sizeof buf, stdin) == NULL) {
282 if (len > 0 && buf[len-1] == '\n') buf[len-1] = '\0';
284 /* Split the buffer up at whitespace. */
285 cmd = strtok (buf, " \t");
290 while (i < sizeof argv / sizeof argv[0] &&
291 (argv[i] = strtok (NULL, " \t")) != NULL)
293 if (i == sizeof argv / sizeof argv[0]) {
294 fprintf (stderr, "guestfish: too many arguments in command\n");
298 if (issue_command (cmd, argv) == -1) {
299 if (!prompt) exit (1);
302 if (prompt) printf ("\n");
306 cmdline (char *argv[], int optind, int argc)
311 if (optind >= argc) return;
313 cmd = argv[optind++];
314 if (strcmp (cmd, ":") == 0) {
315 fprintf (stderr, "guestfish: empty command on command line\n");
318 params = &argv[optind];
320 /* Search for end of command list or ":" ... */
321 while (optind < argc && strcmp (argv[optind], ":") != 0)
324 if (optind == argc) {
325 if (issue_command (cmd, params) == -1) exit (1);
328 if (issue_command (cmd, params) == -1) exit (1);
329 cmdline (argv, optind+1, argc);
334 issue_command (const char *cmd, char *argv[])
338 for (argc = 0; argv[argc] != NULL; ++argc)
341 if (strcasecmp (cmd, "help") == 0) {
345 display_command (argv[0]);
348 else if (strcasecmp (cmd, "quit") == 0 ||
349 strcasecmp (cmd, "exit") == 0 ||
350 strcasecmp (cmd, "q") == 0) {
354 else if (strcasecmp (cmd, "alloc") == 0 ||
355 strcasecmp (cmd, "allocate") == 0) {
357 fprintf (stderr, "use 'alloc file size' to create an image\n");
364 if (parse_size (argv[1], &size) == -1)
368 fprintf (stderr, "can't allocate or add disks after launching\n");
372 fd = open (argv[0], O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_TRUNC, 0666);
378 if (posix_fallocate (fd, 0, size) == -1) {
379 perror ("fallocate");
385 if (close (fd) == -1) {
391 if (guestfs_add_drive (g, argv[0]) == -1) {
400 return run_action (cmd, argc, argv);
404 list_builtin_commands (void)
406 /* help and quit should appear at the top */
407 printf ("%-20s %s\n",
408 "help", "display a list of commands or help on a command");
409 printf ("%-20s %s\n",
410 "quit", "quit guestfish");
412 printf ("%-20s %s\n",
413 "alloc", "allocate an image");
415 /* actions are printed after this (see list_commands) */
419 display_builtin_command (const char *cmd)
421 /* help for actions is auto-generated, see display_command */
423 if (strcasecmp (cmd, "alloc") == 0)
424 printf ("alloc - allocate an image\n"
425 " alloc <filename> <size>\n"
427 " This creates an empty (zeroed) file of the given size,\n"
428 " and then adds so it can be further examined.\n"
430 " For more advanced image creation, see qemu-img utility.\n"
432 " Size can be specified (where <nn> means a number):\n"
433 " <nn> number of kilobytes\n"
434 " eg: 1440 standard 3.5\" floppy\n"
435 " <nn>K or <nn>KB number of kilobytes\n"
436 " <nn>M or <nn>MB number of megabytes\n"
437 " <nn>G or <nn>GB number of gigabytes\n"
438 " <nn>sects number of 512 byte sectors\n");
439 else if (strcasecmp (cmd, "help") == 0)
440 printf ("help - display a list of commands or help on a command\n"
443 else if (strcasecmp (cmd, "quit") == 0)
444 printf ("quit - quit guestfish\n"
447 fprintf (stderr, "%s: command not known, use -h to list all commands\n",
451 /* Parse size parameter of alloc command. */
453 parse_size (const char *str, off_t *size_rtn)
458 /* Note that the parsing here is looser than what is specified in the
459 * help, but we may tighten it up in future so beware.
461 if (sscanf (str, "%"SCNu64"%c", &size, &type) == 2) {
463 case 'k': case 'K': size *= 1024; break;
464 case 'm': case 'M': size *= 1024 * 1024; break;
465 case 'g': case 'G': size *= 1024 * 1024 * 1024; break;
466 case 's': size *= 512; break;
468 fprintf (stderr, "could not parse size specification '%s'\n", str);
472 else if (sscanf (str, "%"SCNu64, &size) == 1)
475 fprintf (stderr, "could not parse size specification '%s'\n", str);
479 /* XXX 32 bit file offsets, if anyone uses them? GCC should give
480 * a warning here anyhow.
488 free_strings (char **argv)
492 for (argc = 0; argv[argc] != NULL; ++argc)
498 print_strings (char * const * const argv)
502 for (argc = 0; argv[argc] != NULL; ++argc)
503 printf ("%s\n", argv[argc]);
507 is_true (const char *str)
510 strcasecmp (str, "0") != 0 &&
511 strcasecmp (str, "f") != 0 &&
512 strcasecmp (str, "false") != 0 &&
513 strcasecmp (str, "n") != 0 &&
514 strcasecmp (str, "no") != 0;
517 /* This is quite inadequate for real use. For example, there is no way
518 * to specify an empty list. We need to use a real parser to allow
519 * quoting, empty lists, etc.
522 parse_string_list (const char *str)
525 const char *p, *pend;
529 for (i = 0; str[i]; ++i)
530 if (str[i] == ':') argc++;
532 argv = malloc (sizeof (char *) * (argc+1));
533 if (argv == NULL) { perror ("malloc"); exit (1); }
538 pend = strchrnul (p, ':');
539 argv[i] = strndup (p, pend-p);
541 p = *pend == ':' ? pend+1 : pend;