From: Karel Zak Date: Mon, 27 Apr 2009 12:33:22 +0000 (+0200) Subject: blkid: use /sys/block/dm-/dm/name X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6f52b6e9ae8a02285cab0e19bffc2f599e97f517;p=util-linux blkid: use /sys/block/dm-/dm/name The Linux kernel (since 2.6.29, patch 784aae735d9b0bba3f8b9faef4c8b30df3bf0128) exports the real DM device names in /sys/block//dm/name. The sysfs based solution is nicer and faster than scan for devno in /dev/mapper/. CC: Milan Broz Signed-off-by: Karel Zak --- diff --git a/libs/blkid/src/devname.c b/libs/blkid/src/devname.c index d2f53330..ef686f4b 100644 --- a/libs/blkid/src/devname.c +++ b/libs/blkid/src/devname.c @@ -152,6 +152,30 @@ static int is_dm_leaf(const char *devname) return ret; } +/* + * Since 2.6.29 (patch 784aae735d9b0bba3f8b9faef4c8b30df3bf0128) kernel sysfs + * provides the real DM device names in /sys/block//dm/name + */ +static char *get_dm_name(const char *ptname) +{ + FILE *f; + size_t sz; + char path[256], name[256], *res = NULL; + + snprintf(path, sizeof(path), "/sys/block/%s/dm/name", ptname); + if ((f = fopen(path, "r")) == NULL) + return NULL; + + /* read "\n" from sysfs */ + if (fgets(name, sizeof(name), f) && (sz = strlen(name)) > 1) { + name[sz - 1] = '\0'; + snprintf(path, sizeof(path), "/dev/mapper/%s", name); + res = blkid_strdup(path); + } + fclose(f); + return res; +} + /* * Probe a single block device to add to the device cache. */ @@ -183,7 +207,9 @@ static void probe_one(blkid_cache cache, const char *ptname, * to standard /dev/mapper/. */ if (!strncmp(ptname, "dm-", 3) && isdigit(ptname[3])) { - blkid__scan_dir("/dev/mapper", devno, 0, &devname); + devname = get_dm_name(ptname); + if (!devname) + blkid__scan_dir("/dev/mapper", devno, 0, &devname); if (devname) goto get_dev; }