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.
37 #define DISABLE_GUESTFS_ERRORS_FOR(stmt) do { \
38 guestfs_error_handler_cb old_error_cb; \
39 void *old_error_data; \
40 old_error_cb = guestfs_get_error_handler (g, &old_error_data); \
41 guestfs_set_error_handler (g, NULL, NULL); \
43 guestfs_set_error_handler (g, old_error_cb, old_error_data); \
46 /* 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);
306 /* Must be no extra arguments on the command line. */
308 usage (EXIT_FAILURE);
310 /* -h and --csv doesn't make sense. Spreadsheets will corrupt these
311 * fields. (RHBZ#600977).
314 fprintf (stderr, _("%s: you cannot use -h and --csv options together.\n"),
319 /* Nothing selected for output, means --filesystems is implied. */
321 output = OUTPUT_FILESYSTEMS;
323 /* What columns will be displayed? */
324 columns = COLUMN_NAME;
326 columns |= COLUMN_TYPE;
327 columns |= COLUMN_SIZE;
328 if ((output & OUTPUT_FILESYSTEMS)) {
329 columns |= COLUMN_VFS_TYPE;
330 columns |= COLUMN_VFS_LABEL;
332 if ((output & (OUTPUT_PARTITIONS|OUTPUT_LVS)))
333 columns |= COLUMN_PARENT_NAME;
335 columns |= COLUMN_UUID;
338 /* Display title by default only in long mode. */
343 /* User must have specified some drives. */
345 usage (EXIT_FAILURE);
348 add_drives (drvs, 'a');
350 if (guestfs_launch (g) == -1)
353 /* Free up data structures, no longer needed after this point. */
366 static void do_output_filesystems (void);
367 static void do_output_lvs (void);
368 static void do_output_vgs (void);
369 static void do_output_pvs (void);
370 static void do_output_partitions (void);
371 static void do_output_blockdevs (void);
372 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);
373 static void write_row_strings (char **strings, size_t len);
376 do_output_title (void)
378 const char *headings[NR_COLUMNS];
381 /* NB. These strings are not localized and must not contain spaces. */
382 if ((columns & COLUMN_NAME))
383 headings[len++] = "Name";
384 if ((columns & COLUMN_TYPE))
385 headings[len++] = "Type";
386 if ((columns & COLUMN_VFS_TYPE))
387 headings[len++] = "VFS";
388 if ((columns & COLUMN_VFS_LABEL))
389 headings[len++] = "Label";
390 if ((columns & COLUMN_SIZE))
391 headings[len++] = "Size";
392 if ((columns & COLUMN_PARENT_NAME))
393 headings[len++] = "Parent";
394 if ((columns & COLUMN_UUID))
395 headings[len++] = "UUID";
396 assert (len <= NR_COLUMNS);
398 write_row_strings ((char **) headings, len);
404 /* The ordering here is trying to be most specific -> least specific,
405 * although that is not required or guaranteed.
407 if ((output & OUTPUT_FILESYSTEMS))
408 do_output_filesystems ();
410 if ((output & OUTPUT_LVS))
413 if ((output & OUTPUT_VGS))
416 if ((output & OUTPUT_PVS))
419 if ((output & OUTPUT_PARTITIONS))
420 do_output_partitions ();
422 if ((output & OUTPUT_BLOCKDEVS))
423 do_output_blockdevs ();
427 do_output_filesystems (void)
432 fses = guestfs_list_filesystems (g);
436 for (i = 0; fses[i] != NULL; i += 2) {
437 char *dev, *vfs_label = NULL, *vfs_uuid = NULL;
440 /* Skip swap and unknown, unless --extra flag was given. */
441 if (!(output & OUTPUT_FILESYSTEMS_EXTRA) &&
442 (STREQ (fses[i+1], "swap") || STREQ (fses[i+1], "unknown")))
445 dev = canonical_device (fses[i]);
447 /* Only bother to look these up if we will be displaying them,
448 * otherwise pass them as NULL.
450 if ((columns & COLUMN_VFS_LABEL)) {
451 DISABLE_GUESTFS_ERRORS_FOR (
452 vfs_label = guestfs_vfs_label (g, fses[i]);
454 if (vfs_label == NULL) {
455 vfs_label = strdup ("");
462 if ((columns & COLUMN_UUID)) {
463 DISABLE_GUESTFS_ERRORS_FOR (
464 vfs_uuid = guestfs_vfs_uuid (g, fses[i]);
466 if (vfs_uuid == NULL) {
467 vfs_uuid = strdup ("");
474 if ((columns & COLUMN_SIZE)) {
475 size = guestfs_blockdev_getsize64 (g, fses[i]);
480 write_row (dev, "filesystem",
481 fses[i+1], vfs_label, size, NULL, vfs_uuid);
499 lvs = guestfs_lvs (g);
503 for (i = 0; lvs[i] != NULL; ++i) {
504 char *uuid = NULL, *parent_name = NULL;
507 if ((columns & COLUMN_SIZE)) {
508 size = guestfs_blockdev_getsize64 (g, lvs[i]);
512 if ((columns & COLUMN_UUID)) {
513 uuid = guestfs_lvuuid (g, lvs[i]);
517 if ((columns & COLUMN_PARENT_NAME)) {
518 parent_name = strdup (lvs[i]);
519 if (parent_name == NULL) {
523 char *p = strrchr (parent_name, '/');
528 write_row (lvs[i], "lv",
529 NULL, NULL, size, parent_name, uuid);
542 struct guestfs_lvm_vg_list *vgs;
545 vgs = guestfs_vgs_full (g);
549 for (i = 0; i < vgs->len; ++i) {
553 strcpy (name, "/dev/");
554 strcpy (&name[5], vgs->val[i].vg_name);
555 memcpy (uuid, vgs->val[i].vg_uuid, 32);
557 write_row (name, "vg",
558 NULL, NULL, (int64_t) vgs->val[i].vg_size, NULL, uuid);
562 guestfs_free_lvm_vg_list (vgs);
568 struct guestfs_lvm_pv_list *pvs;
571 pvs = guestfs_pvs_full (g);
575 for (i = 0; i < pvs->len; ++i) {
579 dev = canonical_device (pvs->val[i].pv_name);
581 memcpy (uuid, pvs->val[i].pv_uuid, 32);
583 write_row (dev, "pv",
584 NULL, NULL, (int64_t) pvs->val[i].pv_size, NULL, uuid);
589 guestfs_free_lvm_pv_list (pvs);
593 do_output_partitions (void)
598 parts = guestfs_list_partitions (g);
602 for (i = 0; parts[i] != NULL; ++i) {
603 char *dev, *parent_name = NULL;
606 dev = canonical_device (parts[i]);
608 if ((columns & COLUMN_SIZE)) {
609 size = guestfs_blockdev_getsize64 (g, parts[i]);
613 if ((columns & COLUMN_PARENT_NAME)) {
614 parent_name = guestfs_part_to_dev (g, parts[i]);
615 if (parent_name == NULL)
617 char *p = canonical_device (parent_name);
622 write_row (dev, "partition",
623 NULL, NULL, size, parent_name, NULL);
634 do_output_blockdevs (void)
639 devices = guestfs_list_devices (g);
643 for (i = 0; devices[i] != NULL; ++i) {
647 dev = canonical_device (devices[i]);
649 if ((columns & COLUMN_SIZE)) {
650 size = guestfs_blockdev_getsize64 (g, devices[i]);
655 write_row (dev, "device",
656 NULL, NULL, size, NULL, NULL);
665 /* /dev/vda1 -> /dev/sda. Returns a string which the caller must free. */
667 canonical_device (const char *dev)
669 char *ret = strdup (dev);
675 if (STRPREFIX (ret, "/dev/") &&
676 (ret[5] == 'h' || ret[5] == 'v') &&
678 c_isalpha (ret[7]) &&
679 (c_isdigit (ret[8]) || ret[8] == '\0'))
686 write_row (const char *name, const char *type,
687 const char *vfs_type, const char *vfs_label,
688 int64_t size, const char *parent_name, const char *uuid)
690 const char *strings[NR_COLUMNS];
692 char hum[LONGEST_HUMAN_READABLE];
695 if ((columns & COLUMN_NAME))
696 strings[len++] = name;
697 if ((columns & COLUMN_TYPE))
698 strings[len++] = type;
699 if ((columns & COLUMN_VFS_TYPE))
700 strings[len++] = vfs_type;
701 if ((columns & COLUMN_VFS_LABEL))
702 strings[len++] = vfs_label;
703 if ((columns & COLUMN_SIZE)) {
707 human_readable ((uintmax_t) size, hum,
708 human_round_to_nearest|human_autoscale|
709 human_base_1024|human_SI,
713 snprintf (num, sizeof num, "%" PRIi64, size);
714 strings[len++] = num;
718 strings[len++] = NULL;
720 if ((columns & COLUMN_PARENT_NAME))
721 strings[len++] = parent_name;
722 if ((columns & COLUMN_UUID))
723 strings[len++] = uuid;
724 assert (len <= NR_COLUMNS);
726 write_row_strings ((char **) strings, len);
729 static void add_row (char **strings, size_t len);
730 static void write_csv_field (const char *field);
733 write_row_strings (char **strings, size_t len)
736 /* Text mode. Because we want the columns to line up, we can't
737 * output directly, but instead need to save up the rows and
738 * output them at the end.
740 add_row (strings, len);
742 else { /* CSV mode: output it directly, quoted */
745 for (i = 0; i < len; ++i) {
748 if (strings[i] != NULL)
749 write_csv_field (strings[i]);
755 /* Function to quote CSV fields on output without requiring an
759 write_csv_field (const char *field)
762 int needs_quoting = 0;
764 len = strlen (field);
766 for (i = 0; i < len; ++i) {
767 if (field[i] == ' ' || field[i] == '"' ||
768 field[i] == '\n' || field[i] == ',') {
774 if (!needs_quoting) {
775 printf ("%s", field);
779 /* Quoting for CSV fields. */
781 for (i = 0; i < len; ++i) {
782 if (field[i] == '"') {
791 /* This code is only used in text mode (non-CSV output). */
792 static char ***rows = NULL;
793 static size_t nr_rows = 0;
794 static size_t max_width[NR_COLUMNS];
797 add_row (char **strings, size_t len)
802 assert (len <= NR_COLUMNS);
804 row = malloc (sizeof (char *) * len);
810 for (i = 0; i < len; ++i) {
812 row[i] = strdup (strings[i]);
813 if (row[i] == NULL) {
818 /* Keep a running total of the max width of each column. */
819 slen = strlen (strings[i]);
821 slen = 1; /* because "" is printed as "-" */
822 if (slen > max_width[i])
829 rows = realloc (rows, sizeof (char **) * (nr_rows + 1));
838 /* In text mode we saved up all the output so that we can print the
844 size_t i, j, k, len, space_btwn;
849 /* How much space between columns? Try 2 spaces between columns, but
850 * if that just pushes us over 72 columns, use 1 space.
854 for (j = 0; j < NR_COLUMNS; ++j)
855 i += max_width[j] + space_btwn;
859 for (i = 0; i < nr_rows; ++i) {
860 char **row = rows[i];
864 for (j = 0; j < NR_COLUMNS; ++j) {
865 /* Ignore columns which are completely empty. This also deals
866 * with the fact that we didn't remember the length of each row
869 if (max_width[j] == 0)
877 if (row[j] == NULL || STREQ (row[j], "")) {
881 printf ("%s", row[j]);
882 len = strlen (row[j]);
886 assert (len <= max_width[j]);
887 k = max_width[j] - len + space_btwn;