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 /* These globals are shared with options.c. */
41 int keys_from_stdin = 0;
43 const char *libvirt_uri = NULL;
46 static int csv = 0; /* --csv */
47 static int human = 0; /* --human-readable|-h */
49 /* What is selected for output. */
50 #define OUTPUT_FILESYSTEMS 1
51 #define OUTPUT_FILESYSTEMS_EXTRA 2
52 #define OUTPUT_PARTITIONS 4
53 #define OUTPUT_BLOCKDEVS 8
57 #define OUTPUT_ALL INT_MAX
58 static int output = 0;
60 /* What columns to output. This is in display order. */
61 #define COLUMN_NAME 1 /* always shown */
63 #define COLUMN_VFS_TYPE 4 /* if --filesystems */
64 #define COLUMN_VFS_LABEL 8 /* if --filesystems */
65 #define COLUMN_SIZE 16 /* bytes, or human-readable if -h */
66 #define COLUMN_PARENT_NAME 32 /* only for partitions, LVs */
67 #define COLUMN_UUID 64 /* if --uuid */
71 static char *canonical_device (const char *dev);
72 static void do_output_title (void);
73 static void do_output (void);
74 static void do_output_end (void);
77 bad_cast (char const *s)
82 static void __attribute__((noreturn))
85 if (status != EXIT_SUCCESS)
86 fprintf (stderr, _("Try `%s --help' for more information.\n"),
90 _("%s: list filesystems, partitions, block devices, LVM in a VM\n"
91 "Copyright (C) 2010 Red Hat Inc.\n"
93 " %s [--options] -d domname\n"
94 " %s [--options] -a disk.img [-a disk.img ...]\n"
96 " -a|--add image Add image\n"
97 " --all Display everything\n"
98 " --blkdevs|--block-devices\n"
99 " Display block devices\n"
100 " -c|--connect uri Specify libvirt URI for -d option\n"
101 " --csv Output as Comma-Separated Values\n"
102 " -d|--domain guest Add disks from libvirt guest\n"
103 " --echo-keys Don't turn off echo for passphrases\n"
104 " --extra Display swap and data filesystems\n"
105 " --filesystems Display mountable filesystems\n"
106 " --format[=raw|..] Force disk format for -a option\n"
107 " -h|--human-readable Human-readable sizes in --long output\n"
108 " --help Display brief help\n"
109 " --keys-from-stdin Read passphrases from stdin\n"
110 " -l|--long Long output\n"
111 " --lvs|--logvols|--logical-volumes\n"
112 " Display LVM logical volumes\n"
113 " --no-title No title in --long output\n"
114 " --parts|--partitions Display partitions\n"
115 " --pvs|--physvols|--physical-volumes\n"
116 " Display LVM physical volumes\n"
117 " --uuid|--uuids Add UUIDs to --long output\n"
118 " -v|--verbose Verbose messages\n"
119 " -V|--version Display version and exit\n"
120 " --vgs|--volgroups|--volume-groups\n"
121 " Display LVM volume groups\n"
122 " -x Trace libguestfs API calls\n"
123 "For more information, see the manpage %s(1).\n"),
124 program_name, program_name, program_name,
131 main (int argc, char *argv[])
133 /* Set global program name that is not polluted with libtool artifacts. */
134 set_program_name (argv[0]);
136 setlocale (LC_ALL, "");
137 bindtextdomain (PACKAGE, LOCALEBASEDIR);
138 textdomain (PACKAGE);
140 enum { HELP_OPTION = CHAR_MAX + 1 };
142 static const char *options = "a:c:d:hlvVx";
143 static const struct option long_options[] = {
144 { "add", 1, 0, 'a' },
146 { "blkdevs", 0, 0, 0 },
147 { "block-devices", 0, 0, 0 },
148 { "connect", 1, 0, 'c' },
150 { "domain", 1, 0, 'd' },
151 { "echo-keys", 0, 0, 0 },
152 { "extra", 0, 0, 0 },
153 { "filesystems", 0, 0, 0 },
154 { "format", 2, 0, 0 },
155 { "help", 0, 0, HELP_OPTION },
156 { "human-readable", 0, 0, 'h' },
157 { "keys-from-stdin", 0, 0, 0 },
158 { "long", 0, 0, 'l' },
159 { "logical-volumes", 0, 0, 0 },
160 { "logvols", 0, 0, 0 },
162 { "no-title", 0, 0, 0 },
163 { "parts", 0, 0, 0 },
164 { "partitions", 0, 0, 0 },
165 { "physical-volumes", 0, 0, 0 },
166 { "physvols", 0, 0, 0 },
169 { "uuids", 0, 0, 0 },
170 { "verbose", 0, 0, 'v' },
171 { "version", 0, 0, 'V' },
173 { "volgroups", 0, 0, 0 },
174 { "volume-groups", 0, 0, 0 },
177 struct drv *drvs = NULL;
179 const char *format = NULL;
182 int no_title = 0; /* --no-title */
183 int long_mode = 0; /* --long|-l */
184 int uuid = 0; /* --uuid */
187 g = guestfs_create ();
189 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
193 argv[0] = bad_cast (program_name);
196 c = getopt_long (argc, argv, options, long_options, &option_index);
200 case 0: /* options which are long only */
201 if (STREQ (long_options[option_index].name, "keys-from-stdin")) {
203 } else if (STREQ (long_options[option_index].name, "echo-keys")) {
205 } else if (STREQ (long_options[option_index].name, "format")) {
206 if (!optarg || STREQ (optarg, ""))
210 } else if (STREQ (long_options[option_index].name, "all")) {
212 } else if (STREQ (long_options[option_index].name, "blkdevs") ||
213 STREQ (long_options[option_index].name, "block-devices")) {
214 output |= OUTPUT_BLOCKDEVS;
215 } else if (STREQ (long_options[option_index].name, "csv")) {
217 } else if (STREQ (long_options[option_index].name, "extra")) {
218 output |= OUTPUT_FILESYSTEMS;
219 output |= OUTPUT_FILESYSTEMS_EXTRA;
220 } else if (STREQ (long_options[option_index].name, "filesystems")) {
221 output |= OUTPUT_FILESYSTEMS;
222 } else if (STREQ (long_options[option_index].name, "logical-volumes") ||
223 STREQ (long_options[option_index].name, "logvols") ||
224 STREQ (long_options[option_index].name, "lvs")) {
225 output |= OUTPUT_LVS;
226 } else if (STREQ (long_options[option_index].name, "no-title")) {
228 } else if (STREQ (long_options[option_index].name, "parts") ||
229 STREQ (long_options[option_index].name, "partitions")) {
230 output |= OUTPUT_PARTITIONS;
231 } else if (STREQ (long_options[option_index].name, "physical-volumes") ||
232 STREQ (long_options[option_index].name, "physvols") ||
233 STREQ (long_options[option_index].name, "pvs")) {
234 output |= OUTPUT_PVS;
235 } else if (STREQ (long_options[option_index].name, "uuid") ||
236 STREQ (long_options[option_index].name, "uuids")) {
238 } else if (STREQ (long_options[option_index].name, "vgs") ||
239 STREQ (long_options[option_index].name, "volgroups") ||
240 STREQ (long_options[option_index].name, "volume-groups")) {
241 output |= OUTPUT_VGS;
243 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
244 program_name, long_options[option_index].name, option_index);
282 usage (EXIT_SUCCESS);
285 usage (EXIT_FAILURE);
289 /* These are really constants, but they have to be variables for the
290 * options parsing code. Assert here that they have known-good
293 assert (read_only == 1);
294 assert (inspector == 0);
296 /* Must be no extra arguments on the command line. */
298 usage (EXIT_FAILURE);
300 /* -h and --csv doesn't make sense. Spreadsheets will corrupt these
301 * fields. (RHBZ#600977).
304 fprintf (stderr, _("%s: you cannot use -h and --csv options together.\n"),
309 /* Nothing selected for output, means --filesystems is implied. */
311 output = OUTPUT_FILESYSTEMS;
313 /* What columns will be displayed? */
314 columns = COLUMN_NAME;
316 columns |= COLUMN_TYPE;
317 columns |= COLUMN_SIZE;
318 if ((output & OUTPUT_FILESYSTEMS)) {
319 columns |= COLUMN_VFS_TYPE;
320 columns |= COLUMN_VFS_LABEL;
322 if ((output & (OUTPUT_PARTITIONS|OUTPUT_LVS)))
323 columns |= COLUMN_PARENT_NAME;
325 columns |= COLUMN_UUID;
328 /* Display title by default only in long mode. */
333 /* User must have specified some drives. */
335 usage (EXIT_FAILURE);
338 add_drives (drvs, 'a');
340 if (guestfs_launch (g) == -1)
343 /* Free up data structures, no longer needed after this point. */
356 static void do_output_filesystems (void);
357 static void do_output_lvs (void);
358 static void do_output_vgs (void);
359 static void do_output_pvs (void);
360 static void do_output_partitions (void);
361 static void do_output_blockdevs (void);
362 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);
363 static void write_row_strings (char **strings, size_t len);
366 do_output_title (void)
368 const char *headings[NR_COLUMNS];
371 /* NB. These strings are not localized and must not contain spaces. */
372 if ((columns & COLUMN_NAME))
373 headings[len++] = "Name";
374 if ((columns & COLUMN_TYPE))
375 headings[len++] = "Type";
376 if ((columns & COLUMN_VFS_TYPE))
377 headings[len++] = "VFS";
378 if ((columns & COLUMN_VFS_LABEL))
379 headings[len++] = "Label";
380 if ((columns & COLUMN_SIZE))
381 headings[len++] = "Size";
382 if ((columns & COLUMN_PARENT_NAME))
383 headings[len++] = "Parent";
384 if ((columns & COLUMN_UUID))
385 headings[len++] = "UUID";
386 assert (len <= NR_COLUMNS);
388 write_row_strings ((char **) headings, len);
394 /* The ordering here is trying to be most specific -> least specific,
395 * although that is not required or guaranteed.
397 if ((output & OUTPUT_FILESYSTEMS))
398 do_output_filesystems ();
400 if ((output & OUTPUT_LVS))
403 if ((output & OUTPUT_VGS))
406 if ((output & OUTPUT_PVS))
409 if ((output & OUTPUT_PARTITIONS))
410 do_output_partitions ();
412 if ((output & OUTPUT_BLOCKDEVS))
413 do_output_blockdevs ();
417 do_output_filesystems (void)
422 fses = guestfs_list_filesystems (g);
426 for (i = 0; fses[i] != NULL; i += 2) {
427 char *dev, *vfs_label = NULL, *vfs_uuid = NULL;
430 /* Skip swap and unknown, unless --extra flag was given. */
431 if (!(output & OUTPUT_FILESYSTEMS_EXTRA) &&
432 (STREQ (fses[i+1], "swap") || STREQ (fses[i+1], "unknown")))
435 dev = canonical_device (fses[i]);
437 /* Only bother to look these up if we will be displaying them,
438 * otherwise pass them as NULL.
440 if ((columns & COLUMN_VFS_LABEL)) {
441 vfs_label = guestfs_vfs_label (g, fses[i]);
442 if (vfs_label == NULL)
445 if ((columns & COLUMN_UUID)) {
446 vfs_uuid = guestfs_vfs_uuid (g, fses[i]);
447 if (vfs_uuid == NULL)
450 if ((columns & COLUMN_SIZE)) {
451 size = guestfs_blockdev_getsize64 (g, fses[i]);
456 write_row (dev, "filesystem",
457 fses[i+1], vfs_label, size, NULL, vfs_uuid);
475 lvs = guestfs_lvs (g);
479 for (i = 0; lvs[i] != NULL; ++i) {
480 char *uuid = NULL, *parent_name = NULL;
483 if ((columns & COLUMN_SIZE)) {
484 size = guestfs_blockdev_getsize64 (g, lvs[i]);
488 if ((columns & COLUMN_UUID)) {
489 uuid = guestfs_lvuuid (g, lvs[i]);
493 if ((columns & COLUMN_PARENT_NAME)) {
494 parent_name = strdup (lvs[i]);
495 if (parent_name == NULL) {
499 char *p = strrchr (parent_name, '/');
504 write_row (lvs[i], "lv",
505 NULL, NULL, size, parent_name, uuid);
518 struct guestfs_lvm_vg_list *vgs;
521 vgs = guestfs_vgs_full (g);
525 for (i = 0; i < vgs->len; ++i) {
529 strcpy (name, "/dev/");
530 strcpy (&name[5], vgs->val[i].vg_name);
531 memcpy (uuid, vgs->val[i].vg_uuid, 32);
533 write_row (name, "vg",
534 NULL, NULL, (int64_t) vgs->val[i].vg_size, NULL, uuid);
538 guestfs_free_lvm_vg_list (vgs);
544 struct guestfs_lvm_pv_list *pvs;
547 pvs = guestfs_pvs_full (g);
551 for (i = 0; i < pvs->len; ++i) {
555 dev = canonical_device (pvs->val[i].pv_name);
557 memcpy (uuid, pvs->val[i].pv_uuid, 32);
559 write_row (dev, "pv",
560 NULL, NULL, (int64_t) pvs->val[i].pv_size, NULL, uuid);
565 guestfs_free_lvm_pv_list (pvs);
569 do_output_partitions (void)
574 parts = guestfs_list_partitions (g);
578 for (i = 0; parts[i] != NULL; ++i) {
579 char *dev, *parent_name = NULL;
582 dev = canonical_device (parts[i]);
584 if ((columns & COLUMN_SIZE)) {
585 size = guestfs_blockdev_getsize64 (g, parts[i]);
589 if ((columns & COLUMN_PARENT_NAME)) {
590 parent_name = guestfs_part_to_dev (g, parts[i]);
591 if (parent_name == NULL)
593 char *p = canonical_device (parent_name);
598 write_row (dev, "partition",
599 NULL, NULL, size, parent_name, NULL);
610 do_output_blockdevs (void)
615 devices = guestfs_list_devices (g);
619 for (i = 0; devices[i] != NULL; ++i) {
623 dev = canonical_device (devices[i]);
625 if ((columns & COLUMN_SIZE)) {
626 size = guestfs_blockdev_getsize64 (g, devices[i]);
631 write_row (dev, "device",
632 NULL, NULL, size, NULL, NULL);
641 /* /dev/vda1 -> /dev/sda. Returns a string which the caller must free. */
643 canonical_device (const char *dev)
645 char *ret = strdup (dev);
651 if (STRPREFIX (ret, "/dev/") &&
652 (ret[5] == 'h' || ret[5] == 'v') &&
654 c_isalpha (ret[7]) &&
655 (c_isdigit (ret[8]) || ret[8] == '\0'))
662 write_row (const char *name, const char *type,
663 const char *vfs_type, const char *vfs_label,
664 int64_t size, const char *parent_name, const char *uuid)
666 const char *strings[NR_COLUMNS];
668 char hum[LONGEST_HUMAN_READABLE];
671 if ((columns & COLUMN_NAME))
672 strings[len++] = name;
673 if ((columns & COLUMN_TYPE))
674 strings[len++] = type;
675 if ((columns & COLUMN_VFS_TYPE))
676 strings[len++] = vfs_type;
677 if ((columns & COLUMN_VFS_LABEL))
678 strings[len++] = vfs_label;
679 if ((columns & COLUMN_SIZE)) {
683 human_readable ((uintmax_t) size, hum,
684 human_round_to_nearest|human_autoscale|
685 human_base_1024|human_SI,
689 snprintf (num, sizeof num, "%" PRIi64, size);
690 strings[len++] = num;
694 strings[len++] = NULL;
696 if ((columns & COLUMN_PARENT_NAME))
697 strings[len++] = parent_name;
698 if ((columns & COLUMN_UUID))
699 strings[len++] = uuid;
700 assert (len <= NR_COLUMNS);
702 write_row_strings ((char **) strings, len);
705 static void add_row (char **strings, size_t len);
706 static void write_csv_field (const char *field);
709 write_row_strings (char **strings, size_t len)
712 /* Text mode. Because we want the columns to line up, we can't
713 * output directly, but instead need to save up the rows and
714 * output them at the end.
716 add_row (strings, len);
718 else { /* CSV mode: output it directly, quoted */
721 for (i = 0; i < len; ++i) {
724 if (strings[i] != NULL)
725 write_csv_field (strings[i]);
731 /* Function to quote CSV fields on output without requiring an
735 write_csv_field (const char *field)
738 int needs_quoting = 0;
740 len = strlen (field);
742 for (i = 0; i < len; ++i) {
743 if (field[i] == ' ' || field[i] == '"' ||
744 field[i] == '\n' || field[i] == ',') {
750 if (!needs_quoting) {
751 printf ("%s", field);
755 /* Quoting for CSV fields. */
757 for (i = 0; i < len; ++i) {
758 if (field[i] == '"') {
767 /* This code is only used in text mode (non-CSV output). */
768 static char ***rows = NULL;
769 static size_t nr_rows = 0;
770 static size_t max_width[NR_COLUMNS];
773 add_row (char **strings, size_t len)
778 assert (len <= NR_COLUMNS);
780 row = malloc (sizeof (char *) * len);
786 for (i = 0; i < len; ++i) {
788 row[i] = strdup (strings[i]);
789 if (row[i] == NULL) {
794 /* Keep a running total of the max width of each column. */
795 slen = strlen (strings[i]);
797 slen = 1; /* because "" is printed as "-" */
798 if (slen > max_width[i])
805 rows = realloc (rows, sizeof (char **) * (nr_rows + 1));
814 /* In text mode we saved up all the output so that we can print the
820 size_t i, j, k, len, space_btwn;
825 /* How much space between columns? Try 2 spaces between columns, but
826 * if that just pushes us over 72 columns, use 1 space.
830 for (j = 0; j < NR_COLUMNS; ++j)
831 i += max_width[j] + space_btwn;
835 for (i = 0; i < nr_rows; ++i) {
836 char **row = rows[i];
840 for (j = 0; j < NR_COLUMNS; ++j) {
841 /* Ignore columns which are completely empty. This also deals
842 * with the fact that we didn't remember the length of each row
845 if (max_width[j] == 0)
853 if (row[j] == NULL || STREQ (row[j], "")) {
857 printf ("%s", row[j]);
858 len = strlen (row[j]);
862 assert (len <= max_width[j]);
863 k = max_width[j] - len + space_btwn;