block: resolve /dev/mapper/* name for /dev/dm-0 when hotplugging
authorDaniel Golle <daniel@makrotopia.org>
Wed, 14 Jul 2021 03:05:03 +0000 (04:05 +0100)
committerDaniel Golle <daniel@makrotopia.org>
Wed, 14 Jul 2021 04:30:36 +0000 (05:30 +0100)
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
block.c

diff --git a/block.c b/block.c
index 5b0517eb95c8a22b928aedd06eb85b4fa8191c0e..4416c91a2517bcdf756a854229ea2ccb6eb253c6 100644 (file)
--- a/block.c
+++ b/block.c
@@ -1171,7 +1171,28 @@ static int mount_action(char *action, char *device, int type)
 
 static int main_hotplug(int argc, char **argv)
 {
-       return mount_action(getenv("ACTION"), getenv("DEVNAME"), TYPE_HOTPLUG);
+       char *devname = getenv("DEVNAME");
+
+       /* resolve device mapper name for dm-* if possible */
+       if (devname && strlen(devname) >= 2 && !strncmp(devname, "dm-", 3)) {
+               char *dmdevnamep;
+               char dmname[256];
+
+               if (asprintf(&dmdevnamep, "/sys/%s/dm/name", getenv("DEVPATH")) == -1)
+                       exit(ENOMEM);
+
+               FILE *fp = fopen(dmdevnamep, "r");
+               free(dmdevnamep);
+
+               if (fp) {
+                       if (fgets(dmname, sizeof(dmname), fp))
+                               devname = dmname;
+
+                       fclose(fp);
+               }
+       }
+
+       return mount_action(getenv("ACTION"), devname, TYPE_HOTPLUG);
 }
 
 static int main_autofs(int argc, char **argv)