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) {