]> 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>
Thu, 14 May 2020 13:48:26 +0000 (15:48 +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 227a1e9f2c646a3f96116bfe9eb2318ddefcf57c..38e99c05c76ad4610b44611df4aefae8791094ef 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 51b9f6ad7c20c2556e4744248008f99dc35ce64d..3f21dcee2bfbb84b3ff72dfbd12460a49226768e 100644 (file)
@@ -1163,6 +1163,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) {