1
0
mirror of https://github.com/systemd/systemd synced 2025-10-06 00:13:24 +02:00

blockdev-list,repart: optionally hide zero-size block devices

Block devices with removable media (e.g. SD card readers) indicate a
missing medium with a zero size. Optionally ignore such block devices
that carry no medium currently.
This commit is contained in:
Lennart Poettering
2025-09-05 14:23:12 +02:00
parent ed90a0cdc9
commit ba793df4b9
4 changed files with 27 additions and 12 deletions

View File

@@ -9789,10 +9789,14 @@ static int vl_method_list_candidate_devices(
sd_varlink_method_flags_t flags,
void *userdata) {
bool ignore_root = false;
struct {
bool ignore_root;
bool ignore_empty;
} p = {};
static const sd_json_dispatch_field dispatch_table[] = {
{ "ignoreRoot", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, 0, 0 },
{ "ignoreRoot", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, voffsetof(p, ignore_root), 0 },
{ "ignoreEmpty", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, voffsetof(p, ignore_empty), 0 },
{}
};
@@ -9800,7 +9804,7 @@ static int vl_method_list_candidate_devices(
assert(link);
r = sd_varlink_dispatch(link, parameters, dispatch_table, &ignore_root);
r = sd_varlink_dispatch(link, parameters, dispatch_table, &p);
if (r != 0)
return r;
@@ -9815,7 +9819,8 @@ static int vl_method_list_candidate_devices(
BLOCKDEV_LIST_SHOW_SYMLINKS|
BLOCKDEV_LIST_REQUIRE_PARTITION_SCANNING|
BLOCKDEV_LIST_IGNORE_ZRAM|
(ignore_root ? BLOCKDEV_LIST_IGNORE_ROOT : 0),
(p.ignore_empty ? BLOCKDEV_LIST_IGNORE_EMPTY : 0)|
(p.ignore_root ? BLOCKDEV_LIST_IGNORE_ROOT : 0),
&l,
&n);
if (r < 0)

View File

@@ -104,6 +104,20 @@ int blockdev_list(BlockDevListFlags flags, BlockDevice **ret_devices, size_t *re
}
}
uint64_t size = UINT64_MAX;
if (FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_EMPTY) || ret_devices) {
r = device_get_sysattr_u64(dev, "size", &size);
if (r < 0)
log_debug_errno(r, "Failed to acquire size of device '%s', ignoring: %m", node);
else
size *= 512; /* the 'size' sysattr is always in multiples of 512, even on 4K sector block devices! */
if (size == 0 && FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_EMPTY)) {
log_debug("Device '%s' has a zero size, assuming drive without a medium, skipping.", node);
continue;
}
}
_cleanup_strv_free_ char **list = NULL;
if (FLAGS_SET(flags, BLOCKDEV_LIST_SHOW_SYMLINKS)) {
@@ -115,18 +129,11 @@ int blockdev_list(BlockDevListFlags flags, BlockDevice **ret_devices, size_t *re
}
if (ret_devices) {
uint64_t diskseq = UINT64_MAX, size = UINT64_MAX;
uint64_t diskseq = UINT64_MAX;
r = sd_device_get_diskseq(dev, &diskseq);
if (r < 0)
log_debug_errno(r, "Failed to acquire diskseq of device '%s', ignoring: %m", node);
r = device_get_sysattr_u64(dev, "size", &size);
if (r < 0)
log_debug_errno(r, "Failed to acquire size of device '%s', ignoring: %m", node);
else
size *= 512; /* the 'size' sysattr is always in multiples of 512, even on 4K sector block devices! */
if (!GREEDY_REALLOC(l, n+1))
return log_oom();

View File

@@ -9,6 +9,7 @@ typedef enum BlockDevListFlags {
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) */
} BlockDevListFlags;
typedef struct BlockDevice {

View File

@@ -10,6 +10,8 @@ static SD_VARLINK_DEFINE_METHOD(
SD_VARLINK_DEFINE_OUTPUT(node, SD_VARLINK_STRING, 0),
SD_VARLINK_FIELD_COMMENT("Control whether to include the root disk of the currently booted OS in the list. Defaults to false, i.e. the root disk is included."),
SD_VARLINK_DEFINE_INPUT(ignoreRoot, SD_VARLINK_BOOL, SD_VARLINK_NULLABLE),
SD_VARLINK_FIELD_COMMENT("Control whether to include block devices with zero size in the list, i.e. typically block devices without any inserted medium. Defaults to false, i.e. empty block devices are included."),
SD_VARLINK_DEFINE_INPUT(ignoreEmpty, SD_VARLINK_BOOL, SD_VARLINK_NULLABLE),
SD_VARLINK_FIELD_COMMENT("List of symlinks pointing to the device node, if any."),
SD_VARLINK_DEFINE_OUTPUT(symlinks, SD_VARLINK_STRING, SD_VARLINK_ARRAY|SD_VARLINK_NULLABLE),
SD_VARLINK_FIELD_COMMENT("The Linux kernel disk sequence number identifying the medium."),