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.
40 static void mount_mps (struct mp *mp);
41 static void interactive (void);
42 static void shell_script (void);
43 static void script (int prompt);
44 static void cmdline (char *argv[], int optind, int argc);
45 static int issue_command (const char *cmd, char *argv[]);
46 static int parse_size (const char *str, off_t *size_rtn);
48 /* Currently open libguestfs handle. */
56 launch (guestfs_h *_g)
61 if (guestfs_launch (g) == -1)
63 if (guestfs_wait_ready (g) == -1)
74 "guestfish: guest filesystem shell\n"
75 "guestfish lets you edit virtual machine filesystems\n"
76 "Copyright (C) 2009 Red Hat Inc.\n"
78 " guestfish [--options] cmd [: cmd : cmd ...]\n"
79 "or for interactive use:\n"
81 "or from a shell script:\n"
87 " -h|--cmd-help List available commands\n"
88 " -h|--cmd-help cmd Display detailed help on 'cmd'\n"
89 " -a|--add image Add image\n"
90 " -m|--mount dev[:mnt] Mount dev on mnt (if omitted, /)\n"
91 " -n|--no-sync Don't autosync\n"
92 /*" --ro|-r All mounts are read-only\n"*/
93 " -v|--verbose Verbose messages\n"
94 "For more information, see the manpage guestfish(1).\n");
98 main (int argc, char *argv[])
100 static const char *options = "a:h::m:v?";
101 static struct option long_options[] = {
102 { "add", 1, 0, 'a' },
103 { "cmd-help", 2, 0, 'h' },
104 { "help", 0, 0, '?' },
105 { "mount", 1, 0, 'm' },
106 { "no-sync", 0, 0, 'n' },
107 { "verbose", 0, 0, 'v' },
110 struct mp *mps = NULL;
115 /* guestfs_create is meant to be a lightweight operation, so
116 * it's OK to do it early here.
118 g = guestfs_create ();
120 fprintf (stderr, "guestfs_create: failed to create handle\n");
124 guestfs_set_autosync (g, 1);
126 /* If developing, add . to the path. Note that libtools interferes
127 * with this because uninstalled guestfish is a shell script that runs
128 * the real program with an absolute path. Detect that too.
131 (argv[0][0] != '/' || strstr (argv[0], "/.libs/lt-") != NULL))
132 guestfs_set_path (g, ".:" GUESTFS_DEFAULT_PATH);
135 c = getopt_long (argc, argv, options, long_options, NULL);
140 if (access (optarg, R_OK) != 0) {
144 if (guestfs_add_drive (g, optarg) == -1)
150 display_command (optarg);
151 else if (argv[optind] && argv[optind][0] != '-')
152 display_command (argv[optind++]);
158 mp = malloc (sizeof (struct mp));
163 p = strchr (optarg, ':');
166 mp->mountpoint = p+1;
168 mp->mountpoint = "/";
175 guestfs_set_autosync (g, 0);
180 guestfs_set_verbose (g, verbose);
188 fprintf (stderr, "guestfish: unexpected command line option 0x%x\n", c);
193 /* If we've got mountpoints, we must launch the guest and mount them. */
195 if (launch (g) == -1) exit (1);
199 /* Interactive, shell script, or command(s) on the command line? */
200 if (optind >= argc) {
207 cmdline (argv, optind, argc);
213 pod2text (const char *heading, const char *str)
217 fp = popen ("pod2text", "w");
219 /* pod2text failed, maybe not found, so let's just print the
220 * source instead, since that's better than doing nothing.
222 printf ("%s\n\n%s\n", heading, str);
225 fputs ("=head1 ", fp);
232 /* List is built in reverse order, so mount them in reverse order. */
234 mount_mps (struct mp *mp)
237 mount_mps (mp->next);
238 if (guestfs_mount (g, mp->device, mp->mountpoint) == -1)
265 "Welcome to guestfish, the libguestfs filesystem interactive shell for\n"
266 "editing virtual machine filesystems.\n"
268 "Type: 'help' for help with commands\n"
269 " 'quit' to quit the shell\n"
273 if (prompt) printf ("><fs> ");
274 if (fgets (buf, sizeof buf, stdin) == NULL) {
280 if (len > 0 && buf[len-1] == '\n') buf[len-1] = '\0';
282 /* Split the buffer up at whitespace. */
283 cmd = strtok (buf, " \t");
288 while (i < sizeof argv / sizeof argv[0] &&
289 (argv[i] = strtok (NULL, " \t")) != NULL)
291 if (i == sizeof argv / sizeof argv[0]) {
292 fprintf (stderr, "guestfish: too many arguments in command\n");
296 if (issue_command (cmd, argv) == -1) {
297 if (!prompt) exit (1);
300 if (prompt) printf ("\n");
304 cmdline (char *argv[], int optind, int argc)
309 if (optind >= argc) return;
311 cmd = argv[optind++];
312 if (strcmp (cmd, ":") == 0) {
313 fprintf (stderr, "guestfish: empty command on command line\n");
316 params = &argv[optind];
318 /* Search for end of command list or ":" ... */
319 while (optind < argc && strcmp (argv[optind], ":") != 0)
322 if (optind == argc) {
323 if (issue_command (cmd, params) == -1) exit (1);
326 if (issue_command (cmd, params) == -1) exit (1);
327 cmdline (argv, optind+1, argc);
332 issue_command (const char *cmd, char *argv[])
336 for (argc = 0; argv[argc] != NULL; ++argc)
339 if (strcasecmp (cmd, "help") == 0) {
343 display_command (argv[0]);
346 else if (strcasecmp (cmd, "quit") == 0 ||
347 strcasecmp (cmd, "exit") == 0 ||
348 strcasecmp (cmd, "q") == 0) {
352 else if (strcasecmp (cmd, "alloc") == 0 ||
353 strcasecmp (cmd, "allocate") == 0) {
355 fprintf (stderr, "use 'alloc file size' to create an image\n");
362 if (parse_size (argv[1], &size) == -1)
366 fprintf (stderr, "can't allocate or add disks after launching\n");
370 fd = open (argv[0], O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_TRUNC, 0666);
376 if (posix_fallocate (fd, 0, size) == -1) {
377 perror ("fallocate");
383 if (close (fd) == -1) {
389 if (guestfs_add_drive (g, argv[0]) == -1) {
398 return run_action (cmd, argc, argv);
402 list_builtin_commands (void)
404 /* help and quit should appear at the top */
405 printf ("%-20s %s\n",
406 "help", "display a list of commands or help on a command");
407 printf ("%-20s %s\n",
408 "quit", "quit guestfish");
410 printf ("%-20s %s\n",
411 "alloc", "allocate an image");
413 /* actions are printed after this (see list_commands) */
417 display_builtin_command (const char *cmd)
419 /* help for actions is auto-generated, see display_command */
421 if (strcasecmp (cmd, "alloc") == 0)
422 printf ("alloc - allocate an image\n"
423 " alloc <filename> <size>\n"
425 " This creates an empty (zeroed) file of the given size,\n"
426 " and then adds so it can be further examined.\n"
428 " For more advanced image creation, see qemu-img utility.\n"
430 " Size can be specified (where <nn> means a number):\n"
431 " <nn> number of kilobytes\n"
432 " eg: 1440 standard 3.5\" floppy\n"
433 " <nn>K or <nn>KB number of kilobytes\n"
434 " <nn>M or <nn>MB number of megabytes\n"
435 " <nn>G or <nn>GB number of gigabytes\n"
436 " <nn>sects number of 512 byte sectors\n");
437 else if (strcasecmp (cmd, "help") == 0)
438 printf ("help - display a list of commands or help on a command\n"
441 else if (strcasecmp (cmd, "quit") == 0)
442 printf ("quit - quit guestfish\n"
445 fprintf (stderr, "%s: command not known, use -h to list all commands\n",
449 /* Parse size parameter of alloc command. */
451 parse_size (const char *str, off_t *size_rtn)
456 /* Note that the parsing here is looser than what is specified in the
457 * help, but we may tighten it up in future so beware.
459 if (sscanf (str, "%"SCNu64"%c", &size, &type) == 2) {
461 case 'k': case 'K': size *= 1024; break;
462 case 'm': case 'M': size *= 1024 * 1024; break;
463 case 'g': case 'G': size *= 1024 * 1024 * 1024; break;
464 case 's': size *= 512; break;
466 fprintf (stderr, "could not parse size specification '%s'\n", str);
470 else if (sscanf (str, "%"SCNu64, &size) == 1)
473 fprintf (stderr, "could not parse size specification '%s'\n", str);
477 printf ("size = %lu\n", size);
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 **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;