[Libguestfs] [PATCH v6 2/7] daemon: Changing the way that we detect if a device contains partitions.

Mykola Ivanets stenavin at gmail.com
Tue May 1 00:53:59 UTC 2018


Instead of using part_to_dev to find such devices we open the device's
directory under /sys/block/<device> and look for entries starting with
<device>, eg. /sys/block/sda/sda1.
---
 daemon/listfs.ml | 40 +++++++++++++++++++++-------------------
 1 file changed, 21 insertions(+), 19 deletions(-)

diff --git a/daemon/listfs.ml b/daemon/listfs.ml
index 56ebadeda..55ace8e9c 100644
--- a/daemon/listfs.ml
+++ b/daemon/listfs.ml
@@ -24,31 +24,15 @@ let rec list_filesystems () =
   let has_lvm2 = Optgroups.lvm2_available () in
   let has_ldm = Optgroups.ldm_available () in
 
+  (* Devices. *)
   let devices = Devsparts.list_devices () in
-  let partitions = Devsparts.list_partitions () in
-  let mds = Md.list_md_devices () in
-
-  (* Look to see if any devices directly contain filesystems
-   * (RHBZ#590167).  However vfs-type will fail to tell us anything
-   * useful about devices which just contain partitions, so we also
-   * get the list of partitions and exclude the corresponding devices
-   * by using part-to-dev.
-   *)
-  let devices_containing_partitions = List.fold_left (
-    fun set part ->
-      StringSet.add (Devsparts.part_to_dev part) set
-  ) StringSet.empty partitions in
-  let devices = List.filter (
-    fun dev ->
-      not (StringSet.mem dev devices_containing_partitions)
-  ) devices in
-
-  (* Use vfs-type to check for filesystems on devices. *)
+  let devices = List.filter is_not_partitioned_device devices in
   let ret = List.filter_map check_with_vfs_type devices in
 
   (* Use vfs-type to check for filesystems on partitions, but
    * ignore MBR partition type 42 used by LDM.
    *)
+  let partitions = Devsparts.list_partitions () in
   let ret =
     ret @
       List.filter_map (
@@ -60,6 +44,7 @@ let rec list_filesystems () =
       ) partitions in
 
   (* Use vfs-type to check for filesystems on md devices. *)
+  let mds = Md.list_md_devices () in
   let ret = ret @ List.filter_map check_with_vfs_type mds in
 
   (* LVM. *)
@@ -85,6 +70,23 @@ let rec list_filesystems () =
 
   List.flatten ret
 
+(* Look to see if device can directly contain filesystem (RHBZ#590167).
+ * Partitioned devices cannot contain fileystem, so we will exclude such devices.
+ *)
+and is_not_partitioned_device device =
+  assert (String.is_prefix device "/dev/");
+  let device = String.sub device 5 (String.length device - 5) in
+  let dir = "/sys/block/" ^ device in
+
+  try
+    (* Open the device's directory under /sys/block/<device> and
+     * look for entries starting with <device>, eg. /sys/block/sda/sda1
+     *)
+    let parts = Array.to_list (Sys.readdir dir) in
+    let has_partition = List.exists (fun part -> String.is_prefix part device) parts in
+    not has_partition
+  with Sys_error (_) -> true
+
 (* Use vfs-type to check for a filesystem of some sort of [device].
  * Returns [Some [device, vfs_type; ...]] if found (there may be
  * multiple devices found in the case of btrfs), else [None] if nothing
-- 
2.17.0




More information about the Libguestfs mailing list