Disable test for RHBZ#576879 comment 5.
[libguestfs.git] / src / inspect.c
index 11a5374..992573a 100644 (file)
@@ -31,6 +31,7 @@
 #include <hivex.h>
 #include <augeas.h>
 
+#include "c-ctype.h"
 #include "ignore-value.h"
 #include "xstrtol.h"
 
@@ -57,6 +58,8 @@ static pcre *re_xdev;
 static pcre *re_windows_version;
 
 static void compile_regexps (void) __attribute__((constructor));
+static void free_regexps (void) __attribute__((destructor));
+
 static void
 compile_regexps (void)
 {
@@ -88,6 +91,22 @@ compile_regexps (void)
   COMPILE (re_windows_version, "^(\\d+)\\.(\\d+)", 0);
 }
 
+static void
+free_regexps (void)
+{
+  pcre_free (re_file_elf);
+  pcre_free (re_file_win64);
+  pcre_free (re_elf_ppc64);
+  pcre_free (re_fedora);
+  pcre_free (re_rhel_old);
+  pcre_free (re_rhel);
+  pcre_free (re_rhel_no_minor);
+  pcre_free (re_debian);
+  pcre_free (re_aug_seq);
+  pcre_free (re_xdev);
+  pcre_free (re_windows_version);
+}
+
 /* Match a regular expression which contains no captures.  Returns
  * true if it matches or false if it doesn't.
  */
@@ -209,6 +228,15 @@ is_regular_file (const char *filename)
 static char *
 cpio_arch (guestfs_h *g, const char *file, const char *path)
 {
+  TMP_TEMPLATE_ON_STACK (dir);
+#define dir_len (strlen (dir))
+#define initrd_len (dir_len + 16)
+  char initrd[initrd_len];
+#define cmd_len (dir_len + 256)
+  char cmd[cmd_len];
+#define bin_len (dir_len + 32)
+  char bin[bin_len];
+
   char *ret = NULL;
 
   const char *method;
@@ -219,20 +247,16 @@ cpio_arch (guestfs_h *g, const char *file, const char *path)
   else
     method = "cat";
 
-  char dir[] = "/tmp/initrd.XXXXXX";
-#define dir_len (sizeof dir)
   if (mkdtemp (dir) == NULL) {
     perrorf (g, "mkdtemp");
     goto out;
   }
 
-  char dir_initrd[dir_len + 16];
-  snprintf (dir_initrd, dir_len + 16, "%s/initrd", dir);
-  if (guestfs_download (g, path, dir_initrd) == -1)
+  snprintf (initrd, initrd_len, "%s/initrd", dir);
+  if (guestfs_download (g, path, initrd) == -1)
     goto out;
 
-  char cmd[dir_len + 256];
-  snprintf (cmd, dir_len + 256,
+  snprintf (cmd, cmd_len,
             "cd %s && %s initrd | cpio --quiet -id " INITRD_BINARIES1,
             dir, method);
   int r = system (cmd);
@@ -241,11 +265,10 @@ cpio_arch (guestfs_h *g, const char *file, const char *path)
     goto out;
   }
 
-  char bin[dir_len + 32];
   const char *bins[] = INITRD_BINARIES2;
   size_t i;
   for (i = 0; i < sizeof bins / sizeof bins[0]; ++i) {
-    snprintf (bin, dir_len + 32, "%s/%s", dir, bins[i]);
+    snprintf (bin, bin_len, "%s/%s", dir, bins[i]);
 
     if (is_regular_file (bin)) {
       int flags = g->verbose ? MAGIC_DEBUG : 0;
@@ -287,11 +310,14 @@ cpio_arch (guestfs_h *g, const char *file, const char *path)
    * contain shell meta-characters because of the way it was
    * constructed above.
    */
-  snprintf (cmd, dir_len + 256, "rm -rf %s", dir);
+  snprintf (cmd, cmd_len, "rm -rf %s", dir);
   ignore_value (system (cmd));
 
   return ret;
 #undef dir_len
+#undef initrd_len
+#undef cmd_len
+#undef bin_len
 }
 
 char *
@@ -806,6 +832,12 @@ add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
       STREQ (mp, "/sys"))
     return 0;
 
+  /* Ignore /dev/fd (floppy disks) (RHBZ#642929) and CD-ROM drives. */
+  if ((STRPREFIX (spec, "/dev/fd") && c_isdigit (spec[7])) ||
+      STREQ (spec, "/dev/floppy") ||
+      STREQ (spec, "/dev/cdrom"))
+    return 0;
+
   /* Resolve UUID= and LABEL= to the actual device. */
   char *device = NULL;
   if (STRPREFIX (spec, "UUID="))
@@ -853,39 +885,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;
 }
 
@@ -965,6 +1012,13 @@ static int
 check_windows_registry (guestfs_h *g, struct inspect_fs *fs,
                         const char *systemroot)
 {
+  TMP_TEMPLATE_ON_STACK (dir);
+#define dir_len (strlen (dir))
+#define software_hive_len (dir_len + 16)
+  char software_hive[software_hive_len];
+#define cmd_len (dir_len + 16)
+  char cmd[cmd_len];
+
   size_t len = strlen (systemroot) + 64;
   char software[len];
   snprintf (software, len, "%s/system32/config/software", systemroot);
@@ -980,15 +1034,12 @@ check_windows_registry (guestfs_h *g, struct inspect_fs *fs,
   hive_h *h = NULL;
   hive_value_h *values = NULL;
 
-  char dir[] = "/tmp/winreg.XXXXXX";
-#define dir_len 18
   if (mkdtemp (dir) == NULL) {
     perrorf (g, "mkdtemp");
     goto out;
   }
 
-  char software_hive[dir_len + 16];
-  snprintf (software_hive, dir_len + 16, "%s/software", dir);
+  snprintf (software_hive, software_hive_len, "%s/software", dir);
 
   if (guestfs_download (g, software_path, software_hive) == -1)
     goto out;
@@ -1074,10 +1125,11 @@ check_windows_registry (guestfs_h *g, struct inspect_fs *fs,
    * contain shell meta-characters because of the way it was
    * constructed above.
    */
-  char cmd[dir_len + 16];
-  snprintf (cmd, dir_len + 16, "rm -rf %s", dir);
+  snprintf (cmd, cmd_len, "rm -rf %s", dir);
   ignore_value (system (cmd));
 #undef dir_len
+#undef software_hive_len
+#undef cmd_len
 
   return ret;
 }