out of tree build: erlang
[libguestfs.git] / cat / virt-filesystems.c
index 580710d..de7a243 100644 (file)
@@ -1,5 +1,5 @@
 /* virt-filesystems
- * Copyright (C) 2010 Red Hat Inc.
+ * Copyright (C) 2010-2011 Red Hat Inc.
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -13,7 +13,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
 #include <config.h>
 #include <inttypes.h>
 #include <unistd.h>
 #include <getopt.h>
+#include <locale.h>
 #include <assert.h>
+#include <string.h>
+#include <libintl.h>
 
 #include "c-ctype.h"
 #include "human.h"
 #include "guestfs.h"
 #include "options.h"
 
+#define DISABLE_GUESTFS_ERRORS_FOR(stmt) do {                           \
+    guestfs_error_handler_cb old_error_cb;                              \
+    void *old_error_data;                                               \
+    old_error_cb = guestfs_get_error_handler (g, &old_error_data);      \
+    guestfs_set_error_handler (g, NULL, NULL);                          \
+    stmt;                                                               \
+    guestfs_set_error_handler (g, old_error_cb, old_error_data);        \
+  } while (0)
+
 /* These globals are shared with options.c. */
 guestfs_h *g;
 
 int read_only = 1;
+int live = 0;
 int verbose = 0;
 int keys_from_stdin = 0;
 int echo_keys = 0;
@@ -62,10 +75,11 @@ static int output = 0;
 #define COLUMN_TYPE               2
 #define COLUMN_VFS_TYPE           4 /* if --filesystems */
 #define COLUMN_VFS_LABEL          8 /* if --filesystems */
-#define COLUMN_SIZE              16 /* bytes, or human-readable if -h */
-#define COLUMN_PARENT_NAME       32 /* only for partitions, LVs */
-#define COLUMN_UUID              64 /* if --uuid */
-#define NR_COLUMNS                7
+#define COLUMN_MBR               16
+#define COLUMN_SIZE              32 /* bytes, or human-readable if -h */
+#define COLUMN_PARENT_NAME       64 /* only for partitions, LVs */
+#define COLUMN_UUID             128 /* if --uuid */
+#define NR_COLUMNS                8
 static int columns;
 
 static char *canonical_device (const char *dev);
@@ -90,8 +104,8 @@ usage (int status)
            _("%s: list filesystems, partitions, block devices, LVM in a VM\n"
              "Copyright (C) 2010 Red Hat Inc.\n"
              "Usage:\n"
-             "  %s [--options] -d domname file\n"
-             "  %s [--options] -a disk.img [-a disk.img ...] file\n"
+             "  %s [--options] -d domname\n"
+             "  %s [--options] -a disk.img [-a disk.img ...]\n"
              "Options:\n"
              "  -a|--add image       Add image\n"
              "  --all                Display everything\n"
@@ -292,6 +306,7 @@ main (int argc, char *argv[])
    */
   assert (read_only == 1);
   assert (inspector == 0);
+  assert (live == 0);
 
   /* Must be no extra arguments on the command line. */
   if (optind != argc)
@@ -321,6 +336,8 @@ main (int argc, char *argv[])
     }
     if ((output & (OUTPUT_PARTITIONS|OUTPUT_LVS)))
       columns |= COLUMN_PARENT_NAME;
+    if ((output & OUTPUT_PARTITIONS))
+      columns |= COLUMN_MBR;
     if (uuid)
       columns |= COLUMN_UUID;
   }
@@ -359,7 +376,7 @@ static void do_output_vgs (void);
 static void do_output_pvs (void);
 static void do_output_partitions (void);
 static void do_output_blockdevs (void);
-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);
+static void write_row (const char *name, const char *type, const char *vfs_type, const char *vfs_label, int mbr_id, int64_t size, const char *parent_name, const char *uuid);
 static void write_row_strings (char **strings, size_t len);
 
 static void
@@ -377,6 +394,8 @@ do_output_title (void)
     headings[len++] = "VFS";
   if ((columns & COLUMN_VFS_LABEL))
     headings[len++] = "Label";
+  if ((columns & COLUMN_MBR))
+    headings[len++] = "MBR";
   if ((columns & COLUMN_SIZE))
     headings[len++] = "Size";
   if ((columns & COLUMN_PARENT_NAME))
@@ -438,14 +457,28 @@ do_output_filesystems (void)
      * otherwise pass them as NULL.
      */
     if ((columns & COLUMN_VFS_LABEL)) {
-      vfs_label = guestfs_vfs_label (g, fses[i]);
-      if (vfs_label == NULL)
-        exit (EXIT_FAILURE);
+      DISABLE_GUESTFS_ERRORS_FOR (
+        vfs_label = guestfs_vfs_label (g, fses[i]);
+      );
+      if (vfs_label == NULL) {
+        vfs_label = strdup ("");
+        if (!vfs_label) {
+          perror ("strdup");
+          exit (EXIT_FAILURE);
+        }
+      }
     }
     if ((columns & COLUMN_UUID)) {
-      vfs_uuid = guestfs_vfs_uuid (g, fses[i]);
-      if (vfs_uuid == NULL)
-        exit (EXIT_FAILURE);
+      DISABLE_GUESTFS_ERRORS_FOR (
+        vfs_uuid = guestfs_vfs_uuid (g, fses[i]);
+      );
+      if (vfs_uuid == NULL) {
+        vfs_uuid = strdup ("");
+        if (!vfs_uuid) {
+          perror ("strdup");
+          exit (EXIT_FAILURE);
+        }
+      }
     }
     if ((columns & COLUMN_SIZE)) {
       size = guestfs_blockdev_getsize64 (g, fses[i]);
@@ -454,7 +487,7 @@ do_output_filesystems (void)
     }
 
     write_row (dev, "filesystem",
-               fses[i+1], vfs_label, size, NULL, vfs_uuid);
+               fses[i+1], vfs_label, -1, size, NULL, vfs_uuid);
 
     free (dev);
     free (vfs_label);
@@ -502,7 +535,7 @@ do_output_lvs (void)
     }
 
     write_row (lvs[i], "lv",
-               NULL, NULL, size, parent_name, uuid);
+               NULL, NULL, -1, size, parent_name, uuid);
 
     free (uuid);
     free (parent_name);
@@ -531,7 +564,7 @@ do_output_vgs (void)
     memcpy (uuid, vgs->val[i].vg_uuid, 32);
     uuid[32] = '\0';
     write_row (name, "vg",
-               NULL, NULL, (int64_t) vgs->val[i].vg_size, NULL, uuid);
+               NULL, NULL, -1, (int64_t) vgs->val[i].vg_size, NULL, uuid);
 
   }
 
@@ -557,7 +590,7 @@ do_output_pvs (void)
     memcpy (uuid, pvs->val[i].pv_uuid, 32);
     uuid[32] = '\0';
     write_row (dev, "pv",
-               NULL, NULL, (int64_t) pvs->val[i].pv_size, NULL, uuid);
+               NULL, NULL, -1, (int64_t) pvs->val[i].pv_size, NULL, uuid);
 
     free (dev);
   }
@@ -565,6 +598,32 @@ do_output_pvs (void)
   guestfs_free_lvm_pv_list (pvs);
 }
 
+static int
+get_mbr_id (const char *dev, const char *parent_name)
+{
+  char *parttype = NULL;
+  int mbr_id = -1, partnum;
+
+  DISABLE_GUESTFS_ERRORS_FOR (
+    parttype = guestfs_part_get_parttype (g, parent_name);
+  );
+
+  if (parttype && STREQ (parttype, "msdos")) {
+    DISABLE_GUESTFS_ERRORS_FOR (
+      partnum = guestfs_part_to_partnum (g, dev);
+    );
+    if (partnum >= 0) {
+      DISABLE_GUESTFS_ERRORS_FOR (
+        mbr_id = guestfs_part_get_mbr_id (g, parent_name, partnum);
+      );
+    }
+  }
+
+  free (parttype);
+
+  return mbr_id;
+}
+
 static void
 do_output_partitions (void)
 {
@@ -578,6 +637,7 @@ do_output_partitions (void)
   for (i = 0; parts[i] != NULL; ++i) {
     char *dev, *parent_name = NULL;
     int64_t size = -1;
+    int mbr_id = -1;
 
     dev = canonical_device (parts[i]);
 
@@ -590,13 +650,17 @@ do_output_partitions (void)
       parent_name = guestfs_part_to_dev (g, parts[i]);
       if (parent_name == NULL)
         exit (EXIT_FAILURE);
+
+      if ((columns & COLUMN_MBR))
+        mbr_id = get_mbr_id (parts[i], parent_name);
+
       char *p = canonical_device (parent_name);
       free (parent_name);
       parent_name = p;
     }
 
     write_row (dev, "partition",
-               NULL, NULL, size, parent_name, NULL);
+               NULL, NULL, mbr_id, size, parent_name, NULL);
 
     free (dev);
     free (parent_name);
@@ -629,7 +693,7 @@ do_output_blockdevs (void)
     }
 
     write_row (dev, "device",
-               NULL, NULL, size, NULL, NULL);
+               NULL, NULL, -1, size, NULL, NULL);
 
     free (dev);
     free (devices[i]);
@@ -660,13 +724,14 @@ canonical_device (const char *dev)
 
 static void
 write_row (const char *name, const char *type,
-           const char *vfs_type, const char *vfs_label,
+           const char *vfs_type, const char *vfs_label, int mbr_id,
            int64_t size, const char *parent_name, const char *uuid)
 {
   const char *strings[NR_COLUMNS];
   size_t len = 0;
   char hum[LONGEST_HUMAN_READABLE];
   char num[256];
+  char mbr_id_str[3];
 
   if ((columns & COLUMN_NAME))
     strings[len++] = name;
@@ -676,6 +741,13 @@ write_row (const char *name, const char *type,
     strings[len++] = vfs_type;
   if ((columns & COLUMN_VFS_LABEL))
     strings[len++] = vfs_label;
+  if ((columns & COLUMN_MBR)) {
+    if (mbr_id >= 0) {
+      snprintf (mbr_id_str, sizeof mbr_id_str, "%02x", mbr_id);
+      strings[len++] = mbr_id_str;
+    } else
+      strings[len++] = NULL;
+  }
   if ((columns & COLUMN_SIZE)) {
     if (size >= 0) {
       if (human) {