[Libguestfs] [PATCH v5 2/3] daemon: list-filesystems: Don't list partitions which cannot hold file system.

Mykola Ivanets stenavin at gmail.com
Fri Apr 27 12:53:40 UTC 2018


From: Nikolay Ivanets <stenavin at gmail.com>

The following partitions are filtered out:
1. Partitioned md devices (just as partitioned physical devices are filtered out).
2. Extended MBR partitions.
3. LDM Partitions (MBR and GPT partitions used by Windows Logical Disk Manager).
4. Microsoft Reserved Partitions (GUID E3C9E316-0B5C-4DB8-817D-F92DF00215AE).
5. Windows Snapshot Partitions (GUID CADDEBF1-4400-4DE8-B103-12117DCF3CCF).
---
 daemon/listfs.ml | 130 +++++++++++++++++++++++++++++------------------
 1 file changed, 81 insertions(+), 49 deletions(-)

diff --git a/daemon/listfs.ml b/daemon/listfs.ml
index 56ebadeda..5499bacb2 100644
--- a/daemon/listfs.ml
+++ b/daemon/listfs.ml
@@ -25,39 +25,18 @@ let rec list_filesystems () =
   let has_ldm = Optgroups.ldm_available () in
 
   let devices = Devsparts.list_devices () in
-  let partitions = Devsparts.list_partitions () in
+  let devices = List.filter check_device devices 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
+  let mds = List.filter check_device mds in
 
   (* Use vfs-type to check for filesystems on devices. *)
   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 ret =
-    ret @
-      List.filter_map (
-        fun part ->
-          if not has_ldm || not (is_mbr_partition_type_42 part) then
-            check_with_vfs_type part
-          else
-            None                (* ignore type 42 *)
-      ) partitions in
+  let partitions = Devsparts.list_partitions () in
+  let partitions = List.filter check_partition partitions in
+
+  (* Use vfs-type to check for filesystems on partitions. *)
+  let ret = ret @ List.filter_map check_with_vfs_type partitions in
 
   (* Use vfs-type to check for filesystems on md devices. *)
   let ret = ret @ List.filter_map check_with_vfs_type mds in
@@ -75,16 +54,86 @@ let rec list_filesystems () =
   let ret =
     if has_ldm then (
       let ldmvols = Ldm.list_ldm_volumes () in
-      let ldmparts = Ldm.list_ldm_partitions () in
       (* Use vfs-type to check for filesystems on Windows dynamic disks. *)
-      ret @
-        List.filter_map check_with_vfs_type ldmvols @
-        List.filter_map check_with_vfs_type ldmparts
+      ret @ List.filter_map check_with_vfs_type ldmvols
     )
     else ret in
 
   List.flatten ret
 
+and is_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 
+    List.exists (fun part -> String.is_prefix part device) parts 
+  with Sys_error (_) -> false
+
+(* 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
+ * exclude such devices.
+ *)
+and check_device device =
+  not (is_partitioned_device device)
+
+and is_ignored_gpt_type gpt_type =
+  match gpt_type with
+  (* Windows Logical Disk Manager metadata partition. *)
+  | "5808C8AA-7E8F-42E0-85D2-E1E90434CFB3" -> Optgroups.ldm_available ()
+  (* Windows Logical Disk Manager data partition. *)
+  | "AF9B60A0-1431-4F62-BC68-3311714A69AD" -> Optgroups.ldm_available ()
+  (* Microsoft Reserved Partition. *)
+  | "E3C9E316-0B5C-4DB8-817D-F92DF00215AE" -> true
+  (* Windows Snapshot Partition. *)
+  | "CADDEBF1-4400-4DE8-B103-12117DCF3CCF" -> true
+  | _ -> false
+  
+(* We should ignore Windows Logical Disk Manager (LDM) partitions,
+ * because these are members of a Windows dynamic disk group.  Trying
+ * to read them will cause errors (RHBZ#887520).  Assuming that
+ * libguestfs was compiled with ldm support, we'll get the filesystems
+ * on these later.  We also ignores Microsoft Reserved Partitions,
+ * Windows Snapshot Partition and extended MBR partitions because they
+ * cannot hold filesystem.
+ *)
+and check_partition partition =
+  try
+    let device = Devsparts.part_to_dev partition in
+    let partnum = Devsparts.part_to_partnum partition in
+    let parttype = Parted.part_get_parttype device in
+
+    let is_gpt = parttype = "gpt" in
+    let is_mbr = parttype = "msdos" in
+    let is_gpt_or_mbr = is_gpt || is_mbr in
+
+    let is_mbr_extended =
+      if is_mbr then (
+        let mbr_type = Parted.part_get_mbr_part_type device partnum in
+        mbr_type = "extended"
+      )
+      else false in
+
+    let is_gpt_ignored =
+      if is_gpt_or_mbr then (
+        (* MBR partition id will be converted into corresponding GPT type. *)
+        let gpt_type = Parted.part_get_gpt_type device partnum in
+        is_ignored_gpt_type gpt_type
+      )
+      else false in
+    
+    not (is_mbr_extended || is_gpt_ignored)
+  with exn ->
+        if verbose () then
+          eprintf "check_partition: %s: %s\n"
+            partition (Printexc.to_string exn);
+        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
@@ -140,20 +189,3 @@ and check_with_vfs_type device =
 
   else
     Some [mountable, vfs_type]
-
-(* We should ignore partitions that have MBR type byte 0x42, because
- * these are members of a Windows dynamic disk group.  Trying to read
- * them will cause errors (RHBZ#887520).  Assuming that libguestfs was
- * compiled with ldm support, we'll get the filesystems on these later.
- *)
-and is_mbr_partition_type_42 partition =
-  try
-    let partnum = Devsparts.part_to_partnum partition in
-    let device = Devsparts.part_to_dev partition in
-    let mbr_id = Parted.part_get_mbr_id device partnum in
-    mbr_id = 0x42
-  with exn ->
-     if verbose () then
-       eprintf "is_mbr_partition_type_42: %s: %s\n"
-               partition (Printexc.to_string exn);
-     false
-- 
2.17.0




More information about the Libguestfs mailing list