[LTP] [PATCH] tst_find_backing_dev: Get dev name from /sys/dev/block/*/uevent

Richard Palethorpe rpalethorpe@suse.com
Tue Oct 25 16:59:10 CEST 2022


mountinfo doesn't always contain the correct device name. For example
/dev/root may be displayed, but not exist in devtmpfs[1].

The unevent file in sysfs is another way of finding the device name
from the major and minor numbers. Possibly it always displays the
proper device name.

One caveat is the sysfs can be disabled, so this commit does not
remove the mountinfo method altogether, but leaves it as a fallback.

Alessandro Carminati originally sent two patches[1] which added the
uevent file method as a fallback. However it seems like the better
method.

[1]: https://lore.kernel.org/ltp/Y0023HcAOlhfAcJw@lab.hqhome163.com/

Signed-off-by: Richard Palethorpe <rpalethorpe@suse.com>
Suggested-by: Alessandro Carminati <alessandro.carminati@gmail.com>
Reported-by: Alessandro Carminati <alessandro.carminati@gmail.com>
---

Alessandro, it seems you tried to edit the last patch by hand? In any
case it did not apply and I ended up making some other changes. So I
took the liberty of submitting a new patch.

Thanks,
Richard.

 lib/tst_device.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/lib/tst_device.c b/lib/tst_device.c
index 8419b80c3..676903fff 100644
--- a/lib/tst_device.c
+++ b/lib/tst_device.c
@@ -526,14 +526,30 @@ void tst_find_backing_dev(const char *path, char *dev)
 	unsigned int dev_major, dev_minor, line_mjr, line_mnr;
 	unsigned int len, best_match_len = 1;
 	char mnt_point[PATH_MAX];
+	char uevent_path[PATH_MAX];
+	char dev_name[NAME_MAX];
 
 	if (stat(path, &buf) < 0)
 		tst_brkm(TWARN | TERRNO, NULL, "stat() failed");
 
+	*dev = '\0';
 	dev_major = major(buf.st_dev);
 	dev_minor = minor(buf.st_dev);
+
+	sprintf(uevent_path,
+		"/sys/dev/block/%d:%d/uevent", dev_major, dev_minor);
+
+	if (!access(uevent_path, R_OK)) {
+		FILE_LINES_SCANF(NULL, uevent_path, "DEVNAME=%s", dev_name);
+
+		if (dev_name[0])
+			sprintf(dev, "/dev/%s", dev_name);
+	}
+
+	if (!stat(dev, &buf))
+		goto out;
+
 	file = SAFE_FOPEN(NULL, "/proc/self/mountinfo", "r");
-	*dev = '\0';
 
 	while (fgets(line, sizeof(line), file)) {
 		if (sscanf(line, "%*d %*d %d:%d %*s %s",
@@ -564,7 +580,7 @@ void tst_find_backing_dev(const char *path, char *dev)
 
 	if (stat(dev, &buf) < 0)
 		tst_brkm(TWARN | TERRNO, NULL, "stat(%s) failed", dev);
-
+out:
 	if (S_ISBLK(buf.st_mode) != 1)
 		tst_brkm(TCONF, NULL, "dev(%s) isn't a block dev", dev);
 }
-- 
2.36.1



More information about the ltp mailing list