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 <kzak@redhat.com>
This commit is contained in:
Karel Zak 2020-09-09 12:18:07 +02:00
parent bb6c51a650
commit 14bb8e3ca6
3 changed files with 23 additions and 1 deletions

View 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,

View 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
*/

View 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;
}