• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 25196166722

30 Apr 2026 07:30PM UTC coverage: 72.134% (+0.3%) from 71.849%
25196166722

push

github

bluca
mkosi: update debian commit reference to 1302f123d

* 1302f123d9 Restrict wildcard for new files
* a6d0098d10 Install new files for upstream build
* ce07fd7616 d/t/boot-and-services: use coreutils tunable in apparmor test (LP: #2125614)

324804 of 450278 relevant lines covered (72.13%)

1187716.32 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.58
/src/shared/blockdev-list.c
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) {
19✔
18
        assert(d);
19✔
19

20
        d->node = mfree(d->node);
19✔
21
        d->symlinks = strv_free(d->symlinks);
19✔
22
        d->model = mfree(d->model);
19✔
23
        d->vendor = mfree(d->vendor);
19✔
24
        d->subsystem = mfree(d->subsystem);
19✔
25
 }
19✔
26

27
void block_device_array_free(BlockDevice *d, size_t n_devices) {
9✔
28

29
        FOREACH_ARRAY(i, d, n_devices)
28✔
30
                block_device_done(i);
19✔
31

32
        free(d);
9✔
33
}
9✔
34

35
static int blockdev_get_prop(sd_device *d, const char *prop1, const char *prop2, char **ret_value) {
57✔
36
        int r, ret = 0;
57✔
37

38
        assert(d);
57✔
39
        assert(prop1);
57✔
40
        assert(ret_value);
57✔
41

42
        FOREACH_STRING(prop, prop1, prop2) {
151✔
43
                const char *m = NULL;
95✔
44
                r = sd_device_get_property_value(d, prop, &m);
95✔
45
                if (r < 0 && r != -ENOENT)
95✔
46
                        RET_GATHER(ret, log_device_debug_errno(d, r, "Failed to acquire '%s' from device, ignoring: %m", prop));
×
47
                else if (!isempty(m))
95✔
48
                        return strdup_to(ret_value, m);
1✔
49
        }
50

51
        return ret < 0 ? ret : -ENOENT;
56✔
52
}
53

54
static int blockdev_get_subsystem(sd_device *d, char **ret_subsystem) {
19✔
55
        int r;
19✔
56

57
        assert(d);
19✔
58
        assert(ret_subsystem);
19✔
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);
19✔
66
        if (r >= 0)
19✔
67
                return r;
19✔
68

69
        int ret = r != -ENOENT ? r : 0;
18✔
70
        sd_device *q = d;
18✔
71
        for (;;) {
54✔
72
                r = sd_device_get_parent(q, &q);
36✔
73
                if (r < 0) {
36✔
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;
36✔
80
                r = sd_device_get_subsystem(q, &s);
36✔
81
                if (r < 0)
36✔
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"))
72✔
84
                        return strdup_to(ret_subsystem, s);
18✔
85
        }
86

87
        return ret < 0 ? ret : -ENOENT;
×
88
}
89

90
int blockdev_list(BlockDevListFlags flags, BlockDevice **ret_devices, size_t *ret_n_devices) {
15✔
91
        _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
15✔
92
        int r;
15✔
93

94
        assert(!!ret_devices == !!ret_n_devices);
15✔
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;
15✔
100
        size_t n = 0;
15✔
101
        CLEANUP_ARRAY(l, n, block_device_array_free);
15✔
102

103
        dev_t root_devno = 0, whole_root_devno = 0;
15✔
104
        if (FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_ROOT)) {
15✔
105
                r = blockdev_get_root(LOG_DEBUG, &root_devno);
11✔
106
                if (r < 0)
11✔
107
                        log_debug_errno(r, "Failed to get block device of root device, ignoring: %m");
×
108
                else if (r > 0) {
11✔
109
                        r = block_get_whole_disk(root_devno, &whole_root_devno);
11✔
110
                        if (r < 0)
11✔
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)
15✔
119
                return log_oom();
×
120

121
        r = sd_device_enumerator_add_match_subsystem(e, "block", /* match= */ true);
15✔
122
        if (r < 0)
15✔
123
                return log_error_errno(r, "Failed to add subsystem match: %m");
×
124

125
        if (FLAGS_SET(flags, BLOCKDEV_LIST_REQUIRE_LUKS)) {
15✔
126
                r = sd_device_enumerator_add_match_property(e, "ID_FS_TYPE", "crypto_LUKS");
1✔
127
                if (r < 0)
1✔
128
                        return log_error_errno(r, "Failed to add match for LUKS block devices: %m");
×
129
        }
130

131
        FOREACH_DEVICE(e, dev) {
103✔
132
                const char *node;
88✔
133

134
                r = sd_device_get_devname(dev, &node);
88✔
135
                if (r < 0) {
88✔
136
                        log_device_warning_errno(dev, r, "Failed to get device node of discovered block device, ignoring: %m");
×
137
                        continue;
62✔
138
                }
139

140
                if (FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_ROOT) && root_devno != 0) {
88✔
141
                        dev_t devno;
73✔
142

143
                        r = sd_device_get_devnum(dev, &devno);
73✔
144
                        if (r < 0) {
73✔
145
                                log_device_warning_errno(dev, r, "Failed to get major/minor of discovered block device, ignoring: %m");
×
146
                                continue;
22✔
147
                        }
148

149
                        if (devnum_set_and_equal(devno, root_devno) ||
73✔
150
                            devnum_set_and_equal(devno, whole_root_devno))
62✔
151
                                continue;
22✔
152
                }
153

154
                if (FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_ZRAM)) {
66✔
155
                        r = device_sysname_startswith(dev, "zram");
20✔
156
                        if (r < 0) {
20✔
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)
20✔
161
                                continue;
×
162
                }
163

164
                if (FLAGS_SET(flags, BLOCKDEV_LIST_REQUIRE_PARTITION_SCANNING)) {
66✔
165
                        r = blockdev_partscan_enabled(dev);
16✔
166
                        if (r < 0) {
16✔
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) {
16✔
171
                                log_device_debug(dev, "Device '%s' does not support partition scanning, skipping.", node);
14✔
172
                                continue;
14✔
173
                        }
174
                }
175

176
                uint64_t size = UINT64_MAX;
52✔
177
                if (FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_EMPTY) || ret_devices) {
52✔
178
                        r = device_get_sysattr_u64(dev, "size", &size);
45✔
179
                        if (r < 0)
45✔
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 */
45✔
184

185
                        if (size == 0 && FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_EMPTY)) {
45✔
186
                                log_device_debug(dev, "Device '%s' has a zero size, assuming drive without a medium, skipping.", node);
26✔
187
                                continue;
26✔
188
                        }
189
                }
190

191
                int ro = -1;
26✔
192
                if (FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_READ_ONLY) || FLAGS_SET(flags, BLOCKDEV_LIST_METADATA)) {
26✔
193
                        r = device_get_sysattr_bool(dev, "ro");
21✔
194
                        if (r < 0)
21✔
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)) {
21✔
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)) {
26✔
207
                        FOREACH_DEVICE_DEVLINK(dev, sl)
316✔
208
                                if (strv_extend(&list, sl) < 0)
290✔
209
                                        return log_oom();
×
210

211
                        strv_sort(list);
26✔
212
                }
213

214
                _cleanup_free_ char *model = NULL, *vendor = NULL, *subsystem = NULL;
26✔
215
                if (FLAGS_SET(flags, BLOCKDEV_LIST_METADATA)) {
26✔
216
                        (void) blockdev_get_prop(dev, "ID_MODEL_FROM_DATABASE", "ID_MODEL", &model);
19✔
217
                        (void) blockdev_get_prop(dev, "ID_VENDOR_FROM_DATABASE", "ID_VENDOR", &vendor);
19✔
218
                        (void) blockdev_get_subsystem(dev, &subsystem);
19✔
219
                }
220

221
                if (ret_devices) {
26✔
222
                        uint64_t diskseq = UINT64_MAX;
19✔
223
                        r = sd_device_get_diskseq(dev, &diskseq);
19✔
224
                        if (r < 0)
19✔
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))
19✔
228
                                return log_oom();
×
229

230
                        _cleanup_free_ char *m = strdup(node);
19✔
231
                        if (!m)
19✔
232
                                return log_oom();
×
233

234
                        l[n++] = (BlockDevice) {
19✔
235
                                .node = TAKE_PTR(m),
19✔
236
                                .symlinks = TAKE_PTR(list),
19✔
237
                                .diskseq = diskseq,
238
                                .size = size,
239
                                .model = TAKE_PTR(model),
19✔
240
                                .vendor = TAKE_PTR(vendor),
19✔
241
                                .subsystem = TAKE_PTR(subsystem),
19✔
242
                                .read_only = ro,
243
                        };
244

245
                } else {
246
                        printf("%s\n", node);
7✔
247

248
                        if (FLAGS_SET(flags, BLOCKDEV_LIST_SHOW_SYMLINKS))
7✔
249
                                STRV_FOREACH(i, list)
67✔
250
                                        printf("%s%s%s%s\n", on_tty() ? "    " : "", ansi_grey(), *i, ansi_normal());
180✔
251
                }
252
        }
253

254
        if (ret_devices)
15✔
255
                *ret_devices = TAKE_PTR(l);
11✔
256
        if (ret_n_devices)
15✔
257
                *ret_n_devices = n;
11✔
258

259
        return 0;
260
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc