From: Lennart Poettering Date: Thu, 28 Aug 2025 09:18:16 +0000 (+0200) Subject: blockdev-list,repart: optionally, filter list of candidate block device and remove... X-Git-Tag: v259-rc1~467^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ed90a0cdc9e407c024135096c03f8e8228cfafac;p=thirdparty%2Fsystemd.git blockdev-list,repart: optionally, filter list of candidate block device and remove OS root disk --- diff --git a/src/repart/repart.c b/src/repart/repart.c index 0c7b77f8450..71d73f0783a 100644 --- a/src/repart/repart.c +++ b/src/repart/repart.c @@ -9789,11 +9789,18 @@ static int vl_method_list_candidate_devices( sd_varlink_method_flags_t flags, void *userdata) { + bool ignore_root = false; + + static const sd_json_dispatch_field dispatch_table[] = { + { "ignoreRoot", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, 0, 0 }, + {} + }; + int r; assert(link); - r = sd_varlink_dispatch(link, parameters, /* dispatch_table= */ NULL, /* userdata= */ NULL); + r = sd_varlink_dispatch(link, parameters, dispatch_table, &ignore_root); if (r != 0) return r; @@ -9804,7 +9811,13 @@ static int vl_method_list_candidate_devices( size_t n = 0; CLEANUP_ARRAY(l, n, block_device_array_free); - r = blockdev_list(BLOCKDEV_LIST_SHOW_SYMLINKS|BLOCKDEV_LIST_REQUIRE_PARTITION_SCANNING|BLOCKDEV_LIST_IGNORE_ZRAM, &l, &n); + r = blockdev_list( + BLOCKDEV_LIST_SHOW_SYMLINKS| + BLOCKDEV_LIST_REQUIRE_PARTITION_SCANNING| + BLOCKDEV_LIST_IGNORE_ZRAM| + (ignore_root ? BLOCKDEV_LIST_IGNORE_ROOT : 0), + &l, + &n); if (r < 0) return r; diff --git a/src/shared/blockdev-list.c b/src/shared/blockdev-list.c index 3c54219393e..7e6b5b7e474 100644 --- a/src/shared/blockdev-list.c +++ b/src/shared/blockdev-list.c @@ -39,6 +39,14 @@ int blockdev_list(BlockDevListFlags flags, BlockDevice **ret_devices, size_t *re size_t n = 0; CLEANUP_ARRAY(l, n, block_device_array_free); + dev_t root_devno = 0; + if (FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_ROOT)) + if (blockdev_get_root(LOG_DEBUG, &root_devno) > 0) { + r = block_get_whole_disk(root_devno, &root_devno); + if (r < 0) + log_debug_errno(r, "Failed to get whole block device of root device: %m"); + } + if (sd_device_enumerator_new(&e) < 0) return log_oom(); @@ -61,6 +69,19 @@ int blockdev_list(BlockDevListFlags flags, BlockDevice **ret_devices, size_t *re continue; } + if (FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_ROOT) && root_devno != 0) { + dev_t devno; + + r = sd_device_get_devnum(dev, &devno); + if (r < 0) { + log_warning_errno(r, "Failed to get major/minor of discovered block device, ignoring: %m"); + continue; + } + + if (devno == root_devno) + continue; + } + if (FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_ZRAM)) { r = device_sysname_startswith(dev, "zram"); if (r < 0) { diff --git a/src/shared/blockdev-list.h b/src/shared/blockdev-list.h index 63e9fad10d9..525c34f49b5 100644 --- a/src/shared/blockdev-list.h +++ b/src/shared/blockdev-list.h @@ -4,10 +4,11 @@ #include "forward.h" typedef enum BlockDevListFlags { - BLOCKDEV_LIST_SHOW_SYMLINKS = 1 << 0, - BLOCKDEV_LIST_REQUIRE_PARTITION_SCANNING = 1 << 1, - BLOCKDEV_LIST_IGNORE_ZRAM = 1 << 2, - BLOCKDEV_LIST_REQUIRE_LUKS = 1 << 3, + 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 */ } BlockDevListFlags; typedef struct BlockDevice { diff --git a/src/shared/varlink-io.systemd.Repart.c b/src/shared/varlink-io.systemd.Repart.c index 4f531fe39c8..f1ea620990d 100644 --- a/src/shared/varlink-io.systemd.Repart.c +++ b/src/shared/varlink-io.systemd.Repart.c @@ -8,6 +8,8 @@ static SD_VARLINK_DEFINE_METHOD( ListCandidateDevices, SD_VARLINK_FIELD_COMMENT("The device node path of the block device."), 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("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."),