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.
39 static void mount_mps (struct mp *mp);
40 static void interactive (void);
41 static void shell_script (void);
42 static void script (int prompt);
43 static void cmdline (char *argv[], int optind, int argc);
44 static int issue_command (const char *cmd, char *argv[]);
45 static int parse_size (const char *str, off_t *size_rtn);
47 /* Currently open libguestfs handle. */
58 if (guestfs_launch (g) == -1)
60 if (guestfs_wait_ready (g) == -1)
71 "guestfish: guest filesystem shell\n"
72 "guestfish lets you edit virtual machine filesystems\n"
73 "Copyright (C) 2009 Red Hat Inc.\n"
75 " guestfish [--options] cmd [: cmd : cmd ...]\n"
76 "or for interactive use:\n"
78 "or from a shell script:\n"
84 " -h|--cmd-help List available commands\n"
85 " -h|--cmd-help cmd Display detailed help on 'cmd'\n"
86 " -a|--add image Add image\n"
87 " -m|--mount dev[:mnt] Mount dev on mnt (if omitted, /)\n"
88 " -n|--no-sync Don't autosync\n"
89 /*" --ro|-r All mounts are read-only\n"*/
90 " -v|--verbose Verbose messages\n"
91 "For more information, see the manpage guestfish(1).\n");
95 main (int argc, char *argv[])
97 static const char *options = "a:h::m:v?";
98 static struct option long_options[] = {
100 { "cmd-help", 2, 0, 'h' },
101 { "help", 0, 0, '?' },
102 { "mount", 1, 0, 'm' },
103 { "no-sync", 0, 0, 'n' },
104 { "verbose", 0, 0, 'v' },
107 struct mp *mps = NULL;
112 /* guestfs_create is meant to be a lightweight operation, so
113 * it's OK to do it early here.
115 g = guestfs_create ();
117 fprintf (stderr, "guestfs_create: failed to create handle\n");
121 guestfs_set_autosync (g, 1);
123 /* If developing, add . to the path. Note that libtools interferes
124 * with this because uninstalled guestfish is a shell script that runs
125 * the real program with an absolute path. Detect that too.
128 (argv[0][0] != '/' || strstr (argv[0], "/.libs/lt-") != NULL))
129 guestfs_set_path (g, ".:" GUESTFS_DEFAULT_PATH);
132 c = getopt_long (argc, argv, options, long_options, NULL);
137 if (access (optarg, R_OK) != 0) {
141 if (guestfs_add_drive (g, optarg) == -1)
147 display_command (optarg);
148 else if (argv[optind] && argv[optind][0] != '-')
149 display_command (argv[optind++]);
155 mp = malloc (sizeof (struct mp));
160 p = strchr (optarg, ':');
163 mp->mountpoint = p+1;
165 mp->mountpoint = "/";
172 guestfs_set_autosync (g, 0);
177 guestfs_set_verbose (g, verbose);
185 fprintf (stderr, "guestfish: unexpected command line option 0x%x\n", c);
190 /* If we've got mountpoints, we must launch the guest and mount them. */
192 if (launch () == -1) exit (1);
196 /* Interactive, shell script, or command(s) on the command line? */
197 if (optind >= argc) {
204 cmdline (argv, optind, argc);
210 pod2text (const char *heading, const char *str)
214 fp = popen ("pod2text", "w");
216 /* pod2text failed, maybe not found, so let's just print the
217 * source instead, since that's better than doing nothing.
219 printf ("%s\n\n%s\n", heading, str);
222 fputs ("=head1 ", fp);
229 /* List is built in reverse order, so mount them in reverse order. */
231 mount_mps (struct mp *mp)
234 mount_mps (mp->next);
235 if (guestfs_mount (g, mp->device, mp->mountpoint) == -1)
262 "Welcome to guestfish, the libguestfs filesystem interactive shell for\n"
263 "editing virtual machine filesystems.\n"
265 "Type: 'help' for help with commands\n"
266 " 'quit' to quit the shell\n"
270 if (prompt) printf ("><fs> ");
271 if (fgets (buf, sizeof buf, stdin) == NULL) {
277 if (len > 0 && buf[len-1] == '\n') buf[len-1] = '\0';
279 /* Split the buffer up at whitespace. */
280 cmd = strtok (buf, " \t");
285 while (i < sizeof argv / sizeof argv[0] &&
286 (argv[i] = strtok (NULL, " \t")) != NULL)
288 if (i == sizeof argv / sizeof argv[0]) {
289 fprintf (stderr, "guestfish: too many arguments in command\n");
293 if (issue_command (cmd, argv) == -1) {
294 if (!prompt) exit (1);
297 if (prompt) printf ("\n");
301 cmdline (char *argv[], int optind, int argc)
306 if (optind >= argc) return;
308 cmd = argv[optind++];
309 if (strcmp (cmd, ":") == 0) {
310 fprintf (stderr, "guestfish: empty command on command line\n");
313 params = &argv[optind];
315 /* Search for end of command list or ":" ... */
316 while (optind < argc && strcmp (argv[optind], ":") != 0)
319 if (optind == argc) {
320 if (issue_command (cmd, params) == -1) exit (1);
323 if (issue_command (cmd, params) == -1) exit (1);
324 cmdline (argv, optind+1, argc);
329 issue_command (const char *cmd, char *argv[])
333 for (argc = 0; argv[argc] != NULL; ++argc)
336 if (strcasecmp (cmd, "help") == 0) {
340 display_command (argv[0]);
343 else if (strcasecmp (cmd, "quit") == 0 ||
344 strcasecmp (cmd, "exit") == 0 ||
345 strcasecmp (cmd, "q") == 0) {
349 else if (strcasecmp (cmd, "add") == 0 ||
350 strcasecmp (cmd, "drive") == 0 ||
351 strcasecmp (cmd, "add-drive") == 0 ||
352 strcasecmp (cmd, "add_drive") == 0) {
354 fprintf (stderr, "use 'add image' to add a guest image\n");
358 return guestfs_add_drive (g, argv[0]);
360 else if (strcasecmp (cmd, "cdrom") == 0 ||
361 strcasecmp (cmd, "add-cdrom") == 0 ||
362 strcasecmp (cmd, "add_cdrom") == 0) {
364 fprintf (stderr, "use 'cdrom image' to add a CD-ROM image\n");
368 return guestfs_add_cdrom (g, argv[0]);
370 else if (strcasecmp (cmd, "alloc") == 0 ||
371 strcasecmp (cmd, "allocate") == 0) {
373 fprintf (stderr, "use 'alloc file size' to create an image\n");
380 if (parse_size (argv[1], &size) == -1)
384 fprintf (stderr, "can't allocate or add disks after launching\n");
388 fd = open (argv[0], O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_TRUNC, 0666);
394 if (posix_fallocate (fd, 0, size) == -1) {
395 perror ("fallocate");
401 if (close (fd) == -1) {
407 if (guestfs_add_drive (g, argv[0]) == -1) {
415 else if (strcasecmp (cmd, "launch") == 0 ||
416 strcasecmp (cmd, "run") == 0) {
418 fprintf (stderr, "'launch' command takes no parameters\n");
425 return run_action (cmd, argc, argv);
429 list_builtin_commands (void)
431 /* help and quit should appear at the top */
432 printf ("%-20s %s\n",
433 "help", "display a list of commands or help on a command");
434 printf ("%-20s %s\n",
435 "quit", "quit guestfish");
437 /* then the non-actions, in alphabetical order */
438 printf ("%-20s %s\n",
439 "add", "add a guest image to be examined or modified");
440 printf ("%-20s %s\n",
441 "alloc", "allocate an image");
442 printf ("%-20s %s\n",
443 "cdrom", "add a CD-ROM image to be examined");
444 printf ("%-20s %s\n",
445 "launch", "launch the subprocess");
447 /* actions are printed after this (see list_commands) */
451 display_builtin_command (const char *cmd)
453 /* help for actions is auto-generated, see display_command */
455 if (strcasecmp (cmd, "add") == 0)
456 printf ("add - add a guest image to be examined or modified\n"
458 else if (strcasecmp (cmd, "alloc") == 0)
459 printf ("alloc - allocate an image\n"
460 " alloc <filename> <size>\n"
462 " This creates an empty (zeroed) file of the given size,\n"
463 " and then adds so it can be further examined.\n"
465 " For more advanced image creation, see qemu-img utility.\n"
467 " Size can be specified (where <nn> means a number):\n"
468 " <nn> number of kilobytes\n"
469 " eg: 1440 standard 3.5\" floppy\n"
470 " <nn>K or <nn>KB number of kilobytes\n"
471 " <nn>M or <nn>MB number of megabytes\n"
472 " <nn>G or <nn>GB number of gigabytes\n"
473 " <nn>sects number of 512 byte sectors\n");
474 else if (strcasecmp (cmd, "cdrom") == 0)
475 printf ("cdrom - add a CD-ROM image to be examined\n"
476 " cdrom <iso-file>\n");
477 else if (strcasecmp (cmd, "help") == 0)
478 printf ("help - display a list of commands or help on a command\n"
481 else if (strcasecmp (cmd, "quit") == 0)
482 printf ("quit - quit guestfish\n"
484 else if (strcasecmp (cmd, "launch") == 0)
485 printf ("launch - launch the subprocess\n"
488 fprintf (stderr, "%s: command not known, use -h to list all commands\n",
492 /* Parse size parameter of alloc command. */
494 parse_size (const char *str, off_t *size_rtn)
499 /* Note that the parsing here is looser than what is specified in the
500 * help, but we may tighten it up in future so beware.
502 if (sscanf (str, "%"SCNu64"%c", &size, &type) == 2) {
504 case 'k': case 'K': size *= 1024; break;
505 case 'm': case 'M': size *= 1024 * 1024; break;
506 case 'g': case 'G': size *= 1024 * 1024 * 1024; break;
507 case 's': size *= 512; break;
509 fprintf (stderr, "could not parse size specification '%s'\n", str);
513 else if (sscanf (str, "%"SCNu64, &size) == 1)
516 fprintf (stderr, "could not parse size specification '%s'\n", str);
520 printf ("size = %lu\n", size);
522 /* XXX 32 bit file offsets, if anyone uses them? GCC should give
523 * a warning here anyhow.
531 free_strings (char **argv)
535 for (argc = 0; argv[argc] != NULL; ++argc)
541 print_strings (char **argv)
545 for (argc = 0; argv[argc] != NULL; ++argc)
546 printf ("%s\n", argv[argc]);