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);
510 lvs = guestfs_lvs (g);
514 for (i = 0; lvs[i] != NULL; ++i) {
515 char *uuid = NULL, *parent_name = NULL;
518 if ((columns & COLUMN_SIZE)) {
519 size = guestfs_blockdev_getsize64 (g, lvs[i]);
523 if ((columns & COLUMN_UUID)) {
524 uuid = guestfs_lvuuid (g, lvs[i]);
528 if ((columns & COLUMN_PARENT_NAME)) {
529 parent_name = strdup (lvs[i]);
530 if (parent_name == NULL) {
534 char *p = strrchr (parent_name, '/');
539 write_row (lvs[i], "lv",
540 NULL, NULL, -1, size, parent_name, uuid);
553 struct guestfs_lvm_vg_list *vgs;
556 vgs = guestfs_vgs_full (g);
560 for (i = 0; i < vgs->len; ++i) {
564 strcpy (name, "/dev/");
565 strcpy (&name[5], vgs->val[i].vg_name);
566 memcpy (uuid, vgs->val[i].vg_uuid, 32);
568 write_row (name, "vg",
569 NULL, NULL, -1, (int64_t) vgs->val[i].vg_size, NULL, uuid);
573 guestfs_free_lvm_vg_list (vgs);
579 struct guestfs_lvm_pv_list *pvs;
582 pvs = guestfs_pvs_full (g);
586 for (i = 0; i < pvs->len; ++i) {
590 dev = canonical_device (pvs->val[i].pv_name);
592 memcpy (uuid, pvs->val[i].pv_uuid, 32);
594 write_row (dev, "pv",
595 NULL, NULL, -1, (int64_t) pvs->val[i].pv_size, NULL, uuid);
600 guestfs_free_lvm_pv_list (pvs);
604 get_mbr_id (const char *dev, const char *parent_name)
606 char *parttype = NULL;
607 int mbr_id = -1, partnum;
609 DISABLE_GUESTFS_ERRORS_FOR (
610 parttype = guestfs_part_get_parttype (g, parent_name);
613 if (parttype && STREQ (parttype, "msdos")) {
614 DISABLE_GUESTFS_ERRORS_FOR (
615 partnum = guestfs_part_to_partnum (g, dev);
618 DISABLE_GUESTFS_ERRORS_FOR (
619 mbr_id = guestfs_part_get_mbr_id (g, parent_name, partnum);
630 do_output_partitions (void)
635 parts = guestfs_list_partitions (g);
639 for (i = 0; parts[i] != NULL; ++i) {
640 char *dev, *parent_name = NULL;
644 dev = canonical_device (parts[i]);
646 if ((columns & COLUMN_SIZE)) {
647 size = guestfs_blockdev_getsize64 (g, parts[i]);
651 if ((columns & COLUMN_PARENT_NAME)) {
652 parent_name = guestfs_part_to_dev (g, parts[i]);
653 if (parent_name == NULL)
656 if ((columns & COLUMN_MBR))
657 mbr_id = get_mbr_id (parts[i], parent_name);
659 char *p = canonical_device (parent_name);
664 write_row (dev, "partition",
665 NULL, NULL, mbr_id, size, parent_name, NULL);
676 do_output_blockdevs (void)
681 devices = guestfs_list_devices (g);
685 for (i = 0; devices[i] != NULL; ++i) {
689 dev = canonical_device (devices[i]);
691 if ((columns & COLUMN_SIZE)) {
692 size = guestfs_blockdev_getsize64 (g, devices[i]);
697 write_row (dev, "device",
698 NULL, NULL, -1, size, NULL, NULL);
707 /* /dev/vda1 -> /dev/sda. Returns a string which the caller must free. */
709 canonical_device (const char *dev)
711 char *ret = strdup (dev);
717 if (STRPREFIX (ret, "/dev/") &&
718 (ret[5] == 'h' || ret[5] == 'v') &&
720 c_isalpha (ret[7]) &&
721 (c_isdigit (ret[8]) || ret[8] == '\0'))
728 write_row (const char *name, const char *type,
729 const char *vfs_type, const char *vfs_label, int mbr_id,
730 int64_t size, const char *parent_name, const char *uuid)
732 const char *strings[NR_COLUMNS];
734 char hum[LONGEST_HUMAN_READABLE];
738 if ((columns & COLUMN_NAME))
739 strings[len++] = name;
740 if ((columns & COLUMN_TYPE))
741 strings[len++] = type;
742 if ((columns & COLUMN_VFS_TYPE))
743 strings[len++] = vfs_type;
744 if ((columns & COLUMN_VFS_LABEL))
745 strings[len++] = vfs_label;
746 if ((columns & COLUMN_MBR)) {
748 snprintf (mbr_id_str, sizeof mbr_id_str, "%02x", mbr_id);
749 strings[len++] = mbr_id_str;
751 strings[len++] = NULL;
753 if ((columns & COLUMN_SIZE)) {
757 human_readable ((uintmax_t) size, hum,
758 human_round_to_nearest|human_autoscale|
759 human_base_1024|human_SI,
763 snprintf (num, sizeof num, "%" PRIi64, size);
764 strings[len++] = num;
768 strings[len++] = NULL;
770 if ((columns & COLUMN_PARENT_NAME))
771 strings[len++] = parent_name;
772 if ((columns & COLUMN_UUID))
773 strings[len++] = uuid;
774 assert (len <= NR_COLUMNS);
776 write_row_strings ((char **) strings, len);
779 static void add_row (char **strings, size_t len);
780 static void write_csv_field (const char *field);
783 write_row_strings (char **strings, size_t len)
786 /* Text mode. Because we want the columns to line up, we can't
787 * output directly, but instead need to save up the rows and
788 * output them at the end.
790 add_row (strings, len);
792 else { /* CSV mode: output it directly, quoted */
795 for (i = 0; i < len; ++i) {
798 if (strings[i] != NULL)
799 write_csv_field (strings[i]);
805 /* Function to quote CSV fields on output without requiring an
809 write_csv_field (const char *field)
812 int needs_quoting = 0;
814 len = strlen (field);
816 for (i = 0; i < len; ++i) {
817 if (field[i] == ' ' || field[i] == '"' ||
818 field[i] == '\n' || field[i] == ',') {
824 if (!needs_quoting) {
825 printf ("%s", field);
829 /* Quoting for CSV fields. */
831 for (i = 0; i < len; ++i) {
832 if (field[i] == '"') {
841 /* This code is only used in text mode (non-CSV output). */
842 static char ***rows = NULL;
843 static size_t nr_rows = 0;
844 static size_t max_width[NR_COLUMNS];
847 add_row (char **strings, size_t len)
852 assert (len <= NR_COLUMNS);
854 row = malloc (sizeof (char *) * len);
860 for (i = 0; i < len; ++i) {
862 row[i] = strdup (strings[i]);
863 if (row[i] == NULL) {
868 /* Keep a running total of the max width of each column. */
869 slen = strlen (strings[i]);
871 slen = 1; /* because "" is printed as "-" */
872 if (slen > max_width[i])
879 rows = realloc (rows, sizeof (char **) * (nr_rows + 1));
888 /* In text mode we saved up all the output so that we can print the
894 size_t i, j, k, len, space_btwn;
899 /* How much space between columns? Try 2 spaces between columns, but
900 * if that just pushes us over 72 columns, use 1 space.
904 for (j = 0; j < NR_COLUMNS; ++j)
905 i += max_width[j] + space_btwn;
909 for (i = 0; i < nr_rows; ++i) {
910 char **row = rows[i];
914 for (j = 0; j < NR_COLUMNS; ++j) {
915 /* Ignore columns which are completely empty. This also deals
916 * with the fact that we didn't remember the length of each row
919 if (max_width[j] == 0)
927 if (row[j] == NULL || STREQ (row[j], "")) {
931 printf ("%s", row[j]);
932 len = strlen (row[j]);
936 assert (len <= max_width[j]);
937 k = max_width[j] - len + space_btwn;