]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsblk: Ignore hidden devices
authorRitika Srivastava <ritika.srivastava@oracle.com>
Wed, 13 May 2020 22:06:10 +0000 (15:06 -0700)
committerKarel Zak <kzak@redhat.com>
Fri, 15 May 2020 10:27:19 +0000 (12:27 +0200)
Lsblk throws the following error for nvmeNcXnY devices.

lsblk: nvme1c1n1: unknown device name

This is because nvmeNcXnY devices are hidden and do not have
the file /sys/block/<nvmeNcXnY>/dev.

Following patch was added
https://git.kernel.org/pub/scm/utils/util-linux/util-linux.git/commit/?id=d51f05bfecb299a830897106460bf395be440c0a
Which made lsblk read from /sys/block/<nvmeNcXnY>/device/dev
which do exist for nvmeNcXnY devices.

After the above patch, the unknown error goes away.
However, another error is encountered in the very next step.

nvme1c1n1: failed to initialize sysfs handler

This is because lsblk looks for /sys/dev/block/242:1
(nvmeNcXnY major:minor) pathname which usually exists for other
block devices but not for the nvmeNcXnY devices as they are hidden.

Below patch does not even print this error for hidden devices
and exits silently.

[kzak@redhat.com: - add prefix to make sysfs_devname_is_hidden()
                    usable for  /sys dumps
                  - use the function in initialize_device() more early]

Signed-off-by: Ritika Srivastava <ritika.srivastava@oracle.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
include/sysfs.h
lib/sysfs.c
misc-utils/lsblk.c

index edda8ca991a5a415786787ebd94ad6639c0512ca..dcd2a148d98666deaf757668a039b192a7f1366c 100644 (file)
@@ -93,8 +93,11 @@ int sysfs_devno_to_wholedisk(dev_t dev, char *diskname,
                              size_t len, dev_t *diskdevno);
 int sysfs_devno_is_dm_private(dev_t devno, char **uuid);
 int sysfs_devno_is_wholedisk(dev_t devno);
+
 dev_t sysfs_devname_to_devno(const char *name);
 dev_t __sysfs_devname_to_devno(const char *prefix, const char *name, const char *parent);
+int sysfs_devname_is_hidden(const char *prefix, const char *name);
+
 char *sysfs_devno_to_devpath(dev_t devno, char *buf, size_t bufsiz);
 char *sysfs_devno_to_devname(dev_t devno, char *buf, size_t bufsiz);
 int sysfs_devno_count_partitions(dev_t devno);
index ce944001f9f4e9ae4979e73efef73635fd11bb2e..a15f557932a4e8fd4f61d352b9f24b19ce6f3741 100644 (file)
@@ -839,6 +839,35 @@ static dev_t read_devno(const char *path)
        return dev;
 }
 
+int sysfs_devname_is_hidden(const char *prefix, const char *name)
+{
+       char buf[PATH_MAX];
+       int rc = 0, hidden = 0, len;
+       FILE *f;
+
+       if (strncmp("/dev/", name, 5) == 0)
+               return 0;
+       /*
+        * Create path to /sys/block/<name>/hidden
+        */
+       len = snprintf(buf, sizeof(buf),
+                       "%s" _PATH_SYS_BLOCK "/%s/hidden",
+                       prefix, name);
+
+       if (len < 0 || (size_t) len + 1 > sizeof(buf))
+               return 0;
+
+       f = fopen(buf, "r" UL_CLOEXECSTR);
+       if (!f)
+               return 0;
+
+       rc = fscanf(f, "%d", &hidden);
+       fclose(f);
+
+       return rc == 1 ? hidden : 0;
+}
+
+
 dev_t __sysfs_devname_to_devno(const char *prefix, const char *name, const char *parent)
 {
        char buf[PATH_MAX];
index 72ac7b483215f69bba784a20b402acd3b4048edd..fd342b92ca96b9ed7a27ab709f10e560473d7e90 100644 (file)
@@ -1158,6 +1158,11 @@ static int initialize_device(struct lsblk_device *dev,
        DBG(DEV, ul_debugobj(dev, "initialize %s [wholedisk=%p %s]",
                        name, wholedisk, wholedisk ? wholedisk->name : ""));
 
+       if (sysfs_devname_is_hidden(lsblk->sysroot, name)) {
+               DBG(DEV, ul_debugobj(dev, "%s: hidden, ignore", name));
+               return -1;
+       }
+
        dev->name = xstrdup(name);
 
        if (wholedisk) {