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