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., 675 Mass Ave, Cambridge, MA 02139, 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 */
78 #define COLUMN_SIZE 16 /* bytes, or human-readable if -h */
79 #define COLUMN_PARENT_NAME 32 /* only for partitions, LVs */
80 #define COLUMN_UUID 64 /* if --uuid */
84 static char *canonical_device (const char *dev);
85 static void do_output_title (void);
86 static void do_output (void);
87 static void do_output_end (void);
90 bad_cast (char const *s)
95 static void __attribute__((noreturn))
98 if (status != EXIT_SUCCESS)
99 fprintf (stderr, _("Try `%s --help' for more information.\n"),
103 _("%s: list filesystems, partitions, block devices, LVM in a VM\n"
104 "Copyright (C) 2010 Red Hat Inc.\n"
106 " %s [--options] -d domname\n"
107 " %s [--options] -a disk.img [-a disk.img ...]\n"
109 " -a|--add image Add image\n"
110 " --all Display everything\n"
111 " --blkdevs|--block-devices\n"
112 " Display block devices\n"
113 " -c|--connect uri Specify libvirt URI for -d option\n"
114 " --csv Output as Comma-Separated Values\n"
115 " -d|--domain guest Add disks from libvirt guest\n"
116 " --echo-keys Don't turn off echo for passphrases\n"
117 " --extra Display swap and data filesystems\n"
118 " --filesystems Display mountable filesystems\n"
119 " --format[=raw|..] Force disk format for -a option\n"
120 " -h|--human-readable Human-readable sizes in --long output\n"
121 " --help Display brief help\n"
122 " --keys-from-stdin Read passphrases from stdin\n"
123 " -l|--long Long output\n"
124 " --lvs|--logvols|--logical-volumes\n"
125 " Display LVM logical volumes\n"
126 " --no-title No title in --long output\n"
127 " --parts|--partitions Display partitions\n"
128 " --pvs|--physvols|--physical-volumes\n"
129 " Display LVM physical volumes\n"
130 " --uuid|--uuids Add UUIDs to --long output\n"
131 " -v|--verbose Verbose messages\n"
132 " -V|--version Display version and exit\n"
133 " --vgs|--volgroups|--volume-groups\n"
134 " Display LVM volume groups\n"
135 " -x Trace libguestfs API calls\n"
136 "For more information, see the manpage %s(1).\n"),
137 program_name, program_name, program_name,
144 main (int argc, char *argv[])
146 /* Set global program name that is not polluted with libtool artifacts. */
147 set_program_name (argv[0]);
149 setlocale (LC_ALL, "");
150 bindtextdomain (PACKAGE, LOCALEBASEDIR);
151 textdomain (PACKAGE);
153 enum { HELP_OPTION = CHAR_MAX + 1 };
155 static const char *options = "a:c:d:hlvVx";
156 static const struct option long_options[] = {
157 { "add", 1, 0, 'a' },
159 { "blkdevs", 0, 0, 0 },
160 { "block-devices", 0, 0, 0 },
161 { "connect", 1, 0, 'c' },
163 { "domain", 1, 0, 'd' },
164 { "echo-keys", 0, 0, 0 },
165 { "extra", 0, 0, 0 },
166 { "filesystems", 0, 0, 0 },
167 { "format", 2, 0, 0 },
168 { "help", 0, 0, HELP_OPTION },
169 { "human-readable", 0, 0, 'h' },
170 { "keys-from-stdin", 0, 0, 0 },
171 { "long", 0, 0, 'l' },
172 { "logical-volumes", 0, 0, 0 },
173 { "logvols", 0, 0, 0 },
175 { "no-title", 0, 0, 0 },
176 { "parts", 0, 0, 0 },
177 { "partitions", 0, 0, 0 },
178 { "physical-volumes", 0, 0, 0 },
179 { "physvols", 0, 0, 0 },
182 { "uuids", 0, 0, 0 },
183 { "verbose", 0, 0, 'v' },
184 { "version", 0, 0, 'V' },
186 { "volgroups", 0, 0, 0 },
187 { "volume-groups", 0, 0, 0 },
190 struct drv *drvs = NULL;
192 const char *format = NULL;
195 int no_title = 0; /* --no-title */
196 int long_mode = 0; /* --long|-l */
197 int uuid = 0; /* --uuid */
200 g = guestfs_create ();
202 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
206 argv[0] = bad_cast (program_name);
209 c = getopt_long (argc, argv, options, long_options, &option_index);
213 case 0: /* options which are long only */
214 if (STREQ (long_options[option_index].name, "keys-from-stdin")) {
216 } else if (STREQ (long_options[option_index].name, "echo-keys")) {
218 } else if (STREQ (long_options[option_index].name, "format")) {
219 if (!optarg || STREQ (optarg, ""))
223 } else if (STREQ (long_options[option_index].name, "all")) {
225 } else if (STREQ (long_options[option_index].name, "blkdevs") ||
226 STREQ (long_options[option_index].name, "block-devices")) {
227 output |= OUTPUT_BLOCKDEVS;
228 } else if (STREQ (long_options[option_index].name, "csv")) {
230 } else if (STREQ (long_options[option_index].name, "extra")) {
231 output |= OUTPUT_FILESYSTEMS;
232 output |= OUTPUT_FILESYSTEMS_EXTRA;
233 } else if (STREQ (long_options[option_index].name, "filesystems")) {
234 output |= OUTPUT_FILESYSTEMS;
235 } else if (STREQ (long_options[option_index].name, "logical-volumes") ||
236 STREQ (long_options[option_index].name, "logvols") ||
237 STREQ (long_options[option_index].name, "lvs")) {
238 output |= OUTPUT_LVS;
239 } else if (STREQ (long_options[option_index].name, "no-title")) {
241 } else if (STREQ (long_options[option_index].name, "parts") ||
242 STREQ (long_options[option_index].name, "partitions")) {
243 output |= OUTPUT_PARTITIONS;
244 } else if (STREQ (long_options[option_index].name, "physical-volumes") ||
245 STREQ (long_options[option_index].name, "physvols") ||
246 STREQ (long_options[option_index].name, "pvs")) {
247 output |= OUTPUT_PVS;
248 } else if (STREQ (long_options[option_index].name, "uuid") ||
249 STREQ (long_options[option_index].name, "uuids")) {
251 } else if (STREQ (long_options[option_index].name, "vgs") ||
252 STREQ (long_options[option_index].name, "volgroups") ||
253 STREQ (long_options[option_index].name, "volume-groups")) {
254 output |= OUTPUT_VGS;
256 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
257 program_name, long_options[option_index].name, option_index);
295 usage (EXIT_SUCCESS);
298 usage (EXIT_FAILURE);
302 /* These are really constants, but they have to be variables for the
303 * options parsing code. Assert here that they have known-good
306 assert (read_only == 1);
307 assert (inspector == 0);
310 /* Must be no extra arguments on the command line. */
312 usage (EXIT_FAILURE);
314 /* -h and --csv doesn't make sense. Spreadsheets will corrupt these
315 * fields. (RHBZ#600977).
318 fprintf (stderr, _("%s: you cannot use -h and --csv options together.\n"),
323 /* Nothing selected for output, means --filesystems is implied. */
325 output = OUTPUT_FILESYSTEMS;
327 /* What columns will be displayed? */
328 columns = COLUMN_NAME;
330 columns |= COLUMN_TYPE;
331 columns |= COLUMN_SIZE;
332 if ((output & OUTPUT_FILESYSTEMS)) {
333 columns |= COLUMN_VFS_TYPE;
334 columns |= COLUMN_VFS_LABEL;
336 if ((output & (OUTPUT_PARTITIONS|OUTPUT_LVS)))
337 columns |= COLUMN_PARENT_NAME;
339 columns |= COLUMN_UUID;
342 /* Display title by default only in long mode. */
347 /* User must have specified some drives. */
349 usage (EXIT_FAILURE);
352 add_drives (drvs, 'a');
354 if (guestfs_launch (g) == -1)
357 /* Free up data structures, no longer needed after this point. */
370 static void do_output_filesystems (void);
371 static void do_output_lvs (void);
372 static void do_output_vgs (void);
373 static void do_output_pvs (void);
374 static void do_output_partitions (void);
375 static void do_output_blockdevs (void);
376 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);
377 static void write_row_strings (char **strings, size_t len);
380 do_output_title (void)
382 const char *headings[NR_COLUMNS];
385 /* NB. These strings are not localized and must not contain spaces. */
386 if ((columns & COLUMN_NAME))
387 headings[len++] = "Name";
388 if ((columns & COLUMN_TYPE))
389 headings[len++] = "Type";
390 if ((columns & COLUMN_VFS_TYPE))
391 headings[len++] = "VFS";
392 if ((columns & COLUMN_VFS_LABEL))
393 headings[len++] = "Label";
394 if ((columns & COLUMN_SIZE))
395 headings[len++] = "Size";
396 if ((columns & COLUMN_PARENT_NAME))
397 headings[len++] = "Parent";
398 if ((columns & COLUMN_UUID))
399 headings[len++] = "UUID";
400 assert (len <= NR_COLUMNS);
402 write_row_strings ((char **) headings, len);
408 /* The ordering here is trying to be most specific -> least specific,
409 * although that is not required or guaranteed.
411 if ((output & OUTPUT_FILESYSTEMS))
412 do_output_filesystems ();
414 if ((output & OUTPUT_LVS))
417 if ((output & OUTPUT_VGS))
420 if ((output & OUTPUT_PVS))
423 if ((output & OUTPUT_PARTITIONS))
424 do_output_partitions ();
426 if ((output & OUTPUT_BLOCKDEVS))
427 do_output_blockdevs ();
431 do_output_filesystems (void)
436 fses = guestfs_list_filesystems (g);
440 for (i = 0; fses[i] != NULL; i += 2) {
441 char *dev, *vfs_label = NULL, *vfs_uuid = NULL;
444 /* Skip swap and unknown, unless --extra flag was given. */
445 if (!(output & OUTPUT_FILESYSTEMS_EXTRA) &&
446 (STREQ (fses[i+1], "swap") || STREQ (fses[i+1], "unknown")))
449 dev = canonical_device (fses[i]);
451 /* Only bother to look these up if we will be displaying them,
452 * otherwise pass them as NULL.
454 if ((columns & COLUMN_VFS_LABEL)) {
455 DISABLE_GUESTFS_ERRORS_FOR (
456 vfs_label = guestfs_vfs_label (g, fses[i]);
458 if (vfs_label == NULL) {
459 vfs_label = strdup ("");
466 if ((columns & COLUMN_UUID)) {
467 DISABLE_GUESTFS_ERRORS_FOR (
468 vfs_uuid = guestfs_vfs_uuid (g, fses[i]);
470 if (vfs_uuid == NULL) {
471 vfs_uuid = strdup ("");
478 if ((columns & COLUMN_SIZE)) {
479 size = guestfs_blockdev_getsize64 (g, fses[i]);
484 write_row (dev, "filesystem",
485 fses[i+1], vfs_label, size, NULL, vfs_uuid);
503 lvs = guestfs_lvs (g);
507 for (i = 0; lvs[i] != NULL; ++i) {
508 char *uuid = NULL, *parent_name = NULL;
511 if ((columns & COLUMN_SIZE)) {
512 size = guestfs_blockdev_getsize64 (g, lvs[i]);
516 if ((columns & COLUMN_UUID)) {
517 uuid = guestfs_lvuuid (g, lvs[i]);
521 if ((columns & COLUMN_PARENT_NAME)) {
522 parent_name = strdup (lvs[i]);
523 if (parent_name == NULL) {
527 char *p = strrchr (parent_name, '/');
532 write_row (lvs[i], "lv",
533 NULL, NULL, size, parent_name, uuid);
546 struct guestfs_lvm_vg_list *vgs;
549 vgs = guestfs_vgs_full (g);
553 for (i = 0; i < vgs->len; ++i) {
557 strcpy (name, "/dev/");
558 strcpy (&name[5], vgs->val[i].vg_name);
559 memcpy (uuid, vgs->val[i].vg_uuid, 32);
561 write_row (name, "vg",
562 NULL, NULL, (int64_t) vgs->val[i].vg_size, NULL, uuid);
566 guestfs_free_lvm_vg_list (vgs);
572 struct guestfs_lvm_pv_list *pvs;
575 pvs = guestfs_pvs_full (g);
579 for (i = 0; i < pvs->len; ++i) {
583 dev = canonical_device (pvs->val[i].pv_name);
585 memcpy (uuid, pvs->val[i].pv_uuid, 32);
587 write_row (dev, "pv",
588 NULL, NULL, (int64_t) pvs->val[i].pv_size, NULL, uuid);
593 guestfs_free_lvm_pv_list (pvs);
597 do_output_partitions (void)
602 parts = guestfs_list_partitions (g);
606 for (i = 0; parts[i] != NULL; ++i) {
607 char *dev, *parent_name = NULL;
610 dev = canonical_device (parts[i]);
612 if ((columns & COLUMN_SIZE)) {
613 size = guestfs_blockdev_getsize64 (g, parts[i]);
617 if ((columns & COLUMN_PARENT_NAME)) {
618 parent_name = guestfs_part_to_dev (g, parts[i]);
619 if (parent_name == NULL)
621 char *p = canonical_device (parent_name);
626 write_row (dev, "partition",
627 NULL, NULL, size, parent_name, NULL);
638 do_output_blockdevs (void)
643 devices = guestfs_list_devices (g);
647 for (i = 0; devices[i] != NULL; ++i) {
651 dev = canonical_device (devices[i]);
653 if ((columns & COLUMN_SIZE)) {
654 size = guestfs_blockdev_getsize64 (g, devices[i]);
659 write_row (dev, "device",
660 NULL, NULL, size, NULL, NULL);
669 /* /dev/vda1 -> /dev/sda. Returns a string which the caller must free. */
671 canonical_device (const char *dev)
673 char *ret = strdup (dev);
679 if (STRPREFIX (ret, "/dev/") &&
680 (ret[5] == 'h' || ret[5] == 'v') &&
682 c_isalpha (ret[7]) &&
683 (c_isdigit (ret[8]) || ret[8] == '\0'))
690 write_row (const char *name, const char *type,
691 const char *vfs_type, const char *vfs_label,
692 int64_t size, const char *parent_name, const char *uuid)
694 const char *strings[NR_COLUMNS];
696 char hum[LONGEST_HUMAN_READABLE];
699 if ((columns & COLUMN_NAME))
700 strings[len++] = name;
701 if ((columns & COLUMN_TYPE))
702 strings[len++] = type;
703 if ((columns & COLUMN_VFS_TYPE))
704 strings[len++] = vfs_type;
705 if ((columns & COLUMN_VFS_LABEL))
706 strings[len++] = vfs_label;
707 if ((columns & COLUMN_SIZE)) {
711 human_readable ((uintmax_t) size, hum,
712 human_round_to_nearest|human_autoscale|
713 human_base_1024|human_SI,
717 snprintf (num, sizeof num, "%" PRIi64, size);
718 strings[len++] = num;
722 strings[len++] = NULL;
724 if ((columns & COLUMN_PARENT_NAME))
725 strings[len++] = parent_name;
726 if ((columns & COLUMN_UUID))
727 strings[len++] = uuid;
728 assert (len <= NR_COLUMNS);
730 write_row_strings ((char **) strings, len);
733 static void add_row (char **strings, size_t len);
734 static void write_csv_field (const char *field);
737 write_row_strings (char **strings, size_t len)
740 /* Text mode. Because we want the columns to line up, we can't
741 * output directly, but instead need to save up the rows and
742 * output them at the end.
744 add_row (strings, len);
746 else { /* CSV mode: output it directly, quoted */
749 for (i = 0; i < len; ++i) {
752 if (strings[i] != NULL)
753 write_csv_field (strings[i]);
759 /* Function to quote CSV fields on output without requiring an
763 write_csv_field (const char *field)
766 int needs_quoting = 0;
768 len = strlen (field);
770 for (i = 0; i < len; ++i) {
771 if (field[i] == ' ' || field[i] == '"' ||
772 field[i] == '\n' || field[i] == ',') {
778 if (!needs_quoting) {
779 printf ("%s", field);
783 /* Quoting for CSV fields. */
785 for (i = 0; i < len; ++i) {
786 if (field[i] == '"') {
795 /* This code is only used in text mode (non-CSV output). */
796 static char ***rows = NULL;
797 static size_t nr_rows = 0;
798 static size_t max_width[NR_COLUMNS];
801 add_row (char **strings, size_t len)
806 assert (len <= NR_COLUMNS);
808 row = malloc (sizeof (char *) * len);
814 for (i = 0; i < len; ++i) {
816 row[i] = strdup (strings[i]);
817 if (row[i] == NULL) {
822 /* Keep a running total of the max width of each column. */
823 slen = strlen (strings[i]);
825 slen = 1; /* because "" is printed as "-" */
826 if (slen > max_width[i])
833 rows = realloc (rows, sizeof (char **) * (nr_rows + 1));
842 /* In text mode we saved up all the output so that we can print the
848 size_t i, j, k, len, space_btwn;
853 /* How much space between columns? Try 2 spaces between columns, but
854 * if that just pushes us over 72 columns, use 1 space.
858 for (j = 0; j < NR_COLUMNS; ++j)
859 i += max_width[j] + space_btwn;
863 for (i = 0; i < nr_rows; ++i) {
864 char **row = rows[i];
868 for (j = 0; j < NR_COLUMNS; ++j) {
869 /* Ignore columns which are completely empty. This also deals
870 * with the fact that we didn't remember the length of each row
873 if (max_width[j] == 0)
881 if (row[j] == NULL || STREQ (row[j], "")) {
885 printf ("%s", row[j]);
886 len = strlen (row[j]);
890 assert (len <= max_width[j]);
891 k = max_width[j] - len + space_btwn;