From: Lennart Poettering Date: Fri, 5 Sep 2025 12:23:12 +0000 (+0200) Subject: blockdev-list,repart: optionally hide zero-size block devices X-Git-Tag: v259-rc1~467^2~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ba793df4b93a4d5f658393aaabfcad355ecda20a;p=thirdparty%2Fsystemd.git 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. --- diff --git a/src/repart/repart.c b/src/repart/repart.c index 71d73f0783a..a27a75c9938 100644 --- a/src/repart/repart.c +++ b/src/repart/repart.c @@ -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) diff --git a/src/shared/blockdev-list.c b/src/shared/blockdev-list.c index 7e6b5b7e474..16a0e2bb25c 100644 --- a/src/shared/blockdev-list.c +++ b/src/shared/blockdev-list.c @@ -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(); diff --git a/src/shared/blockdev-list.h b/src/shared/blockdev-list.h index 525c34f49b5..d03daffc3d8 100644 --- a/src/shared/blockdev-list.h +++ b/src/shared/blockdev-list.h @@ -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 { diff --git a/src/shared/varlink-io.systemd.Repart.c b/src/shared/varlink-io.systemd.Repart.c index f1ea620990d..ba02b7da530 100644 --- a/src/shared/varlink-io.systemd.Repart.c +++ b/src/shared/varlink-io.systemd.Repart.c @@ -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."),