[Libguestfs] [PATCH 1/2] Fix bogus partition number passed to guestfs___check_for_filesystem_on

Matthew Booth mbooth at redhat.com
Thu Feb 7 14:31:09 UTC 2013


Partition number was being passed to guestfs___check_for_filesystem_on
based on an index in list_partition. However, this ignores the
possibility of multiple block devices.

This change makes guestfs___check_for_filesystem_on examine the
passed-in device directly to determine if it is a whole device, or
what its partition number is.
---
 src/guestfs-internal.h |  2 +-
 src/inspect-fs.c       | 45 +++++++++++++++++++++++++--------------------
 src/inspect.c          |  8 ++++----
 3 files changed, 30 insertions(+), 25 deletions(-)

diff --git a/src/guestfs-internal.h b/src/guestfs-internal.h
index 86d024a..cf298f2 100644
--- a/src/guestfs-internal.h
+++ b/src/guestfs-internal.h
@@ -524,7 +524,7 @@ extern struct inspect_fs *guestfs___search_for_root (guestfs_h *g, const char *r
 /* inspect-fs.c */
 extern int guestfs___is_file_nocase (guestfs_h *g, const char *);
 extern int guestfs___is_dir_nocase (guestfs_h *g, const char *);
-extern int guestfs___check_for_filesystem_on (guestfs_h *g, const char *device, int is_block, int is_partnum);
+extern int guestfs___check_for_filesystem_on (guestfs_h *g, const char *device);
 extern int guestfs___parse_unsigned_int (guestfs_h *g, const char *str);
 extern int guestfs___parse_unsigned_int_ignore_trailing (guestfs_h *g, const char *str);
 extern int guestfs___parse_major_minor (guestfs_h *g, struct inspect_fs *fs);
diff --git a/src/inspect-fs.c b/src/inspect-fs.c
index ce075db..0069dc6 100644
--- a/src/inspect-fs.c
+++ b/src/inspect-fs.c
@@ -79,47 +79,47 @@ free_regexps (void)
   pcre_free (re_major_minor);
 }
 
-static int check_filesystem (guestfs_h *g, const char *device, int is_block, int is_partnum);
+static int check_filesystem (guestfs_h *g, const char *device,
+                             int whole_device);
 static int extend_fses (guestfs_h *g);
 
 /* Find out if 'device' contains a filesystem.  If it does, add
  * another entry in g->fses.
  */
 int
-guestfs___check_for_filesystem_on (guestfs_h *g, const char *device,
-                                   int is_block, int is_partnum)
+guestfs___check_for_filesystem_on (guestfs_h *g, const char *device)
 {
-  CLEANUP_FREE char *vfs_type = NULL;
-  int is_swap, r;
-  struct inspect_fs *fs;
+  int r;
 
   /* Get vfs-type in order to check if it's a Linux(?) swap device.
    * If there's an error we should ignore it, so to do that we have to
    * temporarily replace the error handler with a null one.
    */
   guestfs_push_error_handler (g, NULL, NULL);
-  vfs_type = guestfs_vfs_type (g, device);
+  CLEANUP_FREE char *vfs_type = guestfs_vfs_type (g, device);
   guestfs_pop_error_handler (g);
 
-  is_swap = vfs_type && STREQ (vfs_type, "swap");
-
-  debug (g, "check_for_filesystem_on: %s %d %d (%s)",
-         device, is_block, is_partnum,
-         vfs_type ? vfs_type : "failed to get vfs type");
+  debug (g, "check_for_filesystem_on: %s (%s)",
+         device, vfs_type ? vfs_type : "failed to get vfs type");
 
-  if (is_swap) {
+  if (vfs_type && STREQ (vfs_type, "swap")) {
     if (extend_fses (g) == -1)
       return -1;
-    fs = &g->fses[g->nr_fses-1];
+    struct inspect_fs *fs = &g->fses[g->nr_fses-1];
     fs->device = safe_strdup (g, device);
     return 0;
   }
 
   /* If it's a whole device, see if it is an install ISO. */
-  if (is_block) {
+  int whole_device = guestfs_is_whole_device (g, device);
+  if (whole_device == -1) {
+    return -1;
+  }
+
+  if (whole_device) {
     if (extend_fses (g) == -1)
       return -1;
-    fs = &g->fses[g->nr_fses-1];
+    struct inspect_fs *fs = &g->fses[g->nr_fses-1];
 
     r = guestfs___check_installer_iso (g, fs, device);
     if (r == -1) {              /* Fatal error. */
@@ -149,7 +149,7 @@ guestfs___check_for_filesystem_on (guestfs_h *g, const char *device,
     return 0;
 
   /* Do the rest of the checks. */
-  r = check_filesystem (g, device, is_block, is_partnum);
+  r = check_filesystem (g, device, whole_device);
 
   /* Unmount the filesystem. */
   if (guestfs_umount_all (g) == -1)
@@ -165,12 +165,17 @@ guestfs___check_for_filesystem_on (guestfs_h *g, const char *device,
  * (eg. /dev/sda1 => is_partnum == 1).
  */
 static int
-check_filesystem (guestfs_h *g, const char *device,
-                  int is_block, int is_partnum)
+check_filesystem (guestfs_h *g, const char *device, int whole_device)
 {
   if (extend_fses (g) == -1)
     return -1;
 
+  int partnum = -1;
+  if (!whole_device) {
+    partnum = guestfs_part_to_partnum (g, device);
+    /* If this returns an error it just means it's not a partition */
+  }
+
   struct inspect_fs *fs = &g->fses[g->nr_fses-1];
 
   fs->device = safe_strdup (g, device);
@@ -292,7 +297,7 @@ check_filesystem (guestfs_h *g, const char *device,
    * Skip these checks if it's not a whole device (eg. CD) or the
    * first partition (eg. bootable USB key).
    */
-  else if ((is_block || is_partnum == 1) &&
+  else if ((whole_device || partnum == 1) &&
            (guestfs_is_file (g, "/isolinux/isolinux.cfg") > 0 ||
             guestfs_is_dir (g, "/EFI/BOOT") > 0 ||
             guestfs_is_file (g, "/images/install.img") > 0 ||
diff --git a/src/inspect.c b/src/inspect.c
index c51c3f5..d0eb012 100644
--- a/src/inspect.c
+++ b/src/inspect.c
@@ -64,7 +64,7 @@ guestfs__inspect_os (guestfs_h *g)
 
   size_t i;
   for (i = 0; devices[i] != NULL; ++i) {
-    if (guestfs___check_for_filesystem_on (g, devices[i], 1, 0) == -1) {
+    if (guestfs___check_for_filesystem_on (g, devices[i]) == -1) {
       guestfs___free_string_list (devices);
       guestfs___free_inspect_info (g);
       return NULL;
@@ -83,7 +83,7 @@ guestfs__inspect_os (guestfs_h *g)
     if (parent_device_already_probed (g, partitions[i]))
       continue;
 
-    if (guestfs___check_for_filesystem_on (g, partitions[i], 0, i+1) == -1) {
+    if (guestfs___check_for_filesystem_on (g, partitions[i]) == -1) {
       guestfs___free_string_list (partitions);
       guestfs___free_inspect_info (g);
       return NULL;
@@ -99,7 +99,7 @@ guestfs__inspect_os (guestfs_h *g)
   }
 
   for (i = 0; mds[i] != NULL; ++i) {
-    if (guestfs___check_for_filesystem_on (g, mds[i], 0, i+1) == -1) {
+    if (guestfs___check_for_filesystem_on (g, mds[i]) == -1) {
       guestfs___free_string_list (mds);
       guestfs___free_inspect_info (g);
       return NULL;
@@ -117,7 +117,7 @@ guestfs__inspect_os (guestfs_h *g)
     }
 
     for (i = 0; lvs[i] != NULL; ++i) {
-      if (guestfs___check_for_filesystem_on (g, lvs[i], 0, 0) == -1) {
+      if (guestfs___check_for_filesystem_on (g, lvs[i]) == -1) {
         guestfs___free_string_list (lvs);
         guestfs___free_inspect_info (g);
         return NULL;
-- 
1.8.1




More information about the Libguestfs mailing list