[LTP] [PATCH] Fix tst_find_backing_dev when no initramfs

Alessandro Carminati alessandro.carminati@gmail.com
Tue Oct 25 11:11:02 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 ioctl_loop05 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>
---
 lib/tst_device.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/lib/tst_device.c b/lib/tst_device.c
index 8419b80c3..c67328db3 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 devnum_info_fn[PATH_MAX];
+       char new_dev_fn_buf[PATH_MAX];

        if (stat(path, &buf) < 0)
                tst_brkm(TWARN | TERRNO, NULL, "stat() failed");
@@ -562,6 +564,20 @@ 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) && (strcmp("/dev/root", dev)==0) {
+               sprintf(devnum_info_fn, "/sys/dev/block/%d:%d/uevent", dev_major, dev_minor);
+               file = SAFE_FOPEN(NULL, devnum_info_fn, "r");
+               while (fgets(line, sizeof(line), file)) {
+                       if (sscanf(line, "%[^=]=%s", devnum_info_fn, new_dev_fn_buf) != 2)
+                               continue;
+                       if (strcmp("DEVNAME", devnum_info_fn) == 0) {
+                               sprintf(dev, "/dev/%s", new_dev_fn_buf);
+                               break;
+                       }
+               }
+               SAFE_FCLOSE(NULL, file);
+       }
+
        if (stat(dev, &buf) < 0)
                tst_brkm(TWARN | TERRNO, NULL, "stat(%s) failed", dev);

--
2.34.1

On Mon, Oct 24, 2022 at 02:49:32PM +0100, Richard Palethorpe wrote:
> Hello,
> 
> Alessandro Carminati <alessandro.carminati@gmail.com> writes:
> 
> > 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.
> 
> Thanks I probably would have hit this issue sooner or later.
> 
> >
> > 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];
> 
> It would be more readable to use three buffers and give them meaningful names.
> 
> >  
> >  	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) {
> 
> As there is no harm in calling stat twice; could just do
> 
> if (stat(dev, &buf) < 0) && strcmp("/dev/root", dev) == 0) {
> 
> Or even replace with strcmp with errno == ENOENT and use this as a
> general fallback.
> 
> > +		if (strcmp("/dev/root", dev) != 0) {
> > +			tst_brkm(TWARN | TERRNO, NULL, "stat(%s) failed", dev);
> > +		} else {
> 
> Then remove this if statement
> 
> > +			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
> 
> 
> -- 
> Thank you,
> Richard.


More information about the ltp mailing list