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.
37 static void mount_mps (struct mp *mp);
38 static void interactive (void);
39 static void shell_script (void);
40 static void script (int prompt);
41 static void cmdline (char *argv[], int optind, int argc);
42 static int issue_command (const char *cmd, char *argv[]);
44 /* Currently open libguestfs handle. */
55 if (guestfs_launch (g) == -1)
57 if (guestfs_wait_ready (g) == -1)
68 "guestfish: guest filesystem shell\n"
69 "guestfish lets you edit virtual machine filesystems\n"
70 "Copyright (C) 2009 Red Hat Inc.\n"
72 " guestfish [--options] cmd [: cmd : cmd ...]\n"
73 "or for interactive use:\n"
75 "or from a shell script:\n"
81 " -h|--cmd-help List available commands\n"
82 " -h|--cmd-help cmd Display detailed help on 'cmd'\n"
83 " -a image Add image\n"
84 " -m dev[:mnt] Mount dev on mnt (if omitted, /)\n"
85 " -n|--no-sync Don't autosync\n"
86 /*" --ro|-r All mounts are read-only\n"*/
87 " -v|--verbose Verbose messages\n"
88 "For more information, see the manpage guestfish(1).\n");
92 main (int argc, char *argv[])
94 static const char *options = "a:h::m:v?";
95 static struct option long_options[] = {
97 { "cmd-help", 2, 0, 'h' },
98 { "help", 0, 0, '?' },
99 { "mount", 1, 0, 'm' },
100 { "no-sync", 0, 0, 'n' },
101 { "verbose", 0, 0, 'v' },
104 struct mp *mps = NULL;
109 /* guestfs_create is meant to be a lightweight operation, so
110 * it's OK to do it early here.
112 g = guestfs_create ();
114 fprintf (stderr, "guestfs_create: failed to create handle\n");
118 guestfs_set_autosync (g, 1);
121 c = getopt_long (argc, argv, options, long_options, NULL);
126 if (access (optarg, R_OK) != 0) {
130 if (guestfs_add_drive (g, optarg) == -1)
136 display_command (optarg);
137 else if (argv[optind] && argv[optind][0] != '-')
138 display_command (argv[optind++]);
144 mp = malloc (sizeof (struct mp));
149 p = strchr (optarg, ':');
152 mp->mountpoint = p+1;
154 mp->mountpoint = "/";
161 guestfs_set_autosync (g, 0);
166 guestfs_set_verbose (g, verbose);
174 fprintf (stderr, "guestfish: unexpected command line option 0x%x\n", c);
179 /* If we've got mountpoints, we must launch the guest and mount them. */
181 if (launch () == -1) exit (1);
185 /* Interactive, shell script, or command(s) on the command line? */
186 if (optind >= argc) {
193 cmdline (argv, optind, argc);
199 pod2text (const char *heading, const char *str)
203 fp = popen ("pod2text", "w");
205 /* pod2text failed, maybe not found, so let's just print the
206 * source instead, since that's better than doing nothing.
208 printf ("%s\n\n%s\n", heading, str);
211 fputs ("=head1 ", fp);
218 /* List is built in reverse order, so mount them in reverse order. */
220 mount_mps (struct mp *mp)
223 mount_mps (mp->next);
224 if (guestfs_mount (g, mp->device, mp->mountpoint) == -1)
251 "Welcome to guestfish, the libguestfs filesystem interactive shell for\n"
252 "editing virtual machine filesystems.\n"
254 "Type: 'help' for help with commands\n"
255 " 'quit' to quit the shell\n"
259 if (prompt) printf ("><fs> ");
260 if (fgets (buf, sizeof buf, stdin) == NULL) {
266 if (len > 0 && buf[len-1] == '\n') buf[len-1] = '\0';
268 /* Split the buffer up at whitespace. */
269 cmd = strtok (buf, " \t");
274 while (i < sizeof argv / sizeof argv[0] &&
275 (argv[i] = strtok (NULL, " \t")) != NULL)
277 if (i == sizeof argv / sizeof argv[0]) {
278 fprintf (stderr, "guestfish: too many arguments in command\n");
282 (void) issue_command (cmd, argv);
284 if (prompt) printf ("\n");
288 cmdline (char *argv[], int optind, int argc)
293 if (optind >= argc) return;
295 cmd = argv[optind++];
296 if (strcmp (cmd, ":") == 0) {
297 fprintf (stderr, "guestfish: empty command on command line\n");
300 params = &argv[optind];
302 /* Search for end of command list or ":" ... */
303 while (optind < argc && strcmp (argv[optind], ":") != 0)
306 if (optind == argc) {
307 if (issue_command (cmd, params) == -1) exit (1);
310 if (issue_command (cmd, params) == -1) exit (1);
311 cmdline (argv, optind+1, argc);
316 issue_command (const char *cmd, char *argv[])
320 for (argc = 0; argv[argc] != NULL; ++argc)
323 if (strcasecmp (cmd, "help") == 0) {
327 display_command (argv[0]);
330 else if (strcasecmp (cmd, "quit") == 0 ||
331 strcasecmp (cmd, "exit") == 0 ||
332 strcasecmp (cmd, "q") == 0)
334 else if (strcasecmp (cmd, "add") == 0 ||
335 strcasecmp (cmd, "drive") == 0 ||
336 strcasecmp (cmd, "add_drive") == 0) {
338 fprintf (stderr, "use 'add image' to add a guest image\n");
342 return guestfs_add_drive (g, argv[0]);
344 else if (strcasecmp (cmd, "cdrom") == 0) {
346 fprintf (stderr, "use 'cdrom image' to add a CD-ROM image\n");
350 return guestfs_add_cdrom (g, argv[0]);
352 else if (strcasecmp (cmd, "launch") == 0) {
354 fprintf (stderr, "'launch' command takes no parameters\n");
361 return run_action (cmd, argc, argv);
365 list_builtin_commands (void)
367 printf ("%-20s %s\n",
368 "help", "display a list of commands or help on a command");
369 printf ("%-20s %s\n",
370 "quit", "quit guestfish");
371 printf ("%-20s %s\n",
372 "add", "add a guest image to be examined or modified");
373 printf ("%-20s %s\n",
374 "cdrom", "add a CD-ROM image to be examined");
375 printf ("%-20s %s\n",
376 "launch", "launch the subprocess");
380 display_builtin_command (const char *cmd)
382 if (strcasecmp (cmd, "add") == 0)
383 printf ("add - add a guest image to be examined or modified\n"
385 else if (strcasecmp (cmd, "cdrom") == 0)
386 printf ("cdrom - add a CD-ROM image to be examined\n"
387 " cdrom <iso-file>\n");
388 else if (strcasecmp (cmd, "help") == 0)
389 printf ("help - display a list of commands or help on a command\n"
392 else if (strcasecmp (cmd, "quit") == 0)
393 printf ("quit - quit guestfish\n"
395 else if (strcasecmp (cmd, "launch") == 0)
396 printf ("launch - launch the subprocess\n"
399 fprintf (stderr, "%s: command not known, use -h to list all commands\n",