fish: Add guestfish --live, guestmount --live options.
[libguestfs.git] / df / main.c
1 /* virt-df
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 <stdint.h>
24 #include <inttypes.h>
25 #include <unistd.h>
26 #include <getopt.h>
27 #include <assert.h>
28
29 #ifdef HAVE_LIBVIRT
30 #include <libvirt/libvirt.h>
31 #include <libvirt/virterror.h>
32 #endif
33
34 #include "progname.h"
35
36 #include "guestfs.h"
37 #include "options.h"
38 #include "virt-df.h"
39
40 /* These globals are shared with options.c. */
41 guestfs_h *g;
42
43 int read_only = 1;
44 int live = 0;
45 int verbose = 0;
46 int keys_from_stdin = 0;
47 int echo_keys = 0;
48 const char *libvirt_uri = NULL;
49 int inspector = 0;
50
51 int csv = 0;                    /* --csv */
52 int human = 0;                  /* --human-readable|-h */
53 int inodes = 0;                 /* --inodes */
54 int one_per_guest = 0;          /* --one-per-guest */
55 int uuid = 0;                   /* --uuid */
56
57 static inline char *
58 bad_cast (char const *s)
59 {
60   return (char *) s;
61 }
62
63 static void __attribute__((noreturn))
64 usage (int status)
65 {
66   if (status != EXIT_SUCCESS)
67     fprintf (stderr, _("Try `%s --help' for more information.\n"),
68              program_name);
69   else {
70     fprintf (stdout,
71            _("%s: display free space on virtual filesystems\n"
72              "Copyright (C) 2010 Red Hat Inc.\n"
73              "Usage:\n"
74              "  %s [--options] -d domname\n"
75              "  %s [--options] -a disk.img [-a disk.img ...]\n"
76              "Options:\n"
77              "  -a|--add image       Add image\n"
78              "  -c|--connect uri     Specify libvirt URI for -d option\n"
79              "  --csv                Output as Comma-Separated Values\n"
80              "  -d|--domain guest    Add disks from libvirt guest\n"
81              "  --format[=raw|..]    Force disk format for -a option\n"
82              "  -h|--human-readable  Human-readable sizes in --long output\n"
83              "  --help               Display brief help\n"
84              "  -i|--inodes          Display inodes\n"
85              "  --one-per-guest      Separate appliance per guest\n"
86              "  --uuid               Add UUIDs to --long output\n"
87              "  -v|--verbose         Verbose messages\n"
88              "  -V|--version         Display version and exit\n"
89              "  -x                   Trace libguestfs API calls\n"
90              "For more information, see the manpage %s(1).\n"),
91              program_name, program_name, program_name,
92              program_name);
93   }
94   exit (status);
95 }
96
97 int
98 main (int argc, char *argv[])
99 {
100   /* Set global program name that is not polluted with libtool artifacts.  */
101   set_program_name (argv[0]);
102
103   setlocale (LC_ALL, "");
104   bindtextdomain (PACKAGE, LOCALEBASEDIR);
105   textdomain (PACKAGE);
106
107   enum { HELP_OPTION = CHAR_MAX + 1 };
108
109   static const char *options = "a:c:d:hivVx";
110   static const struct option long_options[] = {
111     { "add", 1, 0, 'a' },
112     { "connect", 1, 0, 'c' },
113     { "csv", 0, 0, 0 },
114     { "domain", 1, 0, 'd' },
115     { "format", 2, 0, 0 },
116     { "help", 0, 0, HELP_OPTION },
117     { "human-readable", 0, 0, 'h' },
118     { "inodes", 0, 0, 'i' },
119     { "one-per-guest", 0, 0, 0 },
120     { "uuid", 0, 0, 0 },
121     { "verbose", 0, 0, 'v' },
122     { "version", 0, 0, 'V' },
123     { 0, 0, 0, 0 }
124   };
125   struct drv *drvs = NULL;
126   struct drv *drv;
127   const char *format = NULL;
128   int c;
129   int option_index;
130
131   g = guestfs_create ();
132   if (g == NULL) {
133     fprintf (stderr, _("guestfs_create: failed to create handle\n"));
134     exit (EXIT_FAILURE);
135   }
136
137   argv[0] = bad_cast (program_name);
138
139   for (;;) {
140     c = getopt_long (argc, argv, options, long_options, &option_index);
141     if (c == -1) break;
142
143     switch (c) {
144     case 0:                     /* options which are long only */
145       if (STREQ (long_options[option_index].name, "format")) {
146         if (!optarg || STREQ (optarg, ""))
147           format = NULL;
148         else
149           format = optarg;
150       } else if (STREQ (long_options[option_index].name, "csv")) {
151         csv = 1;
152       } else if (STREQ (long_options[option_index].name, "one-per-guest")) {
153         one_per_guest = 1;
154       } else if (STREQ (long_options[option_index].name, "uuid")) {
155         uuid = 1;
156       } else {
157         fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
158                  program_name, long_options[option_index].name, option_index);
159         exit (EXIT_FAILURE);
160       }
161       break;
162
163     case 'a':
164       OPTION_a;
165       break;
166
167     case 'c':
168       OPTION_c;
169       break;
170
171     case 'd':
172       OPTION_d;
173       break;
174
175     case 'h':
176       human = 1;
177       break;
178
179     case 'i':
180       inodes = 1;
181       break;
182
183     case 'v':
184       OPTION_v;
185       break;
186
187     case 'V':
188       OPTION_V;
189       break;
190
191     case 'x':
192       OPTION_x;
193       break;
194
195     case HELP_OPTION:
196       usage (EXIT_SUCCESS);
197
198     default:
199       usage (EXIT_FAILURE);
200     }
201   }
202
203   /* Old-style syntax?  There were no -a or -d options in the old
204    * virt-df which is how we detect this.
205    */
206   if (drvs == NULL) {
207     while (optind < argc) {
208       if (strchr (argv[optind], '/') ||
209           access (argv[optind], F_OK) == 0) { /* simulate -a option */
210         drv = malloc (sizeof (struct drv));
211         if (!drv) {
212           perror ("malloc");
213           exit (EXIT_FAILURE);
214         }
215         drv->type = drv_a;
216         drv->a.filename = argv[optind];
217         drv->a.format = NULL;
218         drv->next = drvs;
219         drvs = drv;
220       } else {                  /* simulate -d option */
221         drv = malloc (sizeof (struct drv));
222         if (!drv) {
223           perror ("malloc");
224           exit (EXIT_FAILURE);
225         }
226         drv->type = drv_d;
227         drv->d.guest = argv[optind];
228         drv->next = drvs;
229         drvs = drv;
230       }
231
232       optind++;
233     }
234   }
235
236   /* These are really constants, but they have to be variables for the
237    * options parsing code.  Assert here that they have known-good
238    * values.
239    */
240   assert (read_only == 1);
241   assert (inspector == 0);
242   assert (live == 0);
243
244   /* Must be no extra arguments on the command line. */
245   if (optind != argc)
246     usage (EXIT_FAILURE);
247
248   /* -h and --csv doesn't make sense.  Spreadsheets will corrupt these
249    * fields.  (RHBZ#600977).
250    */
251   if (human && csv) {
252     fprintf (stderr, _("%s: you cannot use -h and --csv options together.\n"),
253              program_name);
254     exit (EXIT_FAILURE);
255   }
256
257   /* If the user didn't specify any drives, then we ask libvirt for
258    * the full list of guests and drives, which we add in batches.
259    */
260   if (drvs == NULL) {
261 #ifdef HAVE_LIBVIRT
262     get_domains_from_libvirt ();
263 #else
264     fprintf (stderr, _("%s: compiled without support for libvirt.\n"),
265              program_name);
266     exit (EXIT_FAILURE);
267 #endif
268   }
269   else {
270     const char *name;
271
272     /* Add domains/drives from the command line (for a single guest). */
273     add_drives (drvs, 'a');
274
275     if (guestfs_launch (g) == -1)
276       exit (EXIT_FAILURE);
277
278     print_title ();
279
280     /* Synthesize a display name. */
281     switch (drvs->type) {
282     case drv_a:
283       name = strrchr (drvs->a.filename, '/');
284       if (name == NULL)
285         name = drvs->a.filename;
286       else
287         name++; /* skip '/' character */
288       break;
289     case drv_d:
290       name = drvs->d.guest;
291       break;
292     case drv_N:
293     default:
294       abort ();
295     }
296
297     /* XXX regression: in the Perl version we cached the UUID from the
298      * libvirt domain handle so it was available to us here.  In this
299      * version the libvirt domain handle is hidden inside
300      * guestfs_add_domain so the UUID is not available easily for
301      * single '-d' command-line options.
302      */
303     (void) df_on_handle (name, NULL, NULL, 0);
304
305     /* Free up data structures, no longer needed after this point. */
306     free_drives (drvs);
307   }
308
309   guestfs_close (g);
310
311   exit (EXIT_SUCCESS);
312 }