Re: [dm-devel] [PATCH 08/10] kpartx: use partition UUID for non-DM devices

2017-05-08 Thread Martin Wilck
On Fri, 2017-05-05 at 23:30 +0100, Alasdair G Kergon wrote:
> On Sat, May 06, 2017 at 12:05:57AM +0200, Martin Wilck wrote:
> > 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.
> 
>  
> Indeed - always supply a uuid for every dm device created.
> Add a standard prefix to the beginning of it, ideally "KPARTX-" to be
> consistent with other dm tools ("LVM-", "CRYPT-" etc.) if you're
> able to break compatibility, but "part" is still unique and
> adequate.  
> (Stacked devices can end up with a stack of prefixes - only the
> leftmost one
> counts.)

As noted in my reply to Hannes' mail, I'm using the prefix "part%u-",
the same prefix that kpartx currently used for partitions of DM
devices.

The full format e.g. for /dev/mapper/loop0p1 is 

"part1-7:0-kpartx-Wh5pYvM7uc60hh74" 

and correspondingly, the "fake UUID" used for /dev/loop0 would be 

  "7:0-kpartx-Wh5pYvM7uc60hh74"

If you don't like the fact that this starts with "7:0", changing this
to e.g. "KPARTX-7.0-$something" is no problem as long as patch isn't
applied.

The last part ("Wh...") is a hard-coded string, just to avoid
accidental conflicts. I was wondering whether it would make sense to
some sort of encoded "host ID" to make this UUID truly "universal". My
personal opinion is that this would be over-engineered, because these
UUID will be visible only only on the host that called "kpartx -a" in
the first place.

Cheers,
Martin

-- 
Dr. Martin Wilck , Tel. +49 (0)911 74053 2107
SUSE Linux GmbH, GF: Felix Imendörffer, Jane Smithard, Graham Norton
HRB 21284 (AG Nürnberg)

--
dm-devel mailing list
dm-devel@redhat.com
https://www.redhat.com/mailman/listinfo/dm-devel

Re: [dm-devel] [PATCH 08/10] kpartx: use partition UUID for non-DM devices

2017-05-05 Thread Alasdair G Kergon
On Sat, May 06, 2017 at 12:05:57AM +0200, Martin Wilck wrote:
> 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.
 
Indeed - always supply a uuid for every dm device created.
Add a standard prefix to the beginning of it, ideally "KPARTX-" to be
consistent with other dm tools ("LVM-", "CRYPT-" etc.) if you're
able to break compatibility, but "part" is still unique and adequate.  
(Stacked devices can end up with a stack of prefixes - only the leftmost one
counts.)

Alasdair

--
dm-devel mailing list
dm-devel@redhat.com
https://www.redhat.com/mailman/listinfo/dm-devel


[dm-devel] [PATCH 08/10] kpartx: use partition UUID for non-DM devices

2017-05-05 Thread Martin Wilck
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 
---
 kpartx/devmapper.c |  2 +-
 kpartx/kpartx.c| 29 +
 2 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/kpartx/devmapper.c b/kpartx/devmapper.c
index 5380b2e9..40ec26cf 100644
--- a/kpartx/devmapper.c
+++ b/kpartx/devmapper.c
@@ -505,7 +505,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);
diff --git a/kpartx/kpartx.c b/kpartx/kpartx.c
index e2056b7f..d9057fba 100644
--- a/kpartx/kpartx.c
+++ b/kpartx/kpartx.c
@@ -60,6 +60,26 @@ struct pt {
 int ptct = 0;
 int udev_sync = 0;
 
+/*
+ * UUID format for partitions created on non-DM devices
+ * ${UUID_PREFIX}${MAJOR}:${MINOR}-${NONDM_UUID_SUFFIX}"
+ * The suffix should be sufficiently random to avoid unintended conflicts,
+ * and shouldn't be changed.
+ * The value below is a bas64-encoded 96-bit random number.
+ */
+#define NONDM_UUID_SUFFIX "-kpartx-Wh5pYvM7uc60hh74"
+
+static char *
+nondm_create_uuid(dev_t devt)
+{
+#define NONDM_UUID_BUFLEN (32 + sizeof(NONDM_UUID_SUFFIX))
+   static char uuid_buf[NONDM_UUID_BUFLEN];
+   snprintf(uuid_buf, sizeof(uuid_buf), "%u:%u%s",
+major(devt), minor(devt), NONDM_UUID_SUFFIX);
+   uuid_buf[NONDM_UUID_BUFLEN-1] = '\0';
+   return uuid_buf;
+}
+
 static void
 addpts(char *t, ptreader f)
 {
@@ -360,6 +380,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;
else if (!force_devmap &&
-- 
2.12.2

--
dm-devel mailing list
dm-devel@redhat.com
https://www.redhat.com/mailman/listinfo/dm-devel