Implemented autosync, make it the default for guestfish.
[libguestfs.git] / fish / fish.c
1 /* guestfish - the filesystem interactive shell
2  * Copyright (C) 2009 Red Hat Inc. 
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <getopt.h>
26
27 #include <guestfs.h>
28
29 #include "fish.h"
30
31 struct mp {
32   struct mp *next;
33   char *device;
34   char *mountpoint;
35 };
36
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[]);
43
44 /* Currently open libguestfs handle. */
45 guestfs_h *g;
46 int g_launched = 0;
47
48 int quit = 0;
49 int verbose = 0;
50
51 int
52 launch (void)
53 {
54   if (!g_launched) {
55     if (guestfs_launch (g) == -1)
56       return -1;
57     if (guestfs_wait_ready (g) == -1)
58       return -1;
59     g_launched = 1;
60   }
61   return 0;
62 }
63
64 static void
65 usage (void)
66 {
67   fprintf (stderr,
68            "guestfish: guest filesystem shell\n"
69            "guestfish lets you edit virtual machine filesystems\n"
70            "Copyright (C) 2009 Red Hat Inc.\n"
71            "Usage:\n"
72            "  guestfish [--options] cmd [: cmd : cmd ...]\n"
73            "or for interactive use:\n"
74            "  guestfish\n"
75            "or from a shell script:\n"
76            "  guestfish <<EOF\n"
77            "  cmd\n"
78            "  ...\n"
79            "  EOF\n"
80            "Options:\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");
89 }
90
91 int
92 main (int argc, char *argv[])
93 {
94   static const char *options = "a:h::m:v?";
95   static struct option long_options[] = {
96     { "add", 1, 0, 'a' },
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' },
102     { 0, 0, 0, 0 }
103   };
104   struct mp *mps = NULL;
105   struct mp *mp;
106   char *p;
107   int c;
108
109   /* guestfs_create is meant to be a lightweight operation, so
110    * it's OK to do it early here.
111    */
112   g = guestfs_create ();
113   if (g == NULL) {
114     fprintf (stderr, "guestfs_create: failed to create handle\n");
115     exit (1);
116   }
117
118   guestfs_set_autosync (g, 1);
119
120   for (;;) {
121     c = getopt_long (argc, argv, options, long_options, NULL);
122     if (c == -1) break;
123
124     switch (c) {
125     case 'a':
126       if (access (optarg, R_OK) != 0) {
127         perror (optarg);
128         exit (1);
129       }
130       if (guestfs_add_drive (g, optarg) == -1)
131         exit (1);
132       break;
133
134     case 'h':
135       if (optarg)
136         display_command (optarg);
137       else if (argv[optind] && argv[optind][0] != '-')
138         display_command (argv[optind++]);
139       else
140         list_commands ();
141       exit (0);
142
143     case 'm':
144       mp = malloc (sizeof (struct mp));
145       if (!mp) {
146         perror ("malloc");
147         exit (1);
148       }
149       p = strchr (optarg, ':');
150       if (p) {
151         *p = '\0';
152         mp->mountpoint = p+1;
153       } else
154         mp->mountpoint = "/";
155       mp->device = optarg;
156       mp->next = mps;
157       mps = mp->next;
158       break;
159
160     case 'n':
161       guestfs_set_autosync (g, 0);
162       break;
163
164     case 'v':
165       verbose++;
166       guestfs_set_verbose (g, verbose);
167       break;
168
169     case '?':
170       usage ();
171       exit (0);
172
173     default:
174       fprintf (stderr, "guestfish: unexpected command line option 0x%x\n", c);
175       exit (1);
176     }
177   }
178
179   /* If we've got mountpoints, we must launch the guest and mount them. */
180   if (mps != NULL) {
181     if (launch () == -1) exit (1);
182     mount_mps (mps);
183   }
184
185   /* Interactive, shell script, or command(s) on the command line? */
186   if (optind >= argc) {
187     if (isatty (0))
188       interactive ();
189     else
190       shell_script ();
191   }
192   else
193     cmdline (argv, optind, argc);
194
195   exit (0);
196 }
197
198 void
199 pod2text (const char *heading, const char *str)
200 {
201   FILE *fp;
202
203   fp = popen ("pod2text", "w");
204   if (fp == NULL) {
205     /* pod2text failed, maybe not found, so let's just print the
206      * source instead, since that's better than doing nothing.
207      */
208     printf ("%s\n\n%s\n", heading, str);
209     return;
210   }
211   fputs ("=head1 ", fp);
212   fputs (heading, fp);
213   fputs ("\n\n", fp);
214   fputs (str, fp);
215   pclose (fp);
216 }
217
218 /* List is built in reverse order, so mount them in reverse order. */
219 static void
220 mount_mps (struct mp *mp)
221 {
222   if (mp) {
223     mount_mps (mp->next);
224     if (guestfs_mount (g, mp->device, mp->mountpoint) == -1)
225       exit (1);
226   }
227 }
228
229 static void
230 interactive (void)
231 {
232   script (1);
233 }
234
235 static void
236 shell_script (void)
237 {
238   script (0);
239 }
240
241 static void
242 script (int prompt)
243 {
244   char buf[8192];
245   char *cmd;
246   char *argv[64];
247   int len, i;
248
249   if (prompt)
250     printf ("\n"
251             "Welcome to guestfish, the libguestfs filesystem interactive shell for\n"
252             "editing virtual machine filesystems.\n"
253             "\n"
254             "Type: 'help' for help with commands\n"
255             "      'quit' to quit the shell\n"
256             "\n");
257
258   while (!quit) {
259     if (prompt) printf ("><fs> ");
260     if (fgets (buf, sizeof buf, stdin) == NULL) {
261       quit = 1;
262       break;
263     }
264
265     len = strlen (buf);
266     if (len > 0 && buf[len-1] == '\n') buf[len-1] = '\0';
267
268     /* Split the buffer up at whitespace. */
269     cmd = strtok (buf, " \t");
270     if (cmd == NULL)
271       continue;
272
273     i = 0;
274     while (i < sizeof argv / sizeof argv[0] &&
275            (argv[i] = strtok (NULL, " \t")) != NULL)
276       i++;
277     if (i == sizeof argv / sizeof argv[0]) {
278       fprintf (stderr, "guestfish: too many arguments in command\n");
279       exit (1);
280     }
281
282     (void) issue_command (cmd, argv);
283   }
284   if (prompt) printf ("\n");
285 }
286
287 static void
288 cmdline (char *argv[], int optind, int argc)
289 {
290   const char *cmd;
291   char **params;
292
293   if (optind >= argc) return;
294
295   cmd = argv[optind++];
296   if (strcmp (cmd, ":") == 0) {
297     fprintf (stderr, "guestfish: empty command on command line\n");
298     exit (1);
299   }
300   params = &argv[optind];
301
302   /* Search for end of command list or ":" ... */
303   while (optind < argc && strcmp (argv[optind], ":") != 0)
304     optind++;
305
306   if (optind == argc) {
307     if (issue_command (cmd, params) == -1) exit (1);
308   } else {
309     argv[optind] = NULL;
310     if (issue_command (cmd, params) == -1) exit (1);
311     cmdline (argv, optind+1, argc);
312   }
313 }
314
315 static int
316 issue_command (const char *cmd, char *argv[])
317 {
318   int argc;
319
320   for (argc = 0; argv[argc] != NULL; ++argc)
321     ;
322
323   if (strcasecmp (cmd, "help") == 0) {
324     if (argc == 0)
325       list_commands ();
326     else
327       display_command (argv[0]);
328     return 0;
329   }
330   else if (strcasecmp (cmd, "quit") == 0 ||
331            strcasecmp (cmd, "exit") == 0 ||
332            strcasecmp (cmd, "q") == 0)
333     exit (0);
334   else if (strcasecmp (cmd, "add") == 0 ||
335            strcasecmp (cmd, "drive") == 0 ||
336            strcasecmp (cmd, "add_drive") == 0) {
337     if (argc != 1) {
338       fprintf (stderr, "use 'add image' to add a guest image\n");
339       return -1;
340     }
341     else
342       return guestfs_add_drive (g, argv[0]);
343   }
344   else if (strcasecmp (cmd, "cdrom") == 0) {
345     if (argc != 1) {
346       fprintf (stderr, "use 'cdrom image' to add a CD-ROM image\n");
347       return -1;
348     }
349     else
350       return guestfs_add_cdrom (g, argv[0]);
351   }
352   else if (strcasecmp (cmd, "launch") == 0) {
353     if (argc != 0) {
354       fprintf (stderr, "'launch' command takes no parameters\n");
355       return -1;
356     }
357     else
358       return launch ();
359   }
360   else
361     return run_action (cmd, argc, argv);
362 }
363
364 void
365 list_builtin_commands (void)
366 {
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");
377 }
378
379 void
380 display_builtin_command (const char *cmd)
381 {
382   if (strcasecmp (cmd, "add") == 0)
383     printf ("add - add a guest image to be examined or modified\n"
384             "     add <image>\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"
390             "     help cmd\n"
391             "     help\n");
392   else if (strcasecmp (cmd, "quit") == 0)
393     printf ("quit - quit guestfish\n"
394             "     quit\n");
395   else if (strcasecmp (cmd, "launch") == 0)
396     printf ("launch - launch the subprocess\n"
397             "     launch\n");
398   else
399     fprintf (stderr, "%s: command not known, use -h to list all commands\n",
400              cmd);
401 }