From f1ea0f0d95a43c15acafc8f8bc18df76a17871a8 Mon Sep 17 00:00:00 2001 From: cenhuilin Date: Thu, 7 Aug 2025 10:48:21 +0800 Subject: [PATCH] fix lsblk ignore empty devices --- ...e-only-loopdevs-without-backing-file.patch | 101 ++++++++++++++++++ ...lk-print-zero-rather-than-empty-SIZE.patch | 27 +++++ ...lsblk-show-all-empty-except-loopdevs.patch | 41 +++++++ util-linux.spec | 11 +- 4 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 backport-lsblk-ignore-only-loopdevs-without-backing-file.patch create mode 100644 backport-lsblk-print-zero-rather-than-empty-SIZE.patch create mode 100644 backport-lsblk-show-all-empty-except-loopdevs.patch diff --git a/backport-lsblk-ignore-only-loopdevs-without-backing-file.patch b/backport-lsblk-ignore-only-loopdevs-without-backing-file.patch new file mode 100644 index 0000000..3f39747 --- /dev/null +++ b/backport-lsblk-ignore-only-loopdevs-without-backing-file.patch @@ -0,0 +1,101 @@ +From 14bb8e3ca6abaab9a584413d2dd74a6eb2cc18d0 Mon Sep 17 00:00:00 2001 +From: Karel Zak +Date: Thu, 7 Aug 2025 09:58:32 +0800 +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 0e3a751..65bd579 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 e669254..7777f6d 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 fd342b9..7a57024 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" + +@@ -1146,6 +1147,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 + */ +@@ -1197,7 +1207,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; + } +-- +2.33.0 + diff --git a/backport-lsblk-print-zero-rather-than-empty-SIZE.patch b/backport-lsblk-print-zero-rather-than-empty-SIZE.patch new file mode 100644 index 0000000..4614210 --- /dev/null +++ b/backport-lsblk-print-zero-rather-than-empty-SIZE.patch @@ -0,0 +1,27 @@ +From bb6c51a650fe3469aee819016826c4f2ed2c97c6 Mon Sep 17 00:00:00 2001 +From: Karel Zak +Date: Thu, 7 Aug 2025 10:00:54 +0800 +Subject: [PATCH] lsblk: print zero rather than empty SIZE + +Addresses: https://github.com/karelzak/util-linux/issues/1118 +Signed-off-by: Karel Zak +--- + misc-utils/lsblk.c | 2 -- + 1 file changed, 2 deletions(-) + +diff --git a/misc-utils/lsblk.c b/misc-utils/lsblk.c +index 7a57024..d3a8cbf 100644 +--- a/misc-utils/lsblk.c ++++ b/misc-utils/lsblk.c +@@ -905,8 +905,6 @@ static char *device_get_data( + ul_path_read_string(dev->sysfs, &str, "device/vendor"); + break; + case COL_SIZE: +- if (!dev->size) +- break; + if (lsblk->bytes) + xasprintf(&str, "%ju", dev->size); + else +-- +2.33.0 + diff --git a/backport-lsblk-show-all-empty-except-loopdevs.patch b/backport-lsblk-show-all-empty-except-loopdevs.patch new file mode 100644 index 0000000..dfea76b --- /dev/null +++ b/backport-lsblk-show-all-empty-except-loopdevs.patch @@ -0,0 +1,41 @@ +From c90a8f1a3cdf116e050708098f0188349b55abc3 Mon Sep 17 00:00:00 2001 +From: Karel Zak +Date: Thu, 7 Aug 2025 10:03:06 +0800 +Subject: [PATCH] lsblk: show all empty, except loopdevs + +This patch improves the previous commit to accept also another empty devices. + +Addresses: https://github.com/karelzak/util-linux/issues/1118 +Signed-off-by: Karel Zak +--- + misc-utils/lsblk.c | 13 +++++++++---- + 1 file changed, 9 insertions(+), 4 deletions(-) + +diff --git a/misc-utils/lsblk.c b/misc-utils/lsblk.c +index d3a8cbf..c132274 100644 +--- a/misc-utils/lsblk.c ++++ b/misc-utils/lsblk.c +@@ -1147,11 +1147,16 @@ static void devtree_to_scols(struct lsblk_devtree *tr, struct libscols_table *ta + + static int ignore_empty(struct lsblk_device *dev) + { +- if (dev->size != 0) ++ /* show all non-empty devices */ ++ if (dev->size) + return 0; +- if (dev->maj == LOOPDEV_MAJOR && loopdev_has_backing_file(dev->filename)) +- return 0; +- return 1; ++ ++ /* ignore empty loop devices without backing file */ ++ if (dev->maj == LOOPDEV_MAJOR && ++ !loopdev_has_backing_file(dev->filename)) ++ return 1; ++ ++ return 0; + } + + /* +-- +2.33.0 + diff --git a/util-linux.spec b/util-linux.spec index 843f3e1..1bce9f3 100644 --- a/util-linux.spec +++ b/util-linux.spec @@ -2,7 +2,7 @@ Name: util-linux Version: 2.35.2 -Release: 21 +Release: 22 Summary: A random collection of Linux utilities License: GPLv2 and GPLv2+ and LGPLv2+ and BSD with advertising and Public Domain URL: https://git.kernel.org/pub/scm/utils/util-linux/util-linux.git @@ -100,6 +100,9 @@ Patch9003: backport-uuidd-fix-open-lock-state-issue.patch Patch9004: backport-libfdisk-dos-fix-default-partition-start.patch Patch9005: backport-tests-sfdisk-fill-correctly-gaps-if-default-start-re.patch Patch9006: backport-libfdisk-fix-last-free-sector-detection-if-partition.patch +Patch9007: backport-lsblk-ignore-only-loopdevs-without-backing-file.patch +Patch9008: backport-lsblk-show-all-empty-except-loopdevs.patch +Patch9009: backport-lsblk-print-zero-rather-than-empty-SIZE.patch %description @@ -447,6 +450,12 @@ fi %{_mandir}/man8/{swapoff.8*,swapon.8*,switch_root.8*,umount.8*,wdctl.8.gz,wipefs.8*,zramctl.8*} %changelog +* Thu Aug 07 2025 cenhuilin - 2.35.2-22 +- Type:bugfix +- CVE:NA +- SUG:NA +- DESC:fix lsblk ignore empty devices + * Mon Jul 14 2025 cenhuilin - 2.35.2-21 - Type:bugfix - CVE:NA -- Gitee