kpartx: use partition UUID for non-DM devices
authorMartin Wilck <mwilck@suse.com>
Mon, 15 May 2017 15:37:19 +0000 (17:37 +0200)
committerChristophe Varoqui <christophe.varoqui@opensvc.com>
Wed, 17 May 2017 21:44:32 +0000 (23:44 +0200)
For dm devices, kpartx uses an UUID check at partition removal
to make sure it only deletes partitions it previously created.
Non-DM parent devices such as loop devices don't generally have
a UUID.

Introduce a "fake" UUID for these devices to make sure kpartx
deletes only devices it had created previously. Otherwise kpartx
might e.g. delete LVM LVs that are inside a device it is trying
to delete partitions for. It seems to be wiser to make sure the
user delete these manually before running "kpartx -d".

With the fake UUID in place, we can re-introduce the UUID check
for non-DM device that was removed in the earlier patch
"kpartx: dm_remove_partmaps: support non-dm devices".

This disables also deletion of partition mappings created
by earlier versions of kpartx. If kpartx has been updated after
partition mappings were created, the "-f" flag can be used
to force delting these partitions, too.

Signed-off-by: Martin Wilck <mwilck@suse.com>
kpartx/devmapper.c
kpartx/kpartx.c

index b714ba4..4ab58ce 100644 (file)
@@ -511,7 +511,7 @@ do_foreach_partmaps (const char * mapname, const char *uuid,
                /*
                 * skip if uuids don't match
                 */
-               if (is_dmdev && uuid && dm_compare_uuid(uuid, names->name)) {
+               if (uuid && dm_compare_uuid(uuid, names->name)) {
                        if (rd->verbose)
                                printf("%s: is not a kpartx partition. Not removing\n",
                                       names->name);
index 65ec2f7..eb5e4d3 100644 (file)
@@ -60,6 +60,31 @@ struct pt {
 int ptct = 0;
 int udev_sync = 1;
 
+/*
+ * UUID format for partitions created on non-DM devices
+ * ${UUID_PREFIX}devnode_${MAJOR}:${MINOR}_${NONDM_UUID_SUFFIX}"
+ * where ${UUID_PREFIX} is "part${PARTNO}-" (see devmapper.c).
+ *
+ * The suffix should be sufficiently unique to avoid incidental conflicts;
+ * the value below is a base64-encoded random number.
+ * The UUID format shouldn't be changed between kpartx releases.
+ */
+#define NONDM_UUID_PREFIX "devnode"
+#define NONDM_UUID_SUFFIX "Wh5pYvM"
+
+static char *
+nondm_create_uuid(dev_t devt)
+{
+#define NONDM_UUID_BUFLEN (34 + sizeof(NONDM_UUID_PREFIX) + \
+                          sizeof(NONDM_UUID_SUFFIX))
+       static char uuid_buf[NONDM_UUID_BUFLEN];
+       snprintf(uuid_buf, sizeof(uuid_buf), "%s_%u:%u_%s",
+                NONDM_UUID_PREFIX, major(devt), minor(devt),
+                NONDM_UUID_SUFFIX);
+       uuid_buf[NONDM_UUID_BUFLEN-1] = '\0';
+       return uuid_buf;
+}
+
 static void
 addpts(char *t, ptreader f)
 {
@@ -364,6 +389,15 @@ main(int argc, char **argv){
                        uuid = dm_mapuuid(mapname);
        }
 
+       /*
+        * We are called for a non-DM device.
+        * Make up a fake UUID for the device, unless "-d -f" is given.
+        * This allows deletion of partitions created with older kpartx
+        * versions which didn't use the fake UUID during creation.
+        */
+       if (!uuid && !(what == DELETE && force_devmap))
+               uuid = nondm_create_uuid(buf.st_rdev);
+
        if (!mapname)
                mapname = device + off;