From 14bb8e3ca6abaab9a584413d2dd74a6eb2cc18d0 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 9 Sep 2020 12:18:07 +0200 Subject: [PATCH] lsblk: ignore only loopdevs without backing file * do not ignore all empty devices, we need more smart solution * ignore only loop devices without backing file, for example: # touch img # losetup -f img losetup: img: Warning: file is smaller than 512 bytes; the loop device may be useless or invisible for system tools. - old version display nothing - new version: # lsblk /dev/loop0 NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT loop0 7:0 0 0B 0 loop Addresses: https://github.com/karelzak/util-linux/issues/1118 Signed-off-by: Karel Zak --- include/loopdev.h | 1 + lib/loopdev.c | 11 +++++++++++ misc-utils/lsblk.c | 12 +++++++++++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/include/loopdev.h b/include/loopdev.h index 0e3a7517a..65bd579d2 100644 --- a/include/loopdev.h +++ b/include/loopdev.h @@ -134,6 +134,7 @@ extern int is_loopdev(const char *device); extern int loopdev_is_autoclear(const char *device); extern char *loopdev_get_backing_file(const char *device); +extern int loopdev_has_backing_file(const char *device); extern int loopdev_is_used(const char *device, const char *filename, uint64_t offset, uint64_t sizelimit, int flags); extern char *loopdev_find_by_backing_file(const char *filename, diff --git a/lib/loopdev.c b/lib/loopdev.c index 1581f9cc2..c4b492dc5 100644 --- a/lib/loopdev.c +++ b/lib/loopdev.c @@ -1619,6 +1619,17 @@ char *loopdev_get_backing_file(const char *device) return res; } +int loopdev_has_backing_file(const char *device) +{ + char *tmp = loopdev_get_backing_file(device); + + if (tmp) { + free(tmp); + return 1; + } + return 0; +} + /* * Returns: TRUE/FALSE */ diff --git a/misc-utils/lsblk.c b/misc-utils/lsblk.c index 49c15ab78..82eae6bad 100644 --- a/misc-utils/lsblk.c +++ b/misc-utils/lsblk.c @@ -50,6 +50,7 @@ #include "closestream.h" #include "optutils.h" #include "fileutils.h" +#include "loopdev.h" #include "lsblk.h" @@ -1149,6 +1150,15 @@ static void devtree_to_scols(struct lsblk_devtree *tr, struct libscols_table *ta device_to_scols(dev, NULL, tab, NULL); } +static int ignore_empty(struct lsblk_device *dev) +{ + if (dev->size != 0) + return 0; + if (dev->maj == LOOPDEV_MAJOR && loopdev_has_backing_file(dev->filename)) + return 0; + return 1; +} + /* * Reads very basic information about the device from sysfs into the device struct */ @@ -1200,7 +1210,7 @@ static int initialize_device(struct lsblk_device *dev, dev->size <<= 9; /* in bytes */ /* Ignore devices of zero size */ - if (!lsblk->all_devices && dev->size == 0) { + if (!lsblk->all_devices && ignore_empty(dev)) { DBG(DEV, ul_debugobj(dev, "zero size device -- ignore")); return -1; }