2 * Copyright (C) 2010-2011 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
39 #define DISABLE_GUESTFS_ERRORS_FOR(stmt) do { \
40 guestfs_error_handler_cb old_error_cb; \
41 void *old_error_data; \
42 old_error_cb = guestfs_get_error_handler (g, &old_error_data); \
43 guestfs_set_error_handler (g, NULL, NULL); \
45 guestfs_set_error_handler (g, old_error_cb, old_error_data); \
48 /* These globals are shared with options.c. */
54 int keys_from_stdin = 0;
56 const char *libvirt_uri = NULL;
59 static int csv = 0; /* --csv */
60 static int human = 0; /* --human-readable|-h */
62 /* What is selected for output. */
63 #define OUTPUT_FILESYSTEMS 1
64 #define OUTPUT_FILESYSTEMS_EXTRA 2
65 #define OUTPUT_PARTITIONS 4
66 #define OUTPUT_BLOCKDEVS 8
70 #define OUTPUT_ALL INT_MAX
71 static int output = 0;
73 /* What columns to output. This is in display order. */
74 #define COLUMN_NAME 1 /* always shown */
76 #define COLUMN_VFS_TYPE 4 /* if --filesystems */
77 #define COLUMN_VFS_LABEL 8 /* if --filesystems */
79 #define COLUMN_SIZE 32 /* bytes, or human-readable if -h */
80 #define COLUMN_PARENT_NAME 64 /* only for partitions, LVs */
81 #define COLUMN_UUID 128 /* if --uuid */
85 static char *canonical_device (const char *dev);
86 static void do_output_title (void);
87 static void do_output (void);
88 static void do_output_end (void);
91 bad_cast (char const *s)
96 static void __attribute__((noreturn))
99 if (status != EXIT_SUCCESS)
100 fprintf (stderr, _("Try `%s --help' for more information.\n"),
104 _("%s: list filesystems, partitions, block devices, LVM in a VM\n"
105 "Copyright (C) 2010 Red Hat Inc.\n"
107 " %s [--options] -d domname\n"
108 " %s [--options] -a disk.img [-a disk.img ...]\n"
110 " -a|--add image Add image\n"
111 " --all Display everything\n"
112 " --blkdevs|--block-devices\n"
113 " Display block devices\n"
114 " -c|--connect uri Specify libvirt URI for -d option\n"
115 " --csv Output as Comma-Separated Values\n"
116 " -d|--domain guest Add disks from libvirt guest\n"
117 " --echo-keys Don't turn off echo for passphrases\n"
118 " --extra Display swap and data filesystems\n"
119 " --filesystems Display mountable filesystems\n"
120 " --format[=raw|..] Force disk format for -a option\n"
121 " -h|--human-readable Human-readable sizes in --long output\n"
122 " --help Display brief help\n"
123 " --keys-from-stdin Read passphrases from stdin\n"
124 " -l|--long Long output\n"
125 " --lvs|--logvols|--logical-volumes\n"
126 " Display LVM logical volumes\n"
127 " --no-title No title in --long output\n"
128 " --parts|--partitions Display partitions\n"
129 " --pvs|--physvols|--physical-volumes\n"
130 " Display LVM physical volumes\n"
131 " --uuid|--uuids Add UUIDs to --long output\n"
132 " -v|--verbose Verbose messages\n"
133 " -V|--version Display version and exit\n"
134 " --vgs|--volgroups|--volume-groups\n"
135 " Display LVM volume groups\n"
136 " -x Trace libguestfs API calls\n"
137 "For more information, see the manpage %s(1).\n"),
138 program_name, program_name, program_name,
145 main (int argc, char *argv[])
147 /* Set global program name that is not polluted with libtool artifacts. */
148 set_program_name (argv[0]);
150 setlocale (LC_ALL, "");
151 bindtextdomain (PACKAGE, LOCALEBASEDIR);
152 textdomain (PACKAGE);
154 enum { HELP_OPTION = CHAR_MAX + 1 };
156 static const char *options = "a:c:d:hlvVx";
157 static const struct option long_options[] = {
158 { "add", 1, 0, 'a' },
160 { "blkdevs", 0, 0, 0 },
161 { "block-devices", 0, 0, 0 },
162 { "connect", 1, 0, 'c' },
164 { "domain", 1, 0, 'd' },
165 { "echo-keys", 0, 0, 0 },
166 { "extra", 0, 0, 0 },
167 { "filesystems", 0, 0, 0 },
168 { "format", 2, 0, 0 },
169 { "help", 0, 0, HELP_OPTION },
170 { "human-readable", 0, 0, 'h' },
171 { "keys-from-stdin", 0, 0, 0 },
172 { "long", 0, 0, 'l' },
173 { "logical-volumes", 0, 0, 0 },
174 { "logvols", 0, 0, 0 },
176 { "no-title", 0, 0, 0 },
177 { "parts", 0, 0, 0 },
178 { "partitions", 0, 0, 0 },
179 { "physical-volumes", 0, 0, 0 },
180 { "physvols", 0, 0, 0 },
183 { "uuids", 0, 0, 0 },
184 { "verbose", 0, 0, 'v' },
185 { "version", 0, 0, 'V' },
187 { "volgroups", 0, 0, 0 },
188 { "volume-groups", 0, 0, 0 },
191 struct drv *drvs = NULL;
193 const char *format = NULL;
196 int no_title = 0; /* --no-title */
197 int long_mode = 0; /* --long|-l */
198 int uuid = 0; /* --uuid */
201 g = guestfs_create ();
203 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
207 argv[0] = bad_cast (program_name);
210 c = getopt_long (argc, argv, options, long_options, &option_index);
214 case 0: /* options which are long only */
215 if (STREQ (long_options[option_index].name, "keys-from-stdin")) {
217 } else if (STREQ (long_options[option_index].name, "echo-keys")) {
219 } else if (STREQ (long_options[option_index].name, "format")) {
220 if (!optarg || STREQ (optarg, ""))
224 } else if (STREQ (long_options[option_index].name, "all")) {
226 } else if (STREQ (long_options[option_index].name, "blkdevs") ||
227 STREQ (long_options[option_index].name, "block-devices")) {
228 output |= OUTPUT_BLOCKDEVS;
229 } else if (STREQ (long_options[option_index].name, "csv")) {
231 } else if (STREQ (long_options[option_index].name, "extra")) {
232 output |= OUTPUT_FILESYSTEMS;
233 output |= OUTPUT_FILESYSTEMS_EXTRA;
234 } else if (STREQ (long_options[option_index].name, "filesystems")) {
235 output |= OUTPUT_FILESYSTEMS;
236 } else if (STREQ (long_options[option_index].name, "logical-volumes") ||
237 STREQ (long_options[option_index].name, "logvols") ||
238 STREQ (long_options[option_index].name, "lvs")) {
239 output |= OUTPUT_LVS;
240 } else if (STREQ (long_options[option_index].name, "no-title")) {
242 } else if (STREQ (long_options[option_index].name, "parts") ||
243 STREQ (long_options[option_index].name, "partitions")) {
244 output |= OUTPUT_PARTITIONS;
245 } else if (STREQ (long_options[option_index].name, "physical-volumes") ||
246 STREQ (long_options[option_index].name, "physvols") ||
247 STREQ (long_options[option_index].name, "pvs")) {
248 output |= OUTPUT_PVS;
249 } else if (STREQ (long_options[option_index].name, "uuid") ||
250 STREQ (long_options[option_index].name, "uuids")) {
252 } else if (STREQ (long_options[option_index].name, "vgs") ||
253 STREQ (long_options[option_index].name, "volgroups") ||
254 STREQ (long_options[option_index].name, "volume-groups")) {
255 output |= OUTPUT_VGS;
257 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
258 program_name, long_options[option_index].name, option_index);
296 usage (EXIT_SUCCESS);
299 usage (EXIT_FAILURE);
303 /* These are really constants, but they have to be variables for the
304 * options parsing code. Assert here that they have known-good
307 assert (read_only == 1);
308 assert (inspector == 0);
311 /* Must be no extra arguments on the command line. */
313 usage (EXIT_FAILURE);
315 /* -h and --csv doesn't make sense. Spreadsheets will corrupt these
316 * fields. (RHBZ#600977).
319 fprintf (stderr, _("%s: you cannot use -h and --csv options together.\n"),
324 /* Nothing selected for output, means --filesystems is implied. */
326 output = OUTPUT_FILESYSTEMS;
328 /* What columns will be displayed? */
329 columns = COLUMN_NAME;
331 columns |= COLUMN_TYPE;
332 columns |= COLUMN_SIZE;
333 if ((output & OUTPUT_FILESYSTEMS)) {
334 columns |= COLUMN_VFS_TYPE;
335 columns |= COLUMN_VFS_LABEL;
337 if ((output & (OUTPUT_PARTITIONS|OUTPUT_LVS)))
338 columns |= COLUMN_PARENT_NAME;
339 if ((output & OUTPUT_PARTITIONS))
340 columns |= COLUMN_MBR;
342 columns |= COLUMN_UUID;
345 /* Display title by default only in long mode. */
350 /* User must have specified some drives. */
352 usage (EXIT_FAILURE);
355 add_drives (drvs, 'a');
357 if (guestfs_launch (g) == -1)
360 /* Free up data structures, no longer needed after this point. */
373 static void do_output_filesystems (void);
374 static void do_output_lvs (void);
375 static void do_output_vgs (void);
376 static void do_output_pvs (void);
377 static void do_output_partitions (void);
378 static void do_output_blockdevs (void);
379 static void write_row (const char *name, const char *type, const char *vfs_type, const char *vfs_label, int mbr_id, int64_t size, const char *parent_name, const char *uuid);
380 static void write_row_strings (char **strings, size_t len);
383 do_output_title (void)
385 const char *headings[NR_COLUMNS];
388 /* NB. These strings are not localized and must not contain spaces. */
389 if ((columns & COLUMN_NAME))
390 headings[len++] = "Name";
391 if ((columns & COLUMN_TYPE))
392 headings[len++] = "Type";
393 if ((columns & COLUMN_VFS_TYPE))
394 headings[len++] = "VFS";
395 if ((columns & COLUMN_VFS_LABEL))
396 headings[len++] = "Label";
397 if ((columns & COLUMN_MBR))
398 headings[len++] = "MBR";
399 if ((columns & COLUMN_SIZE))
400 headings[len++] = "Size";
401 if ((columns & COLUMN_PARENT_NAME))
402 headings[len++] = "Parent";
403 if ((columns & COLUMN_UUID))
404 headings[len++] = "UUID";
405 assert (len <= NR_COLUMNS);
407 write_row_strings ((char **) headings, len);
413 /* The ordering here is trying to be most specific -> least specific,
414 * although that is not required or guaranteed.
416 if ((output & OUTPUT_FILESYSTEMS))
417 do_output_filesystems ();
419 if ((output & OUTPUT_LVS))
422 if ((output & OUTPUT_VGS))
425 if ((output & OUTPUT_PVS))
428 if ((output & OUTPUT_PARTITIONS))
429 do_output_partitions ();
431 if ((output & OUTPUT_BLOCKDEVS))
432 do_output_blockdevs ();
436 do_output_filesystems (void)
441 fses = guestfs_list_filesystems (g);
445 for (i = 0; fses[i] != NULL; i += 2) {
446 char *dev, *vfs_label = NULL, *vfs_uuid = NULL;
449 /* Skip swap and unknown, unless --extra flag was given. */
450 if (!(output & OUTPUT_FILESYSTEMS_EXTRA) &&
451 (STREQ (fses[i+1], "swap") || STREQ (fses[i+1], "unknown")))
454 dev = canonical_device (fses[i]);
456 /* Only bother to look these up if we will be displaying them,
457 * otherwise pass them as NULL.
459 if ((columns & COLUMN_VFS_LABEL)) {
460 DISABLE_GUESTFS_ERRORS_FOR (
461 vfs_label = guestfs_vfs_label (g, fses[i]);
463 if (vfs_label == NULL) {
464 vfs_label = strdup ("");
471 if ((columns & COLUMN_UUID)) {
472 DISABLE_GUESTFS_ERRORS_FOR (
473 vfs_uuid = guestfs_vfs_uuid (g, fses[i]);
475 if (vfs_uuid == NULL) {
476 vfs_uuid = strdup ("");
483 if ((columns & COLUMN_SIZE)) {
484 size = guestfs_blockdev_getsize64 (g, fses[i]);
489 write_row (dev, "filesystem",
490 fses[i+1], vfs_label, -1, size, NULL, vfs_uuid);
508 lvs = guestfs_lvs (g);
512 for (i = 0; lvs[i] != NULL; ++i) {
513 char *uuid = NULL, *parent_name = NULL;
516 if ((columns & COLUMN_SIZE)) {
517 size = guestfs_blockdev_getsize64 (g, lvs[i]);
521 if ((columns & COLUMN_UUID)) {
522 uuid = guestfs_lvuuid (g, lvs[i]);
526 if ((columns & COLUMN_PARENT_NAME)) {
527 parent_name = strdup (lvs[i]);
528 if (parent_name == NULL) {
532 char *p = strrchr (parent_name, '/');
537 write_row (lvs[i], "lv",
538 NULL, NULL, -1, size, parent_name, uuid);
551 struct guestfs_lvm_vg_list *vgs;
554 vgs = guestfs_vgs_full (g);
558 for (i = 0; i < vgs->len; ++i) {
562 strcpy (name, "/dev/");
563 strcpy (&name[5], vgs->val[i].vg_name);
564 memcpy (uuid, vgs->val[i].vg_uuid, 32);
566 write_row (name, "vg",
567 NULL, NULL, -1, (int64_t) vgs->val[i].vg_size, NULL, uuid);
571 guestfs_free_lvm_vg_list (vgs);
577 struct guestfs_lvm_pv_list *pvs;
580 pvs = guestfs_pvs_full (g);
584 for (i = 0; i < pvs->len; ++i) {
588 dev = canonical_device (pvs->val[i].pv_name);
590 memcpy (uuid, pvs->val[i].pv_uuid, 32);
592 write_row (dev, "pv",
593 NULL, NULL, -1, (int64_t) pvs->val[i].pv_size, NULL, uuid);
598 guestfs_free_lvm_pv_list (pvs);
602 get_mbr_id (const char *dev, const char *parent_name)
604 char *parttype = NULL;
605 int mbr_id = -1, partnum;
607 DISABLE_GUESTFS_ERRORS_FOR (
608 parttype = guestfs_part_get_parttype (g, parent_name);
611 if (parttype && STREQ (parttype, "msdos")) {
612 DISABLE_GUESTFS_ERRORS_FOR (
613 partnum = guestfs_part_to_partnum (g, dev);
616 DISABLE_GUESTFS_ERRORS_FOR (
617 mbr_id = guestfs_part_get_mbr_id (g, parent_name, partnum);
628 do_output_partitions (void)
633 parts = guestfs_list_partitions (g);
637 for (i = 0; parts[i] != NULL; ++i) {
638 char *dev, *parent_name = NULL;
642 dev = canonical_device (parts[i]);
644 if ((columns & COLUMN_SIZE)) {
645 size = guestfs_blockdev_getsize64 (g, parts[i]);
649 if ((columns & COLUMN_PARENT_NAME)) {
650 parent_name = guestfs_part_to_dev (g, parts[i]);
651 if (parent_name == NULL)
654 if ((columns & COLUMN_MBR))
655 mbr_id = get_mbr_id (parts[i], parent_name);
657 char *p = canonical_device (parent_name);
662 write_row (dev, "partition",
663 NULL, NULL, mbr_id, size, parent_name, NULL);
674 do_output_blockdevs (void)
679 devices = guestfs_list_devices (g);
683 for (i = 0; devices[i] != NULL; ++i) {
687 dev = canonical_device (devices[i]);
689 if ((columns & COLUMN_SIZE)) {
690 size = guestfs_blockdev_getsize64 (g, devices[i]);
695 write_row (dev, "device",
696 NULL, NULL, -1, size, NULL, NULL);
705 /* /dev/vda1 -> /dev/sda. Returns a string which the caller must free. */
707 canonical_device (const char *dev)
709 char *ret = strdup (dev);
715 if (STRPREFIX (ret, "/dev/") &&
716 (ret[5] == 'h' || ret[5] == 'v') &&
718 c_isalpha (ret[7]) &&
719 (c_isdigit (ret[8]) || ret[8] == '\0'))
726 write_row (const char *name, const char *type,
727 const char *vfs_type, const char *vfs_label, int mbr_id,
728 int64_t size, const char *parent_name, const char *uuid)
730 const char *strings[NR_COLUMNS];
732 char hum[LONGEST_HUMAN_READABLE];
736 if ((columns & COLUMN_NAME))
737 strings[len++] = name;
738 if ((columns & COLUMN_TYPE))
739 strings[len++] = type;
740 if ((columns & COLUMN_VFS_TYPE))
741 strings[len++] = vfs_type;
742 if ((columns & COLUMN_VFS_LABEL))
743 strings[len++] = vfs_label;
744 if ((columns & COLUMN_MBR)) {
746 snprintf (mbr_id_str, sizeof mbr_id_str, "%02x", mbr_id);
747 strings[len++] = mbr_id_str;
749 strings[len++] = NULL;
751 if ((columns & COLUMN_SIZE)) {
755 human_readable ((uintmax_t) size, hum,
756 human_round_to_nearest|human_autoscale|
757 human_base_1024|human_SI,
761 snprintf (num, sizeof num, "%" PRIi64, size);
762 strings[len++] = num;
766 strings[len++] = NULL;
768 if ((columns & COLUMN_PARENT_NAME))
769 strings[len++] = parent_name;
770 if ((columns & COLUMN_UUID))
771 strings[len++] = uuid;
772 assert (len <= NR_COLUMNS);
774 write_row_strings ((char **) strings, len);
777 static void add_row (char **strings, size_t len);
778 static void write_csv_field (const char *field);
781 write_row_strings (char **strings, size_t len)
784 /* Text mode. Because we want the columns to line up, we can't
785 * output directly, but instead need to save up the rows and
786 * output them at the end.
788 add_row (strings, len);
790 else { /* CSV mode: output it directly, quoted */
793 for (i = 0; i < len; ++i) {
796 if (strings[i] != NULL)
797 write_csv_field (strings[i]);
803 /* Function to quote CSV fields on output without requiring an
807 write_csv_field (const char *field)
810 int needs_quoting = 0;
812 len = strlen (field);
814 for (i = 0; i < len; ++i) {
815 if (field[i] == ' ' || field[i] == '"' ||
816 field[i] == '\n' || field[i] == ',') {
822 if (!needs_quoting) {
823 printf ("%s", field);
827 /* Quoting for CSV fields. */
829 for (i = 0; i < len; ++i) {
830 if (field[i] == '"') {
839 /* This code is only used in text mode (non-CSV output). */
840 static char ***rows = NULL;
841 static size_t nr_rows = 0;
842 static size_t max_width[NR_COLUMNS];
845 add_row (char **strings, size_t len)
850 assert (len <= NR_COLUMNS);
852 row = malloc (sizeof (char *) * len);
858 for (i = 0; i < len; ++i) {
860 row[i] = strdup (strings[i]);
861 if (row[i] == NULL) {
866 /* Keep a running total of the max width of each column. */
867 slen = strlen (strings[i]);
869 slen = 1; /* because "" is printed as "-" */
870 if (slen > max_width[i])
877 rows = realloc (rows, sizeof (char **) * (nr_rows + 1));
886 /* In text mode we saved up all the output so that we can print the
892 size_t i, j, k, len, space_btwn;
897 /* How much space between columns? Try 2 spaces between columns, but
898 * if that just pushes us over 72 columns, use 1 space.
902 for (j = 0; j < NR_COLUMNS; ++j)
903 i += max_width[j] + space_btwn;
907 for (i = 0; i < nr_rows; ++i) {
908 char **row = rows[i];
912 for (j = 0; j < NR_COLUMNS; ++j) {
913 /* Ignore columns which are completely empty. This also deals
914 * with the fact that we didn't remember the length of each row
917 if (max_width[j] == 0)
925 if (row[j] == NULL || STREQ (row[j], "")) {
929 printf ("%s", row[j]);
930 len = strlen (row[j]);
934 assert (len <= max_width[j]);
935 k = max_width[j] - len + space_btwn;