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. */
52 int keys_from_stdin = 0;
54 const char *libvirt_uri = NULL;
57 static int csv = 0; /* --csv */
58 static int human = 0; /* --human-readable|-h */
60 /* What is selected for output. */
61 #define OUTPUT_FILESYSTEMS 1
62 #define OUTPUT_FILESYSTEMS_EXTRA 2
63 #define OUTPUT_PARTITIONS 4
64 #define OUTPUT_BLOCKDEVS 8
68 #define OUTPUT_ALL INT_MAX
69 static int output = 0;
71 /* What columns to output. This is in display order. */
72 #define COLUMN_NAME 1 /* always shown */
74 #define COLUMN_VFS_TYPE 4 /* if --filesystems */
75 #define COLUMN_VFS_LABEL 8 /* if --filesystems */
76 #define COLUMN_SIZE 16 /* bytes, or human-readable if -h */
77 #define COLUMN_PARENT_NAME 32 /* only for partitions, LVs */
78 #define COLUMN_UUID 64 /* if --uuid */
82 static char *canonical_device (const char *dev);
83 static void do_output_title (void);
84 static void do_output (void);
85 static void do_output_end (void);
88 bad_cast (char const *s)
93 static void __attribute__((noreturn))
96 if (status != EXIT_SUCCESS)
97 fprintf (stderr, _("Try `%s --help' for more information.\n"),
101 _("%s: list filesystems, partitions, block devices, LVM in a VM\n"
102 "Copyright (C) 2010 Red Hat Inc.\n"
104 " %s [--options] -d domname\n"
105 " %s [--options] -a disk.img [-a disk.img ...]\n"
107 " -a|--add image Add image\n"
108 " --all Display everything\n"
109 " --blkdevs|--block-devices\n"
110 " Display block devices\n"
111 " -c|--connect uri Specify libvirt URI for -d option\n"
112 " --csv Output as Comma-Separated Values\n"
113 " -d|--domain guest Add disks from libvirt guest\n"
114 " --echo-keys Don't turn off echo for passphrases\n"
115 " --extra Display swap and data filesystems\n"
116 " --filesystems Display mountable filesystems\n"
117 " --format[=raw|..] Force disk format for -a option\n"
118 " -h|--human-readable Human-readable sizes in --long output\n"
119 " --help Display brief help\n"
120 " --keys-from-stdin Read passphrases from stdin\n"
121 " -l|--long Long output\n"
122 " --lvs|--logvols|--logical-volumes\n"
123 " Display LVM logical volumes\n"
124 " --no-title No title in --long output\n"
125 " --parts|--partitions Display partitions\n"
126 " --pvs|--physvols|--physical-volumes\n"
127 " Display LVM physical volumes\n"
128 " --uuid|--uuids Add UUIDs to --long output\n"
129 " -v|--verbose Verbose messages\n"
130 " -V|--version Display version and exit\n"
131 " --vgs|--volgroups|--volume-groups\n"
132 " Display LVM volume groups\n"
133 " -x Trace libguestfs API calls\n"
134 "For more information, see the manpage %s(1).\n"),
135 program_name, program_name, program_name,
142 main (int argc, char *argv[])
144 /* Set global program name that is not polluted with libtool artifacts. */
145 set_program_name (argv[0]);
147 setlocale (LC_ALL, "");
148 bindtextdomain (PACKAGE, LOCALEBASEDIR);
149 textdomain (PACKAGE);
151 enum { HELP_OPTION = CHAR_MAX + 1 };
153 static const char *options = "a:c:d:hlvVx";
154 static const struct option long_options[] = {
155 { "add", 1, 0, 'a' },
157 { "blkdevs", 0, 0, 0 },
158 { "block-devices", 0, 0, 0 },
159 { "connect", 1, 0, 'c' },
161 { "domain", 1, 0, 'd' },
162 { "echo-keys", 0, 0, 0 },
163 { "extra", 0, 0, 0 },
164 { "filesystems", 0, 0, 0 },
165 { "format", 2, 0, 0 },
166 { "help", 0, 0, HELP_OPTION },
167 { "human-readable", 0, 0, 'h' },
168 { "keys-from-stdin", 0, 0, 0 },
169 { "long", 0, 0, 'l' },
170 { "logical-volumes", 0, 0, 0 },
171 { "logvols", 0, 0, 0 },
173 { "no-title", 0, 0, 0 },
174 { "parts", 0, 0, 0 },
175 { "partitions", 0, 0, 0 },
176 { "physical-volumes", 0, 0, 0 },
177 { "physvols", 0, 0, 0 },
180 { "uuids", 0, 0, 0 },
181 { "verbose", 0, 0, 'v' },
182 { "version", 0, 0, 'V' },
184 { "volgroups", 0, 0, 0 },
185 { "volume-groups", 0, 0, 0 },
188 struct drv *drvs = NULL;
190 const char *format = NULL;
193 int no_title = 0; /* --no-title */
194 int long_mode = 0; /* --long|-l */
195 int uuid = 0; /* --uuid */
198 g = guestfs_create ();
200 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
204 argv[0] = bad_cast (program_name);
207 c = getopt_long (argc, argv, options, long_options, &option_index);
211 case 0: /* options which are long only */
212 if (STREQ (long_options[option_index].name, "keys-from-stdin")) {
214 } else if (STREQ (long_options[option_index].name, "echo-keys")) {
216 } else if (STREQ (long_options[option_index].name, "format")) {
217 if (!optarg || STREQ (optarg, ""))
221 } else if (STREQ (long_options[option_index].name, "all")) {
223 } else if (STREQ (long_options[option_index].name, "blkdevs") ||
224 STREQ (long_options[option_index].name, "block-devices")) {
225 output |= OUTPUT_BLOCKDEVS;
226 } else if (STREQ (long_options[option_index].name, "csv")) {
228 } else if (STREQ (long_options[option_index].name, "extra")) {
229 output |= OUTPUT_FILESYSTEMS;
230 output |= OUTPUT_FILESYSTEMS_EXTRA;
231 } else if (STREQ (long_options[option_index].name, "filesystems")) {
232 output |= OUTPUT_FILESYSTEMS;
233 } else if (STREQ (long_options[option_index].name, "logical-volumes") ||
234 STREQ (long_options[option_index].name, "logvols") ||
235 STREQ (long_options[option_index].name, "lvs")) {
236 output |= OUTPUT_LVS;
237 } else if (STREQ (long_options[option_index].name, "no-title")) {
239 } else if (STREQ (long_options[option_index].name, "parts") ||
240 STREQ (long_options[option_index].name, "partitions")) {
241 output |= OUTPUT_PARTITIONS;
242 } else if (STREQ (long_options[option_index].name, "physical-volumes") ||
243 STREQ (long_options[option_index].name, "physvols") ||
244 STREQ (long_options[option_index].name, "pvs")) {
245 output |= OUTPUT_PVS;
246 } else if (STREQ (long_options[option_index].name, "uuid") ||
247 STREQ (long_options[option_index].name, "uuids")) {
249 } else if (STREQ (long_options[option_index].name, "vgs") ||
250 STREQ (long_options[option_index].name, "volgroups") ||
251 STREQ (long_options[option_index].name, "volume-groups")) {
252 output |= OUTPUT_VGS;
254 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
255 program_name, long_options[option_index].name, option_index);
293 usage (EXIT_SUCCESS);
296 usage (EXIT_FAILURE);
300 /* These are really constants, but they have to be variables for the
301 * options parsing code. Assert here that they have known-good
304 assert (read_only == 1);
305 assert (inspector == 0);
308 /* Must be no extra arguments on the command line. */
310 usage (EXIT_FAILURE);
312 /* -h and --csv doesn't make sense. Spreadsheets will corrupt these
313 * fields. (RHBZ#600977).
316 fprintf (stderr, _("%s: you cannot use -h and --csv options together.\n"),
321 /* Nothing selected for output, means --filesystems is implied. */
323 output = OUTPUT_FILESYSTEMS;
325 /* What columns will be displayed? */
326 columns = COLUMN_NAME;
328 columns |= COLUMN_TYPE;
329 columns |= COLUMN_SIZE;
330 if ((output & OUTPUT_FILESYSTEMS)) {
331 columns |= COLUMN_VFS_TYPE;
332 columns |= COLUMN_VFS_LABEL;
334 if ((output & (OUTPUT_PARTITIONS|OUTPUT_LVS)))
335 columns |= COLUMN_PARENT_NAME;
337 columns |= COLUMN_UUID;
340 /* Display title by default only in long mode. */
345 /* User must have specified some drives. */
347 usage (EXIT_FAILURE);
350 add_drives (drvs, 'a');
352 if (guestfs_launch (g) == -1)
355 /* Free up data structures, no longer needed after this point. */
368 static void do_output_filesystems (void);
369 static void do_output_lvs (void);
370 static void do_output_vgs (void);
371 static void do_output_pvs (void);
372 static void do_output_partitions (void);
373 static void do_output_blockdevs (void);
374 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);
375 static void write_row_strings (char **strings, size_t len);
378 do_output_title (void)
380 const char *headings[NR_COLUMNS];
383 /* NB. These strings are not localized and must not contain spaces. */
384 if ((columns & COLUMN_NAME))
385 headings[len++] = "Name";
386 if ((columns & COLUMN_TYPE))
387 headings[len++] = "Type";
388 if ((columns & COLUMN_VFS_TYPE))
389 headings[len++] = "VFS";
390 if ((columns & COLUMN_VFS_LABEL))
391 headings[len++] = "Label";
392 if ((columns & COLUMN_SIZE))
393 headings[len++] = "Size";
394 if ((columns & COLUMN_PARENT_NAME))
395 headings[len++] = "Parent";
396 if ((columns & COLUMN_UUID))
397 headings[len++] = "UUID";
398 assert (len <= NR_COLUMNS);
400 write_row_strings ((char **) headings, len);
406 /* The ordering here is trying to be most specific -> least specific,
407 * although that is not required or guaranteed.
409 if ((output & OUTPUT_FILESYSTEMS))
410 do_output_filesystems ();
412 if ((output & OUTPUT_LVS))
415 if ((output & OUTPUT_VGS))
418 if ((output & OUTPUT_PVS))
421 if ((output & OUTPUT_PARTITIONS))
422 do_output_partitions ();
424 if ((output & OUTPUT_BLOCKDEVS))
425 do_output_blockdevs ();
429 do_output_filesystems (void)
434 fses = guestfs_list_filesystems (g);
438 for (i = 0; fses[i] != NULL; i += 2) {
439 char *dev, *vfs_label = NULL, *vfs_uuid = NULL;
442 /* Skip swap and unknown, unless --extra flag was given. */
443 if (!(output & OUTPUT_FILESYSTEMS_EXTRA) &&
444 (STREQ (fses[i+1], "swap") || STREQ (fses[i+1], "unknown")))
447 dev = canonical_device (fses[i]);
449 /* Only bother to look these up if we will be displaying them,
450 * otherwise pass them as NULL.
452 if ((columns & COLUMN_VFS_LABEL)) {
453 DISABLE_GUESTFS_ERRORS_FOR (
454 vfs_label = guestfs_vfs_label (g, fses[i]);
456 if (vfs_label == NULL) {
457 vfs_label = strdup ("");
464 if ((columns & COLUMN_UUID)) {
465 DISABLE_GUESTFS_ERRORS_FOR (
466 vfs_uuid = guestfs_vfs_uuid (g, fses[i]);
468 if (vfs_uuid == NULL) {
469 vfs_uuid = strdup ("");
476 if ((columns & COLUMN_SIZE)) {
477 size = guestfs_blockdev_getsize64 (g, fses[i]);
482 write_row (dev, "filesystem",
483 fses[i+1], vfs_label, size, NULL, vfs_uuid);
501 lvs = guestfs_lvs (g);
505 for (i = 0; lvs[i] != NULL; ++i) {
506 char *uuid = NULL, *parent_name = NULL;
509 if ((columns & COLUMN_SIZE)) {
510 size = guestfs_blockdev_getsize64 (g, lvs[i]);
514 if ((columns & COLUMN_UUID)) {
515 uuid = guestfs_lvuuid (g, lvs[i]);
519 if ((columns & COLUMN_PARENT_NAME)) {
520 parent_name = strdup (lvs[i]);
521 if (parent_name == NULL) {
525 char *p = strrchr (parent_name, '/');
530 write_row (lvs[i], "lv",
531 NULL, NULL, size, parent_name, uuid);
544 struct guestfs_lvm_vg_list *vgs;
547 vgs = guestfs_vgs_full (g);
551 for (i = 0; i < vgs->len; ++i) {
555 strcpy (name, "/dev/");
556 strcpy (&name[5], vgs->val[i].vg_name);
557 memcpy (uuid, vgs->val[i].vg_uuid, 32);
559 write_row (name, "vg",
560 NULL, NULL, (int64_t) vgs->val[i].vg_size, NULL, uuid);
564 guestfs_free_lvm_vg_list (vgs);
570 struct guestfs_lvm_pv_list *pvs;
573 pvs = guestfs_pvs_full (g);
577 for (i = 0; i < pvs->len; ++i) {
581 dev = canonical_device (pvs->val[i].pv_name);
583 memcpy (uuid, pvs->val[i].pv_uuid, 32);
585 write_row (dev, "pv",
586 NULL, NULL, (int64_t) pvs->val[i].pv_size, NULL, uuid);
591 guestfs_free_lvm_pv_list (pvs);
595 do_output_partitions (void)
600 parts = guestfs_list_partitions (g);
604 for (i = 0; parts[i] != NULL; ++i) {
605 char *dev, *parent_name = NULL;
608 dev = canonical_device (parts[i]);
610 if ((columns & COLUMN_SIZE)) {
611 size = guestfs_blockdev_getsize64 (g, parts[i]);
615 if ((columns & COLUMN_PARENT_NAME)) {
616 parent_name = guestfs_part_to_dev (g, parts[i]);
617 if (parent_name == NULL)
619 char *p = canonical_device (parent_name);
624 write_row (dev, "partition",
625 NULL, NULL, size, parent_name, NULL);
636 do_output_blockdevs (void)
641 devices = guestfs_list_devices (g);
645 for (i = 0; devices[i] != NULL; ++i) {
649 dev = canonical_device (devices[i]);
651 if ((columns & COLUMN_SIZE)) {
652 size = guestfs_blockdev_getsize64 (g, devices[i]);
657 write_row (dev, "device",
658 NULL, NULL, size, NULL, NULL);
667 /* /dev/vda1 -> /dev/sda. Returns a string which the caller must free. */
669 canonical_device (const char *dev)
671 char *ret = strdup (dev);
677 if (STRPREFIX (ret, "/dev/") &&
678 (ret[5] == 'h' || ret[5] == 'v') &&
680 c_isalpha (ret[7]) &&
681 (c_isdigit (ret[8]) || ret[8] == '\0'))
688 write_row (const char *name, const char *type,
689 const char *vfs_type, const char *vfs_label,
690 int64_t size, const char *parent_name, const char *uuid)
692 const char *strings[NR_COLUMNS];
694 char hum[LONGEST_HUMAN_READABLE];
697 if ((columns & COLUMN_NAME))
698 strings[len++] = name;
699 if ((columns & COLUMN_TYPE))
700 strings[len++] = type;
701 if ((columns & COLUMN_VFS_TYPE))
702 strings[len++] = vfs_type;
703 if ((columns & COLUMN_VFS_LABEL))
704 strings[len++] = vfs_label;
705 if ((columns & COLUMN_SIZE)) {
709 human_readable ((uintmax_t) size, hum,
710 human_round_to_nearest|human_autoscale|
711 human_base_1024|human_SI,
715 snprintf (num, sizeof num, "%" PRIi64, size);
716 strings[len++] = num;
720 strings[len++] = NULL;
722 if ((columns & COLUMN_PARENT_NAME))
723 strings[len++] = parent_name;
724 if ((columns & COLUMN_UUID))
725 strings[len++] = uuid;
726 assert (len <= NR_COLUMNS);
728 write_row_strings ((char **) strings, len);
731 static void add_row (char **strings, size_t len);
732 static void write_csv_field (const char *field);
735 write_row_strings (char **strings, size_t len)
738 /* Text mode. Because we want the columns to line up, we can't
739 * output directly, but instead need to save up the rows and
740 * output them at the end.
742 add_row (strings, len);
744 else { /* CSV mode: output it directly, quoted */
747 for (i = 0; i < len; ++i) {
750 if (strings[i] != NULL)
751 write_csv_field (strings[i]);
757 /* Function to quote CSV fields on output without requiring an
761 write_csv_field (const char *field)
764 int needs_quoting = 0;
766 len = strlen (field);
768 for (i = 0; i < len; ++i) {
769 if (field[i] == ' ' || field[i] == '"' ||
770 field[i] == '\n' || field[i] == ',') {
776 if (!needs_quoting) {
777 printf ("%s", field);
781 /* Quoting for CSV fields. */
783 for (i = 0; i < len; ++i) {
784 if (field[i] == '"') {
793 /* This code is only used in text mode (non-CSV output). */
794 static char ***rows = NULL;
795 static size_t nr_rows = 0;
796 static size_t max_width[NR_COLUMNS];
799 add_row (char **strings, size_t len)
804 assert (len <= NR_COLUMNS);
806 row = malloc (sizeof (char *) * len);
812 for (i = 0; i < len; ++i) {
814 row[i] = strdup (strings[i]);
815 if (row[i] == NULL) {
820 /* Keep a running total of the max width of each column. */
821 slen = strlen (strings[i]);
823 slen = 1; /* because "" is printed as "-" */
824 if (slen > max_width[i])
831 rows = realloc (rows, sizeof (char **) * (nr_rows + 1));
840 /* In text mode we saved up all the output so that we can print the
846 size_t i, j, k, len, space_btwn;
851 /* How much space between columns? Try 2 spaces between columns, but
852 * if that just pushes us over 72 columns, use 1 space.
856 for (j = 0; j < NR_COLUMNS; ++j)
857 i += max_width[j] + space_btwn;
861 for (i = 0; i < nr_rows; ++i) {
862 char **row = rows[i];
866 for (j = 0; j < NR_COLUMNS; ++j) {
867 /* Ignore columns which are completely empty. This also deals
868 * with the fact that we didn't remember the length of each row
871 if (max_width[j] == 0)
879 if (row[j] == NULL || STREQ (row[j], "")) {
883 printf ("%s", row[j]);
884 len = strlen (row[j]);
888 assert (len <= max_width[j]);
889 k = max_width[j] - len + space_btwn;