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

systemd / systemd / 15057632786

15 May 2025 09:01PM UTC coverage: 72.267% (+0.02%) from 72.244%
15057632786

push

github

bluca
man: document how to hook stuff into system wakeup

Fixes: #6364

298523 of 413084 relevant lines covered (72.27%)

738132.88 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 <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,006✔
26
        struct stat st;
5,006✔
27
        dev_t devnum;
5,006✔
28
        int r;
5,006✔
29

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

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

36
        if (S_ISBLK(st.st_mode))
5,006✔
37
                devnum = st.st_rdev;
4,886✔
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,923✔
56
        return 0;
4,923✔
57
}
58

59
int block_device_is_whole_disk(sd_device *dev) {
120,786✔
60
        int r;
120,786✔
61

62
        assert(dev);
120,786✔
63

64
        r = device_in_subsystem(dev, "block");
120,786✔
65
        if (r < 0)
120,786✔
66
                return r;
120,786✔
67
        if (r == 0)
120,786✔
68
                return -ENOTBLK;
69

70
        return device_is_devtype(dev, "disk");
60,577✔
71
}
72

73
int block_device_get_whole_disk(sd_device *dev, sd_device **ret) {
89,792✔
74
        int r;
89,792✔
75

76
        assert(dev);
89,792✔
77
        assert(ret);
89,792✔
78

79
        /* Do not unref returned sd_device object. */
80

81
        r = block_device_is_whole_disk(dev);
89,792✔
82
        if (r < 0)
89,792✔
83
                return r;
84
        if (r == 0) {
29,583✔
85
                r = sd_device_get_parent(dev, &dev);
21,432✔
86
                if (r == -ENOENT) /* Already removed? Let's return a recognizable error. */
21,432✔
87
                        return -ENODEV;
88
                if (r < 0)
21,409✔
89
                        return r;
90

91
                r = block_device_is_whole_disk(dev);
21,409✔
92
                if (r < 0)
21,409✔
93
                        return r;
94
                if (r == 0)
21,409✔
95
                        return -ENXIO;
96
        }
97

98
        *ret = dev;
29,560✔
99
        return 0;
29,560✔
100
}
101

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

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

110
        assert(dev);
2,763✔
111
        assert(ret);
2,763✔
112

113
        FOREACH_DEVICE_CHILD_WITH_SUFFIX(dev, child, suffix) {
5,534✔
114
                sd_device *child_whole_disk;
2,771✔
115
                dev_t n;
2,771✔
116

117
                if (!path_startswith(suffix, "slaves"))
2,771✔
118
                        continue;
2,771✔
119

120
                if (block_device_get_whole_disk(child, &child_whole_disk) < 0)
×
121
                        continue;
×
122

123
                if (sd_device_get_devnum(child_whole_disk, &n) < 0)
×
124
                        continue;
×
125

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

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

142
        if (!first_found)
2,763✔
143
                return -ENOENT;
144

145
        *ret = TAKE_PTR(first_found);
×
146
        return 0;
×
147
}
148

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

154
        assert(fd >= 0);
3,875✔
155
        assert(ret);
3,875✔
156

157
        r = fd_get_devnum(fd, flags, &devnum);
3,875✔
158
        if (r < 0)
3,875✔
159
                return r;
160

161
        r = sd_device_new_from_devnum(&dev, 'b', devnum);
3,792✔
162
        if (r < 0)
3,792✔
163
                return r;
164

165
        if (FLAGS_SET(flags, BLOCK_DEVICE_LOOKUP_ORIGINATING)) {
3,792✔
166
                _cleanup_(sd_device_unrefp) sd_device *dev_origin = NULL;
1✔
167
                sd_device *dev_whole_disk;
1✔
168

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

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

180
        if (FLAGS_SET(flags, BLOCK_DEVICE_LOOKUP_WHOLE_DISK)) {
3,792✔
181
                sd_device *dev_whole_disk;
219✔
182

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

187
                *ret = sd_device_ref(dev_whole_disk);
219✔
188
                return 0;
219✔
189
        }
190

191
        *ret = sd_device_ref(dev);
3,573✔
192
        return 0;
3,573✔
193
}
194

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

198
        assert(path);
3✔
199
        assert(ret);
3✔
200

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

205
        return block_device_new_from_fd(fd, flags, ret);
1✔
206
}
207

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

214
        assert(ret);
3,896✔
215

216
        if (major(d) == 0)
3,896✔
217
                return -ENODEV;
218

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

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

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

239
        r = parse_devnum(s, &devt);
2,781✔
240
        if (r < 0)
2,781✔
241
                return r;
242

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

248
        *ret = devt;
2,781✔
249
        return 1;
2,781✔
250
}
251

252
int get_block_device_fd(int fd, dev_t *ret) {
2,912✔
253
        struct stat st;
2,912✔
254
        int r;
2,912✔
255

256
        assert(fd >= 0);
2,912✔
257
        assert(ret);
2,912✔
258

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

262
        if (fstat(fd, &st))
2,912✔
263
                return -errno;
×
264

265
        if (major(st.st_dev) != 0) {
2,912✔
266
                *ret = st.st_dev;
2,851✔
267
                return 1;
2,851✔
268
        }
269

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

274
        *ret = 0;
61✔
275
        return 0;
61✔
276
}
277

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

281
        assert(path);
138✔
282
        assert(ret);
138✔
283

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

288
        return get_block_device_fd(fd, ret);
138✔
289
}
290

291
int block_get_originating(dev_t dt, dev_t *ret) {
2,762✔
292
        _cleanup_(sd_device_unrefp) sd_device *dev = NULL, *origin = NULL;
5,524✔
293
        int r;
2,762✔
294

295
        assert(ret);
2,762✔
296

297
        r = sd_device_new_from_devnum(&dev, 'b', dt);
2,762✔
298
        if (r < 0)
2,762✔
299
                return r;
300

301
        r = block_device_get_originating(dev, &origin);
2,762✔
302
        if (r < 0)
2,762✔
303
                return r;
304

305
        return sd_device_get_devnum(origin, ret);
×
306
}
307

308
int get_block_device_harder_fd(int fd, dev_t *ret) {
2,756✔
309
        int r;
2,756✔
310

311
        assert(fd >= 0);
2,756✔
312
        assert(ret);
2,756✔
313

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

317
        r = get_block_device_fd(fd, ret);
2,756✔
318
        if (r <= 0)
2,756✔
319
                return r;
320

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

325
        return 1;
326
}
327

328
int get_block_device_harder(const char *path, dev_t *ret) {
2,756✔
329
        _cleanup_close_ int fd = -EBADF;
2,756✔
330

331
        assert(path);
2,756✔
332
        assert(ret);
2,756✔
333

334
        fd = open(path, O_RDONLY|O_NOFOLLOW|O_CLOEXEC);
2,756✔
335
        if (fd < 0)
2,756✔
336
                return -errno;
×
337

338
        return get_block_device_harder_fd(fd, ret);
2,756✔
339
}
340

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

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

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

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

356
        if (flock(lock_fd, operation) < 0)
×
357
                return -errno;
×
358

359
        return TAKE_FD(lock_fd);
360
}
361

362
int blockdev_partscan_enabled(sd_device *dev) {
4,701✔
363
        unsigned capability;
4,701✔
364
        int r, ext_range;
4,701✔
365

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

413
        assert(dev);
4,701✔
414

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

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

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

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

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

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

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

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

462
int blockdev_partscan_enabled_fd(int fd) {
1,919✔
463
        _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
1,919✔
464
        int r;
1,919✔
465

466
        assert(fd >= 0);
1,919✔
467

468
        r = block_device_new_from_fd(fd, 0, &dev);
1,919✔
469
        if (r < 0)
1,919✔
470
                return r;
471

472
        return blockdev_partscan_enabled(dev);
1,836✔
473
}
474

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

480
        assert(sysfs_path);
19✔
481

482
        if (depth_left == 0)
19✔
483
                return -EINVAL;
484

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

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

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

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

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

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

512
                return -errno;
×
513
        }
514

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

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

525
                        break; /* No more underlying devices */
526
                }
527

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

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

538
                found_encrypted = true;
×
539
        }
540

541
        return found_encrypted;
542
}
543

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

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

555
        xsprintf_sys_block_path(p, NULL, devt);
16✔
556

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

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

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

571
        xsprintf_sys_block_path(p, NULL, devt);
3✔
572

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

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

580
        assert(fd >= 0);
1,131✔
581
        assert(ret);
1,131✔
582

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

587
        return block_get_whole_disk(devt, ret);
1,131✔
588
}
589

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

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

597
        return fd_get_whole_disk(fd, backing, ret);
1,131✔
598
}
599

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

607
        assert(fd >= 0);
222✔
608
        assert(name);
222✔
609
        assert(nr > 0);
222✔
610

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

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

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

626
        strcpy(bp.devname, name);
222✔
627

628
        return RET_NERRNO(ioctl(fd, BLKPG, &ba));
222✔
629
}
630

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

636
        assert(fd >= 0);
93✔
637
        assert(name);
93✔
638
        assert(nr > 0);
93✔
639

640
        struct blkpg_partition bp = {
93✔
641
                .pno = nr,
642
        };
643

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

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

653
        strcpy(bp.devname, name);
93✔
654

655
        return RET_NERRNO(ioctl(fd, BLKPG, &ba));
93✔
656
}
657

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

664
        assert(fd >= 0);
×
665
        assert(nr > 0);
×
666

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

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

679
        return RET_NERRNO(ioctl(fd, BLKPG, &ba));
×
680
}
681

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

687
        assert(dev);
6,659✔
688
        assert(ret);
6,659✔
689

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

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

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

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

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

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

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

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

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

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

738
        assert(dev || fd >= 0);
3,599✔
739

740
        if (!dev) {
3,599✔
741
                r = block_device_new_from_fd(fd, 0, &dev_unref);
×
742
                if (r < 0)
×
743
                        return r;
744

745
                dev = dev_unref;
×
746
        }
747

748
        r = partition_enumerator_new(dev, &e);
3,599✔
749
        if (r < 0)
3,599✔
750
                return r;
751

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

757
                fd = fd_close;
758
        }
759

760
        FOREACH_DEVICE(e, part) {
3,692✔
761
                const char *v, *devname;
93✔
762
                int nr;
93✔
763

764
                has_partitions = true;
93✔
765

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

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

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

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

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

793
                log_debug("Removed partition %s", devname);
93✔
794
        }
795

796
        return k < 0 ? k : has_partitions;
3,599✔
797
}
798

799

800
int blockdev_reread_partition_table(sd_device *dev) {
3,060✔
801
        _cleanup_close_ int fd = -EBADF;
3,060✔
802

803
        assert(dev);
3,060✔
804

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

807
        fd = sd_device_open(dev, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
3,060✔
808
        if (fd < 0)
3,060✔
809
                return fd;
810

811
        if (flock(fd, LOCK_EX|LOCK_NB) < 0)
3,025✔
812
                return -errno;
776✔
813

814
        if (ioctl(fd, BLKRRPART, 0) < 0)
2,249✔
815
                return -errno;
1,174✔
816

817
        return 0;
818
}
819

820
int blockdev_get_sector_size(int fd, uint32_t *ret) {
2,289✔
821
        int ssz = 0;
2,289✔
822

823
        assert(fd >= 0);
2,289✔
824
        assert(ret);
2,289✔
825

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

831
        *ret = ssz;
2,289✔
832
        return 0;
2,289✔
833
}
834

835
int blockdev_get_device_size(int fd, uint64_t *ret) {
2,100✔
836
        uint64_t sz = 0;
2,100✔
837

838
        assert(fd >= 0);
2,100✔
839
        assert(ret);
2,100✔
840

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

843
        if (ioctl(fd, BLKGETSIZE64, &sz) < 0)
2,100✔
844
                return -errno;
×
845

846
        *ret = sz;
2,100✔
847
        return 0;
2,100✔
848
}
849

850
int blockdev_get_root(int level, dev_t *ret) {
2,756✔
851
        _cleanup_free_ char *p = NULL;
2,756✔
852
        dev_t devno;
2,756✔
853
        int r;
2,756✔
854

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

877
                                if (ret)
×
878
                                        *ret = 0;
×
879

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

894
        if (ret)
2,756✔
895
                *ret = devno;
2,756✔
896

897
        return 1;
898
}
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