[LTP] [PATCH] Fix tst_find_backing_dev when no initramfs

Alessandro Carminati alessandro.carminati@gmail.com
Mon Oct 17 13:05:00 CEST 2022


mount_root() is the kernel function responsible for mounting the primary
rootfs.
A dynamic there, prevents the /dev/root device node in the not yet mounted
files system. For this reason, in the embedded system that starts without
an initramfs, or however a proper initscript, the /dev/root device appears
into the mount table in the / line.
The test tries to open this /dev/root and fails with a warning.
This patch aims to fix this situation.

Signed-off-by: Alessandro Carminati <alessandro.carminati@gmail.com>

typo fixes
---
 lib/tst_device.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/lib/tst_device.c b/lib/tst_device.c
index 8419b80c3..c3427eb31 100644
--- a/lib/tst_device.c
+++ b/lib/tst_device.c
@@ -526,6 +526,8 @@ 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 tmpbuf1[PATH_MAX];
+	char tmpbuf2[PATH_MAX];
 
 	if (stat(path, &buf) < 0)
 		tst_brkm(TWARN | TERRNO, NULL, "stat() failed");
@@ -562,6 +564,24 @@ void tst_find_backing_dev(const char *path, char *dev)
 	if (!*dev)
 		tst_brkm(TBROK, NULL, "Cannot find block device for %s", path);
 
+	if (stat(dev, &buf) < 0) {
+		if (strcmp("/dev/root", dev) != 0) {
+			tst_brkm(TWARN | TERRNO, NULL, "stat(%s) failed", dev);
+		} else {
+			sprintf(tmpbuf1, "/sys/dev/block/%d:%d/uevent", dev_major, dev_minor);
+			file = SAFE_FOPEN(NULL, tmpbuf1, "r");
+			while (fgets(line, sizeof(line), file)) {
+				if (sscanf(line, "%[^=]=%s", tmpbuf1, tmpbuf2) != 2)
+					continue;
+				if (strcmp("DEVNAME", tmpbuf1) == 0) {
+					sprintf(dev, "/dev/%s", tmpbuf2);
+					break;
+				}
+			}
+			SAFE_FCLOSE(NULL, file);
+		}
+	}
+
 	if (stat(dev, &buf) < 0)
 		tst_brkm(TWARN | TERRNO, NULL, "stat(%s) failed", dev);
 
-- 
2.34.1



More information about the ltp mailing list