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. */
51 int keys_from_stdin = 0;
53 const char *libvirt_uri = NULL;
56 static int csv = 0; /* --csv */
57 static int human = 0; /* --human-readable|-h */
59 /* What is selected for output. */
60 #define OUTPUT_FILESYSTEMS 1
61 #define OUTPUT_FILESYSTEMS_EXTRA 2
62 #define OUTPUT_PARTITIONS 4
63 #define OUTPUT_BLOCKDEVS 8
67 #define OUTPUT_ALL INT_MAX
68 static int output = 0;
70 /* What columns to output. This is in display order. */
71 #define COLUMN_NAME 1 /* always shown */
73 #define COLUMN_VFS_TYPE 4 /* if --filesystems */
74 #define COLUMN_VFS_LABEL 8 /* if --filesystems */
75 #define COLUMN_SIZE 16 /* bytes, or human-readable if -h */
76 #define COLUMN_PARENT_NAME 32 /* only for partitions, LVs */
77 #define COLUMN_UUID 64 /* if --uuid */
81 static char *canonical_device (const char *dev);
82 static void do_output_title (void);
83 static void do_output (void);
84 static void do_output_end (void);
87 bad_cast (char const *s)
92 static void __attribute__((noreturn))
95 if (status != EXIT_SUCCESS)
96 fprintf (stderr, _("Try `%s --help' for more information.\n"),
100 _("%s: list filesystems, partitions, block devices, LVM in a VM\n"
101 "Copyright (C) 2010 Red Hat Inc.\n"
103 " %s [--options] -d domname\n"
104 " %s [--options] -a disk.img [-a disk.img ...]\n"
106 " -a|--add image Add image\n"
107 " --all Display everything\n"
108 " --blkdevs|--block-devices\n"
109 " Display block devices\n"
110 " -c|--connect uri Specify libvirt URI for -d option\n"
111 " --csv Output as Comma-Separated Values\n"
112 " -d|--domain guest Add disks from libvirt guest\n"
113 " --echo-keys Don't turn off echo for passphrases\n"
114 " --extra Display swap and data filesystems\n"
115 " --filesystems Display mountable filesystems\n"
116 " --format[=raw|..] Force disk format for -a option\n"
117 " -h|--human-readable Human-readable sizes in --long output\n"
118 " --help Display brief help\n"
119 " --keys-from-stdin Read passphrases from stdin\n"
120 " -l|--long Long output\n"
121 " --lvs|--logvols|--logical-volumes\n"
122 " Display LVM logical volumes\n"
123 " --no-title No title in --long output\n"
124 " --parts|--partitions Display partitions\n"
125 " --pvs|--physvols|--physical-volumes\n"
126 " Display LVM physical volumes\n"
127 " --uuid|--uuids Add UUIDs to --long output\n"
128 " -v|--verbose Verbose messages\n"
129 " -V|--version Display version and exit\n"
130 " --vgs|--volgroups|--volume-groups\n"
131 " Display LVM volume groups\n"
132 " -x Trace libguestfs API calls\n"
133 "For more information, see the manpage %s(1).\n"),
134 program_name, program_name, program_name,
141 main (int argc, char *argv[])
143 /* Set global program name that is not polluted with libtool artifacts. */
144 set_program_name (argv[0]);
146 setlocale (LC_ALL, "");
147 bindtextdomain (PACKAGE, LOCALEBASEDIR);
148 textdomain (PACKAGE);
150 enum { HELP_OPTION = CHAR_MAX + 1 };
152 static const char *options = "a:c:d:hlvVx";
153 static const struct option long_options[] = {
154 { "add", 1, 0, 'a' },
156 { "blkdevs", 0, 0, 0 },
157 { "block-devices", 0, 0, 0 },
158 { "connect", 1, 0, 'c' },
160 { "domain", 1, 0, 'd' },
161 { "echo-keys", 0, 0, 0 },
162 { "extra", 0, 0, 0 },
163 { "filesystems", 0, 0, 0 },
164 { "format", 2, 0, 0 },
165 { "help", 0, 0, HELP_OPTION },
166 { "human-readable", 0, 0, 'h' },
167 { "keys-from-stdin", 0, 0, 0 },
168 { "long", 0, 0, 'l' },
169 { "logical-volumes", 0, 0, 0 },
170 { "logvols", 0, 0, 0 },
172 { "no-title", 0, 0, 0 },
173 { "parts", 0, 0, 0 },
174 { "partitions", 0, 0, 0 },
175 { "physical-volumes", 0, 0, 0 },
176 { "physvols", 0, 0, 0 },
179 { "uuids", 0, 0, 0 },
180 { "verbose", 0, 0, 'v' },
181 { "version", 0, 0, 'V' },
183 { "volgroups", 0, 0, 0 },
184 { "volume-groups", 0, 0, 0 },
187 struct drv *drvs = NULL;
189 const char *format = NULL;
192 int no_title = 0; /* --no-title */
193 int long_mode = 0; /* --long|-l */
194 int uuid = 0; /* --uuid */
197 g = guestfs_create ();
199 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
203 argv[0] = bad_cast (program_name);
206 c = getopt_long (argc, argv, options, long_options, &option_index);
210 case 0: /* options which are long only */
211 if (STREQ (long_options[option_index].name, "keys-from-stdin")) {
213 } else if (STREQ (long_options[option_index].name, "echo-keys")) {
215 } else if (STREQ (long_options[option_index].name, "format")) {
216 if (!optarg || STREQ (optarg, ""))
220 } else if (STREQ (long_options[option_index].name, "all")) {
222 } else if (STREQ (long_options[option_index].name, "blkdevs") ||
223 STREQ (long_options[option_index].name, "block-devices")) {
224 output |= OUTPUT_BLOCKDEVS;
225 } else if (STREQ (long_options[option_index].name, "csv")) {
227 } else if (STREQ (long_options[option_index].name, "extra")) {
228 output |= OUTPUT_FILESYSTEMS;
229 output |= OUTPUT_FILESYSTEMS_EXTRA;
230 } else if (STREQ (long_options[option_index].name, "filesystems")) {
231 output |= OUTPUT_FILESYSTEMS;
232 } else if (STREQ (long_options[option_index].name, "logical-volumes") ||
233 STREQ (long_options[option_index].name, "logvols") ||
234 STREQ (long_options[option_index].name, "lvs")) {
235 output |= OUTPUT_LVS;
236 } else if (STREQ (long_options[option_index].name, "no-title")) {
238 } else if (STREQ (long_options[option_index].name, "parts") ||
239 STREQ (long_options[option_index].name, "partitions")) {
240 output |= OUTPUT_PARTITIONS;
241 } else if (STREQ (long_options[option_index].name, "physical-volumes") ||
242 STREQ (long_options[option_index].name, "physvols") ||
243 STREQ (long_options[option_index].name, "pvs")) {
244 output |= OUTPUT_PVS;
245 } else if (STREQ (long_options[option_index].name, "uuid") ||
246 STREQ (long_options[option_index].name, "uuids")) {
248 } else if (STREQ (long_options[option_index].name, "vgs") ||
249 STREQ (long_options[option_index].name, "volgroups") ||
250 STREQ (long_options[option_index].name, "volume-groups")) {
251 output |= OUTPUT_VGS;
253 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
254 program_name, long_options[option_index].name, option_index);
292 usage (EXIT_SUCCESS);
295 usage (EXIT_FAILURE);
299 /* These are really constants, but they have to be variables for the
300 * options parsing code. Assert here that they have known-good
303 assert (read_only == 1);
304 assert (inspector == 0);
307 /* Must be no extra arguments on the command line. */
309 usage (EXIT_FAILURE);
311 /* -h and --csv doesn't make sense. Spreadsheets will corrupt these
312 * fields. (RHBZ#600977).
315 fprintf (stderr, _("%s: you cannot use -h and --csv options together.\n"),
320 /* Nothing selected for output, means --filesystems is implied. */
322 output = OUTPUT_FILESYSTEMS;
324 /* What columns will be displayed? */
325 columns = COLUMN_NAME;
327 columns |= COLUMN_TYPE;
328 columns |= COLUMN_SIZE;
329 if ((output & OUTPUT_FILESYSTEMS)) {
330 columns |= COLUMN_VFS_TYPE;
331 columns |= COLUMN_VFS_LABEL;
333 if ((output & (OUTPUT_PARTITIONS|OUTPUT_LVS)))
334 columns |= COLUMN_PARENT_NAME;
336 columns |= COLUMN_UUID;
339 /* Display title by default only in long mode. */
344 /* User must have specified some drives. */
346 usage (EXIT_FAILURE);
349 add_drives (drvs, 'a');
351 if (guestfs_launch (g) == -1)
354 /* Free up data structures, no longer needed after this point. */
367 static void do_output_filesystems (void);
368 static void do_output_lvs (void);
369 static void do_output_vgs (void);
370 static void do_output_pvs (void);
371 static void do_output_partitions (void);
372 static void do_output_blockdevs (void);
373 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);
374 static void write_row_strings (char **strings, size_t len);
377 do_output_title (void)
379 const char *headings[NR_COLUMNS];
382 /* NB. These strings are not localized and must not contain spaces. */
383 if ((columns & COLUMN_NAME))
384 headings[len++] = "Name";
385 if ((columns & COLUMN_TYPE))
386 headings[len++] = "Type";
387 if ((columns & COLUMN_VFS_TYPE))
388 headings[len++] = "VFS";
389 if ((columns & COLUMN_VFS_LABEL))
390 headings[len++] = "Label";
391 if ((columns & COLUMN_SIZE))
392 headings[len++] = "Size";
393 if ((columns & COLUMN_PARENT_NAME))
394 headings[len++] = "Parent";
395 if ((columns & COLUMN_UUID))
396 headings[len++] = "UUID";
397 assert (len <= NR_COLUMNS);
399 write_row_strings ((char **) headings, len);
405 /* The ordering here is trying to be most specific -> least specific,
406 * although that is not required or guaranteed.
408 if ((output & OUTPUT_FILESYSTEMS))
409 do_output_filesystems ();
411 if ((output & OUTPUT_LVS))
414 if ((output & OUTPUT_VGS))
417 if ((output & OUTPUT_PVS))
420 if ((output & OUTPUT_PARTITIONS))
421 do_output_partitions ();
423 if ((output & OUTPUT_BLOCKDEVS))
424 do_output_blockdevs ();
428 do_output_filesystems (void)
433 fses = guestfs_list_filesystems (g);
437 for (i = 0; fses[i] != NULL; i += 2) {
438 char *dev, *vfs_label = NULL, *vfs_uuid = NULL;
441 /* Skip swap and unknown, unless --extra flag was given. */
442 if (!(output & OUTPUT_FILESYSTEMS_EXTRA) &&
443 (STREQ (fses[i+1], "swap") || STREQ (fses[i+1], "unknown")))
446 dev = canonical_device (fses[i]);
448 /* Only bother to look these up if we will be displaying them,
449 * otherwise pass them as NULL.
451 if ((columns & COLUMN_VFS_LABEL)) {
452 DISABLE_GUESTFS_ERRORS_FOR (
453 vfs_label = guestfs_vfs_label (g, fses[i]);
455 if (vfs_label == NULL) {
456 vfs_label = strdup ("");
463 if ((columns & COLUMN_UUID)) {
464 DISABLE_GUESTFS_ERRORS_FOR (
465 vfs_uuid = guestfs_vfs_uuid (g, fses[i]);
467 if (vfs_uuid == NULL) {
468 vfs_uuid = strdup ("");
475 if ((columns & COLUMN_SIZE)) {
476 size = guestfs_blockdev_getsize64 (g, fses[i]);
481 write_row (dev, "filesystem",
482 fses[i+1], vfs_label, size, NULL, vfs_uuid);
500 lvs = guestfs_lvs (g);
504 for (i = 0; lvs[i] != NULL; ++i) {
505 char *uuid = NULL, *parent_name = NULL;
508 if ((columns & COLUMN_SIZE)) {
509 size = guestfs_blockdev_getsize64 (g, lvs[i]);
513 if ((columns & COLUMN_UUID)) {
514 uuid = guestfs_lvuuid (g, lvs[i]);
518 if ((columns & COLUMN_PARENT_NAME)) {
519 parent_name = strdup (lvs[i]);
520 if (parent_name == NULL) {
524 char *p = strrchr (parent_name, '/');
529 write_row (lvs[i], "lv",
530 NULL, NULL, size, parent_name, uuid);
543 struct guestfs_lvm_vg_list *vgs;
546 vgs = guestfs_vgs_full (g);
550 for (i = 0; i < vgs->len; ++i) {
554 strcpy (name, "/dev/");
555 strcpy (&name[5], vgs->val[i].vg_name);
556 memcpy (uuid, vgs->val[i].vg_uuid, 32);
558 write_row (name, "vg",
559 NULL, NULL, (int64_t) vgs->val[i].vg_size, NULL, uuid);
563 guestfs_free_lvm_vg_list (vgs);
569 struct guestfs_lvm_pv_list *pvs;
572 pvs = guestfs_pvs_full (g);
576 for (i = 0; i < pvs->len; ++i) {
580 dev = canonical_device (pvs->val[i].pv_name);
582 memcpy (uuid, pvs->val[i].pv_uuid, 32);
584 write_row (dev, "pv",
585 NULL, NULL, (int64_t) pvs->val[i].pv_size, NULL, uuid);
590 guestfs_free_lvm_pv_list (pvs);
594 do_output_partitions (void)
599 parts = guestfs_list_partitions (g);
603 for (i = 0; parts[i] != NULL; ++i) {
604 char *dev, *parent_name = NULL;
607 dev = canonical_device (parts[i]);
609 if ((columns & COLUMN_SIZE)) {
610 size = guestfs_blockdev_getsize64 (g, parts[i]);
614 if ((columns & COLUMN_PARENT_NAME)) {
615 parent_name = guestfs_part_to_dev (g, parts[i]);
616 if (parent_name == NULL)
618 char *p = canonical_device (parent_name);
623 write_row (dev, "partition",
624 NULL, NULL, size, parent_name, NULL);
635 do_output_blockdevs (void)
640 devices = guestfs_list_devices (g);
644 for (i = 0; devices[i] != NULL; ++i) {
648 dev = canonical_device (devices[i]);
650 if ((columns & COLUMN_SIZE)) {
651 size = guestfs_blockdev_getsize64 (g, devices[i]);
656 write_row (dev, "device",
657 NULL, NULL, size, NULL, NULL);
666 /* /dev/vda1 -> /dev/sda. Returns a string which the caller must free. */
668 canonical_device (const char *dev)
670 char *ret = strdup (dev);
676 if (STRPREFIX (ret, "/dev/") &&
677 (ret[5] == 'h' || ret[5] == 'v') &&
679 c_isalpha (ret[7]) &&
680 (c_isdigit (ret[8]) || ret[8] == '\0'))
687 write_row (const char *name, const char *type,
688 const char *vfs_type, const char *vfs_label,
689 int64_t size, const char *parent_name, const char *uuid)
691 const char *strings[NR_COLUMNS];
693 char hum[LONGEST_HUMAN_READABLE];
696 if ((columns & COLUMN_NAME))
697 strings[len++] = name;
698 if ((columns & COLUMN_TYPE))
699 strings[len++] = type;
700 if ((columns & COLUMN_VFS_TYPE))
701 strings[len++] = vfs_type;
702 if ((columns & COLUMN_VFS_LABEL))
703 strings[len++] = vfs_label;
704 if ((columns & COLUMN_SIZE)) {
708 human_readable ((uintmax_t) size, hum,
709 human_round_to_nearest|human_autoscale|
710 human_base_1024|human_SI,
714 snprintf (num, sizeof num, "%" PRIi64, size);
715 strings[len++] = num;
719 strings[len++] = NULL;
721 if ((columns & COLUMN_PARENT_NAME))
722 strings[len++] = parent_name;
723 if ((columns & COLUMN_UUID))
724 strings[len++] = uuid;
725 assert (len <= NR_COLUMNS);
727 write_row_strings ((char **) strings, len);
730 static void add_row (char **strings, size_t len);
731 static void write_csv_field (const char *field);
734 write_row_strings (char **strings, size_t len)
737 /* Text mode. Because we want the columns to line up, we can't
738 * output directly, but instead need to save up the rows and
739 * output them at the end.
741 add_row (strings, len);
743 else { /* CSV mode: output it directly, quoted */
746 for (i = 0; i < len; ++i) {
749 if (strings[i] != NULL)
750 write_csv_field (strings[i]);
756 /* Function to quote CSV fields on output without requiring an
760 write_csv_field (const char *field)
763 int needs_quoting = 0;
765 len = strlen (field);
767 for (i = 0; i < len; ++i) {
768 if (field[i] == ' ' || field[i] == '"' ||
769 field[i] == '\n' || field[i] == ',') {
775 if (!needs_quoting) {
776 printf ("%s", field);
780 /* Quoting for CSV fields. */
782 for (i = 0; i < len; ++i) {
783 if (field[i] == '"') {
792 /* This code is only used in text mode (non-CSV output). */
793 static char ***rows = NULL;
794 static size_t nr_rows = 0;
795 static size_t max_width[NR_COLUMNS];
798 add_row (char **strings, size_t len)
803 assert (len <= NR_COLUMNS);
805 row = malloc (sizeof (char *) * len);
811 for (i = 0; i < len; ++i) {
813 row[i] = strdup (strings[i]);
814 if (row[i] == NULL) {
819 /* Keep a running total of the max width of each column. */
820 slen = strlen (strings[i]);
822 slen = 1; /* because "" is printed as "-" */
823 if (slen > max_width[i])
830 rows = realloc (rows, sizeof (char **) * (nr_rows + 1));
839 /* In text mode we saved up all the output so that we can print the
845 size_t i, j, k, len, space_btwn;
850 /* How much space between columns? Try 2 spaces between columns, but
851 * if that just pushes us over 72 columns, use 1 space.
855 for (j = 0; j < NR_COLUMNS; ++j)
856 i += max_width[j] + space_btwn;
860 for (i = 0; i < nr_rows; ++i) {
861 char **row = rows[i];
865 for (j = 0; j < NR_COLUMNS; ++j) {
866 /* Ignore columns which are completely empty. This also deals
867 * with the fact that we didn't remember the length of each row
870 if (max_width[j] == 0)
878 if (row[j] == NULL || STREQ (row[j], "")) {
882 printf ("%s", row[j]);
883 len = strlen (row[j]);
887 assert (len <= max_width[j]);
888 k = max_width[j] - len + space_btwn;