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

systemd / systemd / 14872145375

06 May 2025 09:07PM UTC coverage: 72.232% (+0.02%) from 72.214%
14872145375

push

github

DaanDeMeyer
string-table: annotate _to_string and _from_string with _const_ and _pure_, respectively

Follow-up for c94f6ab1b

297286 of 411572 relevant lines covered (72.23%)

695615.99 hits per line

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

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

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

9
#include "sd-device.h"
10

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

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

30
        assert(fd >= 0);
5,015✔
31
        assert(ret);
5,015✔
32

33
        if (fstat(fd, &st) < 0)
5,015✔
34
                return -errno;
×
35

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

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

55
        *ret = devnum;
4,932✔
56
        return 0;
4,932✔
57
}
58

59
int block_device_is_whole_disk(sd_device *dev) {
154,035✔
60
        assert(dev);
154,035✔
61

62
        if (!device_in_subsystem(dev, "block"))
154,035✔
63
                return -ENOTBLK;
64

65
        return device_is_devtype(dev, "disk");
63,482✔
66
}
67

68
int block_device_get_whole_disk(sd_device *dev, sd_device **ret) {
122,025✔
69
        int r;
122,025✔
70

71
        assert(dev);
122,025✔
72
        assert(ret);
122,025✔
73

74
        /* Do not unref returned sd_device object. */
75

76
        r = block_device_is_whole_disk(dev);
122,025✔
77
        if (r < 0)
122,025✔
78
                return r;
79
        if (r == 0) {
31,472✔
80
                r = sd_device_get_parent(dev, &dev);
22,473✔
81
                if (r == -ENOENT) /* Already removed? Let's return a recognizable error. */
22,473✔
82
                        return -ENODEV;
83
                if (r < 0)
22,436✔
84
                        return r;
85

86
                r = block_device_is_whole_disk(dev);
22,436✔
87
                if (r < 0)
22,436✔
88
                        return r;
89
                if (r == 0)
22,436✔
90
                        return -ENXIO;
91
        }
92

93
        *ret = dev;
31,435✔
94
        return 0;
31,435✔
95
}
96

97
int block_device_get_originating(sd_device *dev, sd_device **ret) {
2,758✔
98
        _cleanup_(sd_device_unrefp) sd_device *first_found = NULL;
2,758✔
99
        const char *suffix;
2,758✔
100
        dev_t devnum = 0;  /* avoid false maybe-uninitialized warning */
2,758✔
101

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

105
        assert(dev);
2,758✔
106
        assert(ret);
2,758✔
107

108
        FOREACH_DEVICE_CHILD_WITH_SUFFIX(dev, child, suffix) {
5,564✔
109
                sd_device *child_whole_disk;
2,806✔
110
                dev_t n;
2,806✔
111

112
                if (!path_startswith(suffix, "slaves"))
2,806✔
113
                        continue;
2,806✔
114

115
                if (block_device_get_whole_disk(child, &child_whole_disk) < 0)
×
116
                        continue;
×
117

118
                if (sd_device_get_devnum(child_whole_disk, &n) < 0)
×
119
                        continue;
×
120

121
                if (!first_found) {
×
122
                        first_found = sd_device_ref(child);
×
123
                        devnum = n;
×
124
                        continue;
×
125
                }
126

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

137
        if (!first_found)
2,758✔
138
                return -ENOENT;
139

140
        *ret = TAKE_PTR(first_found);
×
141
        return 0;
×
142
}
143

144
int block_device_new_from_fd(int fd, BlockDeviceLookupFlag flags, sd_device **ret) {
3,884✔
145
        _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
3,884✔
146
        dev_t devnum;
3,884✔
147
        int r;
3,884✔
148

149
        assert(fd >= 0);
3,884✔
150
        assert(ret);
3,884✔
151

152
        r = fd_get_devnum(fd, flags, &devnum);
3,884✔
153
        if (r < 0)
3,884✔
154
                return r;
155

156
        r = sd_device_new_from_devnum(&dev, 'b', devnum);
3,801✔
157
        if (r < 0)
3,801✔
158
                return r;
159

160
        if (FLAGS_SET(flags, BLOCK_DEVICE_LOOKUP_ORIGINATING)) {
3,801✔
161
                _cleanup_(sd_device_unrefp) sd_device *dev_origin = NULL;
9✔
162
                sd_device *dev_whole_disk;
9✔
163

164
                r = block_device_get_whole_disk(dev, &dev_whole_disk);
9✔
165
                if (r < 0)
9✔
166
                        return r;
167

168
                r = block_device_get_originating(dev_whole_disk, &dev_origin);
9✔
169
                if (r >= 0)
9✔
170
                        device_unref_and_replace(dev, dev_origin);
×
171
                else if (r != -ENOENT)
9✔
172
                        return r;
173
        }
174

175
        if (FLAGS_SET(flags, BLOCK_DEVICE_LOOKUP_WHOLE_DISK)) {
3,801✔
176
                sd_device *dev_whole_disk;
218✔
177

178
                r = block_device_get_whole_disk(dev, &dev_whole_disk);
218✔
179
                if (r < 0)
218✔
180
                        return r;
218✔
181

182
                *ret = sd_device_ref(dev_whole_disk);
218✔
183
                return 0;
218✔
184
        }
185

186
        *ret = sd_device_ref(dev);
3,583✔
187
        return 0;
3,583✔
188
}
189

190
int block_device_new_from_path(const char *path, BlockDeviceLookupFlag flags, sd_device **ret) {
3✔
191
        _cleanup_close_ int fd = -EBADF;
3✔
192

193
        assert(path);
3✔
194
        assert(ret);
3✔
195

196
        fd = open(path, O_CLOEXEC|O_PATH);
3✔
197
        if (fd < 0)
3✔
198
                return -errno;
2✔
199

200
        return block_device_new_from_fd(fd, flags, ret);
1✔
201
}
202

203
int block_get_whole_disk(dev_t d, dev_t *ret) {
3,883✔
204
        char p[SYS_BLOCK_PATH_MAX("/partition")];
3,883✔
205
        _cleanup_free_ char *s = NULL;
3,883✔
206
        dev_t devt;
3,883✔
207
        int r;
3,883✔
208

209
        assert(ret);
3,883✔
210

211
        if (major(d) == 0)
3,883✔
212
                return -ENODEV;
213

214
        /* If it has a queue this is good enough for us */
215
        xsprintf_sys_block_path(p, "/queue", d);
3,883✔
216
        if (access(p, F_OK) >= 0) {
3,883✔
217
                *ret = d;
1,115✔
218
                return 0;
1,115✔
219
        }
220
        if (errno != ENOENT)
2,768✔
221
                return -errno;
×
222

223
        /* If it is a partition find the originating device */
224
        xsprintf_sys_block_path(p, "/partition", d);
2,768✔
225
        if (access(p, F_OK) < 0)
2,768✔
226
                return -errno;
×
227

228
        /* Get parent dev_t */
229
        xsprintf_sys_block_path(p, "/../dev", d);
2,768✔
230
        r = read_one_line_file(p, &s);
2,768✔
231
        if (r < 0)
2,768✔
232
                return r;
233

234
        r = parse_devnum(s, &devt);
2,768✔
235
        if (r < 0)
2,768✔
236
                return r;
237

238
        /* Only return this if it is really good enough for us. */
239
        xsprintf_sys_block_path(p, "/queue", devt);
2,768✔
240
        if (access(p, F_OK) < 0)
2,768✔
241
                return -errno;
×
242

243
        *ret = devt;
2,768✔
244
        return 1;
2,768✔
245
}
246

247
int get_block_device_fd(int fd, dev_t *ret) {
2,899✔
248
        struct stat st;
2,899✔
249
        int r;
2,899✔
250

251
        assert(fd >= 0);
2,899✔
252
        assert(ret);
2,899✔
253

254
        /* Gets the block device directly backing a file system. If the block device is encrypted, returns
255
         * the device mapper block device. */
256

257
        if (fstat(fd, &st))
2,899✔
258
                return -errno;
×
259

260
        if (major(st.st_dev) != 0) {
2,899✔
261
                *ret = st.st_dev;
2,838✔
262
                return 1;
2,838✔
263
        }
264

265
        r = btrfs_get_block_device_fd(fd, ret);
61✔
266
        if (r != -ENOTTY) /* ENOTTY: not btrfs */
61✔
267
                return r;
268

269
        *ret = 0;
61✔
270
        return 0;
61✔
271
}
272

273
int get_block_device(const char *path, dev_t *ret) {
138✔
274
        _cleanup_close_ int fd = -EBADF;
138✔
275

276
        assert(path);
138✔
277
        assert(ret);
138✔
278

279
        fd = open(path, O_RDONLY|O_NOFOLLOW|O_CLOEXEC);
138✔
280
        if (fd < 0)
138✔
281
                return -errno;
×
282

283
        return get_block_device_fd(fd, ret);
138✔
284
}
285

286
int block_get_originating(dev_t dt, dev_t *ret) {
2,749✔
287
        _cleanup_(sd_device_unrefp) sd_device *dev = NULL, *origin = NULL;
5,498✔
288
        int r;
2,749✔
289

290
        assert(ret);
2,749✔
291

292
        r = sd_device_new_from_devnum(&dev, 'b', dt);
2,749✔
293
        if (r < 0)
2,749✔
294
                return r;
295

296
        r = block_device_get_originating(dev, &origin);
2,749✔
297
        if (r < 0)
2,749✔
298
                return r;
299

300
        return sd_device_get_devnum(origin, ret);
×
301
}
302

303
int get_block_device_harder_fd(int fd, dev_t *ret) {
2,743✔
304
        int r;
2,743✔
305

306
        assert(fd >= 0);
2,743✔
307
        assert(ret);
2,743✔
308

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

312
        r = get_block_device_fd(fd, ret);
2,743✔
313
        if (r <= 0)
2,743✔
314
                return r;
315

316
        r = block_get_originating(*ret, ret);
2,743✔
317
        if (r < 0)
2,743✔
318
                log_debug_errno(r, "Failed to chase block device, ignoring: %m");
2,743✔
319

320
        return 1;
321
}
322

323
int get_block_device_harder(const char *path, dev_t *ret) {
2,743✔
324
        _cleanup_close_ int fd = -EBADF;
2,743✔
325

326
        assert(path);
2,743✔
327
        assert(ret);
2,743✔
328

329
        fd = open(path, O_RDONLY|O_NOFOLLOW|O_CLOEXEC);
2,743✔
330
        if (fd < 0)
2,743✔
331
                return -errno;
×
332

333
        return get_block_device_harder_fd(fd, ret);
2,743✔
334
}
335

336
int lock_whole_block_device(dev_t devt, int operation) {
×
337
        _cleanup_close_ int lock_fd = -EBADF;
×
338
        dev_t whole_devt;
×
339
        int r;
×
340

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

343
        r = block_get_whole_disk(devt, &whole_devt);
×
344
        if (r < 0)
×
345
                return r;
346

347
        lock_fd = r = device_open_from_devnum(S_IFBLK, whole_devt, O_RDONLY|O_CLOEXEC|O_NONBLOCK, NULL);
×
348
        if (r < 0)
×
349
                return r;
350

351
        if (flock(lock_fd, operation) < 0)
×
352
                return -errno;
×
353

354
        return TAKE_FD(lock_fd);
355
}
356

357
int blockdev_partscan_enabled(sd_device *dev) {
4,688✔
358
        unsigned capability;
4,688✔
359
        int r, ext_range;
4,688✔
360

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

408
        assert(dev);
4,688✔
409

410
        /* For v6.10 or newer. */
411
        r = device_get_sysattr_bool(dev, "partscan");
4,688✔
412
        if (r != -ENOENT)
4,688✔
413
                return r;
4,688✔
414

415
        /* Partition block devices never have partition scanning on, there's no concept of sub-partitions for
416
         * partitions. */
417
        if (device_is_devtype(dev, "partition"))
4✔
418
                return false;
419

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

425
        r = device_get_sysattr_int(dev, "ext_range", &ext_range);
×
426
        if (r == -ENOENT) /* If the ext_range file doesn't exist then we are most likely looking at a
×
427
                           * partition block device, not the whole block device. And that means we have no
428
                           * partition scanning on for it (we do for its parent, but not for the partition
429
                           * itself). */
430
                return false;
431
        if (r < 0)
×
432
                return r;
433

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

438
        r = device_get_sysattr_unsigned_full(dev, "capability", 16, &capability);
×
439
        if (r == -ENOENT)
×
440
                return false;
441
        if (r < 0)
×
442
                return r;
443

444
#define GENHD_FL_NO_PART_OLD 0x0200
445
#define GENHD_FL_NO_PART_NEW 0x0004
446
        /* If one of the NO_PART flags is set, part scanning is definitely off. */
447
        if ((capability & (GENHD_FL_NO_PART_OLD | GENHD_FL_NO_PART_NEW)) != 0)
×
448
                return false;
×
449

450
        /* Otherwise, assume part scanning is on, we have no further checks available. Assume the best. */
451
        return true;
452
}
453

454
int blockdev_partscan_enabled_fd(int fd) {
1,924✔
455
        _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
1,924✔
456
        int r;
1,924✔
457

458
        assert(fd >= 0);
1,924✔
459

460
        r = block_device_new_from_fd(fd, 0, &dev);
1,924✔
461
        if (r < 0)
1,924✔
462
                return r;
463

464
        return blockdev_partscan_enabled(dev);
1,841✔
465
}
466

467
static int blockdev_is_encrypted(const char *sysfs_path, unsigned depth_left) {
19✔
468
        _cleanup_free_ char *p = NULL, *uuids = NULL;
19✔
469
        _cleanup_closedir_ DIR *d = NULL;
19✔
470
        int r, found_encrypted = false;
19✔
471

472
        assert(sysfs_path);
19✔
473

474
        if (depth_left == 0)
19✔
475
                return -EINVAL;
476

477
        p = path_join(sysfs_path, "dm/uuid");
19✔
478
        if (!p)
19✔
479
                return -ENOMEM;
480

481
        r = read_one_line_file(p, &uuids);
19✔
482
        if (r != -ENOENT) {
19✔
483
                if (r < 0)
×
484
                        return r;
485

486
                /* The DM device's uuid attribute is prefixed with "CRYPT-" if this is a dm-crypt device. */
487
                if (startswith(uuids, "CRYPT-"))
×
488
                        return true;
489
        }
490

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

494
        p = mfree(p);
19✔
495
        p = path_join(sysfs_path, "slaves");
19✔
496
        if (!p)
19✔
497
                return -ENOMEM;
498

499
        d = opendir(p);
19✔
500
        if (!d) {
19✔
501
                if (errno == ENOENT) /* Doesn't have underlying devices */
19✔
502
                        return false;
503

504
                return -errno;
×
505
        }
506

507
        for (;;) {
×
508
                _cleanup_free_ char *q = NULL;
×
509
                struct dirent *de;
×
510

511
                errno = 0;
×
512
                de = readdir_no_dot(d);
×
513
                if (!de) {
×
514
                        if (errno != 0)
×
515
                                return -errno;
×
516

517
                        break; /* No more underlying devices */
518
                }
519

520
                q = path_join(p, de->d_name);
×
521
                if (!q)
×
522
                        return -ENOMEM;
523

524
                r = blockdev_is_encrypted(q, depth_left - 1);
×
525
                if (r < 0)
×
526
                        return r;
527
                if (r == 0) /* we found one that is not encrypted? then propagate that immediately */
×
528
                        return false;
529

530
                found_encrypted = true;
×
531
        }
532

533
        return found_encrypted;
534
}
535

536
int fd_is_encrypted(int fd) {
18✔
537
        char p[SYS_BLOCK_PATH_MAX(NULL)];
18✔
538
        dev_t devt;
18✔
539
        int r;
18✔
540

541
        r = get_block_device_fd(fd, &devt);
18✔
542
        if (r < 0)
18✔
543
                return r;
18✔
544
        if (r == 0) /* doesn't have a block device */
18✔
545
                return false;
546

547
        xsprintf_sys_block_path(p, NULL, devt);
16✔
548

549
        return blockdev_is_encrypted(p, 10 /* safety net: maximum recursion depth */);
16✔
550
}
551

552
int path_is_encrypted(const char *path) {
7✔
553
        char p[SYS_BLOCK_PATH_MAX(NULL)];
7✔
554
        dev_t devt;
7✔
555
        int r;
7✔
556

557
        r = get_block_device(path, &devt);
7✔
558
        if (r < 0)
7✔
559
                return r;
7✔
560
        if (r == 0) /* doesn't have a block device */
7✔
561
                return false;
562

563
        xsprintf_sys_block_path(p, NULL, devt);
3✔
564

565
        return blockdev_is_encrypted(p, 10 /* safety net: maximum recursion depth */);
3✔
566
}
567

568
int fd_get_whole_disk(int fd, bool backing, dev_t *ret) {
1,131✔
569
        dev_t devt;
1,131✔
570
        int r;
1,131✔
571

572
        assert(fd >= 0);
1,131✔
573
        assert(ret);
1,131✔
574

575
        r = fd_get_devnum(fd, backing ? BLOCK_DEVICE_LOOKUP_BACKING : 0, &devt);
2,256✔
576
        if (r < 0)
1,131✔
577
                return r;
1,131✔
578

579
        return block_get_whole_disk(devt, ret);
1,131✔
580
}
581

582
int path_get_whole_disk(const char *path, bool backing, dev_t *ret) {
1,131✔
583
        _cleanup_close_ int fd = -EBADF;
1,131✔
584

585
        fd = open(path, O_CLOEXEC|O_PATH);
1,131✔
586
        if (fd < 0)
1,131✔
587
                return -errno;
×
588

589
        return fd_get_whole_disk(fd, backing, ret);
1,131✔
590
}
591

592
int block_device_add_partition(
221✔
593
                int fd,
594
                const char *name,
595
                int nr,
596
                uint64_t start,
597
                uint64_t size) {
598

599
        assert(fd >= 0);
221✔
600
        assert(name);
221✔
601
        assert(nr > 0);
221✔
602

603
        struct blkpg_partition bp = {
221✔
604
                .pno = nr,
605
                .start = start,
606
                .length = size,
607
        };
608

609
        struct blkpg_ioctl_arg ba = {
221✔
610
                .op = BLKPG_ADD_PARTITION,
611
                .data = &bp,
612
                .datalen = sizeof(bp),
613
        };
614

615
        if (strlen(name) >= sizeof(bp.devname))
221✔
616
                return -EINVAL;
221✔
617

618
        strcpy(bp.devname, name);
221✔
619

620
        return RET_NERRNO(ioctl(fd, BLKPG, &ba));
221✔
621
}
622

623
int block_device_remove_partition(
92✔
624
                int fd,
625
                const char *name,
626
                int nr) {
627

628
        assert(fd >= 0);
92✔
629
        assert(name);
92✔
630
        assert(nr > 0);
92✔
631

632
        struct blkpg_partition bp = {
92✔
633
                .pno = nr,
634
        };
635

636
        struct blkpg_ioctl_arg ba = {
92✔
637
                .op = BLKPG_DEL_PARTITION,
638
                .data = &bp,
639
                .datalen = sizeof(bp),
640
        };
641

642
        if (strlen(name) >= sizeof(bp.devname))
92✔
643
                return -EINVAL;
92✔
644

645
        strcpy(bp.devname, name);
92✔
646

647
        return RET_NERRNO(ioctl(fd, BLKPG, &ba));
92✔
648
}
649

650
int block_device_resize_partition(
×
651
                int fd,
652
                int nr,
653
                uint64_t start,
654
                uint64_t size) {
655

656
        assert(fd >= 0);
×
657
        assert(nr > 0);
×
658

659
        struct blkpg_partition bp = {
×
660
                .pno = nr,
661
                .start = start,
662
                .length = size,
663
        };
664

665
        struct blkpg_ioctl_arg ba = {
×
666
                .op = BLKPG_RESIZE_PARTITION,
667
                .data = &bp,
668
                .datalen = sizeof(bp),
669
        };
670

671
        return RET_NERRNO(ioctl(fd, BLKPG, &ba));
×
672
}
673

674
int partition_enumerator_new(sd_device *dev, sd_device_enumerator **ret) {
6,662✔
675
        _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
6,662✔
676
        const char *s;
6,662✔
677
        int r;
6,662✔
678

679
        assert(dev);
6,662✔
680
        assert(ret);
6,662✔
681

682
        /* Refuse invocation on partition block device, insist on "whole" device */
683
        r = block_device_is_whole_disk(dev);
6,662✔
684
        if (r < 0)
6,662✔
685
                return r;
686
        if (r == 0)
6,662✔
687
                return -ENXIO; /* return a recognizable error */
688

689
        r = sd_device_enumerator_new(&e);
6,662✔
690
        if (r < 0)
6,662✔
691
                return r;
692

693
        r = sd_device_enumerator_allow_uninitialized(e);
6,662✔
694
        if (r < 0)
6,662✔
695
                return r;
696

697
        r = sd_device_enumerator_add_match_parent(e, dev);
6,662✔
698
        if (r < 0)
6,662✔
699
                return r;
700

701
        r = sd_device_get_sysname(dev, &s);
6,662✔
702
        if (r < 0)
6,662✔
703
                return r;
704

705
        /* Also add sysname check for safety. Hopefully, this also improves performance. */
706
        s = strjoina(s, "*");
33,310✔
707
        r = sd_device_enumerator_add_match_sysname(e, s);
6,662✔
708
        if (r < 0)
6,662✔
709
                return r;
710

711
        r = sd_device_enumerator_add_match_subsystem(e, "block", /* match = */ true);
6,662✔
712
        if (r < 0)
6,662✔
713
                return r;
714

715
        r = sd_device_enumerator_add_match_property(e, "DEVTYPE", "partition");
6,662✔
716
        if (r < 0)
6,662✔
717
                return r;
718

719
        *ret = TAKE_PTR(e);
6,662✔
720
        return 0;
6,662✔
721
}
722

723
int block_device_remove_all_partitions(sd_device *dev, int fd) {
3,603✔
724
        _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
3,603✔
725
        _cleanup_(sd_device_unrefp) sd_device *dev_unref = NULL;
3,603✔
726
        _cleanup_close_ int fd_close = -EBADF;
3,603✔
727
        bool has_partitions = false;
3,603✔
728
        int r, k = 0;
3,603✔
729

730
        assert(dev || fd >= 0);
3,603✔
731

732
        if (!dev) {
3,603✔
733
                r = block_device_new_from_fd(fd, 0, &dev_unref);
×
734
                if (r < 0)
×
735
                        return r;
736

737
                dev = dev_unref;
×
738
        }
739

740
        r = partition_enumerator_new(dev, &e);
3,603✔
741
        if (r < 0)
3,603✔
742
                return r;
743

744
        if (fd < 0) {
3,603✔
745
                fd_close = sd_device_open(dev, O_CLOEXEC|O_NONBLOCK|O_NOCTTY|O_RDONLY);
×
746
                if (fd_close < 0)
×
747
                        return fd_close;
748

749
                fd = fd_close;
750
        }
751

752
        FOREACH_DEVICE(e, part) {
3,695✔
753
                const char *v, *devname;
92✔
754
                int nr;
92✔
755

756
                has_partitions = true;
92✔
757

758
                r = sd_device_get_devname(part, &devname);
92✔
759
                if (r < 0)
92✔
760
                        return r;
×
761

762
                r = sd_device_get_property_value(part, "PARTN", &v);
92✔
763
                if (r < 0)
92✔
764
                        return r;
765

766
                r = safe_atoi(v, &nr);
92✔
767
                if (r < 0)
92✔
768
                        return r;
769

770
                r = btrfs_forget_device(devname);
92✔
771
                if (r < 0 && r != -ENOENT)
92✔
772
                        log_debug_errno(r, "Failed to forget btrfs device %s, ignoring: %m", devname);
×
773

774
                r = block_device_remove_partition(fd, devname, nr);
92✔
775
                if (r == -ENODEV) {
92✔
776
                        log_debug("Kernel removed partition %s before us, ignoring", devname);
×
777
                        continue;
×
778
                }
779
                if (r < 0) {
92✔
780
                        log_debug_errno(r, "Failed to remove partition %s: %m", devname);
×
781
                        k = k < 0 ? k : r;
×
782
                        continue;
×
783
                }
784

785
                log_debug("Removed partition %s", devname);
92✔
786
        }
787

788
        return k < 0 ? k : has_partitions;
3,603✔
789
}
790

791

792
int blockdev_reread_partition_table(sd_device *dev) {
3,059✔
793
        _cleanup_close_ int fd = -EBADF;
3,059✔
794

795
        assert(dev);
3,059✔
796

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

799
        fd = sd_device_open(dev, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
3,059✔
800
        if (fd < 0)
3,059✔
801
                return fd;
802

803
        if (flock(fd, LOCK_EX|LOCK_NB) < 0)
3,019✔
804
                return -errno;
811✔
805

806
        if (ioctl(fd, BLKRRPART, 0) < 0)
2,208✔
807
                return -errno;
1,140✔
808

809
        return 0;
810
}
811

812
int blockdev_get_sector_size(int fd, uint32_t *ret) {
2,288✔
813
        int ssz = 0;
2,288✔
814

815
        assert(fd >= 0);
2,288✔
816
        assert(ret);
2,288✔
817

818
        if (ioctl(fd, BLKSSZGET, &ssz) < 0)
2,288✔
819
                return -errno;
×
820
        if (ssz <= 0) /* make sure the field is initialized */
2,288✔
821
                return log_debug_errno(SYNTHETIC_ERRNO(EIO), "Block device reported invalid sector size %i.", ssz);
×
822

823
        *ret = ssz;
2,288✔
824
        return 0;
2,288✔
825
}
826

827
int blockdev_get_device_size(int fd, uint64_t *ret) {
2,099✔
828
        uint64_t sz = 0;
2,099✔
829

830
        assert(fd >= 0);
2,099✔
831
        assert(ret);
2,099✔
832

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

835
        if (ioctl(fd, BLKGETSIZE64, &sz) < 0)
2,099✔
836
                return -errno;
×
837

838
        *ret = sz;
2,099✔
839
        return 0;
2,099✔
840
}
841

842
int blockdev_get_root(int level, dev_t *ret) {
2,743✔
843
        _cleanup_free_ char *p = NULL;
2,743✔
844
        dev_t devno;
2,743✔
845
        int r;
2,743✔
846

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

869
                                if (ret)
×
870
                                        *ret = 0;
×
871

872
                                return 0;
×
873
                        }
874
                }
875
        } else if (r < 0)
×
876
                return log_full_errno(level, r, "Failed to read symlink /run/systemd/volatile-root: %m");
×
877
        else {
878
                mode_t m;
×
879
                r = device_path_parse_major_minor(p, &m, &devno);
×
880
                if (r < 0)
×
881
                        return log_full_errno(level, r, "Failed to parse major/minor device node: %m");
×
882
                if (!S_ISBLK(m))
×
883
                        return log_full_errno(level, SYNTHETIC_ERRNO(ENOTBLK), "Volatile root device is of wrong type.");
×
884
        }
885

886
        if (ret)
2,743✔
887
                *ret = devno;
2,743✔
888

889
        return 1;
890
}
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