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 void issue_command (const char *cmd, char *argv[]);
44 /* Currently open libguestfs handle. */
54 if (guestfs_launch (g) == -1)
56 if (guestfs_wait_ready (g) == -1)
66 "guestfish: guest filesystem shell\n"
67 "guestfish lets you edit virtual machine filesystems\n"
68 "Copyright (C) 2009 Red Hat Inc.\n"
70 " guestfish [--options] cmd [: cmd ...]\n"
71 "or for interactive use:\n"
73 "or from a shell script:\n"
79 " -h|--cmd-help List available commands\n"
80 " -h|--cmd-help cmd Display detailed help on 'cmd'\n"
81 " -a image Add image\n"
82 " -m dev[:mnt] Mount dev on mnt (if omitted, /)\n"
83 /*" --ro|-r All mounts are read-only\n"*/
84 " -v|--verbose Verbose messages\n"
85 "For more information, see the manpage guestfish(1).\n");
89 main (int argc, char *argv[])
91 static const char *options = "a:h::m:v?";
92 static struct option long_options[] = {
94 { "cmd-help", 2, 0, 'h' },
95 { "help", 0, 0, '?' },
96 { "mount", 1, 0, 'm' },
97 { "verbose", 0, 0, 'v' },
100 struct mp *mps = NULL;
105 /* guestfs_create is meant to be a lightweight operation, so
106 * it's OK to do it early here.
108 g = guestfs_create ();
110 fprintf (stderr, "guestfs_create: failed to create handle\n");
115 c = getopt_long (argc, argv, options, long_options, NULL);
120 if (access (optarg, R_OK) != 0) {
124 if (guestfs_add_drive (g, optarg) == -1)
130 display_command (optarg);
131 else if (argv[optind] && argv[optind][0] != '-')
132 display_command (argv[optind++]);
138 mp = malloc (sizeof (struct mp));
143 p = strchr (optarg, ':');
146 mp->mountpoint = p+1;
148 mp->mountpoint = "/";
159 fprintf (stderr, "guestfish: unexpected command line option 0x%x\n", c);
164 /* If we've got mountpoints, we must launch the guest and mount them. */
170 /* Interactive, shell script, or command(s) on the command line? */
171 if (optind >= argc) {
178 cmdline (argv, optind, argc);
184 pod2text (const char *heading, const char *str)
188 fp = popen ("pod2text", "w");
190 /* pod2text failed, maybe not found, so let's just print the
191 * source instead, since that's better than doing nothing.
193 printf ("%s\n\n%s\n", heading, str);
196 fputs ("=head1 ", fp);
203 /* List is built in reverse order, so mount them in reverse order. */
205 mount_mps (struct mp *mp)
208 mount_mps (mp->next);
209 if (guestfs_mount (g, mp->device, mp->mountpoint) == -1)
235 printf ("Welcome to guestfish, the libguestfs filesystem interactive shell for\n"
236 "editing virtual machine filesystems.\n"
238 "Type: 'help' for help with commands\n"
239 " 'quit' to quit the shell\n"
243 if (prompt) printf ("><fs> ");
244 if (fgets (buf, sizeof buf, stdin) == NULL) {
250 if (len > 0 && buf[len-1] == '\n') buf[len-1] = '\0';
252 /* Split the buffer up at whitespace. */
253 cmd = strtok (buf, " \t");
258 while (i < sizeof argv / sizeof argv[0] &&
259 (argv[i] = strtok (NULL, " \t")) != NULL)
261 if (i == sizeof argv / sizeof argv[0]) {
262 fprintf (stderr, "guestfish: too many arguments in command\n");
266 issue_command (cmd, argv);
268 if (prompt) printf ("\n");
272 cmdline (char *argv[], int optind, int argc)
277 if (optind >= argc) return;
279 cmd = argv[optind++];
280 if (strcmp (cmd, ":") == 0) {
281 fprintf (stderr, "guestfish: empty command on command line\n");
284 params = &argv[optind];
286 /* Search for end of command list or ":" ... */
287 while (optind < argc && strcmp (argv[optind], ":") != 0)
291 issue_command (cmd, params);
294 issue_command (cmd, params);
295 cmdline (argv, optind+1, argc);
300 issue_command (const char *cmd, char *argv[])
304 fprintf (stderr, "cmd = %s", cmd);
305 for (i = 0; argv[i] != NULL; ++i)
306 fprintf (stderr, ", arg[%d]=%s", i, argv[i]);
307 fprintf (stderr, "\n");