util-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ritika Srivastava <ritika.srivastava@oracle.com>
To: util-linux@vger.kernel.org
Cc: ritika.srivastava@oracle.com
Subject: [PATCH 1/1] lsblk: Ignore hidden devices
Date: Wed, 13 May 2020 15:06:10 -0700	[thread overview]
Message-ID: <1589407570-25740-1-git-send-email-ritika.srivastava@oracle.com> (raw)

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.

Signed-off-by: Ritika Srivastava <ritika.srivastava@oracle.com>
---
 include/sysfs.h    |  1 +
 lib/sysfs.c        | 27 +++++++++++++++++++++++++++
 misc-utils/lsblk.c |  4 ++++
 3 files changed, 32 insertions(+)

diff --git a/include/sysfs.h b/include/sysfs.h
index edda8ca..d92401f 100644
--- a/include/sysfs.h
+++ b/include/sysfs.h
@@ -95,6 +95,7 @@ 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 *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 227a1e9..b18e13f 100644
--- a/lib/sysfs.c
+++ b/lib/sysfs.c
@@ -823,6 +823,33 @@ int sysfs_blkdev_scsi_path_contains(struct path_cxt *pc, const char *pattern)
 	return strstr(linkc, pattern) != NULL;
 }
 
+int sysfs_devname_is_hidden(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),
+	_PATH_SYS_BLOCK "/%s/hidden", 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;
+}
+
 static dev_t read_devno(const char *path)
 {
 	FILE *f;
diff --git a/misc-utils/lsblk.c b/misc-utils/lsblk.c
index 51b9f6a..a5ec431 100644
--- a/misc-utils/lsblk.c
+++ b/misc-utils/lsblk.c
@@ -1177,6 +1177,10 @@ static int initialize_device(struct lsblk_device *dev,
 	}
 	DBG(DEV, ul_debugobj(dev, "%s: filename=%s", dev->name, dev->filename));
 
+	/* ignore hidden devices */
+	if (sysfs_devname_is_hidden(name))
+		return -1;
+
 	devno = __sysfs_devname_to_devno(lsblk->sysroot, dev->name, wholedisk ? wholedisk->name : NULL);
 	if (!devno) {
 		DBG(DEV, ul_debugobj(dev, "%s: unknown device name", dev->name));
-- 
1.8.3.1


             reply	other threads:[~2020-05-13 22:20 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-13 22:06 Ritika Srivastava [this message]
2020-05-14 13:56 ` [PATCH 1/1] lsblk: Ignore hidden devices Karel Zak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1589407570-25740-1-git-send-email-ritika.srivastava@oracle.com \
    --to=ritika.srivastava@oracle.com \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).