blockdev-list: also pick up block device size

This commit is contained in:
Lennart Poettering
2025-08-28 10:56:06 +02:00
parent 9f6b2745ea
commit e863e2dbb5
2 changed files with 10 additions and 1 deletions

View File

@@ -6,6 +6,7 @@
#include "ansi-color.h"
#include "blockdev-list.h"
#include "blockdev-util.h"
#include "device-private.h"
#include "device-util.h"
#include "strv.h"
#include "terminal-util.h"
@@ -93,12 +94,18 @@ int blockdev_list(BlockDevListFlags flags, BlockDevice **ret_devices, size_t *re
}
if (ret_devices) {
uint64_t diskseq = UINT64_MAX;
uint64_t diskseq = UINT64_MAX, size = 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();
@@ -110,6 +117,7 @@ int blockdev_list(BlockDevListFlags flags, BlockDevice **ret_devices, size_t *re
.node = TAKE_PTR(m),
.symlinks = TAKE_PTR(list),
.diskseq = diskseq,
.size = size,
};
} else {

View File

@@ -14,6 +14,7 @@ typedef struct BlockDevice {
char *node;
char **symlinks;
uint64_t diskseq;
uint64_t size; /* in bytes */
} BlockDevice;
#define BLOCK_DEVICE_NULL (BlockDevice) { \