fish: Add guestfish --live, guestmount --live options.
[libguestfs.git] / cat / virt-cat.c
1 /* virt-cat
2  * Copyright (C) 2010 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 <inttypes.h>
24 #include <unistd.h>
25 #include <getopt.h>
26 #include <assert.h>
27
28 #include "progname.h"
29
30 #include "guestfs.h"
31 #include "options.h"
32
33 /* Currently open libguestfs handle. */
34 guestfs_h *g;
35
36 int read_only = 1;
37 int live = 0;
38 int verbose = 0;
39 int keys_from_stdin = 0;
40 int echo_keys = 0;
41 const char *libvirt_uri = NULL;
42 int inspector = 1;
43
44 static inline char *
45 bad_cast (char const *s)
46 {
47   return (char *) s;
48 }
49
50 static void __attribute__((noreturn))
51 usage (int status)
52 {
53   if (status != EXIT_SUCCESS)
54     fprintf (stderr, _("Try `%s --help' for more information.\n"),
55              program_name);
56   else {
57     fprintf (stdout,
58            _("%s: display files in a virtual machine\n"
59              "Copyright (C) 2010 Red Hat Inc.\n"
60              "Usage:\n"
61              "  %s [--options] -d domname file [file ...]\n"
62              "  %s [--options] -a disk.img [-a disk.img ...] file [file ...]\n"
63              "Options:\n"
64              "  -a|--add image       Add image\n"
65              "  -c|--connect uri     Specify libvirt URI for -d option\n"
66              "  -d|--domain guest    Add disks from libvirt guest\n"
67              "  --echo-keys          Don't turn off echo for passphrases\n"
68              "  --format[=raw|..]    Force disk format for -a option\n"
69              "  --help               Display brief help\n"
70              "  --keys-from-stdin    Read passphrases from stdin\n"
71              "  -v|--verbose         Verbose messages\n"
72              "  -V|--version         Display version and exit\n"
73              "  -x                   Trace libguestfs API calls\n"
74              "For more information, see the manpage %s(1).\n"),
75              program_name, program_name, program_name,
76              program_name);
77   }
78   exit (status);
79 }
80
81 int
82 main (int argc, char *argv[])
83 {
84   /* Set global program name that is not polluted with libtool artifacts.  */
85   set_program_name (argv[0]);
86
87   setlocale (LC_ALL, "");
88   bindtextdomain (PACKAGE, LOCALEBASEDIR);
89   textdomain (PACKAGE);
90
91   enum { HELP_OPTION = CHAR_MAX + 1 };
92
93   static const char *options = "a:c:d:vVx";
94   static const struct option long_options[] = {
95     { "add", 1, 0, 'a' },
96     { "connect", 1, 0, 'c' },
97     { "domain", 1, 0, 'd' },
98     { "echo-keys", 0, 0, 0 },
99     { "format", 2, 0, 0 },
100     { "help", 0, 0, HELP_OPTION },
101     { "keys-from-stdin", 0, 0, 0 },
102     { "verbose", 0, 0, 'v' },
103     { "version", 0, 0, 'V' },
104     { 0, 0, 0, 0 }
105   };
106   struct drv *drvs = NULL;
107   struct drv *drv;
108   const char *format = NULL;
109   int c;
110   int option_index;
111
112   g = guestfs_create ();
113   if (g == NULL) {
114     fprintf (stderr, _("guestfs_create: failed to create handle\n"));
115     exit (EXIT_FAILURE);
116   }
117
118   argv[0] = bad_cast (program_name);
119
120   for (;;) {
121     c = getopt_long (argc, argv, options, long_options, &option_index);
122     if (c == -1) break;
123
124     switch (c) {
125     case 0:                     /* options which are long only */
126       if (STREQ (long_options[option_index].name, "keys-from-stdin")) {
127         keys_from_stdin = 1;
128       } else if (STREQ (long_options[option_index].name, "echo-keys")) {
129         echo_keys = 1;
130       } else if (STREQ (long_options[option_index].name, "format")) {
131         if (!optarg || STREQ (optarg, ""))
132           format = NULL;
133         else
134           format = optarg;
135       } else {
136         fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
137                  program_name, long_options[option_index].name, option_index);
138         exit (EXIT_FAILURE);
139       }
140       break;
141
142     case 'a':
143       OPTION_a;
144       break;
145
146     case 'c':
147       OPTION_c;
148       break;
149
150     case 'd':
151       OPTION_d;
152       break;
153
154     case 'h':
155       usage (EXIT_SUCCESS);
156
157     case 'v':
158       OPTION_v;
159       break;
160
161     case 'V':
162       OPTION_V;
163       break;
164
165     case 'x':
166       OPTION_x;
167       break;
168
169     case HELP_OPTION:
170       usage (EXIT_SUCCESS);
171
172     default:
173       usage (EXIT_FAILURE);
174     }
175   }
176
177   /* Old-style syntax?  There were no -a or -d options in the old
178    * virt-cat which is how we detect this.
179    */
180   if (drvs == NULL) {
181     /* argc - 1 because last parameter is the single filename. */
182     while (optind < argc - 1) {
183       if (strchr (argv[optind], '/') ||
184           access (argv[optind], F_OK) == 0) { /* simulate -a option */
185         drv = malloc (sizeof (struct drv));
186         if (!drv) {
187           perror ("malloc");
188           exit (EXIT_FAILURE);
189         }
190         drv->type = drv_a;
191         drv->a.filename = argv[optind];
192         drv->a.format = NULL;
193         drv->next = drvs;
194         drvs = drv;
195       } else {                  /* simulate -d option */
196         drv = malloc (sizeof (struct drv));
197         if (!drv) {
198           perror ("malloc");
199           exit (EXIT_FAILURE);
200         }
201         drv->type = drv_d;
202         drv->d.guest = argv[optind];
203         drv->next = drvs;
204         drvs = drv;
205       }
206
207       optind++;
208     }
209   }
210
211   /* These are really constants, but they have to be variables for the
212    * options parsing code.  Assert here that they have known-good
213    * values.
214    */
215   assert (read_only == 1);
216   assert (inspector == 1);
217   assert (live == 0);
218
219   /* User must specify at least one filename on the command line. */
220   if (optind >= argc || argc - optind < 1)
221     usage (EXIT_FAILURE);
222
223   /* User must have specified some drives. */
224   if (drvs == NULL)
225     usage (EXIT_FAILURE);
226
227   /* Add drives, inspect and mount.  Note that inspector is always true,
228    * and there is no -m option.
229    */
230   add_drives (drvs, 'a');
231
232   if (guestfs_launch (g) == -1)
233     exit (EXIT_FAILURE);
234
235   inspect_mount ();
236
237   /* Free up data structures, no longer needed after this point. */
238   free_drives (drvs);
239
240   unsigned errors = 0;
241
242   while (optind < argc) {
243     if (guestfs_download (g, argv[optind], "/dev/stdout") == -1)
244       errors++;
245     optind++;
246   }
247
248   guestfs_close (g);
249
250   exit (errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
251 }