]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsblk: ignore only loopdevs without backing file
authorKarel Zak <kzak@redhat.com>
Wed, 9 Sep 2020 10:18:07 +0000 (12:18 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 9 Sep 2020 10:23:33 +0000 (12:23 +0200)
* 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 <kzak@redhat.com>
include/loopdev.h
lib/loopdev.c
misc-utils/lsblk.c

index 0e3a7517a98513fa58fd3d35c3e45f66216a0dc5..65bd579d2309fae04ab776fdc0468549db0ee614 100644 (file)
@@ -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,
index 1581f9cc252f2649c70549ee31545d232df2f664..c4b492dc5f6bf48592205709b1563c18c4bad86c 100644 (file)
@@ -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
  */
index 49c15ab78ec90cd08f6de700e147dddb85153190..82eae6bad883aca669248e8962f565ea12d40fb3 100644 (file)
@@ -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;
        }