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

systemd / systemd / 21846209963

09 Feb 2026 03:52PM UTC coverage: 72.697% (-0.02%) from 72.716%
21846209963

push

github

daandemeyer
meson: guard symlinks in sysconfdir behind install_sysconfidr

Symlinks to files inside sysconfdir are now only installed if
ìnstall_sysconfdir=true (which is the default).

If sshconfdir,sshdconfdir,shellprofiledir are not inside sysconfdir and
install_sysconfidr=false, these symlinks are still installed to the
configured directory.

311951 of 429113 relevant lines covered (72.7%)

1156102.48 hits per line

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

76.34
/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,320✔
28
        struct stat st;
5,320✔
29
        dev_t devnum;
5,320✔
30
        int r;
5,320✔
31

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

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

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

61
int block_device_is_whole_disk(sd_device *dev) {
127,840✔
62
        int r;
127,840✔
63

64
        assert(dev);
127,840✔
65

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

72
        return device_is_devtype(dev, "disk");
69,430✔
73
}
74

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

78
        assert(dev);
93,500✔
79
        assert(ret);
93,500✔
80

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

83
        r = block_device_is_whole_disk(dev);
93,500✔
84
        if (r < 0)
93,500✔
85
                return r;
86
        if (r == 0) {
35,090✔
87
                r = sd_device_get_parent(dev, &dev);
22,956✔
88
                if (r == -ENOENT) /* Already removed? Let's return a recognizable error. */
22,956✔
89
                        return -ENODEV;
90
                if (r < 0)
22,892✔
91
                        return r;
92

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

100
        *ret = dev;
35,026✔
101
        return 0;
35,026✔
102
}
103

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

115
        FOREACH_DEVICE_CHILD_WITH_SUFFIX(dev, child, suffix) {
7,297✔
116
                sd_device *child_whole_disk;
3,655✔
117
                dev_t n;
3,655✔
118

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

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

151
int block_device_get_originating(sd_device *dev, sd_device **ret, bool recursive) {
3,642✔
152
        _cleanup_(sd_device_unrefp) sd_device *current = NULL;
3,642✔
153
        int r;
3,642✔
154

155
        assert(dev);
3,642✔
156
        assert(ret);
3,642✔
157

158
        if (!recursive)
3,642✔
159
                return block_device_get_originating_one(dev, ret);
9✔
160

161
        current = sd_device_ref(dev);
3,633✔
162

163
        for (;;) {
3,633✔
164
                sd_device *origin;
3,633✔
165

166
                r = block_device_get_originating_one(current, &origin);
3,633✔
167
                if (r == -ENOENT)
3,633✔
168
                        break;
169
                if (r < 0)
×
170
                        return r;
×
171

172
                sd_device_unref(current);
×
173
                current = origin;
×
174
        }
175

176
        if (current == dev)
3,633✔
177
                return -ENOENT;
178

179
        *ret = TAKE_PTR(current);
×
180

181
        return 0;
×
182
}
183

184
int block_device_new_from_fd(int fd, BlockDeviceLookupFlags flags, sd_device **ret) {
4,211✔
185
        _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
4,211✔
186
        dev_t devnum;
4,211✔
187
        int r;
4,211✔
188

189
        assert(fd >= 0);
4,211✔
190
        assert(ret);
4,211✔
191

192
        r = fd_get_devnum(fd, flags, &devnum);
4,211✔
193
        if (r < 0)
4,211✔
194
                return r;
195

196
        r = sd_device_new_from_devnum(&dev, 'b', devnum);
4,107✔
197
        if (r < 0)
4,107✔
198
                return r;
199

200
        if (FLAGS_SET(flags, BLOCK_DEVICE_LOOKUP_ORIGINATING)) {
4,107✔
201
                _cleanup_(sd_device_unrefp) sd_device *dev_origin = NULL;
1✔
202
                sd_device *dev_whole_disk;
1✔
203

204
                r = block_device_get_whole_disk(dev, &dev_whole_disk);
1✔
205
                if (r < 0)
1✔
206
                        return r;
207

208
                r = block_device_get_originating(dev_whole_disk, &dev_origin, /* recursive= */ false);
1✔
209
                if (r >= 0)
1✔
210
                        device_unref_and_replace(dev, dev_origin);
×
211
                else if (r != -ENOENT)
1✔
212
                        return r;
213
        }
214

215
        if (FLAGS_SET(flags, BLOCK_DEVICE_LOOKUP_WHOLE_DISK)) {
4,107✔
216
                sd_device *dev_whole_disk;
325✔
217

218
                r = block_device_get_whole_disk(dev, &dev_whole_disk);
325✔
219
                if (r < 0)
325✔
220
                        return r;
325✔
221

222
                *ret = sd_device_ref(dev_whole_disk);
325✔
223
                return 0;
325✔
224
        }
225

226
        *ret = sd_device_ref(dev);
3,782✔
227
        return 0;
3,782✔
228
}
229

230
int block_device_new_from_path(const char *path, BlockDeviceLookupFlags flags, sd_device **ret) {
3✔
231
        _cleanup_close_ int fd = -EBADF;
3✔
232

233
        assert(path);
3✔
234
        assert(ret);
3✔
235

236
        fd = open(path, O_CLOEXEC|O_PATH);
3✔
237
        if (fd < 0)
3✔
238
                return -errno;
2✔
239

240
        return block_device_new_from_fd(fd, flags, ret);
1✔
241
}
242

243
int block_get_whole_disk(dev_t d, dev_t *ret) {
4,701✔
244
        char p[SYS_BLOCK_PATH_MAX("/partition")];
4,701✔
245
        _cleanup_free_ char *s = NULL;
4,701✔
246
        dev_t devt;
4,701✔
247
        int r;
4,701✔
248

249
        assert(ret);
4,701✔
250

251
        if (major(d) == 0)
4,701✔
252
                return -ENODEV;
253

254
        /* If it has a queue this is good enough for us */
255
        xsprintf_sys_block_path(p, "/queue", d);
4,701✔
256
        if (access(p, F_OK) >= 0) {
4,701✔
257
                *ret = d;
1,094✔
258
                return 0;
1,094✔
259
        }
260
        if (errno != ENOENT)
3,607✔
261
                return -errno;
×
262

263
        /* If it is a partition find the originating device */
264
        xsprintf_sys_block_path(p, "/partition", d);
3,607✔
265
        if (access(p, F_OK) < 0)
3,607✔
266
                return -errno;
×
267

268
        /* Get parent dev_t */
269
        xsprintf_sys_block_path(p, "/../dev", d);
3,607✔
270
        r = read_one_line_file(p, &s);
3,607✔
271
        if (r < 0)
3,607✔
272
                return r;
273

274
        r = parse_devnum(s, &devt);
3,607✔
275
        if (r < 0)
3,607✔
276
                return r;
277

278
        /* Only return this if it is really good enough for us. */
279
        xsprintf_sys_block_path(p, "/queue", devt);
3,607✔
280
        if (access(p, F_OK) < 0)
3,607✔
281
                return -errno;
×
282

283
        *ret = devt;
3,607✔
284
        return 1;
3,607✔
285
}
286

287
int get_block_device_fd(int fd, dev_t *ret) {
3,938✔
288
        struct stat st;
3,938✔
289
        int r;
3,938✔
290

291
        assert(fd >= 0);
3,938✔
292
        assert(ret);
3,938✔
293

294
        /* Gets the block device directly backing a file system. If the block device is encrypted, returns
295
         * the device mapper block device. */
296

297
        if (fstat(fd, &st))
3,938✔
298
                return -errno;
×
299

300
        if (major(st.st_dev) != 0) {
3,938✔
301
                *ret = st.st_dev;
3,826✔
302
                return 1;
3,826✔
303
        }
304

305
        r = btrfs_get_block_device_fd(fd, ret);
112✔
306
        if (r != -ENOTTY) /* ENOTTY: not btrfs */
112✔
307
                return r;
308

309
        *ret = 0;
112✔
310
        return 0;
112✔
311
}
312

313
int get_block_device(const char *path, dev_t *ret) {
234✔
314
        _cleanup_close_ int fd = -EBADF;
234✔
315

316
        assert(path);
234✔
317
        assert(ret);
234✔
318

319
        fd = open(path, O_RDONLY|O_NOFOLLOW|O_CLOEXEC);
234✔
320
        if (fd < 0)
234✔
321
                return -errno;
×
322

323
        return get_block_device_fd(fd, ret);
234✔
324
}
325

326
int block_get_originating(dev_t dt, dev_t *ret, bool recursive) {
3,641✔
327
        _cleanup_(sd_device_unrefp) sd_device *dev = NULL, *origin = NULL;
7,282✔
328
        int r;
3,641✔
329

330
        assert(ret);
3,641✔
331

332
        r = sd_device_new_from_devnum(&dev, 'b', dt);
3,641✔
333
        if (r < 0)
3,641✔
334
                return r;
335

336
        r = block_device_get_originating(dev, &origin, recursive);
3,641✔
337
        if (r < 0)
3,641✔
338
                return r;
339

340
        return sd_device_get_devnum(origin, ret);
×
341
}
342

343
int get_block_device_harder_fd(int fd, dev_t *ret) {
3,633✔
344
        int r;
3,633✔
345

346
        assert(fd >= 0);
3,633✔
347
        assert(ret);
3,633✔
348

349
        /* Gets the backing block device for a file system, and handles LUKS encrypted file systems, looking
350
         * for its underlying physical device, if there is one. */
351

352
        r = get_block_device_fd(fd, ret);
3,633✔
353
        if (r <= 0)
3,633✔
354
                return r;
355

356
        r = block_get_originating(*ret, ret, /* recursive= */ true);
3,633✔
357
        if (r < 0)
3,633✔
358
                log_debug_errno(r, "Failed to chase block device, ignoring: %m");
3,633✔
359

360
        return 1;
361
}
362

363
int get_block_device_harder(const char *path, dev_t *ret) {
3,581✔
364
        _cleanup_close_ int fd = -EBADF;
3,581✔
365

366
        assert(path);
3,581✔
367
        assert(ret);
3,581✔
368

369
        fd = open(path, O_RDONLY|O_NOFOLLOW|O_CLOEXEC);
3,581✔
370
        if (fd < 0)
3,581✔
371
                return -errno;
×
372

373
        return get_block_device_harder_fd(fd, ret);
3,581✔
374
}
375

376
int lock_whole_block_device(dev_t devt, int open_flags, int operation) {
×
377
        _cleanup_close_ int lock_fd = -EBADF;
×
378
        dev_t whole_devt;
×
379
        int r;
×
380

381
        /* Let's get a BSD file lock on the whole block device, as per: https://systemd.io/BLOCK_DEVICE_LOCKING
382
         *
383
         * NB: it matters whether open_flags indicates open for write: only then will the eventual closing of
384
         * the fd trigger udev's partitioning rescanning of the device (as it watches for IN_CLOSE_WRITE),
385
         * hence make sure to pass the right value there. */
386

387
        r = block_get_whole_disk(devt, &whole_devt);
×
388
        if (r < 0)
×
389
                return r;
390

391
        lock_fd = device_open_from_devnum(S_IFBLK, whole_devt, open_flags|O_CLOEXEC|O_NONBLOCK|O_NOCTTY, NULL);
×
392
        if (lock_fd < 0)
×
393
                return lock_fd;
394

395
        if (flock(lock_fd, operation) < 0)
×
396
                return -errno;
×
397

398
        return TAKE_FD(lock_fd);
399
}
400

401
int blockdev_partscan_enabled(sd_device *dev) {
8,251✔
402
        unsigned capability;
8,251✔
403
        int r, ext_range;
8,251✔
404

405
        /* Checks if partition scanning is correctly enabled on the block device.
406
         *
407
         * The 'GENHD_FL_NO_PART_SCAN' flag was introduced by
408
         * https://github.com/torvalds/linux/commit/d27769ec3df1a8de9ca450d2dcd72d1ab259ba32 (v3.2).
409
         * But at that time, the flag is also effectively implied when 'minors' element of 'struct gendisk'
410
         * is 1, which can be check with 'ext_range' sysfs attribute. Explicit flag ('GENHD_FL_NO_PART_SCAN')
411
         * can be obtained from 'capability' sysattr.
412
         *
413
         * With https://github.com/torvalds/linux/commit/46e7eac647b34ed4106a8262f8bedbb90801fadd (v5.17),
414
         * the flag is renamed to GENHD_FL_NO_PART.
415
         *
416
         * With https://github.com/torvalds/linux/commit/1ebe2e5f9d68e94c524aba876f27b945669a7879 (v5.17),
417
         * we can check the flag from 'ext_range' sysfs attribute directly.
418
         *
419
         * With https://github.com/torvalds/linux/commit/430cc5d3ab4d0ba0bd011cfbb0035e46ba92920c (v5.17),
420
         * the value of GENHD_FL_NO_PART is changed from 0x0200 to 0x0004. 💣💣💣
421
         * Note, the new value was used by the GENHD_FL_MEDIA_CHANGE_NOTIFY flag, which was introduced by
422
         * 86ce18d7b7925bfd6b64c061828ca2a857ee83b8 (v2.6.22), and removed by
423
         * 9243c6f3e012a92dd900d97ef45efaf8a8edc448 (v5.7). If we believe the commit message of
424
         * e81cd5a983bb35dabd38ee472cf3fea1c63e0f23, the flag was never used. So, fortunately, we can use
425
         * both the new and old values safely.
426
         *
427
         * With https://github.com/torvalds/linux/commit/b9684a71fca793213378dd410cd11675d973eaa1 (v5.19),
428
         * another flag GD_SUPPRESS_PART_SCAN is introduced for loopback block device, and partition scanning
429
         * is done only when both GENHD_FL_NO_PART and GD_SUPPRESS_PART_SCAN are not set. Before the commit,
430
         * LO_FLAGS_PARTSCAN flag was directly tied with GENHD_FL_NO_PART. But with this change now it is
431
         * tied with GD_SUPPRESS_PART_SCAN. So, LO_FLAGS_PARTSCAN cannot be obtained from 'ext_range'
432
         * sysattr, which corresponds to GENHD_FL_NO_PART, and we need to read 'loop/partscan'. 💣💣💣
433
         *
434
         * With https://github.com/torvalds/linux/commit/73a166d9749230d598320fdae3b687cdc0e2e205 (v6.3),
435
         * the GD_SUPPRESS_PART_SCAN flag is also introduced for userspace block device (ublk). Though, not
436
         * sure if we should support the device...
437
         *
438
         * With https://github.com/torvalds/linux/commit/e81cd5a983bb35dabd38ee472cf3fea1c63e0f23 (v6.3),
439
         * the 'capability' sysfs attribute is deprecated, hence we cannot check flags from it. 💣💣💣
440
         *
441
         * With https://github.com/torvalds/linux/commit/a4217c6740dc64a3eb6815868a9260825e8c68c6 (v6.10,
442
         * backported to v6.6+), the partscan status is directly exposed as 'partscan' sysattr.
443
         *
444
         * To support both old and new kernels, we need to do the following:
445
         * 1) check 'partscan' sysfs attribute where the information is made directly available,
446
         * 2) check if the blockdev refers to a partition, where partscan is not supported,
447
         * 3) check 'loop/partscan' sysfs attribute for loopback block devices, and if '0' we can conclude
448
         *    partition scanning is disabled,
449
         * 4) check 'ext_range' sysfs attribute, and if '1' we can conclude partition scanning is disabled,
450
         * 5) otherwise check 'capability' sysfs attribute for ancient version. */
451

452
        assert(dev);
8,251✔
453

454
        r = device_in_subsystem(dev, "block");
8,251✔
455
        if (r < 0)
8,251✔
456
                return r;
8,251✔
457
        if (r == 0)
8,251✔
458
                return -ENOTBLK;
459

460
        /* For v6.10 or newer. */
461
        r = device_get_sysattr_bool(dev, "partscan");
8,251✔
462
        if (r != -ENOENT)
8,251✔
463
                return r;
464

465
        /* Partition block devices never have partition scanning on, there's no concept of sub-partitions for
466
         * partitions. */
467
        r = device_is_devtype(dev, "partition");
12✔
468
        if (r < 0)
12✔
469
                return r;
470
        if (r > 0)
12✔
471
                return false;
472

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

478
        r = device_get_sysattr_int(dev, "ext_range", &ext_range);
×
479
        if (r == -ENOENT) /* If the ext_range file doesn't exist then we are most likely looking at a
×
480
                           * partition block device, not the whole block device. And that means we have no
481
                           * partition scanning on for it (we do for its parent, but not for the partition
482
                           * itself). */
483
                return false;
484
        if (r < 0)
×
485
                return r;
486

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

491
        r = device_get_sysattr_unsigned_full(dev, "capability", 16, &capability);
×
492
        if (r == -ENOENT)
×
493
                return false;
494
        if (r < 0)
×
495
                return r;
496

497
#define GENHD_FL_NO_PART_OLD 0x0200
498
#define GENHD_FL_NO_PART_NEW 0x0004
499
        /* If one of the NO_PART flags is set, part scanning is definitely off. */
500
        if ((capability & (GENHD_FL_NO_PART_OLD | GENHD_FL_NO_PART_NEW)) != 0)
×
501
                return false;
×
502

503
        /* Otherwise, assume part scanning is on, we have no further checks available. Assume the best. */
504
        return true;
505
}
506

507
int blockdev_partscan_enabled_fd(int fd) {
2,074✔
508
        _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
2,074✔
509
        int r;
2,074✔
510

511
        assert(fd >= 0);
2,074✔
512

513
        r = block_device_new_from_fd(fd, 0, &dev);
2,074✔
514
        if (r < 0)
2,074✔
515
                return r;
516

517
        return blockdev_partscan_enabled(dev);
1,970✔
518
}
519

520
static int blockdev_is_encrypted(const char *sysfs_path, unsigned depth_left) {
20✔
521
        _cleanup_free_ char *p = NULL, *uuids = NULL;
20✔
522
        _cleanup_closedir_ DIR *d = NULL;
20✔
523
        int r, found_encrypted = false;
20✔
524

525
        assert(sysfs_path);
20✔
526

527
        if (depth_left == 0)
20✔
528
                return -EINVAL;
529

530
        p = path_join(sysfs_path, "dm/uuid");
20✔
531
        if (!p)
20✔
532
                return -ENOMEM;
533

534
        r = read_one_line_file(p, &uuids);
20✔
535
        if (r != -ENOENT) {
20✔
536
                if (r < 0)
×
537
                        return r;
538

539
                /* The DM device's uuid attribute is prefixed with "CRYPT-" if this is a dm-crypt device. */
540
                if (startswith(uuids, "CRYPT-"))
×
541
                        return true;
542
        }
543

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

547
        p = mfree(p);
20✔
548
        p = path_join(sysfs_path, "slaves");
20✔
549
        if (!p)
20✔
550
                return -ENOMEM;
551

552
        d = opendir(p);
20✔
553
        if (!d) {
20✔
554
                if (errno == ENOENT) /* Doesn't have underlying devices */
20✔
555
                        return false;
556

557
                return -errno;
×
558
        }
559

560
        for (;;) {
×
561
                _cleanup_free_ char *q = NULL;
×
562
                struct dirent *de;
×
563

564
                errno = 0;
×
565
                de = readdir_no_dot(d);
×
566
                if (!de) {
×
567
                        if (errno != 0)
×
568
                                return -errno;
×
569

570
                        break; /* No more underlying devices */
571
                }
572

573
                q = path_join(p, de->d_name);
×
574
                if (!q)
×
575
                        return -ENOMEM;
576

577
                r = blockdev_is_encrypted(q, depth_left - 1);
×
578
                if (r < 0)
×
579
                        return r;
580
                if (r == 0) /* we found one that is not encrypted? then propagate that immediately */
×
581
                        return false;
582

583
                found_encrypted = true;
×
584
        }
585

586
        return found_encrypted;
587
}
588

589
int fd_is_encrypted(int fd) {
19✔
590
        char p[SYS_BLOCK_PATH_MAX("")];
19✔
591
        dev_t devt;
19✔
592
        int r;
19✔
593

594
        r = get_block_device_fd(fd, &devt);
19✔
595
        if (r < 0)
19✔
596
                return r;
19✔
597
        if (r == 0) /* doesn't have a block device */
19✔
598
                return false;
599

600
        xsprintf_sys_block_path(p, NULL, devt);
17✔
601

602
        return blockdev_is_encrypted(p, 10 /* safety net: maximum recursion depth */);
17✔
603
}
604

605
int path_is_encrypted(const char *path) {
7✔
606
        char p[SYS_BLOCK_PATH_MAX("")];
7✔
607
        dev_t devt;
7✔
608
        int r;
7✔
609

610
        r = get_block_device(path, &devt);
7✔
611
        if (r < 0)
7✔
612
                return r;
7✔
613
        if (r == 0) /* doesn't have a block device */
7✔
614
                return false;
615

616
        xsprintf_sys_block_path(p, NULL, devt);
3✔
617

618
        return blockdev_is_encrypted(p, 10 /* safety net: maximum recursion depth */);
3✔
619
}
620

621
int fd_get_whole_disk(int fd, bool backing, dev_t *ret) {
1,109✔
622
        dev_t devt;
1,109✔
623
        int r;
1,109✔
624

625
        assert(fd >= 0);
1,109✔
626
        assert(ret);
1,109✔
627

628
        r = fd_get_devnum(fd, backing ? BLOCK_DEVICE_LOOKUP_BACKING : 0, &devt);
2,212✔
629
        if (r < 0)
1,109✔
630
                return r;
1,109✔
631

632
        return block_get_whole_disk(devt, ret);
1,109✔
633
}
634

635
int path_get_whole_disk(const char *path, bool backing, dev_t *ret) {
1,109✔
636
        _cleanup_close_ int fd = -EBADF;
1,109✔
637

638
        fd = open(path, O_CLOEXEC|O_PATH);
1,109✔
639
        if (fd < 0)
1,109✔
640
                return -errno;
×
641

642
        return fd_get_whole_disk(fd, backing, ret);
1,109✔
643
}
644

645
int block_device_add_partition(
350✔
646
                int fd,
647
                const char *name,
648
                int nr,
649
                uint64_t start,
650
                uint64_t size) {
651

652
        assert(fd >= 0);
350✔
653
        assert(name);
350✔
654
        assert(nr > 0);
350✔
655

656
        struct blkpg_partition bp = {
350✔
657
                .pno = nr,
658
                .start = start,
659
                .length = size,
660
        };
661

662
        struct blkpg_ioctl_arg ba = {
350✔
663
                .op = BLKPG_ADD_PARTITION,
664
                .data = &bp,
665
                .datalen = sizeof(bp),
666
        };
667

668
        if (strlen(name) >= sizeof(bp.devname))
350✔
669
                return -EINVAL;
350✔
670

671
        strcpy(bp.devname, name);
350✔
672

673
        return RET_NERRNO(ioctl(fd, BLKPG, &ba));
350✔
674
}
675

676
int block_device_remove_partition(
158✔
677
                int fd,
678
                const char *name,
679
                int nr) {
680

681
        assert(fd >= 0);
158✔
682
        assert(name);
158✔
683
        assert(nr > 0);
158✔
684

685
        struct blkpg_partition bp = {
158✔
686
                .pno = nr,
687
        };
688

689
        struct blkpg_ioctl_arg ba = {
158✔
690
                .op = BLKPG_DEL_PARTITION,
691
                .data = &bp,
692
                .datalen = sizeof(bp),
693
        };
694

695
        if (strlen(name) >= sizeof(bp.devname))
158✔
696
                return -EINVAL;
158✔
697

698
        strcpy(bp.devname, name);
158✔
699

700
        return RET_NERRNO(ioctl(fd, BLKPG, &ba));
158✔
701
}
702

703
int block_device_resize_partition(
×
704
                int fd,
705
                int nr,
706
                uint64_t start,
707
                uint64_t size) {
708

709
        assert(fd >= 0);
×
710
        assert(nr > 0);
×
711

712
        struct blkpg_partition bp = {
×
713
                .pno = nr,
714
                .start = start,
715
                .length = size,
716
        };
717

718
        struct blkpg_ioctl_arg ba = {
×
719
                .op = BLKPG_RESIZE_PARTITION,
720
                .data = &bp,
721
                .datalen = sizeof(bp),
722
        };
723

724
        return RET_NERRNO(ioctl(fd, BLKPG, &ba));
×
725
}
726

727
int partition_enumerator_new(sd_device *dev, sd_device_enumerator **ret) {
7,577✔
728
        _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
7,577✔
729
        const char *s;
7,577✔
730
        int r;
7,577✔
731

732
        assert(dev);
7,577✔
733
        assert(ret);
7,577✔
734

735
        /* Refuse invocation on partition block device, insist on "whole" device */
736
        r = block_device_is_whole_disk(dev);
7,577✔
737
        if (r < 0)
7,577✔
738
                return r;
739
        if (r == 0)
7,577✔
740
                return -ENXIO; /* return a recognizable error */
741

742
        r = sd_device_enumerator_new(&e);
7,577✔
743
        if (r < 0)
7,577✔
744
                return r;
745

746
        r = sd_device_enumerator_allow_uninitialized(e);
7,577✔
747
        if (r < 0)
7,577✔
748
                return r;
749

750
        r = sd_device_enumerator_add_match_parent(e, dev);
7,577✔
751
        if (r < 0)
7,577✔
752
                return r;
753

754
        r = sd_device_get_sysname(dev, &s);
7,577✔
755
        if (r < 0)
7,577✔
756
                return r;
757

758
        /* Also add sysname check for safety. Hopefully, this also improves performance. */
759
        s = strjoina(s, "*");
37,885✔
760
        r = sd_device_enumerator_add_match_sysname(e, s);
7,577✔
761
        if (r < 0)
7,577✔
762
                return r;
763

764
        r = sd_device_enumerator_add_match_subsystem(e, "block", /* match= */ true);
7,577✔
765
        if (r < 0)
7,577✔
766
                return r;
767

768
        r = sd_device_enumerator_add_match_property(e, "DEVTYPE", "partition");
7,577✔
769
        if (r < 0)
7,577✔
770
                return r;
771

772
        *ret = TAKE_PTR(e);
7,577✔
773
        return 0;
7,577✔
774
}
775

776
int block_device_remove_all_partitions(sd_device *dev, int fd) {
3,810✔
777
        _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
3,810✔
778
        _cleanup_(sd_device_unrefp) sd_device *dev_unref = NULL;
3,810✔
779
        _cleanup_close_ int fd_close = -EBADF;
3,810✔
780
        bool has_partitions = false;
3,810✔
781
        int r, k = 0;
3,810✔
782

783
        assert(dev || fd >= 0);
3,810✔
784

785
        if (!dev) {
3,810✔
786
                r = block_device_new_from_fd(fd, 0, &dev_unref);
×
787
                if (r < 0)
×
788
                        return r;
789

790
                dev = dev_unref;
×
791
        }
792

793
        r = partition_enumerator_new(dev, &e);
3,810✔
794
        if (r < 0)
3,810✔
795
                return r;
796

797
        if (fd < 0) {
3,810✔
798
                fd_close = sd_device_open(dev, O_CLOEXEC|O_NONBLOCK|O_NOCTTY|O_RDONLY);
×
799
                if (fd_close < 0)
×
800
                        return fd_close;
801

802
                fd = fd_close;
803
        }
804

805
        FOREACH_DEVICE(e, part) {
3,968✔
806
                const char *v, *devname;
158✔
807
                int nr;
158✔
808

809
                has_partitions = true;
158✔
810

811
                r = sd_device_get_devname(part, &devname);
158✔
812
                if (r < 0)
158✔
813
                        return r;
×
814

815
                r = sd_device_get_property_value(part, "PARTN", &v);
158✔
816
                if (r < 0)
158✔
817
                        return r;
818

819
                r = safe_atoi(v, &nr);
158✔
820
                if (r < 0)
158✔
821
                        return r;
822

823
                r = btrfs_forget_device(devname);
158✔
824
                if (r < 0 && r != -ENOENT)
158✔
825
                        log_debug_errno(r, "Failed to forget btrfs device %s, ignoring: %m", devname);
×
826

827
                r = block_device_remove_partition(fd, devname, nr);
158✔
828
                if (r == -ENODEV) {
158✔
829
                        log_debug("Kernel removed partition %s before us, ignoring", devname);
×
830
                        continue;
×
831
                }
832
                if (r < 0) {
158✔
833
                        log_debug_errno(r, "Failed to remove partition %s: %m", devname);
×
834
                        k = k < 0 ? k : r;
×
835
                        continue;
×
836
                }
837

838
                log_debug("Removed partition %s", devname);
158✔
839
        }
840

841
        return k < 0 ? k : has_partitions;
3,810✔
842
}
843

844
int blockdev_get_sector_size(int fd, uint32_t *ret) {
2,452✔
845
        int ssz = 0;
2,452✔
846

847
        assert(fd >= 0);
2,452✔
848
        assert(ret);
2,452✔
849

850
        if (ioctl(fd, BLKSSZGET, &ssz) < 0)
2,452✔
851
                return -errno;
×
852
        if (ssz <= 0) /* make sure the field is initialized */
2,452✔
853
                return log_debug_errno(SYNTHETIC_ERRNO(EIO), "Block device reported invalid sector size %i.", ssz);
×
854

855
        *ret = ssz;
2,452✔
856
        return 0;
2,452✔
857
}
858

859
int blockdev_get_device_size(int fd, uint64_t *ret) {
2,330✔
860
        uint64_t sz = 0;
2,330✔
861

862
        assert(fd >= 0);
2,330✔
863
        assert(ret);
2,330✔
864

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

867
        if (ioctl(fd, BLKGETSIZE64, &sz) < 0)
2,330✔
868
                return -errno;
×
869

870
        *ret = sz;
2,330✔
871
        return 0;
2,330✔
872
}
873

874
int blockdev_get_root(int level, dev_t *ret) {
3,581✔
875
        _cleanup_free_ char *p = NULL;
3,581✔
876
        dev_t devno;
3,581✔
877
        int r;
3,581✔
878

879
        /* Returns the device node backing the root file system. Traces through
880
         * dm-crypt/dm-verity/... Returns > 0 and the devno of the device on success. If there's no block
881
         * device (or multiple) returns 0 and a devno of 0. Failure otherwise.
882
         *
883
         * If the root mount has been replaced by some form of volatile file system (overlayfs), the original
884
         * root block device node is symlinked in /run/systemd/volatile-root. Let's read that here. */
885
        r = readlink_malloc("/run/systemd/volatile-root", &p);
3,581✔
886
        if (r == -ENOENT) { /* volatile-root not found */
3,581✔
887
                r = get_block_device_harder("/", &devno);
3,581✔
888
                if (r == -EUCLEAN)
3,581✔
889
                        return btrfs_log_dev_root(level, r, "root file system");
×
890
                if (r < 0)
3,581✔
891
                        return log_full_errno(level, r, "Failed to determine block device of root file system: %m");
×
892
                if (r == 0) { /* Not backed by a single block device. (Could be NFS or so, or could be multi-device RAID or so) */
3,581✔
893
                        r = get_block_device_harder("/usr", &devno);
×
894
                        if (r == -EUCLEAN)
×
895
                                return btrfs_log_dev_root(level, r, "/usr");
×
896
                        if (r < 0)
×
897
                                return log_full_errno(level, r, "Failed to determine block device of /usr/ file system: %m");
×
898
                        if (r == 0) { /* /usr/ not backed by single block device, either. */
×
899
                                log_debug("Neither root nor /usr/ file system are on a (single) block device.");
×
900

901
                                if (ret)
×
902
                                        *ret = 0;
×
903

904
                                return 0;
×
905
                        }
906
                }
907
        } else if (r < 0)
×
908
                return log_full_errno(level, r, "Failed to read symlink /run/systemd/volatile-root: %m");
×
909
        else {
910
                mode_t m;
×
911
                r = device_path_parse_major_minor(p, &m, &devno);
×
912
                if (r < 0)
×
913
                        return log_full_errno(level, r, "Failed to parse major/minor device node: %m");
×
914
                if (!S_ISBLK(m))
×
915
                        return log_full_errno(level, SYNTHETIC_ERRNO(ENOTBLK), "Volatile root device is of wrong type.");
×
916
        }
917

918
        if (ret)
3,581✔
919
                *ret = devno;
3,581✔
920

921
        return 1;
922
}
923

924
int partition_node_of(const char *node, unsigned nr, char **ret) {
5,701✔
925
        _cleanup_free_ char *fn = NULL, *dn = NULL;
5,701✔
926
        int r;
5,701✔
927

928
        assert(node);
5,701✔
929
        assert(nr > 0);
5,701✔
930
        assert(ret);
5,701✔
931

932
        /* Given a device node path to a block device returns the device node path to the partition block
933
         * device of the specified partition */
934

935
        r = path_split_prefix_filename(node, &dn, &fn);
5,701✔
936
        if (r < 0)
5,701✔
937
                return r;
938
        if (r == O_DIRECTORY)
5,698✔
939
                return -EISDIR;
940

941
        size_t l = strlen(fn);
5,697✔
942
        assert(l > 0); /* underflow check for the subtraction below */
5,697✔
943

944
        bool need_p = ascii_isdigit(fn[l-1]); /* Last char a digit? */
5,697✔
945

946
        _cleanup_free_ char *subnode = NULL;
5,697✔
947
        if (asprintf(&subnode, "%s%s%u", fn, need_p ? "p" : "", nr) < 0)
10,656✔
948
                return -ENOMEM;
949

950
        if (dn) {
5,697✔
951
                _cleanup_free_ char *j = path_join(dn, subnode);
5,695✔
952
                if (!j)
5,695✔
953
                        return -ENOMEM;
×
954

955
                *ret = TAKE_PTR(j);
5,695✔
956
        } else
957
                *ret = TAKE_PTR(subnode);
2✔
958

959
        return 0;
960
}
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