Util-Linux Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH 1/1] lsblk: Ignore hidden devices
@ 2020-05-13 22:06 Ritika Srivastava
  2020-05-14 13:56 ` Karel Zak
  0 siblings, 1 reply; 2+ messages in thread
From: Ritika Srivastava @ 2020-05-13 22:06 UTC (permalink / raw)
  To: util-linux; +Cc: ritika.srivastava

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH 1/1] lsblk: Ignore hidden devices
  2020-05-13 22:06 [PATCH 1/1] lsblk: Ignore hidden devices Ritika Srivastava
@ 2020-05-14 13:56 ` Karel Zak
  0 siblings, 0 replies; 2+ messages in thread
From: Karel Zak @ 2020-05-14 13:56 UTC (permalink / raw)
  To: Ritika Srivastava; +Cc: util-linux

On Wed, May 13, 2020 at 03:06:10PM -0700, Ritika Srivastava wrote:
> 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.

It's interesting that /sys could be inconsistent in
/sys/dev/block/ and /sys/block/.

Applied, thanks.

I did a small change to the patch to make it usable with /sys dumps.
https://github.com/karelzak/util-linux/commit/c8487d854ba5cf5bfcae78d8e5af5587e7622351

    Karel

-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, back to index

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-13 22:06 [PATCH 1/1] lsblk: Ignore hidden devices Ritika Srivastava
2020-05-14 13:56 ` Karel Zak

Util-Linux Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/util-linux/0 util-linux/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 util-linux util-linux/ https://lore.kernel.org/util-linux \
		util-linux@vger.kernel.org
	public-inbox-index util-linux

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.util-linux


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git