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

systemd / systemd / 27854786886

20 Jun 2026 12:28AM UTC coverage: 73.048% (+0.05%) from 72.995%
27854786886

push

github

bluca
report: disable json normalization

Two PRs got merged at the same time, which cause a test to fail,
as they work individually but fail when combined

TEST-74-AUX-UTILS.sh[1688]: + /usr/lib/systemd/systemd-report generate io.systemd.Manager.UnitsTotal
TEST-74-AUX-UTILS.sh[1805]: {"mediaType":"application/vnd.io.systemd.report","metrics":[{"name":"io.systemd.Manager.UnitsTotal","value":249}],"timestamp":"Fri 2026-06-19 19:50:48 UTC"}
TEST-74-AUX-UTILS.sh[1806]: + /usr/lib/systemd/systemd-report generate io.systemd.Manager.UnitsTotal
TEST-74-AUX-UTILS.sh[1807]: + jq .
TEST-74-AUX-UTILS.sh[1807]: {
TEST-74-AUX-UTILS.sh[1807]:   "mediaType": "application/vnd.io.systemd.report",
TEST-74-AUX-UTILS.sh[1807]:   "metrics": [
TEST-74-AUX-UTILS.sh[1807]: {
TEST-74-AUX-UTILS.sh[1807]:   "name": "io.systemd.Manager.UnitsTotal",
TEST-74-AUX-UTILS.sh[1807]:   "value": 249
TEST-74-AUX-UTILS.sh[1807]: }
TEST-74-AUX-UTILS.sh[1807]:   ],
TEST-74-AUX-UTILS.sh[1807]:   "timestamp": "Fri 2026-06-19 19:50:48 UTC"
TEST-74-AUX-UTILS.sh[1807]: }
TEST-74-AUX-UTILS.sh[1688]: + /usr/lib/systemd/systemd-report upload --url=http://localhost:8089/
TEST-74-AUX-UTILS.sh[1808]: Failed to normalize report JSON: Wrong medium type

https://github.com/systemd/systemd/pull/42594
https://github.com/systemd/systemd/pull/42595

Disable normalization for now, and track the issue at
https://github.com/systemd/systemd/issues/42669

Follow-up for 3c2f7c600

339084 of 464191 relevant lines covered (73.05%)

1311580.8 hits per line

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

81.12
/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) {
107✔
18
        assert(d);
107✔
19

20
        d->node = mfree(d->node);
107✔
21
        d->symlinks = strv_free(d->symlinks);
107✔
22
        d->model = mfree(d->model);
107✔
23
        d->vendor = mfree(d->vendor);
107✔
24
        d->subsystem = mfree(d->subsystem);
107✔
25
 }
107✔
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) {
135✔
36
        int r, ret = 0;
135✔
37

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

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

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

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

57
        assert(d);
45✔
58
        assert(ret_subsystem);
45✔
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);
45✔
66
        if (r >= 0)
45✔
67
                return r;
45✔
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_get_root_devnos(BlockDevListFlags flags, dev_t *ret_root, dev_t *ret_whole_root) {
15✔
91
        int r;
15✔
92

93
        /* Looks up the devno of the block device the OS is booted from, plus the whole-disk devno (e.g.
94
         * /dev/sda for /dev/sda3). Returns both as 0 if root can't be determined – which is fine, callers
95
         * pass these to blockdev_list_one() and devnum_set_and_equal() handles zero gracefully.
96
         *
97
         * Split out of blockdev_list() so subscribers can look them up once up-front instead of per uevent. */
98

99
        dev_t root_devno = 0, whole_root_devno = 0;
15✔
100

101
        if (FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_ROOT)) {
15✔
102
                r = blockdev_get_root(LOG_DEBUG, &root_devno);
11✔
103
                if (r < 0)
11✔
104
                        log_debug_errno(r, "Failed to get block device of root device, ignoring: %m");
×
105
                else if (r > 0) {
11✔
106
                        r = block_get_whole_disk(root_devno, &whole_root_devno);
11✔
107
                        if (r < 0)
11✔
108
                                log_debug_errno(r, "Failed to get whole block device of root device, ignoring: %m");
×
109
                }
110
        }
111

112
        if (ret_root)
15✔
113
                *ret_root = root_devno;
15✔
114
        if (ret_whole_root)
15✔
115
                *ret_whole_root = whole_root_devno;
15✔
116

117
        return 0;
15✔
118
}
119

120
int blockdev_list_one(
88✔
121
                sd_device *dev,
122
                BlockDevListFlags flags,
123
                dev_t root_devno,
124
                dev_t whole_root_devno,
125
                BlockDevice *ret) {
126

127
        int r;
88✔
128

129
        assert(dev);
88✔
130

131
        /* Per-device counterpart to blockdev_list(). Applies the same filters and metadata collection,
132
         * and reports via a multi-valued return:
133
         *
134
         *   MATCH_NO       – cleanly fails a static filter (never interesting)
135
         *   MATCH_YES      – fully matches
136
         *   MATCH_FILTERED – passes static filters but fails a dynamic one (IGNORE_EMPTY / IGNORE_READ_ONLY)
137
         *   MATCH_SKIPPED  – an unexpected error tripped during filter evaluation; the caller should
138
         *                   treat this like _FILTERED, but the distinct code makes the soft failure
139
         *                   visible rather than silently swallowed here.
140
         *
141
         * On each of the four success codes *ret is initialized: BLOCK_DEVICE_NULL for MATCH_NO and
142
         * MATCH_SKIPPED, fully populated for MATCH_YES / MATCH_FILTERED (with d.size / d.read_only
143
         * reflecting the current state so callers can tell which dynamic filter tripped). On negative
144
         * return *ret is untouched.
145
         *
146
         * blockdev_list() itself collapses everything except _YES back into "skip". */
147

148
        const char *node;
88✔
149
        r = sd_device_get_devname(dev, &node);
88✔
150
        if (r < 0) {
88✔
151
                log_device_warning_errno(dev, r, "Failed to get device node of discovered block device, ignoring: %m");
×
152
                goto skipped;
×
153
        }
154

155
        if (FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_ROOT) && root_devno != 0) {
88✔
156
                dev_t devno;
73✔
157

158
                r = sd_device_get_devnum(dev, &devno);
73✔
159
                if (r < 0) {
73✔
160
                        log_device_warning_errno(dev, r, "Failed to get major/minor of discovered block device, ignoring: %m");
×
161
                        goto skipped;
×
162
                }
163

164
                if (devnum_set_and_equal(devno, root_devno) ||
73✔
165
                    devnum_set_and_equal(devno, whole_root_devno))
62✔
166
                        goto no_match;
22✔
167
        }
168

169
        if (FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_ZRAM)) {
66✔
170
                r = device_sysname_startswith(dev, "zram");
20✔
171
                if (r < 0) {
20✔
172
                        log_device_warning_errno(dev, r, "Failed to check device name of discovered block device '%s', ignoring: %m", node);
×
173
                        goto skipped;
×
174
                }
175
                if (r > 0)
20✔
176
                        goto no_match;
×
177
        }
178

179
        if (FLAGS_SET(flags, BLOCKDEV_LIST_REQUIRE_PARTITION_SCANNING)) {
66✔
180
                r = blockdev_partscan_enabled(dev);
16✔
181
                if (r < 0) {
16✔
182
                        log_device_warning_errno(dev, r, "Unable to determine whether '%s' supports partition scanning, skipping device: %m", node);
×
183
                        goto skipped;
×
184
                }
185
                if (r == 0) {
16✔
186
                        log_device_debug(dev, "Device '%s' does not support partition scanning, skipping.", node);
14✔
187
                        goto no_match;
14✔
188
                }
189
        }
190

191
        if (FLAGS_SET(flags, BLOCKDEV_LIST_REQUIRE_LUKS)) {
52✔
192
                const char *fstype;
1✔
193
                r = sd_device_get_property_value(dev, "ID_FS_TYPE", &fstype);
1✔
194
                if (r == -ENOENT)
1✔
195
                        goto no_match; /* No detected filesystem → not a LUKS superblock. */
×
196
                if (r < 0) {
1✔
197
                        log_device_warning_errno(dev, r, "Failed to acquire ID_FS_TYPE of device '%s', ignoring: %m", node);
×
198
                        goto skipped;
×
199
                }
200
                if (!streq(fstype, "crypto_LUKS"))
1✔
201
                        goto no_match;
×
202
        }
203

204
        bool dynamic_filtered = false;
52✔
205

206
        uint64_t size = UINT64_MAX;
52✔
207
        if (FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_EMPTY) || ret) {
52✔
208
                r = device_get_sysattr_u64(dev, "size", &size);
45✔
209
                if (r < 0)
45✔
210
                        log_device_debug_errno(dev, r, "Failed to acquire size of device '%s', ignoring: %m", node);
×
211
                else
212
                        /* the 'size' sysattr is always in multiples of 512, even on 4K sector block devices! */
213
                        assert_se(MUL_ASSIGN_SAFE(&size, 512)); /* Overflow check for coverity */
45✔
214

215
                if (size == 0 && FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_EMPTY)) {
45✔
216
                        log_device_debug(dev, "Device '%s' has a zero size, assuming drive without a medium.", node);
26✔
217
                        dynamic_filtered = true;
26✔
218
                }
219
        }
220

221
        int ro = -1;
52✔
222
        if (FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_READ_ONLY) || FLAGS_SET(flags, BLOCKDEV_LIST_METADATA)) {
52✔
223
                r = device_get_sysattr_bool(dev, "ro");
47✔
224
                if (r < 0)
47✔
225
                        log_device_debug_errno(dev, r, "Failed to acquire read-only flag of device '%s', ignoring: %m", node);
×
226
                else
227
                        ro = r;
228

229
                if (ro > 0 && FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_READ_ONLY)) {
47✔
230
                        log_device_debug(dev, "Device '%s' is read-only.", node);
×
231
                        dynamic_filtered = true;
×
232
                }
233
        }
234

235
        if (!ret)
52✔
236
                return dynamic_filtered ? BLOCKDEV_LIST_MATCH_FILTERED : BLOCKDEV_LIST_MATCH_YES;
95✔
237

238
        /* Wrap the metadata population in a nested scope so the cleanup-attributed locals don't
239
         * intersect the goto-target label below (jumping into a protected scope is a clang error). */
240
        {
241
                _cleanup_strv_free_ char **list = NULL;
45✔
242
                if (FLAGS_SET(flags, BLOCKDEV_LIST_SHOW_SYMLINKS)) {
45✔
243
                        FOREACH_DEVICE_DEVLINK(dev, sl)
301✔
244
                                if (strv_extend(&list, sl) < 0)
256✔
245
                                        return log_oom();
×
246

247
                        strv_sort(list);
45✔
248
                }
249

250
                _cleanup_free_ char *model = NULL, *vendor = NULL, *subsystem = NULL;
×
251
                if (FLAGS_SET(flags, BLOCKDEV_LIST_METADATA)) {
45✔
252
                        (void) blockdev_get_prop(dev, "ID_MODEL_FROM_DATABASE", "ID_MODEL", &model);
45✔
253
                        (void) blockdev_get_prop(dev, "ID_VENDOR_FROM_DATABASE", "ID_VENDOR", &vendor);
45✔
254
                        (void) blockdev_get_subsystem(dev, &subsystem);
45✔
255
                }
256

257
                uint64_t diskseq = UINT64_MAX;
45✔
258
                r = sd_device_get_diskseq(dev, &diskseq);
45✔
259
                if (r < 0)
45✔
260
                        log_device_debug_errno(dev, r, "Failed to acquire diskseq of device '%s', ignoring: %m", node);
×
261

262
                _cleanup_free_ char *m = strdup(node);
45✔
263
                if (!m)
45✔
264
                        return log_oom();
×
265

266
                *ret = (BlockDevice) {
45✔
267
                        .node = TAKE_PTR(m),
45✔
268
                        .symlinks = TAKE_PTR(list),
45✔
269
                        .diskseq = diskseq,
270
                        .size = size,
271
                        .model = TAKE_PTR(model),
45✔
272
                        .vendor = TAKE_PTR(vendor),
45✔
273
                        .subsystem = TAKE_PTR(subsystem),
45✔
274
                        .read_only = ro,
275
                };
276

277
                return dynamic_filtered ? BLOCKDEV_LIST_MATCH_FILTERED : BLOCKDEV_LIST_MATCH_YES;
45✔
278
        }
279

280
no_match:
36✔
281
        if (ret)
36✔
282
                *ret = BLOCK_DEVICE_NULL;
28✔
283
        return BLOCKDEV_LIST_MATCH_NO;
284

285
skipped:
×
286
        if (ret)
×
287
                *ret = BLOCK_DEVICE_NULL;
×
288
        return BLOCKDEV_LIST_MATCH_SKIPPED;
289
}
290

291
int blockdev_list(BlockDevListFlags flags, BlockDevice **ret_devices, size_t *ret_n_devices) {
15✔
292
        _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
15✔
293
        int r;
15✔
294

295
        assert(!!ret_devices == !!ret_n_devices);
15✔
296

297
        /* If ret_devices/ret_n_devices are passed, returns a list of matching block devices, otherwise
298
         * prints the list to stdout */
299

300
        BlockDevice *l = NULL;
15✔
301
        size_t n = 0;
15✔
302
        CLEANUP_ARRAY(l, n, block_device_array_free);
15✔
303

304
        dev_t root_devno = 0, whole_root_devno = 0;
15✔
305
        (void) blockdev_list_get_root_devnos(flags, &root_devno, &whole_root_devno);
15✔
306

307
        if (sd_device_enumerator_new(&e) < 0)
15✔
308
                return log_oom();
×
309

310
        r = sd_device_enumerator_add_match_subsystem(e, "block", /* match= */ true);
15✔
311
        if (r < 0)
15✔
312
                return log_error_errno(r, "Failed to add subsystem match: %m");
×
313

314
        if (FLAGS_SET(flags, BLOCKDEV_LIST_REQUIRE_LUKS)) {
15✔
315
                /* blockdev_list_one() enforces this filter authoritatively; the enumerator match is just
316
                 * an optimization so we don't iterate non-LUKS devices here. */
317
                r = sd_device_enumerator_add_match_property(e, "ID_FS_TYPE", "crypto_LUKS");
1✔
318
                if (r < 0)
1✔
319
                        return log_error_errno(r, "Failed to add match for LUKS block devices: %m");
×
320
        }
321

322
        FOREACH_DEVICE(e, dev) {
103✔
323
                _cleanup_(block_device_done) BlockDevice d = BLOCK_DEVICE_NULL;
88✔
324

325
                r = blockdev_list_one(dev, flags, root_devno, whole_root_devno, ret_devices ? &d : NULL);
103✔
326
                if (r < 0)
88✔
327
                        return r;
328
                if (r != BLOCKDEV_LIST_MATCH_YES)
88✔
329
                        continue; /* MATCH_NO or MATCH_FILTERED – bulk enumeration treats both as "skip" */
62✔
330

331
                if (ret_devices) {
26✔
332
                        if (!GREEDY_REALLOC(l, n+1))
19✔
333
                                return log_oom();
×
334

335
                        l[n++] = TAKE_STRUCT(d);
19✔
336
                } else {
337
                        const char *node;
7✔
338
                        if (sd_device_get_devname(dev, &node) < 0)
7✔
339
                                continue;
×
340

341
                        printf("%s\n", node);
7✔
342

343
                        if (FLAGS_SET(flags, BLOCKDEV_LIST_SHOW_SYMLINKS))
7✔
344
                                FOREACH_DEVICE_DEVLINK(dev, sl)
70✔
345
                                        printf("%s%s%s%s\n", on_tty() ? "    " : "", ansi_grey(), sl, ansi_normal());
189✔
346
                }
347
        }
348

349
        if (ret_devices)
15✔
350
                *ret_devices = TAKE_PTR(l);
11✔
351
        if (ret_n_devices)
15✔
352
                *ret_n_devices = n;
11✔
353

354
        return 0;
355
}
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