2 * Copyright (C) 2010 Red Hat Inc.
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.
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.
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.
36 #define DISABLE_GUESTFS_ERRORS_FOR(stmt) do { \
37 guestfs_error_handler_cb old_error_cb; \
38 void *old_error_data; \
39 old_error_cb = guestfs_get_error_handler (g, &old_error_data); \
40 guestfs_set_error_handler (g, NULL, NULL); \
42 guestfs_set_error_handler (g, old_error_cb, old_error_data); \
45 /* These globals are shared with options.c. */
50 int keys_from_stdin = 0;
52 const char *libvirt_uri = NULL;
55 static int csv = 0; /* --csv */
56 static int human = 0; /* --human-readable|-h */
58 /* What is selected for output. */
59 #define OUTPUT_FILESYSTEMS 1
60 #define OUTPUT_FILESYSTEMS_EXTRA 2
61 #define OUTPUT_PARTITIONS 4
62 #define OUTPUT_BLOCKDEVS 8
66 #define OUTPUT_ALL INT_MAX
67 static int output = 0;
69 /* What columns to output. This is in display order. */
70 #define COLUMN_NAME 1 /* always shown */
72 #define COLUMN_VFS_TYPE 4 /* if --filesystems */
73 #define COLUMN_VFS_LABEL 8 /* if --filesystems */
74 #define COLUMN_SIZE 16 /* bytes, or human-readable if -h */
75 #define COLUMN_PARENT_NAME 32 /* only for partitions, LVs */
76 #define COLUMN_UUID 64 /* if --uuid */
80 static char *canonical_device (const char *dev);
81 static void do_output_title (void);
82 static void do_output (void);
83 static void do_output_end (void);
86 bad_cast (char const *s)
91 static void __attribute__((noreturn))
94 if (status != EXIT_SUCCESS)
95 fprintf (stderr, _("Try `%s --help' for more information.\n"),
99 _("%s: list filesystems, partitions, block devices, LVM in a VM\n"
100 "Copyright (C) 2010 Red Hat Inc.\n"
102 " %s [--options] -d domname\n"
103 " %s [--options] -a disk.img [-a disk.img ...]\n"
105 " -a|--add image Add image\n"
106 " --all Display everything\n"
107 " --blkdevs|--block-devices\n"
108 " Display block devices\n"
109 " -c|--connect uri Specify libvirt URI for -d option\n"
110 " --csv Output as Comma-Separated Values\n"
111 " -d|--domain guest Add disks from libvirt guest\n"
112 " --echo-keys Don't turn off echo for passphrases\n"
113 " --extra Display swap and data filesystems\n"
114 " --filesystems Display mountable filesystems\n"
115 " --format[=raw|..] Force disk format for -a option\n"
116 " -h|--human-readable Human-readable sizes in --long output\n"
117 " --help Display brief help\n"
118 " --keys-from-stdin Read passphrases from stdin\n"
119 " -l|--long Long output\n"
120 " --lvs|--logvols|--logical-volumes\n"
121 " Display LVM logical volumes\n"
122 " --no-title No title in --long output\n"
123 " --parts|--partitions Display partitions\n"
124 " --pvs|--physvols|--physical-volumes\n"
125 " Display LVM physical volumes\n"
126 " --uuid|--uuids Add UUIDs to --long output\n"
127 " -v|--verbose Verbose messages\n"
128 " -V|--version Display version and exit\n"
129 " --vgs|--volgroups|--volume-groups\n"
130 " Display LVM volume groups\n"
131 " -x Trace libguestfs API calls\n"
132 "For more information, see the manpage %s(1).\n"),
133 program_name, program_name, program_name,
140 main (int argc, char *argv[])
142 /* Set global program name that is not polluted with libtool artifacts. */
143 set_program_name (argv[0]);
145 setlocale (LC_ALL, "");
146 bindtextdomain (PACKAGE, LOCALEBASEDIR);
147 textdomain (PACKAGE);
149 enum { HELP_OPTION = CHAR_MAX + 1 };
151 static const char *options = "a:c:d:hlvVx";
152 static const struct option long_options[] = {
153 { "add", 1, 0, 'a' },
155 { "blkdevs", 0, 0, 0 },
156 { "block-devices", 0, 0, 0 },
157 { "connect", 1, 0, 'c' },
159 { "domain", 1, 0, 'd' },
160 { "echo-keys", 0, 0, 0 },
161 { "extra", 0, 0, 0 },
162 { "filesystems", 0, 0, 0 },
163 { "format", 2, 0, 0 },
164 { "help", 0, 0, HELP_OPTION },
165 { "human-readable", 0, 0, 'h' },
166 { "keys-from-stdin", 0, 0, 0 },
167 { "long", 0, 0, 'l' },
168 { "logical-volumes", 0, 0, 0 },
169 { "logvols", 0, 0, 0 },
171 { "no-title", 0, 0, 0 },
172 { "parts", 0, 0, 0 },
173 { "partitions", 0, 0, 0 },
174 { "physical-volumes", 0, 0, 0 },
175 { "physvols", 0, 0, 0 },
178 { "uuids", 0, 0, 0 },
179 { "verbose", 0, 0, 'v' },
180 { "version", 0, 0, 'V' },
182 { "volgroups", 0, 0, 0 },
183 { "volume-groups", 0, 0, 0 },
186 struct drv *drvs = NULL;
188 const char *format = NULL;
191 int no_title = 0; /* --no-title */
192 int long_mode = 0; /* --long|-l */
193 int uuid = 0; /* --uuid */
196 g = guestfs_create ();
198 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
202 argv[0] = bad_cast (program_name);
205 c = getopt_long (argc, argv, options, long_options, &option_index);
209 case 0: /* options which are long only */
210 if (STREQ (long_options[option_index].name, "keys-from-stdin")) {
212 } else if (STREQ (long_options[option_index].name, "echo-keys")) {
214 } else if (STREQ (long_options[option_index].name, "format")) {
215 if (!optarg || STREQ (optarg, ""))
219 } else if (STREQ (long_options[option_index].name, "all")) {
221 } else if (STREQ (long_options[option_index].name, "blkdevs") ||
222 STREQ (long_options[option_index].name, "block-devices")) {
223 output |= OUTPUT_BLOCKDEVS;
224 } else if (STREQ (long_options[option_index].name, "csv")) {
226 } else if (STREQ (long_options[option_index].name, "extra")) {
227 output |= OUTPUT_FILESYSTEMS;
228 output |= OUTPUT_FILESYSTEMS_EXTRA;
229 } else if (STREQ (long_options[option_index].name, "filesystems")) {
230 output |= OUTPUT_FILESYSTEMS;
231 } else if (STREQ (long_options[option_index].name, "logical-volumes") ||
232 STREQ (long_options[option_index].name, "logvols") ||
233 STREQ (long_options[option_index].name, "lvs")) {
234 output |= OUTPUT_LVS;
235 } else if (STREQ (long_options[option_index].name, "no-title")) {
237 } else if (STREQ (long_options[option_index].name, "parts") ||
238 STREQ (long_options[option_index].name, "partitions")) {
239 output |= OUTPUT_PARTITIONS;
240 } else if (STREQ (long_options[option_index].name, "physical-volumes") ||
241 STREQ (long_options[option_index].name, "physvols") ||
242 STREQ (long_options[option_index].name, "pvs")) {
243 output |= OUTPUT_PVS;
244 } else if (STREQ (long_options[option_index].name, "uuid") ||
245 STREQ (long_options[option_index].name, "uuids")) {
247 } else if (STREQ (long_options[option_index].name, "vgs") ||
248 STREQ (long_options[option_index].name, "volgroups") ||
249 STREQ (long_options[option_index].name, "volume-groups")) {
250 output |= OUTPUT_VGS;
252 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
253 program_name, long_options[option_index].name, option_index);
291 usage (EXIT_SUCCESS);
294 usage (EXIT_FAILURE);
298 /* These are really constants, but they have to be variables for the
299 * options parsing code. Assert here that they have known-good
302 assert (read_only == 1);
303 assert (inspector == 0);
305 /* Must be no extra arguments on the command line. */
307 usage (EXIT_FAILURE);
309 /* -h and --csv doesn't make sense. Spreadsheets will corrupt these
310 * fields. (RHBZ#600977).
313 fprintf (stderr, _("%s: you cannot use -h and --csv options together.\n"),
318 /* Nothing selected for output, means --filesystems is implied. */
320 output = OUTPUT_FILESYSTEMS;
322 /* What columns will be displayed? */
323 columns = COLUMN_NAME;
325 columns |= COLUMN_TYPE;
326 columns |= COLUMN_SIZE;
327 if ((output & OUTPUT_FILESYSTEMS)) {
328 columns |= COLUMN_VFS_TYPE;
329 columns |= COLUMN_VFS_LABEL;
331 if ((output & (OUTPUT_PARTITIONS|OUTPUT_LVS)))
332 columns |= COLUMN_PARENT_NAME;
334 columns |= COLUMN_UUID;
337 /* Display title by default only in long mode. */
342 /* User must have specified some drives. */
344 usage (EXIT_FAILURE);
347 add_drives (drvs, 'a');
349 if (guestfs_launch (g) == -1)
352 /* Free up data structures, no longer needed after this point. */
365 static void do_output_filesystems (void);
366 static void do_output_lvs (void);
367 static void do_output_vgs (void);
368 static void do_output_pvs (void);
369 static void do_output_partitions (void);
370 static void do_output_blockdevs (void);
371 static void write_row (const char *name, const char *type, const char *vfs_type, const char *vfs_label, int64_t size, const char *parent_name, const char *uuid);
372 static void write_row_strings (char **strings, size_t len);
375 do_output_title (void)
377 const char *headings[NR_COLUMNS];
380 /* NB. These strings are not localized and must not contain spaces. */
381 if ((columns & COLUMN_NAME))
382 headings[len++] = "Name";
383 if ((columns & COLUMN_TYPE))
384 headings[len++] = "Type";
385 if ((columns & COLUMN_VFS_TYPE))
386 headings[len++] = "VFS";
387 if ((columns & COLUMN_VFS_LABEL))
388 headings[len++] = "Label";
389 if ((columns & COLUMN_SIZE))
390 headings[len++] = "Size";
391 if ((columns & COLUMN_PARENT_NAME))
392 headings[len++] = "Parent";
393 if ((columns & COLUMN_UUID))
394 headings[len++] = "UUID";
395 assert (len <= NR_COLUMNS);
397 write_row_strings ((char **) headings, len);
403 /* The ordering here is trying to be most specific -> least specific,
404 * although that is not required or guaranteed.
406 if ((output & OUTPUT_FILESYSTEMS))
407 do_output_filesystems ();
409 if ((output & OUTPUT_LVS))
412 if ((output & OUTPUT_VGS))
415 if ((output & OUTPUT_PVS))
418 if ((output & OUTPUT_PARTITIONS))
419 do_output_partitions ();
421 if ((output & OUTPUT_BLOCKDEVS))
422 do_output_blockdevs ();
426 do_output_filesystems (void)
431 fses = guestfs_list_filesystems (g);
435 for (i = 0; fses[i] != NULL; i += 2) {
436 char *dev, *vfs_label = NULL, *vfs_uuid = NULL;
439 /* Skip swap and unknown, unless --extra flag was given. */
440 if (!(output & OUTPUT_FILESYSTEMS_EXTRA) &&
441 (STREQ (fses[i+1], "swap") || STREQ (fses[i+1], "unknown")))
444 dev = canonical_device (fses[i]);
446 /* Only bother to look these up if we will be displaying them,
447 * otherwise pass them as NULL.
449 if ((columns & COLUMN_VFS_LABEL)) {
450 DISABLE_GUESTFS_ERRORS_FOR (
451 vfs_label = guestfs_vfs_label (g, fses[i]);
453 if (vfs_label == NULL) {
454 vfs_label = strdup ("");
461 if ((columns & COLUMN_UUID)) {
462 DISABLE_GUESTFS_ERRORS_FOR (
463 vfs_uuid = guestfs_vfs_uuid (g, fses[i]);
465 if (vfs_uuid == NULL) {
466 vfs_uuid = strdup ("");
473 if ((columns & COLUMN_SIZE)) {
474 size = guestfs_blockdev_getsize64 (g, fses[i]);
479 write_row (dev, "filesystem",
480 fses[i+1], vfs_label, size, NULL, vfs_uuid);
498 lvs = guestfs_lvs (g);
502 for (i = 0; lvs[i] != NULL; ++i) {
503 char *uuid = NULL, *parent_name = NULL;
506 if ((columns & COLUMN_SIZE)) {
507 size = guestfs_blockdev_getsize64 (g, lvs[i]);
511 if ((columns & COLUMN_UUID)) {
512 uuid = guestfs_lvuuid (g, lvs[i]);
516 if ((columns & COLUMN_PARENT_NAME)) {
517 parent_name = strdup (lvs[i]);
518 if (parent_name == NULL) {
522 char *p = strrchr (parent_name, '/');
527 write_row (lvs[i], "lv",
528 NULL, NULL, size, parent_name, uuid);
541 struct guestfs_lvm_vg_list *vgs;
544 vgs = guestfs_vgs_full (g);
548 for (i = 0; i < vgs->len; ++i) {
552 strcpy (name, "/dev/");
553 strcpy (&name[5], vgs->val[i].vg_name);
554 memcpy (uuid, vgs->val[i].vg_uuid, 32);
556 write_row (name, "vg",
557 NULL, NULL, (int64_t) vgs->val[i].vg_size, NULL, uuid);
561 guestfs_free_lvm_vg_list (vgs);
567 struct guestfs_lvm_pv_list *pvs;
570 pvs = guestfs_pvs_full (g);
574 for (i = 0; i < pvs->len; ++i) {
578 dev = canonical_device (pvs->val[i].pv_name);
580 memcpy (uuid, pvs->val[i].pv_uuid, 32);
582 write_row (dev, "pv",
583 NULL, NULL, (int64_t) pvs->val[i].pv_size, NULL, uuid);
588 guestfs_free_lvm_pv_list (pvs);
592 do_output_partitions (void)
597 parts = guestfs_list_partitions (g);
601 for (i = 0; parts[i] != NULL; ++i) {
602 char *dev, *parent_name = NULL;
605 dev = canonical_device (parts[i]);
607 if ((columns & COLUMN_SIZE)) {
608 size = guestfs_blockdev_getsize64 (g, parts[i]);
612 if ((columns & COLUMN_PARENT_NAME)) {
613 parent_name = guestfs_part_to_dev (g, parts[i]);
614 if (parent_name == NULL)
616 char *p = canonical_device (parent_name);
621 write_row (dev, "partition",
622 NULL, NULL, size, parent_name, NULL);
633 do_output_blockdevs (void)
638 devices = guestfs_list_devices (g);
642 for (i = 0; devices[i] != NULL; ++i) {
646 dev = canonical_device (devices[i]);
648 if ((columns & COLUMN_SIZE)) {
649 size = guestfs_blockdev_getsize64 (g, devices[i]);
654 write_row (dev, "device",
655 NULL, NULL, size, NULL, NULL);
664 /* /dev/vda1 -> /dev/sda. Returns a string which the caller must free. */
666 canonical_device (const char *dev)
668 char *ret = strdup (dev);
674 if (STRPREFIX (ret, "/dev/") &&
675 (ret[5] == 'h' || ret[5] == 'v') &&
677 c_isalpha (ret[7]) &&
678 (c_isdigit (ret[8]) || ret[8] == '\0'))
685 write_row (const char *name, const char *type,
686 const char *vfs_type, const char *vfs_label,
687 int64_t size, const char *parent_name, const char *uuid)
689 const char *strings[NR_COLUMNS];
691 char hum[LONGEST_HUMAN_READABLE];
694 if ((columns & COLUMN_NAME))
695 strings[len++] = name;
696 if ((columns & COLUMN_TYPE))
697 strings[len++] = type;
698 if ((columns & COLUMN_VFS_TYPE))
699 strings[len++] = vfs_type;
700 if ((columns & COLUMN_VFS_LABEL))
701 strings[len++] = vfs_label;
702 if ((columns & COLUMN_SIZE)) {
706 human_readable ((uintmax_t) size, hum,
707 human_round_to_nearest|human_autoscale|
708 human_base_1024|human_SI,
712 snprintf (num, sizeof num, "%" PRIi64, size);
713 strings[len++] = num;
717 strings[len++] = NULL;
719 if ((columns & COLUMN_PARENT_NAME))
720 strings[len++] = parent_name;
721 if ((columns & COLUMN_UUID))
722 strings[len++] = uuid;
723 assert (len <= NR_COLUMNS);
725 write_row_strings ((char **) strings, len);
728 static void add_row (char **strings, size_t len);
729 static void write_csv_field (const char *field);
732 write_row_strings (char **strings, size_t len)
735 /* Text mode. Because we want the columns to line up, we can't
736 * output directly, but instead need to save up the rows and
737 * output them at the end.
739 add_row (strings, len);
741 else { /* CSV mode: output it directly, quoted */
744 for (i = 0; i < len; ++i) {
747 if (strings[i] != NULL)
748 write_csv_field (strings[i]);
754 /* Function to quote CSV fields on output without requiring an
758 write_csv_field (const char *field)
761 int needs_quoting = 0;
763 len = strlen (field);
765 for (i = 0; i < len; ++i) {
766 if (field[i] == ' ' || field[i] == '"' ||
767 field[i] == '\n' || field[i] == ',') {
773 if (!needs_quoting) {
774 printf ("%s", field);
778 /* Quoting for CSV fields. */
780 for (i = 0; i < len; ++i) {
781 if (field[i] == '"') {
790 /* This code is only used in text mode (non-CSV output). */
791 static char ***rows = NULL;
792 static size_t nr_rows = 0;
793 static size_t max_width[NR_COLUMNS];
796 add_row (char **strings, size_t len)
801 assert (len <= NR_COLUMNS);
803 row = malloc (sizeof (char *) * len);
809 for (i = 0; i < len; ++i) {
811 row[i] = strdup (strings[i]);
812 if (row[i] == NULL) {
817 /* Keep a running total of the max width of each column. */
818 slen = strlen (strings[i]);
820 slen = 1; /* because "" is printed as "-" */
821 if (slen > max_width[i])
828 rows = realloc (rows, sizeof (char **) * (nr_rows + 1));
837 /* In text mode we saved up all the output so that we can print the
843 size_t i, j, k, len, space_btwn;
848 /* How much space between columns? Try 2 spaces between columns, but
849 * if that just pushes us over 72 columns, use 1 space.
853 for (j = 0; j < NR_COLUMNS; ++j)
854 i += max_width[j] + space_btwn;
858 for (i = 0; i < nr_rows; ++i) {
859 char **row = rows[i];
863 for (j = 0; j < NR_COLUMNS; ++j) {
864 /* Ignore columns which are completely empty. This also deals
865 * with the fact that we didn't remember the length of each row
868 if (max_width[j] == 0)
876 if (row[j] == NULL || STREQ (row[j], "")) {
880 printf ("%s", row[j]);
881 len = strlen (row[j]);
885 assert (len <= max_width[j]);
886 k = max_width[j] - len + space_btwn;