From c8487d854ba5cf5bfcae78d8e5af5587e7622351 Mon Sep 17 00:00:00 2001 From: Ritika Srivastava Date: Wed, 13 May 2020 15:06:10 -0700 Subject: [PATCH] lsblk: Ignore hidden devices 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//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//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 Signed-off-by: Karel Zak --- include/sysfs.h | 3 +++ lib/sysfs.c | 29 +++++++++++++++++++++++++++++ misc-utils/lsblk.c | 5 +++++ 3 files changed, 37 insertions(+) diff --git a/include/sysfs.h b/include/sysfs.h index edda8ca99..dcd2a148d 100644 --- a/include/sysfs.h +++ b/include/sysfs.h @@ -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); diff --git a/lib/sysfs.c b/lib/sysfs.c index 227a1e9f2..38e99c05c 100644 --- a/lib/sysfs.c +++ b/lib/sysfs.c @@ -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//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]; diff --git a/misc-utils/lsblk.c b/misc-utils/lsblk.c index 51b9f6ad7..3f21dcee2 100644 --- a/misc-utils/lsblk.c +++ b/misc-utils/lsblk.c @@ -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) {