| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-device.h" |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "ansi-color.h" |
| 7 |
#include "blockdev-list.h" |
| 8 |
#include "blockdev-util.h" |
| 9 |
#include "device-private.h" |
| 10 |
#include "device-util.h" |
| 11 |
#include "devnum-util.h" |
| 12 |
#include "errno-util.h" |
| 13 |
#include "string-util.h" |
| 14 |
#include "strv.h" |
| 15 |
#include "terminal-util.h" |
| 16 |
|
| 17 |
void block_device_done(BlockDevice *d) { |
| 18 |
assert(d); |
| 19 |
|
| 20 |
d->node = mfree(d->node); |
| 21 |
d->symlinks = strv_free(d->symlinks); |
| 22 |
d->model = mfree(d->model); |
| 23 |
d->vendor = mfree(d->vendor); |
| 24 |
d->subsystem = mfree(d->subsystem); |
| 25 |
} |
| 26 |
|
| 27 |
void block_device_array_free(BlockDevice *d, size_t n_devices) { |
| 28 |
|
| 29 |
FOREACH_ARRAY(i, d, n_devices) |
| 30 |
block_device_done(i); |
| 31 |
|
| 32 |
free(d); |
| 33 |
} |
| 34 |
|
| 35 |
static int blockdev_get_prop(sd_device *d, const char *prop1, const char *prop2, char **ret_value) { |
| 36 |
int r, ret = 0; |
| 37 |
|
| 38 |
assert(d); |
| 39 |
assert(prop1); |
| 40 |
assert(ret_value); |
| 41 |
|
| 42 |
FOREACH_STRING(prop, prop1, prop2) { |
| 43 |
const char *m = NULL; |
| 44 |
r = sd_device_get_property_value(d, prop, &m); |
| 45 |
if (r < 0 && r != -ENOENT) |
| 46 |
RET_GATHER(ret, log_device_debug_errno(d, r, "Failed to acquire '%s' from device, ignoring: %m", prop)); |
| 47 |
else if (!isempty(m)) |
| 48 |
return strdup_to(ret_value, m); |
| 49 |
} |
| 50 |
|
| 51 |
return ret < 0 ? ret : -ENOENT; |
| 52 |
} |
| 53 |
|
| 54 |
static int blockdev_get_subsystem(sd_device *d, char **ret_subsystem) { |
| 55 |
int r; |
| 56 |
|
| 57 |
assert(d); |
| 58 |
assert(ret_subsystem); |
| 59 |
|
| 60 |
/* We prefer the explicitly set block device subsystem property, because if it is set it's generally |
| 61 |
* the most useful. If it's not set we'll look for the subsystem of the first parent device that |
| 62 |
* isn't of subsystem 'block'. The former covers 'virtual' block devices such as loopback, device |
| 63 |
* mapper, zram, while the latter covers physical block devices such as USB or NVME. */ |
| 64 |
|
| 65 |
r = blockdev_get_prop(d, "ID_BLOCK_SUBSYSTEM", /* prop2= */ NULL, ret_subsystem); |
| 66 |
if (r >= 0) |
| 67 |
return r; |
| 68 |
|
| 69 |
int ret = r != -ENOENT ? r : 0; |
| 70 |
sd_device *q = d; |
| 71 |
for (;;) { |
| 72 |
r = sd_device_get_parent(q, &q); |
| 73 |
if (r < 0) { |
| 74 |
if (r != -ENOENT) |
| 75 |
RET_GATHER(ret, log_device_debug_errno(q, r, "Failed to get parent device, ignoring: %m")); |
| 76 |
break; |
| 77 |
} |
| 78 |
|
| 79 |
const char *s = NULL; |
| 80 |
r = sd_device_get_subsystem(q, &s); |
| 81 |
if (r < 0) |
| 82 |
RET_GATHER(ret, log_device_debug_errno(q, r, "Failed to get subsystem of device, ignoring: %m")); |
| 83 |
else if (!isempty(s) && !streq(s, "block")) |
| 84 |
return strdup_to(ret_subsystem, s); |
| 85 |
} |
| 86 |
|
| 87 |
return ret < 0 ? ret : -ENOENT; |
| 88 |
} |
| 89 |
|
| 90 |
int blockdev_list(BlockDevListFlags flags, BlockDevice **ret_devices, size_t *ret_n_devices) { |
| 91 |
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
| 92 |
int r; |
| 93 |
|
| 94 |
assert(!!ret_devices == !!ret_n_devices); |
| 95 |
|
| 96 |
/* If ret_devices/ret_n_devices are passed, returns a list of matching block devices, otherwise |
| 97 |
* prints the list to stdout */ |
| 98 |
|
| 99 |
BlockDevice *l = NULL; |
| 100 |
size_t n = 0; |
| 101 |
CLEANUP_ARRAY(l, n, block_device_array_free); |
| 102 |
|
| 103 |
dev_t root_devno = 0, whole_root_devno = 0; |
| 104 |
if (FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_ROOT)) { |
| 105 |
r = blockdev_get_root(LOG_DEBUG, &root_devno); |
| 106 |
if (r < 0) |
| 107 |
log_debug_errno(r, "Failed to get block device of root device, ignoring: %m"); |
| 108 |
else if (r > 0) { |
| 109 |
r = block_get_whole_disk(root_devno, &whole_root_devno); |
| 110 |
if (r < 0) |
| 111 |
log_debug_errno(r, "Failed to get whole block device of root device, ignoring: %m"); |
| 112 |
} |
| 113 |
|
| 114 |
/* It's fine if root_devno/whole_root_devno are zero here as devnum_set_and_equal() will |
| 115 |
* happily take that into account – it is in fact its primary raison d'etre. */ |
| 116 |
} |
| 117 |
|
| 118 |
if (sd_device_enumerator_new(&e) < 0) |
| 119 |
return log_oom(); |
| 120 |
|
| 121 |
r = sd_device_enumerator_add_match_subsystem(e, "block", /* match= */ true); |
| 122 |
if (r < 0) |
| 123 |
return log_error_errno(r, "Failed to add subsystem match: %m"); |
| 124 |
|
| 125 |
if (FLAGS_SET(flags, BLOCKDEV_LIST_REQUIRE_LUKS)) { |
| 126 |
r = sd_device_enumerator_add_match_property(e, "ID_FS_TYPE", "crypto_LUKS"); |
| 127 |
if (r < 0) |
| 128 |
return log_error_errno(r, "Failed to add match for LUKS block devices: %m"); |
| 129 |
} |
| 130 |
|
| 131 |
FOREACH_DEVICE(e, dev) { |
| 132 |
const char *node; |
| 133 |
|
| 134 |
r = sd_device_get_devname(dev, &node); |
| 135 |
if (r < 0) { |
| 136 |
log_device_warning_errno(dev, r, "Failed to get device node of discovered block device, ignoring: %m"); |
| 137 |
continue; |
| 138 |
} |
| 139 |
|
| 140 |
if (FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_ROOT) && root_devno != 0) { |
| 141 |
dev_t devno; |
| 142 |
|
| 143 |
r = sd_device_get_devnum(dev, &devno); |
| 144 |
if (r < 0) { |
| 145 |
log_device_warning_errno(dev, r, "Failed to get major/minor of discovered block device, ignoring: %m"); |
| 146 |
continue; |
| 147 |
} |
| 148 |
|
| 149 |
if (devnum_set_and_equal(devno, root_devno) || |
| 150 |
devnum_set_and_equal(devno, whole_root_devno)) |
| 151 |
continue; |
| 152 |
} |
| 153 |
|
| 154 |
if (FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_ZRAM)) { |
| 155 |
r = device_sysname_startswith(dev, "zram"); |
| 156 |
if (r < 0) { |
| 157 |
log_device_warning_errno(dev, r, "Failed to check device name of discovered block device '%s', ignoring: %m", node); |
| 158 |
continue; |
| 159 |
} |
| 160 |
if (r > 0) |
| 161 |
continue; |
| 162 |
} |
| 163 |
|
| 164 |
if (FLAGS_SET(flags, BLOCKDEV_LIST_REQUIRE_PARTITION_SCANNING)) { |
| 165 |
r = blockdev_partscan_enabled(dev); |
| 166 |
if (r < 0) { |
| 167 |
log_device_warning_errno(dev, r, "Unable to determine whether '%s' supports partition scanning, skipping device: %m", node); |
| 168 |
continue; |
| 169 |
} |
| 170 |
if (r == 0) { |
| 171 |
log_device_debug(dev, "Device '%s' does not support partition scanning, skipping.", node); |
| 172 |
continue; |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
uint64_t size = UINT64_MAX; |
| 177 |
if (FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_EMPTY) || ret_devices) { |
| 178 |
r = device_get_sysattr_u64(dev, "size", &size); |
| 179 |
if (r < 0) |
| 180 |
log_device_debug_errno(dev, r, "Failed to acquire size of device '%s', ignoring: %m", node); |
| 181 |
else |
| 182 |
/* the 'size' sysattr is always in multiples of 512, even on 4K sector block devices! */ |
| 183 |
assert_se(MUL_ASSIGN_SAFE(&size, 512)); /* Overflow check for coverity */ |
| 184 |
|
| 185 |
if (size == 0 && FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_EMPTY)) { |
| 186 |
log_device_debug(dev, "Device '%s' has a zero size, assuming drive without a medium, skipping.", node); |
| 187 |
continue; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
int ro = -1; |
| 192 |
if (FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_READ_ONLY) || FLAGS_SET(flags, BLOCKDEV_LIST_METADATA)) { |
| 193 |
r = device_get_sysattr_bool(dev, "ro"); |
| 194 |
if (r < 0) |
| 195 |
log_device_debug_errno(dev, r, "Failed to acquire read-only flag of device '%s', ignoring: %m", node); |
| 196 |
else |
| 197 |
ro = r; |
| 198 |
|
| 199 |
if (ro > 0 && FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_READ_ONLY)) { |
| 200 |
log_device_debug(dev, "Device '%s' is read-only, skipping.", node); |
| 201 |
continue; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
_cleanup_strv_free_ char **list = NULL; |
| 206 |
if (FLAGS_SET(flags, BLOCKDEV_LIST_SHOW_SYMLINKS)) { |
| 207 |
FOREACH_DEVICE_DEVLINK(dev, sl) |
| 208 |
if (strv_extend(&list, sl) < 0) |
| 209 |
return log_oom(); |
| 210 |
|
| 211 |
strv_sort(list); |
| 212 |
} |
| 213 |
|
| 214 |
_cleanup_free_ char *model = NULL, *vendor = NULL, *subsystem = NULL; |
| 215 |
if (FLAGS_SET(flags, BLOCKDEV_LIST_METADATA)) { |
| 216 |
(void) blockdev_get_prop(dev, "ID_MODEL_FROM_DATABASE", "ID_MODEL", &model); |
| 217 |
(void) blockdev_get_prop(dev, "ID_VENDOR_FROM_DATABASE", "ID_VENDOR", &vendor); |
| 218 |
(void) blockdev_get_subsystem(dev, &subsystem); |
| 219 |
} |
| 220 |
|
| 221 |
if (ret_devices) { |
| 222 |
uint64_t diskseq = UINT64_MAX; |
| 223 |
r = sd_device_get_diskseq(dev, &diskseq); |
| 224 |
if (r < 0) |
| 225 |
log_device_debug_errno(dev, r, "Failed to acquire diskseq of device '%s', ignoring: %m", node); |
| 226 |
|
| 227 |
if (!GREEDY_REALLOC(l, n+1)) |
| 228 |
return log_oom(); |
| 229 |
|
| 230 |
_cleanup_free_ char *m = strdup(node); |
| 231 |
if (!m) |
| 232 |
return log_oom(); |
| 233 |
|
| 234 |
l[n++] = (BlockDevice) { |
| 235 |
.node = TAKE_PTR(m), |
| 236 |
.symlinks = TAKE_PTR(list), |
| 237 |
.diskseq = diskseq, |
| 238 |
.size = size, |
| 239 |
.model = TAKE_PTR(model), |
| 240 |
.vendor = TAKE_PTR(vendor), |
| 241 |
.subsystem = TAKE_PTR(subsystem), |
| 242 |
.read_only = ro, |
| 243 |
}; |
| 244 |
|
| 245 |
} else { |
| 246 |
printf("%s\n", node); |
| 247 |
|
| 248 |
if (FLAGS_SET(flags, BLOCKDEV_LIST_SHOW_SYMLINKS)) |
| 249 |
STRV_FOREACH(i, list) |
| 250 |
printf("%s%s%s%s\n", on_tty() ? " " : "", ansi_grey(), *i, ansi_normal()); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
if (ret_devices) |
| 255 |
*ret_devices = TAKE_PTR(l); |
| 256 |
if (ret_n_devices) |
| 257 |
*ret_n_devices = n; |
| 258 |
|
| 259 |
return 0; |
| 260 |
} |