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