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

systemd / systemd / 19020191358

02 Nov 2025 05:04PM UTC coverage: 72.222% (-0.02%) from 72.241%
19020191358

push

github

web-flow
Enhance docs for ukify and direct kernel boots (#39516)

305246 of 422650 relevant lines covered (72.22%)

1085243.28 hits per line

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

39.86
/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 "errno-util.h"
12
#include "string-util.h"
13
#include "strv.h"
14
#include "terminal-util.h"
15

16
void block_device_done(BlockDevice *d) {
×
17
        assert(d);
×
18

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

26
void block_device_array_free(BlockDevice *d, size_t n_devices) {
×
27

28
        FOREACH_ARRAY(i, d, n_devices)
×
29
                block_device_done(d);
×
30

31
        free(d);
×
32
}
×
33

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

37
        assert(d);
×
38
        assert(prop1);
×
39
        assert(ret_value);
×
40

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

50
        return ret < 0 ? ret : -ENOENT;
×
51
}
52

53
static int blockdev_get_subsystem(sd_device *d, char **ret_subsystem) {
×
54
        int r;
×
55

56
        assert(d);
×
57
        assert(ret_subsystem);
×
58

59
        /* We prefer the explicitly set block device subsystem property, because if it is set it's generally
60
         * the most useful. If it's not set we'll look for the subsystem of the first parent device that
61
         * isn't of subsystem 'block'. The former covers 'virtual' block devices such as loopback, device
62
         * mapper, zram, while the latter covers physical block devices such as USB or NVME. */
63

64
        r = blockdev_get_prop(d, "ID_BLOCK_SUBSYSTEM", /* prop2= */ NULL, ret_subsystem);
×
65
        if (r >= 0)
×
66
                return r;
×
67

68
        int ret = r != -ENOENT ? r : 0;
×
69
        sd_device *q = d;
×
70
        for (;;) {
×
71
                r = sd_device_get_parent(q, &q);
×
72
                if (r < 0) {
×
73
                        if (r != -ENOENT)
×
74
                                RET_GATHER(ret, log_device_debug_errno(q, r, "Failed to get parent device, ignoring: %m"));
×
75
                        break;
×
76
                }
77

78
                const char *s = NULL;
×
79
                r = sd_device_get_subsystem(q, &s);
×
80
                if (r < 0)
×
81
                        RET_GATHER(ret, log_device_debug_errno(q, r, "Failed to get subsystem of device, ignoring: %m"));
×
82
                else if (!isempty(s) && !streq(s, "block"))
×
83
                        return strdup_to(ret_subsystem, s);
×
84
        }
85

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

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

93
        assert(!!ret_devices == !!ret_n_devices);
6✔
94

95
        /* If ret_devices/ret_n_devices are passed, returns a list of matching block devices, otherwise
96
         * prints the list to stdout */
97

98
        BlockDevice *l = NULL;
6✔
99
        size_t n = 0;
6✔
100
        CLEANUP_ARRAY(l, n, block_device_array_free);
6✔
101

102
        dev_t root_devno = 0;
6✔
103
        if (FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_ROOT))
6✔
104
                if (blockdev_get_root(LOG_DEBUG, &root_devno) > 0) {
2✔
105
                        r = block_get_whole_disk(root_devno, &root_devno);
2✔
106
                        if (r < 0)
2✔
107
                                log_debug_errno(r, "Failed to get whole block device of root device: %m");
×
108
                }
109

110
        if (sd_device_enumerator_new(&e) < 0)
6✔
111
                return log_oom();
×
112

113
        r = sd_device_enumerator_add_match_subsystem(e, "block", /* match = */ true);
6✔
114
        if (r < 0)
6✔
115
                return log_error_errno(r, "Failed to add subsystem match: %m");
×
116

117
        if (FLAGS_SET(flags, BLOCKDEV_LIST_REQUIRE_LUKS)) {
6✔
118
                r = sd_device_enumerator_add_match_property(e, "ID_FS_TYPE", "crypto_LUKS");
1✔
119
                if (r < 0)
1✔
120
                        return log_error_errno(r, "Failed to add match for LUKS block devices: %m");
×
121
        }
122

123
        FOREACH_DEVICE(e, dev) {
29✔
124
                const char *node;
23✔
125

126
                r = sd_device_get_devname(dev, &node);
23✔
127
                if (r < 0) {
23✔
128
                        log_device_warning_errno(dev, r, "Failed to get device node of discovered block device, ignoring: %m");
×
129
                        continue;
16✔
130
                }
131

132
                if (FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_ROOT) && root_devno != 0) {
23✔
133
                        dev_t devno;
10✔
134

135
                        r = sd_device_get_devnum(dev, &devno);
10✔
136
                        if (r < 0) {
10✔
137
                                log_device_warning_errno(dev, r, "Failed to get major/minor of discovered block device, ignoring: %m");
×
138
                                continue;
2✔
139
                        }
140

141
                        if (devno == root_devno)
10✔
142
                                continue;
2✔
143
                }
144

145
                if (FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_ZRAM)) {
21✔
146
                        r = device_sysname_startswith(dev, "zram");
20✔
147
                        if (r < 0) {
20✔
148
                                log_device_warning_errno(dev, r, "Failed to check device name of discovered block device '%s', ignoring: %m", node);
×
149
                                continue;
×
150
                        }
151
                        if (r > 0)
20✔
152
                                continue;
×
153
                }
154

155
                if (FLAGS_SET(flags, BLOCKDEV_LIST_REQUIRE_PARTITION_SCANNING)) {
21✔
156
                        r = blockdev_partscan_enabled(dev);
16✔
157
                        if (r < 0) {
16✔
158
                                log_device_warning_errno(dev, r, "Unable to determine whether '%s' supports partition scanning, skipping device: %m", node);
×
159
                                continue;
×
160
                        }
161
                        if (r == 0) {
16✔
162
                                log_device_debug(dev, "Device '%s' does not support partition scanning, skipping.", node);
14✔
163
                                continue;
14✔
164
                        }
165
                }
166

167
                uint64_t size = UINT64_MAX;
7✔
168
                if (FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_EMPTY) || ret_devices) {
7✔
169
                        r = device_get_sysattr_u64(dev, "size", &size);
×
170
                        if (r < 0)
×
171
                                log_device_debug_errno(dev, r, "Failed to acquire size of device '%s', ignoring: %m", node);
×
172
                        else
173
                                /* the 'size' sysattr is always in multiples of 512, even on 4K sector block devices! */
174
                                assert_se(MUL_ASSIGN_SAFE(&size, 512)); /* Overflow check for coverity */
×
175

176
                        if (size == 0 && FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_EMPTY)) {
×
177
                                log_device_debug(dev, "Device '%s' has a zero size, assuming drive without a medium, skipping.", node);
×
178
                                continue;
×
179
                        }
180
                }
181

182
                _cleanup_strv_free_ char **list = NULL;
×
183
                if (FLAGS_SET(flags, BLOCKDEV_LIST_SHOW_SYMLINKS)) {
7✔
184
                        FOREACH_DEVICE_DEVLINK(dev, sl)
67✔
185
                                if (strv_extend(&list, sl) < 0)
60✔
186
                                        return log_oom();
×
187

188
                        strv_sort(list);
7✔
189
                }
190

191
                _cleanup_free_ char *model = NULL, *vendor = NULL, *subsystem = NULL;
7✔
192
                if (FLAGS_SET(flags, BLOCKDEV_LIST_METADATA)) {
7✔
193
                        (void) blockdev_get_prop(dev, "ID_MODEL_FROM_DATABASE", "ID_MODEL", &model);
×
194
                        (void) blockdev_get_prop(dev, "ID_VENDOR_FROM_DATABASE", "ID_VENDOR", &vendor);
×
195
                        (void) blockdev_get_subsystem(dev, &subsystem);
×
196
                }
197

198
                if (ret_devices) {
7✔
199
                        uint64_t diskseq = UINT64_MAX;
×
200
                        r = sd_device_get_diskseq(dev, &diskseq);
×
201
                        if (r < 0)
×
202
                                log_device_debug_errno(dev, r, "Failed to acquire diskseq of device '%s', ignoring: %m", node);
×
203

204
                        if (!GREEDY_REALLOC(l, n+1))
×
205
                                return log_oom();
×
206

207
                        _cleanup_free_ char *m = strdup(node);
×
208
                        if (!m)
×
209
                                return log_oom();
×
210

211
                        l[n++] = (BlockDevice) {
×
212
                                .node = TAKE_PTR(m),
×
213
                                .symlinks = TAKE_PTR(list),
×
214
                                .diskseq = diskseq,
215
                                .size = size,
216
                                .model = TAKE_PTR(model),
×
217
                                .vendor = TAKE_PTR(vendor),
×
218
                                .subsystem = TAKE_PTR(subsystem),
×
219
                        };
220

221
                } else {
222
                        printf("%s\n", node);
7✔
223

224
                        if (FLAGS_SET(flags, BLOCKDEV_LIST_SHOW_SYMLINKS))
7✔
225
                                STRV_FOREACH(i, list)
67✔
226
                                        printf("%s%s%s%s\n", on_tty() ? "    " : "", ansi_grey(), *i, ansi_normal());
180✔
227
                }
228
        }
229

230
        if (ret_devices)
6✔
231
                *ret_devices = TAKE_PTR(l);
2✔
232
        if (ret_n_devices)
6✔
233
                *ret_n_devices = n;
2✔
234

235
        return 0;
236
}
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