]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
blockdev-list,repart: optionally hide zero-size block devices
authorLennart Poettering <lennart@poettering.net>
Fri, 5 Sep 2025 12:23:12 +0000 (14:23 +0200)
committerLennart Poettering <lennart@poettering.net>
Tue, 23 Sep 2025 07:25:11 +0000 (09:25 +0200)
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.

src/repart/repart.c
src/shared/blockdev-list.c
src/shared/blockdev-list.h
src/shared/varlink-io.systemd.Repart.c

index 71d73f0783ae9d9257e04d2a2dd6d5c26c9793f3..a27a75c9938da78d7e88771f7ba86a83387e3bf5 100644 (file)
@@ -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)
index 7e6b5b7e474961a2caaeb1fa1de455202c8e105d..16a0e2bb25c4d7add6ba8865a5168b49bed1cc86 100644 (file)
@@ -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();
 
index 525c34f49b53c3cfb2260408e0d1f4dfa58fd4a5..d03daffc3d8c220c130996bee30225a393474ae3 100644 (file)
@@ -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 {
index f1ea620990d5782bd08bab491c3a6ec02ccbf467..ba02b7da530ae47ad014b2ba283ad6603007d875 100644 (file)
@@ -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."),