[Libguestfs] [PATCH 2/2] /dev/mapper paths should not be returned from C inspection APIs

Richard W.M. Jones rjones at redhat.com
Mon Oct 25 12:05:08 UTC 2010


-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
libguestfs lets you edit virtual machines.  Supports shell scripting,
bindings from many languages.  http://et.redhat.com/~rjones/libguestfs/
See what it can do: http://et.redhat.com/~rjones/libguestfs/recipes.html
-------------- next part --------------
>From d88daf9d6baf70457b60c1eb99a5d530b2ff80ab Mon Sep 17 00:00:00 2001
From: Richard W.M. Jones <rjones at redhat.com>
Date: Mon, 25 Oct 2010 12:59:50 +0100
Subject: [PATCH 2/2] /dev/mapper paths should not be returned from C inspection APIs (RHBZ#638899).

With this patch, /dev/mapper paths do not appear in the output
of guestfs_inspect_os, as you can see from this example:

Welcome to guestfish, the libguestfs filesystem interactive shell for
editing virtual machine filesystems.

Type: 'help' for a list of commands
      'man' to read the manual
      'quit' to quit the shell

Operating system: Fedora release 13 (Goddard)
/dev/vg_f13x64/lv_root mounted on /              <--- NB
/dev/vda1 mounted on /boot
---
 src/inspect.c |   49 ++++++++++++++++++++++++++++++++-----------------
 1 files changed, 32 insertions(+), 17 deletions(-)

diff --git a/src/inspect.c b/src/inspect.c
index 1f4096d..3ffb2bd 100644
--- a/src/inspect.c
+++ b/src/inspect.c
@@ -878,39 +878,54 @@ add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
 }
 
 /* Resolve block device name to the libguestfs device name, eg.
- * /dev/xvdb1 => /dev/vdb1.  This assumes that disks were added in the
- * same order as they appear to the real VM, which is a reasonable
- * assumption to make.  Return things like LV names unchanged (or
- * anything we don't recognize).
+ * /dev/xvdb1 => /dev/vdb1; and /dev/mapper/VG-LV => /dev/VG/LV.  This
+ * assumes that disks were added in the same order as they appear to
+ * the real VM, which is a reasonable assumption to make.  Return
+ * anything we don't recognize unchanged.
  */
 static char *
 resolve_fstab_device (guestfs_h *g, const char *spec)
 {
-  char **devices = guestfs_list_devices (g);
-  if (devices == NULL)
-    return NULL;
+  char *a1;
+  char *device = NULL;
 
-  size_t count;
-  for (count = 0; devices[count] != NULL; count++)
-    ;
+  if (STRPREFIX (spec, "/dev/mapper/")) {
+    /* LVM2 does some strange munging on /dev/mapper paths for VGs and
+     * LVs which contain '-' character:
+     *
+     * ><fs> lvcreate LV--test VG--test 32
+     * ><fs> debug ls /dev/mapper
+     * VG----test-LV----test
+     *
+     * This makes it impossible to reverse those paths directly, so
+     * we have implemented lvm_canonical_lv_name in the daemon.
+     */
+    device = guestfs_lvm_canonical_lv_name (g, spec);
+  }
+  else if ((a1 = match1 (g, spec, re_xdev)) != NULL) {
+    char **devices = guestfs_list_devices (g);
+    if (devices == NULL)
+      return NULL;
+
+    size_t count;
+    for (count = 0; devices[count] != NULL; count++)
+      ;
 
-  char *device = NULL;
-  char *a1 = match1 (g, spec, re_xdev);
-  if (a1) {
     size_t i = a1[0] - 'a'; /* a1[0] is always [a-z] because of regex. */
     if (i < count) {
       size_t len = strlen (devices[i]) + strlen (a1) + 16;
       device = safe_malloc (g, len);
       snprintf (device, len, "%s%s", devices[i], &a1[1]);
     }
-  } else {
+
+    free (a1);
+    free_string_list (devices);
+  }
+  else {
     /* Didn't match device pattern, return original spec unchanged. */
     device = safe_strdup (g, spec);
   }
 
-  free (a1);
-  free_string_list (devices);
-
   return device;
 }
 
-- 
1.7.3.1



More information about the Libguestfs mailing list