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 },
{}
};
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;
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)
}
}
+ 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)) {
}
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();
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 {
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."),