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

systemd / systemd / 15232239991

24 May 2025 08:01PM UTC coverage: 72.053% (-0.02%) from 72.07%
15232239991

push

github

web-flow
docs: add man pages for sd_device_enumerator_[new,ref,unref,unrefp] (#37586)

For #20929.

299160 of 415197 relevant lines covered (72.05%)

703671.29 hits per line

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

76.16
/src/shared/blockdev-util.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <linux/blkpg.h>
4
#include <linux/fs.h>
5
#include <sys/file.h>
6
#include <sys/ioctl.h>
7
#include <sys/stat.h>
8
#include <unistd.h>
9

10
#include "sd-device.h"
11

12
#include "alloc-util.h"
13
#include "blockdev-util.h"
14
#include "btrfs-util.h"
15
#include "device-private.h"
16
#include "device-util.h"
17
#include "devnum-util.h"
18
#include "dirent-util.h"
19
#include "errno-util.h"
20
#include "fd-util.h"
21
#include "fileio.h"
22
#include "fs-util.h"
23
#include "parse-util.h"
24
#include "path-util.h"
25
#include "string-util.h"
26

27
static int fd_get_devnum(int fd, BlockDeviceLookupFlag flags, dev_t *ret) {
5,023✔
28
        struct stat st;
5,023✔
29
        dev_t devnum;
5,023✔
30
        int r;
5,023✔
31

32
        assert(fd >= 0);
5,023✔
33
        assert(ret);
5,023✔
34

35
        if (fstat(fd, &st) < 0)
5,023✔
36
                return -errno;
×
37

38
        if (S_ISBLK(st.st_mode))
5,023✔
39
                devnum = st.st_rdev;
4,903✔
40
        else if (!FLAGS_SET(flags, BLOCK_DEVICE_LOOKUP_BACKING))
120✔
41
                return -ENOTBLK;
42
        else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
37✔
43
                return -ENOTBLK;
44
        else if (major(st.st_dev) != 0)
37✔
45
                devnum = st.st_dev;
37✔
46
        else {
47
                /* If major(st.st_dev) is zero, this might mean we are backed by btrfs, which needs special
48
                 * handing, to get the backing device node. */
49

50
                r = btrfs_get_block_device_fd(fd, &devnum);
×
51
                if (r == -ENOTTY) /* not btrfs */
×
52
                        return -ENOTBLK;
53
                if (r < 0)
×
54
                        return r;
55
        }
56

57
        *ret = devnum;
4,940✔
58
        return 0;
4,940✔
59
}
60

61
int block_device_is_whole_disk(sd_device *dev) {
119,933✔
62
        int r;
119,933✔
63

64
        assert(dev);
119,933✔
65

66
        r = device_in_subsystem(dev, "block");
119,933✔
67
        if (r < 0)
119,933✔
68
                return r;
119,933✔
69
        if (r == 0)
119,933✔
70
                return -ENOTBLK;
71

72
        return device_is_devtype(dev, "disk");
59,693✔
73
}
74

75
int block_device_get_whole_disk(sd_device *dev, sd_device **ret) {
89,342✔
76
        int r;
89,342✔
77

78
        assert(dev);
89,342✔
79
        assert(ret);
89,342✔
80

81
        /* Do not unref returned sd_device object. */
82

83
        r = block_device_is_whole_disk(dev);
89,342✔
84
        if (r < 0)
89,342✔
85
                return r;
86
        if (r == 0) {
29,102✔
87
                r = sd_device_get_parent(dev, &dev);
20,984✔
88
                if (r == -ENOENT) /* Already removed? Let's return a recognizable error. */
20,984✔
89
                        return -ENODEV;
90
                if (r < 0)
20,948✔
91
                        return r;
92

93
                r = block_device_is_whole_disk(dev);
20,948✔
94
                if (r < 0)
20,948✔
95
                        return r;
96
                if (r == 0)
20,948✔
97
                        return -ENXIO;
98
        }
99

100
        *ret = dev;
29,066✔
101
        return 0;
29,066✔
102
}
103

104
int block_device_get_originating(sd_device *dev, sd_device **ret) {
2,819✔
105
        _cleanup_(sd_device_unrefp) sd_device *first_found = NULL;
2,819✔
106
        const char *suffix;
2,819✔
107
        dev_t devnum = 0;  /* avoid false maybe-uninitialized warning */
2,819✔
108

109
        /* For the specified block device tries to chase it through the layers, in case LUKS-style DM
110
         * stacking is used, trying to find the next underlying layer. */
111

112
        assert(dev);
2,819✔
113
        assert(ret);
2,819✔
114

115
        FOREACH_DEVICE_CHILD_WITH_SUFFIX(dev, child, suffix) {
5,646✔
116
                sd_device *child_whole_disk;
2,827✔
117
                dev_t n;
2,827✔
118

119
                if (!path_startswith(suffix, "slaves"))
2,827✔
120
                        continue;
2,827✔
121

122
                if (block_device_get_whole_disk(child, &child_whole_disk) < 0)
×
123
                        continue;
×
124

125
                if (sd_device_get_devnum(child_whole_disk, &n) < 0)
×
126
                        continue;
×
127

128
                if (!first_found) {
×
129
                        first_found = sd_device_ref(child);
×
130
                        devnum = n;
×
131
                        continue;
×
132
                }
133

134
                /* We found a device backed by multiple other devices. We don't really support automatic
135
                 * discovery on such setups, with the exception of dm-verity partitions. In this case there
136
                 * are two backing devices: the data partition and the hash partition. We are fine with such
137
                 * setups, however, only if both partitions are on the same physical device. Hence, let's
138
                 * verify this by iterating over every node in the 'slaves/' directory and comparing them with
139
                 * the first that gets returned by readdir(), to ensure they all point to the same device. */
140
                if (n != devnum)
×
141
                        return -ENOTUNIQ;
×
142
        }
143

144
        if (!first_found)
2,819✔
145
                return -ENOENT;
146

147
        *ret = TAKE_PTR(first_found);
×
148
        return 0;
×
149
}
150

151
int block_device_new_from_fd(int fd, BlockDeviceLookupFlag flags, sd_device **ret) {
3,892✔
152
        _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
3,892✔
153
        dev_t devnum;
3,892✔
154
        int r;
3,892✔
155

156
        assert(fd >= 0);
3,892✔
157
        assert(ret);
3,892✔
158

159
        r = fd_get_devnum(fd, flags, &devnum);
3,892✔
160
        if (r < 0)
3,892✔
161
                return r;
162

163
        r = sd_device_new_from_devnum(&dev, 'b', devnum);
3,809✔
164
        if (r < 0)
3,809✔
165
                return r;
166

167
        if (FLAGS_SET(flags, BLOCK_DEVICE_LOOKUP_ORIGINATING)) {
3,809✔
168
                _cleanup_(sd_device_unrefp) sd_device *dev_origin = NULL;
1✔
169
                sd_device *dev_whole_disk;
1✔
170

171
                r = block_device_get_whole_disk(dev, &dev_whole_disk);
1✔
172
                if (r < 0)
1✔
173
                        return r;
174

175
                r = block_device_get_originating(dev_whole_disk, &dev_origin);
1✔
176
                if (r >= 0)
1✔
177
                        device_unref_and_replace(dev, dev_origin);
×
178
                else if (r != -ENOENT)
1✔
179
                        return r;
180
        }
181

182
        if (FLAGS_SET(flags, BLOCK_DEVICE_LOOKUP_WHOLE_DISK)) {
3,809✔
183
                sd_device *dev_whole_disk;
219✔
184

185
                r = block_device_get_whole_disk(dev, &dev_whole_disk);
219✔
186
                if (r < 0)
219✔
187
                        return r;
219✔
188

189
                *ret = sd_device_ref(dev_whole_disk);
219✔
190
                return 0;
219✔
191
        }
192

193
        *ret = sd_device_ref(dev);
3,590✔
194
        return 0;
3,590✔
195
}
196

197
int block_device_new_from_path(const char *path, BlockDeviceLookupFlag flags, sd_device **ret) {
3✔
198
        _cleanup_close_ int fd = -EBADF;
3✔
199

200
        assert(path);
3✔
201
        assert(ret);
3✔
202

203
        fd = open(path, O_CLOEXEC|O_PATH);
3✔
204
        if (fd < 0)
3✔
205
                return -errno;
2✔
206

207
        return block_device_new_from_fd(fd, flags, ret);
1✔
208
}
209

210
int block_get_whole_disk(dev_t d, dev_t *ret) {
3,952✔
211
        char p[SYS_BLOCK_PATH_MAX("/partition")];
3,952✔
212
        _cleanup_free_ char *s = NULL;
3,952✔
213
        dev_t devt;
3,952✔
214
        int r;
3,952✔
215

216
        assert(ret);
3,952✔
217

218
        if (major(d) == 0)
3,952✔
219
                return -ENODEV;
220

221
        /* If it has a queue this is good enough for us */
222
        xsprintf_sys_block_path(p, "/queue", d);
3,952✔
223
        if (access(p, F_OK) >= 0) {
3,952✔
224
                *ret = d;
1,115✔
225
                return 0;
1,115✔
226
        }
227
        if (errno != ENOENT)
2,837✔
228
                return -errno;
×
229

230
        /* If it is a partition find the originating device */
231
        xsprintf_sys_block_path(p, "/partition", d);
2,837✔
232
        if (access(p, F_OK) < 0)
2,837✔
233
                return -errno;
×
234

235
        /* Get parent dev_t */
236
        xsprintf_sys_block_path(p, "/../dev", d);
2,837✔
237
        r = read_one_line_file(p, &s);
2,837✔
238
        if (r < 0)
2,837✔
239
                return r;
240

241
        r = parse_devnum(s, &devt);
2,837✔
242
        if (r < 0)
2,837✔
243
                return r;
244

245
        /* Only return this if it is really good enough for us. */
246
        xsprintf_sys_block_path(p, "/queue", devt);
2,837✔
247
        if (access(p, F_OK) < 0)
2,837✔
248
                return -errno;
×
249

250
        *ret = devt;
2,837✔
251
        return 1;
2,837✔
252
}
253

254
int get_block_device_fd(int fd, dev_t *ret) {
2,968✔
255
        struct stat st;
2,968✔
256
        int r;
2,968✔
257

258
        assert(fd >= 0);
2,968✔
259
        assert(ret);
2,968✔
260

261
        /* Gets the block device directly backing a file system. If the block device is encrypted, returns
262
         * the device mapper block device. */
263

264
        if (fstat(fd, &st))
2,968✔
265
                return -errno;
×
266

267
        if (major(st.st_dev) != 0) {
2,968✔
268
                *ret = st.st_dev;
2,907✔
269
                return 1;
2,907✔
270
        }
271

272
        r = btrfs_get_block_device_fd(fd, ret);
61✔
273
        if (r != -ENOTTY) /* ENOTTY: not btrfs */
61✔
274
                return r;
275

276
        *ret = 0;
61✔
277
        return 0;
61✔
278
}
279

280
int get_block_device(const char *path, dev_t *ret) {
138✔
281
        _cleanup_close_ int fd = -EBADF;
138✔
282

283
        assert(path);
138✔
284
        assert(ret);
138✔
285

286
        fd = open(path, O_RDONLY|O_NOFOLLOW|O_CLOEXEC);
138✔
287
        if (fd < 0)
138✔
288
                return -errno;
×
289

290
        return get_block_device_fd(fd, ret);
138✔
291
}
292

293
int block_get_originating(dev_t dt, dev_t *ret) {
2,818✔
294
        _cleanup_(sd_device_unrefp) sd_device *dev = NULL, *origin = NULL;
5,636✔
295
        int r;
2,818✔
296

297
        assert(ret);
2,818✔
298

299
        r = sd_device_new_from_devnum(&dev, 'b', dt);
2,818✔
300
        if (r < 0)
2,818✔
301
                return r;
302

303
        r = block_device_get_originating(dev, &origin);
2,818✔
304
        if (r < 0)
2,818✔
305
                return r;
306

307
        return sd_device_get_devnum(origin, ret);
×
308
}
309

310
int get_block_device_harder_fd(int fd, dev_t *ret) {
2,812✔
311
        int r;
2,812✔
312

313
        assert(fd >= 0);
2,812✔
314
        assert(ret);
2,812✔
315

316
        /* Gets the backing block device for a file system, and handles LUKS encrypted file systems, looking for its
317
         * immediate parent, if there is one. */
318

319
        r = get_block_device_fd(fd, ret);
2,812✔
320
        if (r <= 0)
2,812✔
321
                return r;
322

323
        r = block_get_originating(*ret, ret);
2,812✔
324
        if (r < 0)
2,812✔
325
                log_debug_errno(r, "Failed to chase block device, ignoring: %m");
2,812✔
326

327
        return 1;
328
}
329

330
int get_block_device_harder(const char *path, dev_t *ret) {
2,812✔
331
        _cleanup_close_ int fd = -EBADF;
2,812✔
332

333
        assert(path);
2,812✔
334
        assert(ret);
2,812✔
335

336
        fd = open(path, O_RDONLY|O_NOFOLLOW|O_CLOEXEC);
2,812✔
337
        if (fd < 0)
2,812✔
338
                return -errno;
×
339

340
        return get_block_device_harder_fd(fd, ret);
2,812✔
341
}
342

343
int lock_whole_block_device(dev_t devt, int operation) {
×
344
        _cleanup_close_ int lock_fd = -EBADF;
×
345
        dev_t whole_devt;
×
346
        int r;
×
347

348
        /* Let's get a BSD file lock on the whole block device, as per: https://systemd.io/BLOCK_DEVICE_LOCKING */
349

350
        r = block_get_whole_disk(devt, &whole_devt);
×
351
        if (r < 0)
×
352
                return r;
353

354
        lock_fd = r = device_open_from_devnum(S_IFBLK, whole_devt, O_RDONLY|O_CLOEXEC|O_NONBLOCK, NULL);
×
355
        if (r < 0)
×
356
                return r;
357

358
        if (flock(lock_fd, operation) < 0)
×
359
                return -errno;
×
360

361
        return TAKE_FD(lock_fd);
362
}
363

364
int blockdev_partscan_enabled(sd_device *dev) {
4,764✔
365
        unsigned capability;
4,764✔
366
        int r, ext_range;
4,764✔
367

368
        /* Checks if partition scanning is correctly enabled on the block device.
369
         *
370
         * The 'GENHD_FL_NO_PART_SCAN' flag was introduced by
371
         * https://github.com/torvalds/linux/commit/d27769ec3df1a8de9ca450d2dcd72d1ab259ba32 (v3.2).
372
         * But at that time, the flag is also effectively implied when 'minors' element of 'struct gendisk'
373
         * is 1, which can be check with 'ext_range' sysfs attribute. Explicit flag ('GENHD_FL_NO_PART_SCAN')
374
         * can be obtained from 'capability' sysattr.
375
         *
376
         * With https://github.com/torvalds/linux/commit/46e7eac647b34ed4106a8262f8bedbb90801fadd (v5.17),
377
         * the flag is renamed to GENHD_FL_NO_PART.
378
         *
379
         * With https://github.com/torvalds/linux/commit/1ebe2e5f9d68e94c524aba876f27b945669a7879 (v5.17),
380
         * we can check the flag from 'ext_range' sysfs attribute directly.
381
         *
382
         * With https://github.com/torvalds/linux/commit/430cc5d3ab4d0ba0bd011cfbb0035e46ba92920c (v5.17),
383
         * the value of GENHD_FL_NO_PART is changed from 0x0200 to 0x0004. 💣💣💣
384
         * Note, the new value was used by the GENHD_FL_MEDIA_CHANGE_NOTIFY flag, which was introduced by
385
         * 86ce18d7b7925bfd6b64c061828ca2a857ee83b8 (v2.6.22), and removed by
386
         * 9243c6f3e012a92dd900d97ef45efaf8a8edc448 (v5.7). If we believe the commit message of
387
         * e81cd5a983bb35dabd38ee472cf3fea1c63e0f23, the flag was never used. So, fortunately, we can use
388
         * both the new and old values safely.
389
         *
390
         * With https://github.com/torvalds/linux/commit/b9684a71fca793213378dd410cd11675d973eaa1 (v5.19),
391
         * another flag GD_SUPPRESS_PART_SCAN is introduced for loopback block device, and partition scanning
392
         * is done only when both GENHD_FL_NO_PART and GD_SUPPRESS_PART_SCAN are not set. Before the commit,
393
         * LO_FLAGS_PARTSCAN flag was directly tied with GENHD_FL_NO_PART. But with this change now it is
394
         * tied with GD_SUPPRESS_PART_SCAN. So, LO_FLAGS_PARTSCAN cannot be obtained from 'ext_range'
395
         * sysattr, which corresponds to GENHD_FL_NO_PART, and we need to read 'loop/partscan'. 💣💣💣
396
         *
397
         * With https://github.com/torvalds/linux/commit/73a166d9749230d598320fdae3b687cdc0e2e205 (v6.3),
398
         * the GD_SUPPRESS_PART_SCAN flag is also introduced for userspace block device (ublk). Though, not
399
         * sure if we should support the device...
400
         *
401
         * With https://github.com/torvalds/linux/commit/e81cd5a983bb35dabd38ee472cf3fea1c63e0f23 (v6.3),
402
         * the 'capability' sysfs attribute is deprecated, hence we cannot check flags from it. 💣💣💣
403
         *
404
         * With https://github.com/torvalds/linux/commit/a4217c6740dc64a3eb6815868a9260825e8c68c6 (v6.10,
405
         * backported to v6.6+), the partscan status is directly exposed as 'partscan' sysattr.
406
         *
407
         * To support both old and new kernels, we need to do the following:
408
         * 1) check 'partscan' sysfs attribute where the information is made directly available,
409
         * 2) check if the blockdev refers to a partition, where partscan is not supported,
410
         * 3) check 'loop/partscan' sysfs attribute for loopback block devices, and if '0' we can conclude
411
         *    partition scanning is disabled,
412
         * 4) check 'ext_range' sysfs attribute, and if '1' we can conclude partition scanning is disabled,
413
         * 5) otherwise check 'capability' sysfs attribute for ancient version. */
414

415
        assert(dev);
4,764✔
416

417
        /* For v6.10 or newer. */
418
        r = device_get_sysattr_bool(dev, "partscan");
4,764✔
419
        if (r != -ENOENT)
4,764✔
420
                return r;
4,764✔
421

422
        /* Partition block devices never have partition scanning on, there's no concept of sub-partitions for
423
         * partitions. */
424
        r = device_is_devtype(dev, "partition");
4✔
425
        if (r < 0)
4✔
426
                return r;
427
        if (r > 0)
4✔
428
                return false;
429

430
        /* For loopback block device, especially for v5.19 or newer. Even if this is enabled, we also need to
431
         * check GENHD_FL_NO_PART flag through 'ext_range' and 'capability' sysfs attributes below. */
432
        if (device_get_sysattr_bool(dev, "loop/partscan") == 0)
×
433
                return false;
434

435
        r = device_get_sysattr_int(dev, "ext_range", &ext_range);
×
436
        if (r == -ENOENT) /* If the ext_range file doesn't exist then we are most likely looking at a
×
437
                           * partition block device, not the whole block device. And that means we have no
438
                           * partition scanning on for it (we do for its parent, but not for the partition
439
                           * itself). */
440
                return false;
441
        if (r < 0)
×
442
                return r;
443

444
        if (ext_range <= 1) /* The value should be always positive, but the kernel uses '%d' for the
×
445
                             * attribute. Let's gracefully handle zero or negative. */
446
                return false;
447

448
        r = device_get_sysattr_unsigned_full(dev, "capability", 16, &capability);
×
449
        if (r == -ENOENT)
×
450
                return false;
451
        if (r < 0)
×
452
                return r;
453

454
#define GENHD_FL_NO_PART_OLD 0x0200
455
#define GENHD_FL_NO_PART_NEW 0x0004
456
        /* If one of the NO_PART flags is set, part scanning is definitely off. */
457
        if ((capability & (GENHD_FL_NO_PART_OLD | GENHD_FL_NO_PART_NEW)) != 0)
×
458
                return false;
×
459

460
        /* Otherwise, assume part scanning is on, we have no further checks available. Assume the best. */
461
        return true;
462
}
463

464
int blockdev_partscan_enabled_fd(int fd) {
1,926✔
465
        _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
1,926✔
466
        int r;
1,926✔
467

468
        assert(fd >= 0);
1,926✔
469

470
        r = block_device_new_from_fd(fd, 0, &dev);
1,926✔
471
        if (r < 0)
1,926✔
472
                return r;
473

474
        return blockdev_partscan_enabled(dev);
1,843✔
475
}
476

477
static int blockdev_is_encrypted(const char *sysfs_path, unsigned depth_left) {
19✔
478
        _cleanup_free_ char *p = NULL, *uuids = NULL;
19✔
479
        _cleanup_closedir_ DIR *d = NULL;
19✔
480
        int r, found_encrypted = false;
19✔
481

482
        assert(sysfs_path);
19✔
483

484
        if (depth_left == 0)
19✔
485
                return -EINVAL;
486

487
        p = path_join(sysfs_path, "dm/uuid");
19✔
488
        if (!p)
19✔
489
                return -ENOMEM;
490

491
        r = read_one_line_file(p, &uuids);
19✔
492
        if (r != -ENOENT) {
19✔
493
                if (r < 0)
×
494
                        return r;
495

496
                /* The DM device's uuid attribute is prefixed with "CRYPT-" if this is a dm-crypt device. */
497
                if (startswith(uuids, "CRYPT-"))
×
498
                        return true;
499
        }
500

501
        /* Not a dm-crypt device itself. But maybe it is on top of one? Follow the links in the "slaves/"
502
         * subdir. */
503

504
        p = mfree(p);
19✔
505
        p = path_join(sysfs_path, "slaves");
19✔
506
        if (!p)
19✔
507
                return -ENOMEM;
508

509
        d = opendir(p);
19✔
510
        if (!d) {
19✔
511
                if (errno == ENOENT) /* Doesn't have underlying devices */
19✔
512
                        return false;
513

514
                return -errno;
×
515
        }
516

517
        for (;;) {
×
518
                _cleanup_free_ char *q = NULL;
×
519
                struct dirent *de;
×
520

521
                errno = 0;
×
522
                de = readdir_no_dot(d);
×
523
                if (!de) {
×
524
                        if (errno != 0)
×
525
                                return -errno;
×
526

527
                        break; /* No more underlying devices */
528
                }
529

530
                q = path_join(p, de->d_name);
×
531
                if (!q)
×
532
                        return -ENOMEM;
533

534
                r = blockdev_is_encrypted(q, depth_left - 1);
×
535
                if (r < 0)
×
536
                        return r;
537
                if (r == 0) /* we found one that is not encrypted? then propagate that immediately */
×
538
                        return false;
539

540
                found_encrypted = true;
×
541
        }
542

543
        return found_encrypted;
544
}
545

546
int fd_is_encrypted(int fd) {
18✔
547
        char p[SYS_BLOCK_PATH_MAX("")];
18✔
548
        dev_t devt;
18✔
549
        int r;
18✔
550

551
        r = get_block_device_fd(fd, &devt);
18✔
552
        if (r < 0)
18✔
553
                return r;
18✔
554
        if (r == 0) /* doesn't have a block device */
18✔
555
                return false;
556

557
        xsprintf_sys_block_path(p, NULL, devt);
16✔
558

559
        return blockdev_is_encrypted(p, 10 /* safety net: maximum recursion depth */);
16✔
560
}
561

562
int path_is_encrypted(const char *path) {
7✔
563
        char p[SYS_BLOCK_PATH_MAX("")];
7✔
564
        dev_t devt;
7✔
565
        int r;
7✔
566

567
        r = get_block_device(path, &devt);
7✔
568
        if (r < 0)
7✔
569
                return r;
7✔
570
        if (r == 0) /* doesn't have a block device */
7✔
571
                return false;
572

573
        xsprintf_sys_block_path(p, NULL, devt);
3✔
574

575
        return blockdev_is_encrypted(p, 10 /* safety net: maximum recursion depth */);
3✔
576
}
577

578
int fd_get_whole_disk(int fd, bool backing, dev_t *ret) {
1,131✔
579
        dev_t devt;
1,131✔
580
        int r;
1,131✔
581

582
        assert(fd >= 0);
1,131✔
583
        assert(ret);
1,131✔
584

585
        r = fd_get_devnum(fd, backing ? BLOCK_DEVICE_LOOKUP_BACKING : 0, &devt);
2,256✔
586
        if (r < 0)
1,131✔
587
                return r;
1,131✔
588

589
        return block_get_whole_disk(devt, ret);
1,131✔
590
}
591

592
int path_get_whole_disk(const char *path, bool backing, dev_t *ret) {
1,131✔
593
        _cleanup_close_ int fd = -EBADF;
1,131✔
594

595
        fd = open(path, O_CLOEXEC|O_PATH);
1,131✔
596
        if (fd < 0)
1,131✔
597
                return -errno;
×
598

599
        return fd_get_whole_disk(fd, backing, ret);
1,131✔
600
}
601

602
int block_device_add_partition(
222✔
603
                int fd,
604
                const char *name,
605
                int nr,
606
                uint64_t start,
607
                uint64_t size) {
608

609
        assert(fd >= 0);
222✔
610
        assert(name);
222✔
611
        assert(nr > 0);
222✔
612

613
        struct blkpg_partition bp = {
222✔
614
                .pno = nr,
615
                .start = start,
616
                .length = size,
617
        };
618

619
        struct blkpg_ioctl_arg ba = {
222✔
620
                .op = BLKPG_ADD_PARTITION,
621
                .data = &bp,
622
                .datalen = sizeof(bp),
623
        };
624

625
        if (strlen(name) >= sizeof(bp.devname))
222✔
626
                return -EINVAL;
222✔
627

628
        strcpy(bp.devname, name);
222✔
629

630
        return RET_NERRNO(ioctl(fd, BLKPG, &ba));
222✔
631
}
632

633
int block_device_remove_partition(
93✔
634
                int fd,
635
                const char *name,
636
                int nr) {
637

638
        assert(fd >= 0);
93✔
639
        assert(name);
93✔
640
        assert(nr > 0);
93✔
641

642
        struct blkpg_partition bp = {
93✔
643
                .pno = nr,
644
        };
645

646
        struct blkpg_ioctl_arg ba = {
93✔
647
                .op = BLKPG_DEL_PARTITION,
648
                .data = &bp,
649
                .datalen = sizeof(bp),
650
        };
651

652
        if (strlen(name) >= sizeof(bp.devname))
93✔
653
                return -EINVAL;
93✔
654

655
        strcpy(bp.devname, name);
93✔
656

657
        return RET_NERRNO(ioctl(fd, BLKPG, &ba));
93✔
658
}
659

660
int block_device_resize_partition(
×
661
                int fd,
662
                int nr,
663
                uint64_t start,
664
                uint64_t size) {
665

666
        assert(fd >= 0);
×
667
        assert(nr > 0);
×
668

669
        struct blkpg_partition bp = {
×
670
                .pno = nr,
671
                .start = start,
672
                .length = size,
673
        };
674

675
        struct blkpg_ioctl_arg ba = {
×
676
                .op = BLKPG_RESIZE_PARTITION,
677
                .data = &bp,
678
                .datalen = sizeof(bp),
679
        };
680

681
        return RET_NERRNO(ioctl(fd, BLKPG, &ba));
×
682
}
683

684
int partition_enumerator_new(sd_device *dev, sd_device_enumerator **ret) {
6,659✔
685
        _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
6,659✔
686
        const char *s;
6,659✔
687
        int r;
6,659✔
688

689
        assert(dev);
6,659✔
690
        assert(ret);
6,659✔
691

692
        /* Refuse invocation on partition block device, insist on "whole" device */
693
        r = block_device_is_whole_disk(dev);
6,659✔
694
        if (r < 0)
6,659✔
695
                return r;
696
        if (r == 0)
6,659✔
697
                return -ENXIO; /* return a recognizable error */
698

699
        r = sd_device_enumerator_new(&e);
6,659✔
700
        if (r < 0)
6,659✔
701
                return r;
702

703
        r = sd_device_enumerator_allow_uninitialized(e);
6,659✔
704
        if (r < 0)
6,659✔
705
                return r;
706

707
        r = sd_device_enumerator_add_match_parent(e, dev);
6,659✔
708
        if (r < 0)
6,659✔
709
                return r;
710

711
        r = sd_device_get_sysname(dev, &s);
6,659✔
712
        if (r < 0)
6,659✔
713
                return r;
714

715
        /* Also add sysname check for safety. Hopefully, this also improves performance. */
716
        s = strjoina(s, "*");
33,295✔
717
        r = sd_device_enumerator_add_match_sysname(e, s);
6,659✔
718
        if (r < 0)
6,659✔
719
                return r;
720

721
        r = sd_device_enumerator_add_match_subsystem(e, "block", /* match = */ true);
6,659✔
722
        if (r < 0)
6,659✔
723
                return r;
724

725
        r = sd_device_enumerator_add_match_property(e, "DEVTYPE", "partition");
6,659✔
726
        if (r < 0)
6,659✔
727
                return r;
728

729
        *ret = TAKE_PTR(e);
6,659✔
730
        return 0;
6,659✔
731
}
732

733
int block_device_remove_all_partitions(sd_device *dev, int fd) {
3,612✔
734
        _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
3,612✔
735
        _cleanup_(sd_device_unrefp) sd_device *dev_unref = NULL;
3,612✔
736
        _cleanup_close_ int fd_close = -EBADF;
3,612✔
737
        bool has_partitions = false;
3,612✔
738
        int r, k = 0;
3,612✔
739

740
        assert(dev || fd >= 0);
3,612✔
741

742
        if (!dev) {
3,612✔
743
                r = block_device_new_from_fd(fd, 0, &dev_unref);
×
744
                if (r < 0)
×
745
                        return r;
746

747
                dev = dev_unref;
×
748
        }
749

750
        r = partition_enumerator_new(dev, &e);
3,612✔
751
        if (r < 0)
3,612✔
752
                return r;
753

754
        if (fd < 0) {
3,612✔
755
                fd_close = sd_device_open(dev, O_CLOEXEC|O_NONBLOCK|O_NOCTTY|O_RDONLY);
×
756
                if (fd_close < 0)
×
757
                        return fd_close;
758

759
                fd = fd_close;
760
        }
761

762
        FOREACH_DEVICE(e, part) {
3,705✔
763
                const char *v, *devname;
93✔
764
                int nr;
93✔
765

766
                has_partitions = true;
93✔
767

768
                r = sd_device_get_devname(part, &devname);
93✔
769
                if (r < 0)
93✔
770
                        return r;
×
771

772
                r = sd_device_get_property_value(part, "PARTN", &v);
93✔
773
                if (r < 0)
93✔
774
                        return r;
775

776
                r = safe_atoi(v, &nr);
93✔
777
                if (r < 0)
93✔
778
                        return r;
779

780
                r = btrfs_forget_device(devname);
93✔
781
                if (r < 0 && r != -ENOENT)
93✔
782
                        log_debug_errno(r, "Failed to forget btrfs device %s, ignoring: %m", devname);
×
783

784
                r = block_device_remove_partition(fd, devname, nr);
93✔
785
                if (r == -ENODEV) {
93✔
786
                        log_debug("Kernel removed partition %s before us, ignoring", devname);
×
787
                        continue;
×
788
                }
789
                if (r < 0) {
93✔
790
                        log_debug_errno(r, "Failed to remove partition %s: %m", devname);
×
791
                        k = k < 0 ? k : r;
×
792
                        continue;
×
793
                }
794

795
                log_debug("Removed partition %s", devname);
93✔
796
        }
797

798
        return k < 0 ? k : has_partitions;
3,612✔
799
}
800

801

802
int blockdev_reread_partition_table(sd_device *dev) {
3,047✔
803
        _cleanup_close_ int fd = -EBADF;
3,047✔
804

805
        assert(dev);
3,047✔
806

807
        /* Try to re-read the partition table. This only succeeds if none of the devices is busy. */
808

809
        fd = sd_device_open(dev, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
3,047✔
810
        if (fd < 0)
3,047✔
811
                return fd;
812

813
        if (flock(fd, LOCK_EX|LOCK_NB) < 0)
3,009✔
814
                return -errno;
713✔
815

816
        if (ioctl(fd, BLKRRPART, 0) < 0)
2,296✔
817
                return -errno;
1,172✔
818

819
        return 0;
820
}
821

822
int blockdev_get_sector_size(int fd, uint32_t *ret) {
2,299✔
823
        int ssz = 0;
2,299✔
824

825
        assert(fd >= 0);
2,299✔
826
        assert(ret);
2,299✔
827

828
        if (ioctl(fd, BLKSSZGET, &ssz) < 0)
2,299✔
829
                return -errno;
×
830
        if (ssz <= 0) /* make sure the field is initialized */
2,299✔
831
                return log_debug_errno(SYNTHETIC_ERRNO(EIO), "Block device reported invalid sector size %i.", ssz);
×
832

833
        *ret = ssz;
2,299✔
834
        return 0;
2,299✔
835
}
836

837
int blockdev_get_device_size(int fd, uint64_t *ret) {
2,110✔
838
        uint64_t sz = 0;
2,110✔
839

840
        assert(fd >= 0);
2,110✔
841
        assert(ret);
2,110✔
842

843
        /* This is just a type-safe wrapper around BLKGETSIZE64 that gets us around having to include messy linux/fs.h in various clients */
844

845
        if (ioctl(fd, BLKGETSIZE64, &sz) < 0)
2,110✔
846
                return -errno;
×
847

848
        *ret = sz;
2,110✔
849
        return 0;
2,110✔
850
}
851

852
int blockdev_get_root(int level, dev_t *ret) {
2,812✔
853
        _cleanup_free_ char *p = NULL;
2,812✔
854
        dev_t devno;
2,812✔
855
        int r;
2,812✔
856

857
        /* Returns the device node backing the root file system. Traces through
858
         * dm-crypt/dm-verity/... Returns > 0 and the devno of the device on success. If there's no block
859
         * device (or multiple) returns 0 and a devno of 0. Failure otherwise.
860
         *
861
         * If the root mount has been replaced by some form of volatile file system (overlayfs), the original
862
         * root block device node is symlinked in /run/systemd/volatile-root. Let's read that here. */
863
        r = readlink_malloc("/run/systemd/volatile-root", &p);
2,812✔
864
        if (r == -ENOENT) { /* volatile-root not found */
2,812✔
865
                r = get_block_device_harder("/", &devno);
2,812✔
866
                if (r == -EUCLEAN)
2,812✔
867
                        return btrfs_log_dev_root(level, r, "root file system");
×
868
                if (r < 0)
2,812✔
869
                        return log_full_errno(level, r, "Failed to determine block device of root file system: %m");
×
870
                if (r == 0) { /* Not backed by a single block device. (Could be NFS or so, or could be multi-device RAID or so) */
2,812✔
871
                        r = get_block_device_harder("/usr", &devno);
×
872
                        if (r == -EUCLEAN)
×
873
                                return btrfs_log_dev_root(level, r, "/usr");
×
874
                        if (r < 0)
×
875
                                return log_full_errno(level, r, "Failed to determine block device of /usr/ file system: %m");
×
876
                        if (r == 0) { /* /usr/ not backed by single block device, either. */
×
877
                                log_debug("Neither root nor /usr/ file system are on a (single) block device.");
×
878

879
                                if (ret)
×
880
                                        *ret = 0;
×
881

882
                                return 0;
×
883
                        }
884
                }
885
        } else if (r < 0)
×
886
                return log_full_errno(level, r, "Failed to read symlink /run/systemd/volatile-root: %m");
×
887
        else {
888
                mode_t m;
×
889
                r = device_path_parse_major_minor(p, &m, &devno);
×
890
                if (r < 0)
×
891
                        return log_full_errno(level, r, "Failed to parse major/minor device node: %m");
×
892
                if (!S_ISBLK(m))
×
893
                        return log_full_errno(level, SYNTHETIC_ERRNO(ENOTBLK), "Volatile root device is of wrong type.");
×
894
        }
895

896
        if (ret)
2,812✔
897
                *ret = devno;
2,812✔
898

899
        return 1;
900
}
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