blockdev-list,repart: optionally, filter list of candidate block device and remove OS root disk

This commit is contained in:
Lennart Poettering
2025-09-23 09:23:55 +02:00
parent 19f2baccce
commit ed90a0cdc9
4 changed files with 43 additions and 6 deletions
+21
View File
@@ -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) {
+5 -4
View File
@@ -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 {
+2
View File
@@ -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."),