[parted-devel] [PATCH 3/6] libparted: don't probe every dm device in probe_all

Phillip Susi psusi at ubuntu.com
Mon Oct 15 04:00:00 UTC 2012


We were probing every dm device.  Only probe dmraid whole disk ( non
partition ) devices instead.  This removes the clutter of LVM logical
volumes, and dmraid partitions from the list, which usually do not make
sense to partition.
---
 NEWS                   |    3 ++
 libparted/arch/linux.c |   86 +++++++++++++++++++++++++++++++++++++++++++++++-
 tests/Makefile.am      |    1 +
 tests/t6002-dm-busy.sh |    1 +
 tests/t6003-dm-hide.sh |   60 +++++++++++++++++++++++++++++++++
 5 files changed, 150 insertions(+), 1 deletion(-)
 create mode 100644 tests/t6003-dm-hide.sh

diff --git a/NEWS b/NEWS
index f182cce..d8fefc8 100644
--- a/NEWS
+++ b/NEWS
@@ -21,6 +21,9 @@ GNU parted NEWS                                    -*- outline -*-
 
 ** Changes in behavior
 
+  Device-mapper devices other than dmraid whole disks will no longer be
+  shown by parted -l.
+
   Added new Linux-specific partition GUID type code
   (0FC63DAF-8483-4772-8E79-3D69D8477DE4) for Linux filesystem data on GPT
   disks.  This type code is now assigned as the default partition type code
diff --git a/libparted/arch/linux.c b/libparted/arch/linux.c
index f0eb4aa..5e5b527 100644
--- a/libparted/arch/linux.c
+++ b/libparted/arch/linux.c
@@ -478,6 +478,89 @@ bad:
         return r;
 }
 
+/* Checks whether the given device-mapper device is part of a dmraid array,
+ * by checking for the string "DMRAID-" at the start of the UUID.
+ */
+static int
+_is_dmraid_device (const char *devpath)
+{
+        int             rc = 0;
+
+        char *dm_name = strrchr (devpath, '/');
+        if (dm_name && *dm_name && *(++dm_name))
+                dm_name = strdup (dm_name);
+        else
+                dm_name = strdup (devpath);
+        if (!dm_name)
+                return 0;
+
+        struct dm_task *task = dm_task_create (DM_DEVICE_DEPS);
+        if (!task)
+                return 0;
+
+        dm_task_set_name (task, dm_name);
+        if (!dm_task_run (task))
+                goto err;
+
+        const char *dmraid_uuid = dm_task_get_uuid (task);
+        if (strncmp (dmraid_uuid, "DMRAID-", 7) == 0) {
+                rc = 1;
+        }
+
+err:
+        free (dm_name);
+        dm_task_destroy (task);
+        return rc;
+}
+
+/* We consider a dm device that is a linear mapping with a  *
+ * single target that also is a dm device to be a partition */
+
+static int _dm_is_part (const char *path)
+{
+        int             rc = 0;
+
+        struct dm_task *task = dm_task_create(DM_DEVICE_DEPS);
+        if (!task)
+                return 0;
+
+        dm_task_set_name(task, path);
+        if (!dm_task_run(task))
+                goto err;
+
+        struct dm_info* info = alloca(sizeof *info);
+        memset(info, '\0', sizeof *info);
+        dm_task_get_info(task, info);
+        if (!info->exists)
+                goto err;
+
+        struct dm_deps *deps = dm_task_get_deps(task);
+        if (!deps)
+                goto err;
+
+        if (deps->count != 1)
+                goto err;
+        if (!_is_dm_major(major(deps->device[0])))
+                goto err;
+        dm_task_destroy(task);
+        if (!(task = dm_task_create(DM_DEVICE_TABLE)))
+                return 0;
+        dm_task_set_name(task, path);
+        if (!dm_task_run(task))
+                goto err;
+
+        char *target_type = NULL;
+        char *params = NULL;
+        uint64_t start, length;
+
+        dm_get_next_target(task, NULL, &start, &length, &target_type, &params);
+        if (strcmp (target_type, "linear"))
+                goto err;
+        rc = 1;
+err:
+        dm_task_destroy(task);
+        return rc;
+}
 
 static int
 _probe_dm_devices ()
@@ -504,7 +587,8 @@ _probe_dm_devices ()
                if (stat (buf, &st) != 0)
                        continue;
 
-               if (_is_dm_major(major(st.st_rdev)))
+               if (_is_dm_major(major(st.st_rdev)) && _is_dmraid_device (buf)
+                   && !_dm_is_part(buf))
                        _ped_device_probe (buf);
        }
        closedir (mapper_dir);
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 4649c0a..4ec08da 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -59,6 +59,7 @@ TESTS = \
   t6000-dm.sh \
   t6001-psep.sh \
   t6002-dm-busy.sh \
+  t6003-dm-hide.sh \
   t6100-mdraid-partitions.sh \
   t7000-scripting.sh \
   t8000-loop.sh \
diff --git a/tests/t6002-dm-busy.sh b/tests/t6002-dm-busy.sh
index 040f18f..40c4f80 100644
--- a/tests/t6002-dm-busy.sh
+++ b/tests/t6002-dm-busy.sh
@@ -83,6 +83,7 @@ Model: Linux device-mapper (linear) (dm)
 Disk DEV: 1049kB
 Sector size (logical/physical): 512B/512B
 Partition Table: msdos
+Disk DEV: 
 
 Number  Start  End     Size   Type     File system  Flags
  2      513kB  1025kB  512kB  primary  fat32        lba
diff --git a/tests/t6003-dm-hide.sh b/tests/t6003-dm-hide.sh
new file mode 100644
index 0000000..3cfdc43
--- /dev/null
+++ b/tests/t6003-dm-hide.sh
@@ -0,0 +1,60 @@
+#!/bin/sh
+# ensure that parted -l only shows dmraid device-mapper devices
+
+# Copyright (C) 2008-2012 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+. "${srcdir=.}/init.sh"; path_prepend_ ../parted
+
+require_root_
+lvm_init_root_dir_
+
+test "x$ENABLE_DEVICE_MAPPER" = xyes \
+  || skip_ "no device-mapper support"
+
+# Device maps names - should be random to not conflict with existing ones on
+# the system
+linear_=plinear-$$
+
+d1=
+f1=
+dev=
+cleanup_fn_() {
+    dmsetup remove $linear_
+    test -n "$d1" && losetup -d "$d1"
+    rm -f "$f1"
+}
+
+f1=$(pwd)/1; d1=$(loop_setup_ "$f1") \
+  || fail=1
+
+# setup: create a mapping
+echo "0 2048 linear $d1 0" | dmsetup create $linear_ || fail=1
+dev="$DM_DEV_DIR/mapper/$linear_"
+
+# device should not show up
+
+parted -l >out 2>&1
+! grep $linear_ out || fail=1
+
+dmsetup remove $linear_
+echo "0 2048 linear $d1 0" | dmsetup create $linear_ -u "DMRAID-fake" || fail=1
+
+# device should now show up
+
+parted -l >out 2>&1
+grep $linear_ out || fail=1
+
+Exit $fail
-- 
1.7.10.4




More information about the parted-devel mailing list