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

systemd / systemd / 18765396043

23 Oct 2025 01:51PM UTC coverage: 72.284% (-0.01%) from 72.295%
18765396043

push

github

YHNdnzj
core: increment start limit counter only when we can start the unit

Otherwise, e.g. requesting to start a unit that is under stopping may
enter the failed state.

This makes
- rename .can_start() -> .test_startable(), and make it allow to return
  boolean and refuse to start units when it returns false,
- refuse earlier to start units that are in the deactivating state, so
  several redundant conditions in .start() can be dropped,
- move checks for unit states mapped to UNIT_ACTIVATING from .start() to
  .test_startable().

Fixes #39247.

13 of 15 new or added lines in 8 files covered. (86.67%)

6946 existing lines in 72 files now uncovered.

304970 of 421905 relevant lines covered (72.28%)

1105466.04 hits per line

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

76.84
/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, BlockDeviceLookupFlags flags, dev_t *ret) {
5,027✔
28
        struct stat st;
5,027✔
29
        dev_t devnum;
5,027✔
30
        int r;
5,027✔
31

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

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

38
        if (S_ISBLK(st.st_mode))
5,027✔
39
                devnum = st.st_rdev;
4,910✔
40
        else if (!FLAGS_SET(flags, BLOCK_DEVICE_LOOKUP_BACKING))
117✔
41
                return -ENOTBLK;
42
        else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
33✔
43
                return -ENOTBLK;
44
        else if (major(st.st_dev) != 0)
33✔
45
                devnum = st.st_dev;
33✔
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,943✔
58
        return 0;
4,943✔
59
}
60

61
int block_device_is_whole_disk(sd_device *dev) {
116,368✔
62
        int r;
116,368✔
63

64
        assert(dev);
116,368✔
65

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

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

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

78
        assert(dev);
85,823✔
79
        assert(ret);
85,823✔
80

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

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

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

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

104
int block_device_get_originating(sd_device *dev, sd_device **ret) {
3,087✔
105
        _cleanup_(sd_device_unrefp) sd_device *first_found = NULL;
3,087✔
106
        const char *suffix;
3,087✔
107
        dev_t devnum = 0;  /* avoid false maybe-uninitialized warning */
3,087✔
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);
3,087✔
113
        assert(ret);
3,087✔
114

115
        FOREACH_DEVICE_CHILD_WITH_SUFFIX(dev, child, suffix) {
6,187✔
116
                sd_device *child_whole_disk;
3,100✔
117
                dev_t n;
3,100✔
118

119
                if (!path_startswith(suffix, "slaves"))
3,100✔
120
                        continue;
3,100✔
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)
3,087✔
145
                return -ENOENT;
146

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

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

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

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

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

167
        if (FLAGS_SET(flags, BLOCK_DEVICE_LOOKUP_ORIGINATING)) {
3,834✔
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,834✔
183
                sd_device *dev_whole_disk;
235✔
184

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

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

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

197
int block_device_new_from_path(const char *path, BlockDeviceLookupFlags 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) {
4,145✔
211
        char p[SYS_BLOCK_PATH_MAX("/partition")];
4,145✔
212
        _cleanup_free_ char *s = NULL;
4,145✔
213
        dev_t devt;
4,145✔
214
        int r;
4,145✔
215

216
        assert(ret);
4,145✔
217

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

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

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

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

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

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

250
        *ret = devt;
3,051✔
251
        return 1;
3,051✔
252
}
253

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

258
        assert(fd >= 0);
3,308✔
259
        assert(ret);
3,308✔
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))
3,308✔
265
                return -errno;
×
266

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

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

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

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

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

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

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

293
int block_get_originating(dev_t dt, dev_t *ret) {
3,086✔
294
        _cleanup_(sd_device_unrefp) sd_device *dev = NULL, *origin = NULL;
6,172✔
295
        int r;
3,086✔
296

297
        assert(ret);
3,086✔
298

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

303
        r = block_device_get_originating(dev, &origin);
3,086✔
304
        if (r < 0)
3,086✔
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) {
3,078✔
311
        int r;
3,078✔
312

313
        assert(fd >= 0);
3,078✔
314
        assert(ret);
3,078✔
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);
3,078✔
320
        if (r <= 0)
3,078✔
321
                return r;
322

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

327
        return 1;
328
}
329

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

333
        assert(path);
3,025✔
334
        assert(ret);
3,025✔
335

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

340
        return get_block_device_harder_fd(fd, ret);
3,025✔
341
}
342

343
int lock_whole_block_device(dev_t devt, int open_flags, 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
         * NB: it matters whether open_flags indicates open for write: only then will the eventual closing of
351
         * the fd trigger udev's partitioning rescanning of the device (as it watches for IN_CLOSE_WRITE),
352
         * hence make sure to pass the right value there. */
353

354
        r = block_get_whole_disk(devt, &whole_devt);
×
355
        if (r < 0)
×
356
                return r;
357

358
        lock_fd = device_open_from_devnum(S_IFBLK, whole_devt, open_flags|O_CLOEXEC|O_NONBLOCK|O_NOCTTY, NULL);
×
359
        if (lock_fd < 0)
×
360
                return lock_fd;
361

UNCOV
362
        if (flock(lock_fd, operation) < 0)
×
UNCOV
363
                return -errno;
×
364

365
        return TAKE_FD(lock_fd);
366
}
367

368
int blockdev_partscan_enabled(sd_device *dev) {
8,551✔
369
        unsigned capability;
8,551✔
370
        int r, ext_range;
8,551✔
371

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

419
        assert(dev);
8,551✔
420

421
        r = device_in_subsystem(dev, "block");
8,551✔
422
        if (r < 0)
8,551✔
423
                return r;
8,551✔
424
        if (r == 0)
8,551✔
425
                return -ENOTBLK;
426

427
        /* For v6.10 or newer. */
428
        r = device_get_sysattr_bool(dev, "partscan");
8,551✔
429
        if (r != -ENOENT)
8,551✔
430
                return r;
431

432
        /* Partition block devices never have partition scanning on, there's no concept of sub-partitions for
433
         * partitions. */
434
        r = device_is_devtype(dev, "partition");
12✔
435
        if (r < 0)
12✔
436
                return r;
437
        if (r > 0)
12✔
438
                return false;
439

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

UNCOV
445
        r = device_get_sysattr_int(dev, "ext_range", &ext_range);
×
UNCOV
446
        if (r == -ENOENT) /* If the ext_range file doesn't exist then we are most likely looking at a
×
447
                           * partition block device, not the whole block device. And that means we have no
448
                           * partition scanning on for it (we do for its parent, but not for the partition
449
                           * itself). */
450
                return false;
451
        if (r < 0)
×
452
                return r;
453

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

458
        r = device_get_sysattr_unsigned_full(dev, "capability", 16, &capability);
×
UNCOV
459
        if (r == -ENOENT)
×
460
                return false;
UNCOV
461
        if (r < 0)
×
462
                return r;
463

464
#define GENHD_FL_NO_PART_OLD 0x0200
465
#define GENHD_FL_NO_PART_NEW 0x0004
466
        /* If one of the NO_PART flags is set, part scanning is definitely off. */
UNCOV
467
        if ((capability & (GENHD_FL_NO_PART_OLD | GENHD_FL_NO_PART_NEW)) != 0)
×
UNCOV
468
                return false;
×
469

470
        /* Otherwise, assume part scanning is on, we have no further checks available. Assume the best. */
471
        return true;
472
}
473

474
int blockdev_partscan_enabled_fd(int fd) {
1,937✔
475
        _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
1,937✔
476
        int r;
1,937✔
477

478
        assert(fd >= 0);
1,937✔
479

480
        r = block_device_new_from_fd(fd, 0, &dev);
1,937✔
481
        if (r < 0)
1,937✔
482
                return r;
483

484
        return blockdev_partscan_enabled(dev);
1,853✔
485
}
486

487
static int blockdev_is_encrypted(const char *sysfs_path, unsigned depth_left) {
20✔
488
        _cleanup_free_ char *p = NULL, *uuids = NULL;
20✔
489
        _cleanup_closedir_ DIR *d = NULL;
20✔
490
        int r, found_encrypted = false;
20✔
491

492
        assert(sysfs_path);
20✔
493

494
        if (depth_left == 0)
20✔
495
                return -EINVAL;
496

497
        p = path_join(sysfs_path, "dm/uuid");
20✔
498
        if (!p)
20✔
499
                return -ENOMEM;
500

501
        r = read_one_line_file(p, &uuids);
20✔
502
        if (r != -ENOENT) {
20✔
UNCOV
503
                if (r < 0)
×
504
                        return r;
505

506
                /* The DM device's uuid attribute is prefixed with "CRYPT-" if this is a dm-crypt device. */
UNCOV
507
                if (startswith(uuids, "CRYPT-"))
×
508
                        return true;
509
        }
510

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

514
        p = mfree(p);
20✔
515
        p = path_join(sysfs_path, "slaves");
20✔
516
        if (!p)
20✔
517
                return -ENOMEM;
518

519
        d = opendir(p);
20✔
520
        if (!d) {
20✔
521
                if (errno == ENOENT) /* Doesn't have underlying devices */
20✔
522
                        return false;
523

524
                return -errno;
×
525
        }
526

UNCOV
527
        for (;;) {
×
UNCOV
528
                _cleanup_free_ char *q = NULL;
×
UNCOV
529
                struct dirent *de;
×
530

531
                errno = 0;
×
UNCOV
532
                de = readdir_no_dot(d);
×
UNCOV
533
                if (!de) {
×
534
                        if (errno != 0)
×
535
                                return -errno;
×
536

537
                        break; /* No more underlying devices */
538
                }
539

540
                q = path_join(p, de->d_name);
×
UNCOV
541
                if (!q)
×
542
                        return -ENOMEM;
543

UNCOV
544
                r = blockdev_is_encrypted(q, depth_left - 1);
×
UNCOV
545
                if (r < 0)
×
546
                        return r;
UNCOV
547
                if (r == 0) /* we found one that is not encrypted? then propagate that immediately */
×
548
                        return false;
549

UNCOV
550
                found_encrypted = true;
×
551
        }
552

553
        return found_encrypted;
554
}
555

556
int fd_is_encrypted(int fd) {
19✔
557
        char p[SYS_BLOCK_PATH_MAX("")];
19✔
558
        dev_t devt;
19✔
559
        int r;
19✔
560

561
        r = get_block_device_fd(fd, &devt);
19✔
562
        if (r < 0)
19✔
563
                return r;
19✔
564
        if (r == 0) /* doesn't have a block device */
19✔
565
                return false;
566

567
        xsprintf_sys_block_path(p, NULL, devt);
17✔
568

569
        return blockdev_is_encrypted(p, 10 /* safety net: maximum recursion depth */);
17✔
570
}
571

572
int path_is_encrypted(const char *path) {
7✔
573
        char p[SYS_BLOCK_PATH_MAX("")];
7✔
574
        dev_t devt;
7✔
575
        int r;
7✔
576

577
        r = get_block_device(path, &devt);
7✔
578
        if (r < 0)
7✔
579
                return r;
7✔
580
        if (r == 0) /* doesn't have a block device */
7✔
581
                return false;
582

583
        xsprintf_sys_block_path(p, NULL, devt);
3✔
584

585
        return blockdev_is_encrypted(p, 10 /* safety net: maximum recursion depth */);
3✔
586
}
587

588
int fd_get_whole_disk(int fd, bool backing, dev_t *ret) {
1,109✔
589
        dev_t devt;
1,109✔
590
        int r;
1,109✔
591

592
        assert(fd >= 0);
1,109✔
593
        assert(ret);
1,109✔
594

595
        r = fd_get_devnum(fd, backing ? BLOCK_DEVICE_LOOKUP_BACKING : 0, &devt);
2,212✔
596
        if (r < 0)
1,109✔
597
                return r;
1,109✔
598

599
        return block_get_whole_disk(devt, ret);
1,109✔
600
}
601

602
int path_get_whole_disk(const char *path, bool backing, dev_t *ret) {
1,109✔
603
        _cleanup_close_ int fd = -EBADF;
1,109✔
604

605
        fd = open(path, O_CLOEXEC|O_PATH);
1,109✔
606
        if (fd < 0)
1,109✔
UNCOV
607
                return -errno;
×
608

609
        return fd_get_whole_disk(fd, backing, ret);
1,109✔
610
}
611

612
int block_device_add_partition(
264✔
613
                int fd,
614
                const char *name,
615
                int nr,
616
                uint64_t start,
617
                uint64_t size) {
618

619
        assert(fd >= 0);
264✔
620
        assert(name);
264✔
621
        assert(nr > 0);
264✔
622

623
        struct blkpg_partition bp = {
264✔
624
                .pno = nr,
625
                .start = start,
626
                .length = size,
627
        };
628

629
        struct blkpg_ioctl_arg ba = {
264✔
630
                .op = BLKPG_ADD_PARTITION,
631
                .data = &bp,
632
                .datalen = sizeof(bp),
633
        };
634

635
        if (strlen(name) >= sizeof(bp.devname))
264✔
636
                return -EINVAL;
264✔
637

638
        strcpy(bp.devname, name);
264✔
639

640
        return RET_NERRNO(ioctl(fd, BLKPG, &ba));
264✔
641
}
642

643
int block_device_remove_partition(
96✔
644
                int fd,
645
                const char *name,
646
                int nr) {
647

648
        assert(fd >= 0);
96✔
649
        assert(name);
96✔
650
        assert(nr > 0);
96✔
651

652
        struct blkpg_partition bp = {
96✔
653
                .pno = nr,
654
        };
655

656
        struct blkpg_ioctl_arg ba = {
96✔
657
                .op = BLKPG_DEL_PARTITION,
658
                .data = &bp,
659
                .datalen = sizeof(bp),
660
        };
661

662
        if (strlen(name) >= sizeof(bp.devname))
96✔
663
                return -EINVAL;
96✔
664

665
        strcpy(bp.devname, name);
96✔
666

667
        return RET_NERRNO(ioctl(fd, BLKPG, &ba));
96✔
668
}
669

UNCOV
670
int block_device_resize_partition(
×
671
                int fd,
672
                int nr,
673
                uint64_t start,
674
                uint64_t size) {
675

UNCOV
676
        assert(fd >= 0);
×
UNCOV
677
        assert(nr > 0);
×
678

UNCOV
679
        struct blkpg_partition bp = {
×
680
                .pno = nr,
681
                .start = start,
682
                .length = size,
683
        };
684

UNCOV
685
        struct blkpg_ioctl_arg ba = {
×
686
                .op = BLKPG_RESIZE_PARTITION,
687
                .data = &bp,
688
                .datalen = sizeof(bp),
689
        };
690

UNCOV
691
        return RET_NERRNO(ioctl(fd, BLKPG, &ba));
×
692
}
693

694
int partition_enumerator_new(sd_device *dev, sd_device_enumerator **ret) {
6,486✔
695
        _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
6,486✔
696
        const char *s;
6,486✔
697
        int r;
6,486✔
698

699
        assert(dev);
6,486✔
700
        assert(ret);
6,486✔
701

702
        /* Refuse invocation on partition block device, insist on "whole" device */
703
        r = block_device_is_whole_disk(dev);
6,486✔
704
        if (r < 0)
6,486✔
705
                return r;
706
        if (r == 0)
6,486✔
707
                return -ENXIO; /* return a recognizable error */
708

709
        r = sd_device_enumerator_new(&e);
6,486✔
710
        if (r < 0)
6,486✔
711
                return r;
712

713
        r = sd_device_enumerator_allow_uninitialized(e);
6,486✔
714
        if (r < 0)
6,486✔
715
                return r;
716

717
        r = sd_device_enumerator_add_match_parent(e, dev);
6,486✔
718
        if (r < 0)
6,486✔
719
                return r;
720

721
        r = sd_device_get_sysname(dev, &s);
6,486✔
722
        if (r < 0)
6,486✔
723
                return r;
724

725
        /* Also add sysname check for safety. Hopefully, this also improves performance. */
726
        s = strjoina(s, "*");
32,430✔
727
        r = sd_device_enumerator_add_match_sysname(e, s);
6,486✔
728
        if (r < 0)
6,486✔
729
                return r;
730

731
        r = sd_device_enumerator_add_match_subsystem(e, "block", /* match = */ true);
6,486✔
732
        if (r < 0)
6,486✔
733
                return r;
734

735
        r = sd_device_enumerator_add_match_property(e, "DEVTYPE", "partition");
6,486✔
736
        if (r < 0)
6,486✔
737
                return r;
738

739
        *ret = TAKE_PTR(e);
6,486✔
740
        return 0;
6,486✔
741
}
742

743
int block_device_remove_all_partitions(sd_device *dev, int fd) {
3,611✔
744
        _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
3,611✔
745
        _cleanup_(sd_device_unrefp) sd_device *dev_unref = NULL;
3,611✔
746
        _cleanup_close_ int fd_close = -EBADF;
3,611✔
747
        bool has_partitions = false;
3,611✔
748
        int r, k = 0;
3,611✔
749

750
        assert(dev || fd >= 0);
3,611✔
751

752
        if (!dev) {
3,611✔
UNCOV
753
                r = block_device_new_from_fd(fd, 0, &dev_unref);
×
UNCOV
754
                if (r < 0)
×
755
                        return r;
756

UNCOV
757
                dev = dev_unref;
×
758
        }
759

760
        r = partition_enumerator_new(dev, &e);
3,611✔
761
        if (r < 0)
3,611✔
762
                return r;
763

764
        if (fd < 0) {
3,611✔
UNCOV
765
                fd_close = sd_device_open(dev, O_CLOEXEC|O_NONBLOCK|O_NOCTTY|O_RDONLY);
×
UNCOV
766
                if (fd_close < 0)
×
767
                        return fd_close;
768

769
                fd = fd_close;
770
        }
771

772
        FOREACH_DEVICE(e, part) {
3,707✔
773
                const char *v, *devname;
96✔
774
                int nr;
96✔
775

776
                has_partitions = true;
96✔
777

778
                r = sd_device_get_devname(part, &devname);
96✔
779
                if (r < 0)
96✔
UNCOV
780
                        return r;
×
781

782
                r = sd_device_get_property_value(part, "PARTN", &v);
96✔
783
                if (r < 0)
96✔
784
                        return r;
785

786
                r = safe_atoi(v, &nr);
96✔
787
                if (r < 0)
96✔
788
                        return r;
789

790
                r = btrfs_forget_device(devname);
96✔
791
                if (r < 0 && r != -ENOENT)
96✔
792
                        log_debug_errno(r, "Failed to forget btrfs device %s, ignoring: %m", devname);
×
793

794
                r = block_device_remove_partition(fd, devname, nr);
96✔
795
                if (r == -ENODEV) {
96✔
UNCOV
796
                        log_debug("Kernel removed partition %s before us, ignoring", devname);
×
UNCOV
797
                        continue;
×
798
                }
799
                if (r < 0) {
96✔
UNCOV
800
                        log_debug_errno(r, "Failed to remove partition %s: %m", devname);
×
UNCOV
801
                        k = k < 0 ? k : r;
×
UNCOV
802
                        continue;
×
803
                }
804

805
                log_debug("Removed partition %s", devname);
96✔
806
        }
807

808
        return k < 0 ? k : has_partitions;
3,611✔
809
}
810

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

814
        assert(fd >= 0);
2,308✔
815
        assert(ret);
2,308✔
816

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

822
        *ret = ssz;
2,308✔
823
        return 0;
2,308✔
824
}
825

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

829
        assert(fd >= 0);
2,168✔
830
        assert(ret);
2,168✔
831

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

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

837
        *ret = sz;
2,168✔
838
        return 0;
2,168✔
839
}
840

841
int blockdev_get_root(int level, dev_t *ret) {
3,025✔
842
        _cleanup_free_ char *p = NULL;
3,025✔
843
        dev_t devno;
3,025✔
844
        int r;
3,025✔
845

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

UNCOV
868
                                if (ret)
×
869
                                        *ret = 0;
×
870

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

885
        if (ret)
3,025✔
886
                *ret = devno;
3,025✔
887

888
        return 1;
889
}
890

891
int partition_node_of(const char *node, unsigned nr, char **ret) {
10,385✔
892
        int r;
10,385✔
893

894
        assert(node);
10,385✔
895
        assert(nr > 0);
10,385✔
896
        assert(ret);
10,385✔
897

898
        /* Given a device node path to a block device returns the device node path to the partition block
899
         * device of the specified partition */
900

901
        _cleanup_free_ char *fn = NULL;
10,385✔
902
        r = path_extract_filename(node, &fn);
10,385✔
903
        if (r < 0)
10,385✔
904
                return r;
905
        if (r == O_DIRECTORY)
10,382✔
906
                return -EISDIR;
907

908
        _cleanup_free_ char *dn = NULL;
10,381✔
909
        r = path_extract_directory(node, &dn);
10,381✔
910
        if (r < 0 && r != -EDESTADDRREQ) /* allow if only filename is specified */
10,381✔
911
                return r;
912

913
        size_t l = strlen(fn);
10,381✔
914
        assert(l > 0); /* underflow check for the subtraction below */
10,381✔
915

916
        bool need_p = ascii_isdigit(fn[l-1]); /* Last char a digit? */
10,381✔
917

918
        _cleanup_free_ char *subnode = NULL;
10,381✔
919
        if (asprintf(&subnode, "%s%s%u", fn, need_p ? "p" : "", nr) < 0)
20,112✔
920
                return -ENOMEM;
921

922
        if (dn) {
10,381✔
923
                _cleanup_free_ char *j = path_join(dn, subnode);
10,379✔
924
                if (!j)
10,379✔
UNCOV
925
                        return -ENOMEM;
×
926

927
                *ret = TAKE_PTR(j);
10,379✔
928
        } else
929
                *ret = TAKE_PTR(subnode);
2✔
930

931
        return 0;
932
}
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