shared/blockdev-list: add flags to ignore virtual devices and partitions
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
#include "device-util.h"
|
||||
#include "devnum-util.h"
|
||||
#include "errno-util.h"
|
||||
#include "path-util.h"
|
||||
#include "string-util.h"
|
||||
#include "strv.h"
|
||||
#include "terminal-util.h"
|
||||
@@ -166,6 +167,29 @@ int blockdev_list_one(
|
||||
goto no_match;
|
||||
}
|
||||
|
||||
if (FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_VIRTUAL)) {
|
||||
const char *devpath;
|
||||
|
||||
r = sd_device_get_devpath(dev, &devpath);
|
||||
if (r < 0) {
|
||||
log_device_warning_errno(dev, r, "Failed to acquire device path of discovered block device '%s', ignoring: %m", node);
|
||||
goto skipped;
|
||||
}
|
||||
|
||||
if (path_startswith(devpath, "/devices/virtual/"))
|
||||
goto no_match;
|
||||
}
|
||||
|
||||
if (FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_PARTITIONS)) {
|
||||
r = device_is_devtype(dev, "partition");
|
||||
if (r < 0) {
|
||||
log_device_warning_errno(dev, r, "Failed to check if device is a partition '%s', ignoring: %m", node);
|
||||
goto skipped;
|
||||
}
|
||||
if (r > 0)
|
||||
goto no_match;
|
||||
}
|
||||
|
||||
if (FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_ZRAM)) {
|
||||
r = device_sysname_startswith(dev, "zram");
|
||||
if (r < 0) {
|
||||
|
||||
@@ -4,14 +4,16 @@
|
||||
#include "forward.h"
|
||||
|
||||
typedef enum BlockDevListFlags {
|
||||
BLOCKDEV_LIST_SHOW_SYMLINKS = 1 << 0, /* Pick up symlinks to block devices too */
|
||||
BLOCKDEV_LIST_REQUIRE_PARTITION_SCANNING = 1 << 1, /* Only consider block devices with partition scanning */
|
||||
BLOCKDEV_LIST_IGNORE_ZRAM = 1 << 2, /* Ignore ZRAM */
|
||||
BLOCKDEV_LIST_REQUIRE_LUKS = 1 << 3, /* Only consider block devices with LUKS superblocks */
|
||||
BLOCKDEV_LIST_IGNORE_ROOT = 1 << 4, /* Ignore the block device we are currently booted from */
|
||||
BLOCKDEV_LIST_IGNORE_EMPTY = 1 << 5, /* Ignore disks of zero size (usually drives without a medium) */
|
||||
BLOCKDEV_LIST_METADATA = 1 << 6, /* Fill in model, vendor, subsystem, read_only */
|
||||
BLOCKDEV_LIST_IGNORE_READ_ONLY = 1 << 7, /* Ignore read-only block devices */
|
||||
BLOCKDEV_LIST_SHOW_SYMLINKS = 1 << 0, /* Pick up symlinks to block devices too */
|
||||
BLOCKDEV_LIST_REQUIRE_PARTITION_SCANNING = 1 << 1, /* Only consider block devices with partition scanning */
|
||||
BLOCKDEV_LIST_IGNORE_VIRTUAL = 1 << 2, /* Ignore virtual devices (devices under /sys/devices/virtual/) */
|
||||
BLOCKDEV_LIST_IGNORE_PARTITIONS = 1 << 3, /* Ignore partitions */
|
||||
BLOCKDEV_LIST_IGNORE_ZRAM = 1 << 4, /* Ignore ZRAM */
|
||||
BLOCKDEV_LIST_IGNORE_ROOT = 1 << 5, /* Ignore the block device we are currently booted from */
|
||||
BLOCKDEV_LIST_IGNORE_EMPTY = 1 << 6, /* Ignore disks of zero size (usually drives without a medium) */
|
||||
BLOCKDEV_LIST_IGNORE_READ_ONLY = 1 << 7, /* Ignore read-only block devices */
|
||||
BLOCKDEV_LIST_REQUIRE_LUKS = 1 << 8, /* Only consider block devices with LUKS superblocks */
|
||||
BLOCKDEV_LIST_METADATA = 1 << 9, /* Fill in model, vendor, subsystem, read_only */
|
||||
} BlockDevListFlags;
|
||||
|
||||
/* The "dynamic" filters – ones the kernel can flip at runtime via sysattrs on a live device.
|
||||
|
||||
Reference in New Issue
Block a user