]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
repart: hide read-only block devices from candidates
authorLennart Poettering <lennart@amutable.com>
Wed, 29 Apr 2026 16:47:48 +0000 (18:47 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 30 Apr 2026 06:04:55 +0000 (08:04 +0200)
If they are read-only they are no candidates, since we cannot write to
them.

src/repart/repart.c
src/shared/blockdev-list.c
src/shared/blockdev-list.h

index 2d35da28c2b12294855423a6450f9fddcc054185..b82827f869ee30062c8cbecdce36ab13f9cb1dac 100644 (file)
@@ -9896,7 +9896,13 @@ static int parse_argv(int argc, char *argv[]) {
 
                 OPTION_LONG("list-devices", NULL,
                             "List candidate block devices to operate on"):
-                        r = blockdev_list(BLOCKDEV_LIST_REQUIRE_PARTITION_SCANNING|BLOCKDEV_LIST_SHOW_SYMLINKS|BLOCKDEV_LIST_IGNORE_ZRAM, /* ret_devices= */ NULL, /* ret_n_devices= */ NULL);
+                        r = blockdev_list(
+                                        BLOCKDEV_LIST_SHOW_SYMLINKS|
+                                        BLOCKDEV_LIST_REQUIRE_PARTITION_SCANNING|
+                                        BLOCKDEV_LIST_IGNORE_ZRAM|
+                                        BLOCKDEV_LIST_IGNORE_READ_ONLY,
+                                        /* ret_devices= */ NULL,
+                                        /* ret_n_devices= */ NULL);
                         if (r < 0)
                                 return r;
 
@@ -10876,6 +10882,7 @@ static int vl_method_list_candidate_devices(
                         BLOCKDEV_LIST_REQUIRE_PARTITION_SCANNING|
                         BLOCKDEV_LIST_IGNORE_ZRAM|
                         BLOCKDEV_LIST_METADATA|
+                        BLOCKDEV_LIST_IGNORE_READ_ONLY|
                         (p.ignore_empty ? BLOCKDEV_LIST_IGNORE_EMPTY : 0)|
                         (p.ignore_root ? BLOCKDEV_LIST_IGNORE_ROOT : 0),
                         &l,
index 0efc90fd546967684aa80cf3f2a50ddc0e434142..181afb42890f544c48b7ff861317a4459d2f19d6 100644 (file)
@@ -188,6 +188,20 @@ int blockdev_list(BlockDevListFlags flags, BlockDevice **ret_devices, size_t *re
                         }
                 }
 
+                int ro = -1;
+                if (FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_READ_ONLY) || FLAGS_SET(flags, BLOCKDEV_LIST_METADATA)) {
+                        r = device_get_sysattr_bool(dev, "ro");
+                        if (r < 0)
+                                log_device_debug_errno(dev, r, "Failed to acquire read-only flag of device '%s', ignoring: %m", node);
+                        else
+                                ro = r;
+
+                        if (ro > 0 && FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_READ_ONLY)) {
+                                log_device_debug(dev, "Device '%s' is read-only, skipping.", node);
+                                continue;
+                        }
+                }
+
                 _cleanup_strv_free_ char **list = NULL;
                 if (FLAGS_SET(flags, BLOCKDEV_LIST_SHOW_SYMLINKS)) {
                         FOREACH_DEVICE_DEVLINK(dev, sl)
@@ -198,17 +212,10 @@ int blockdev_list(BlockDevListFlags flags, BlockDevice **ret_devices, size_t *re
                 }
 
                 _cleanup_free_ char *model = NULL, *vendor = NULL, *subsystem = NULL;
-                int ro = -1;
                 if (FLAGS_SET(flags, BLOCKDEV_LIST_METADATA)) {
                         (void) blockdev_get_prop(dev, "ID_MODEL_FROM_DATABASE", "ID_MODEL", &model);
                         (void) blockdev_get_prop(dev, "ID_VENDOR_FROM_DATABASE", "ID_VENDOR", &vendor);
                         (void) blockdev_get_subsystem(dev, &subsystem);
-
-                        r = device_get_sysattr_bool(dev, "ro");
-                        if (r < 0)
-                                log_device_debug_errno(dev, r, "Failed to acquire read-only flag of device '%s', ignoring: %m", node);
-                        else
-                                ro = r;
                 }
 
                 if (ret_devices) {
index d82345435f7e23ff8a059ae881ef59919e5da3c7..67f8efba9718751155eb127e8fe292c4bd28e688 100644 (file)
@@ -11,6 +11,7 @@ typedef enum BlockDevListFlags {
         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) */
         BLOCKDEV_LIST_METADATA                   = 1 << 6, /* Fill in model, vendor, subsystem, read_only */
+        BLOCKDEV_LIST_IGNORE_READ_ONLY           = 1 << 7, /* Ignore read-only block devices */
 } BlockDevListFlags;
 
 typedef struct BlockDevice {