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/<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>
This commit is contained in:
Ritika Srivastava 2020-05-13 15:06:10 -07:00 committed by Karel Zak
parent 72b155ea6e
commit c8487d854b
3 changed files with 37 additions and 0 deletions

View 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);

View 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];

View 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) {