1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include <linux/blkpg.h>
11 #include "alloc-util.h"
12 #include "blockdev-util.h"
13 #include "btrfs-util.h"
14 #include "device-util.h"
15 #include "devnum-util.h"
16 #include "dirent-util.h"
17 #include "errno-util.h"
21 #include "missing_magic.h"
22 #include "parse-util.h"
24 static int fd_get_devnum(int fd
, BlockDeviceLookupFlag flags
, dev_t
*ret
) {
32 if (fstat(fd
, &st
) < 0)
35 if (S_ISBLK(st
.st_mode
))
37 else if (!FLAGS_SET(flags
, BLOCK_DEVICE_LOOKUP_BACKING
))
39 else if (!S_ISREG(st
.st_mode
) && !S_ISDIR(st
.st_mode
))
41 else if (major(st
.st_dev
) != 0)
44 /* If major(st.st_dev) is zero, this might mean we are backed by btrfs, which needs special
45 * handing, to get the backing device node. */
47 r
= btrfs_get_block_device_fd(fd
, &devnum
);
48 if (r
== -ENOTTY
) /* not btrfs */
58 int block_device_is_whole_disk(sd_device
*dev
) {
64 r
= sd_device_get_subsystem(dev
, &s
);
68 if (!streq(s
, "block"))
71 r
= sd_device_get_devtype(dev
, &s
);
75 return streq(s
, "disk");
78 int block_device_get_whole_disk(sd_device
*dev
, sd_device
**ret
) {
84 /* Do not unref returned sd_device object. */
86 r
= block_device_is_whole_disk(dev
);
90 r
= sd_device_get_parent(dev
, &dev
);
91 if (r
== -ENOENT
) /* Already removed? Let's return a recognizable error. */
96 r
= block_device_is_whole_disk(dev
);
107 static int block_device_get_originating(sd_device
*dev
, sd_device
**ret
) {
108 _cleanup_(sd_device_unrefp
) sd_device
*first_found
= NULL
;
111 dev_t devnum
= 0; /* avoid false maybe-uninitialized warning */
113 /* For the specified block device tries to chase it through the layers, in case LUKS-style DM
114 * stacking is used, trying to find the next underlying layer. */
119 FOREACH_DEVICE_CHILD_WITH_SUFFIX(dev
, child
, suffix
) {
120 sd_device
*child_whole_disk
;
123 if (!path_startswith(suffix
, "slaves"))
126 if (block_device_get_whole_disk(child
, &child_whole_disk
) < 0)
129 if (sd_device_get_devnum(child_whole_disk
, &n
) < 0)
133 first_found
= sd_device_ref(child
);
138 /* We found a device backed by multiple other devices. We don't really support automatic
139 * discovery on such setups, with the exception of dm-verity partitions. In this case there
140 * are two backing devices: the data partition and the hash partition. We are fine with such
141 * setups, however, only if both partitions are on the same physical device. Hence, let's
142 * verify this by iterating over every node in the 'slaves/' directory and comparing them with
143 * the first that gets returned by readdir(), to ensure they all point to the same device. */
151 *ret
= TAKE_PTR(first_found
);
152 return 1; /* found */
155 int block_device_new_from_fd(int fd
, BlockDeviceLookupFlag flags
, sd_device
**ret
) {
156 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
163 r
= fd_get_devnum(fd
, flags
, &devnum
);
167 r
= sd_device_new_from_devnum(&dev
, 'b', devnum
);
171 if (FLAGS_SET(flags
, BLOCK_DEVICE_LOOKUP_ORIGINATING
)) {
172 _cleanup_(sd_device_unrefp
) sd_device
*dev_origin
= NULL
;
173 sd_device
*dev_whole_disk
;
175 r
= block_device_get_whole_disk(dev
, &dev_whole_disk
);
179 r
= block_device_get_originating(dev_whole_disk
, &dev_origin
);
180 if (r
< 0 && r
!= -ENOENT
)
183 device_unref_and_replace(dev
, dev_origin
);
186 if (FLAGS_SET(flags
, BLOCK_DEVICE_LOOKUP_WHOLE_DISK
)) {
187 sd_device
*dev_whole_disk
;
189 r
= block_device_get_whole_disk(dev
, &dev_whole_disk
);
193 *ret
= sd_device_ref(dev_whole_disk
);
197 *ret
= sd_device_ref(dev
);
201 int block_device_new_from_path(const char *path
, BlockDeviceLookupFlag flags
, sd_device
**ret
) {
202 _cleanup_close_
int fd
= -EBADF
;
207 fd
= open(path
, O_CLOEXEC
|O_PATH
);
211 return block_device_new_from_fd(fd
, flags
, ret
);
214 int block_get_whole_disk(dev_t d
, dev_t
*ret
) {
215 char p
[SYS_BLOCK_PATH_MAX("/partition")];
216 _cleanup_free_
char *s
= NULL
;
225 /* If it has a queue this is good enough for us */
226 xsprintf_sys_block_path(p
, "/queue", d
);
227 if (access(p
, F_OK
) >= 0) {
234 /* If it is a partition find the originating device */
235 xsprintf_sys_block_path(p
, "/partition", d
);
236 if (access(p
, F_OK
) < 0)
239 /* Get parent dev_t */
240 xsprintf_sys_block_path(p
, "/../dev", d
);
241 r
= read_one_line_file(p
, &s
);
245 r
= parse_devnum(s
, &devt
);
249 /* Only return this if it is really good enough for us. */
250 xsprintf_sys_block_path(p
, "/queue", devt
);
251 if (access(p
, F_OK
) < 0)
258 int get_block_device_fd(int fd
, dev_t
*ret
) {
265 /* Gets the block device directly backing a file system. If the block device is encrypted, returns
266 * the device mapper block device. */
271 if (major(st
.st_dev
) != 0) {
276 r
= btrfs_get_block_device_fd(fd
, ret
);
279 if (r
!= -ENOTTY
) /* not btrfs */
286 int get_block_device(const char *path
, dev_t
*ret
) {
287 _cleanup_close_
int fd
= -EBADF
;
292 fd
= open(path
, O_RDONLY
|O_NOFOLLOW
|O_CLOEXEC
);
296 return get_block_device_fd(fd
, ret
);
299 int block_get_originating(dev_t dt
, dev_t
*ret
) {
300 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
, *origin
= NULL
;
305 r
= sd_device_new_from_devnum(&dev
, 'b', dt
);
309 r
= block_device_get_originating(dev
, &origin
);
313 return sd_device_get_devnum(origin
, ret
);
316 int get_block_device_harder_fd(int fd
, dev_t
*ret
) {
322 /* Gets the backing block device for a file system, and handles LUKS encrypted file systems, looking for its
323 * immediate parent, if there is one. */
325 r
= get_block_device_fd(fd
, ret
);
329 r
= block_get_originating(*ret
, ret
);
331 log_debug_errno(r
, "Failed to chase block device, ignoring: %m");
336 int get_block_device_harder(const char *path
, dev_t
*ret
) {
337 _cleanup_close_
int fd
= -EBADF
;
342 fd
= open(path
, O_RDONLY
|O_NOFOLLOW
|O_CLOEXEC
);
346 return get_block_device_harder_fd(fd
, ret
);
349 int lock_whole_block_device(dev_t devt
, int operation
) {
350 _cleanup_close_
int lock_fd
= -EBADF
;
354 /* Let's get a BSD file lock on the whole block device, as per: https://systemd.io/BLOCK_DEVICE_LOCKING */
356 r
= block_get_whole_disk(devt
, &whole_devt
);
360 lock_fd
= r
= device_open_from_devnum(S_IFBLK
, whole_devt
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
, NULL
);
364 if (flock(lock_fd
, operation
) < 0)
367 return TAKE_FD(lock_fd
);
370 int blockdev_partscan_enabled(int fd
) {
371 _cleanup_free_
char *p
= NULL
, *buf
= NULL
;
372 unsigned long long ull
;
376 /* Checks if partition scanning is correctly enabled on the block device */
378 if (fstat(fd
, &st
) < 0)
381 if (!S_ISBLK(st
.st_mode
))
384 if (asprintf(&p
, "/sys/dev/block/%u:%u/capability", major(st
.st_rdev
), minor(st
.st_rdev
)) < 0)
387 r
= read_one_line_file(p
, &buf
);
388 if (r
== -ENOENT
) /* If the capability file doesn't exist then we are most likely looking at a
389 * partition block device, not the whole block device. And that means we have no
390 * partition scanning on for it (we do for its parent, but not for the partition
396 r
= safe_atollu_full(buf
, 16, &ull
);
400 #ifndef GENHD_FL_NO_PART_SCAN
401 #define GENHD_FL_NO_PART_SCAN (0x0200)
404 return !FLAGS_SET(ull
, GENHD_FL_NO_PART_SCAN
);
407 static int blockdev_is_encrypted(const char *sysfs_path
, unsigned depth_left
) {
408 _cleanup_free_
char *p
= NULL
, *uuids
= NULL
;
409 _cleanup_closedir_
DIR *d
= NULL
;
410 int r
, found_encrypted
= false;
417 p
= path_join(sysfs_path
, "dm/uuid");
421 r
= read_one_line_file(p
, &uuids
);
426 /* The DM device's uuid attribute is prefixed with "CRYPT-" if this is a dm-crypt device. */
427 if (startswith(uuids
, "CRYPT-"))
431 /* Not a dm-crypt device itself. But maybe it is on top of one? Follow the links in the "slaves/"
435 p
= path_join(sysfs_path
, "slaves");
441 if (errno
== ENOENT
) /* Doesn't have underlying devices */
448 _cleanup_free_
char *q
= NULL
;
452 de
= readdir_no_dot(d
);
457 break; /* No more underlying devices */
460 q
= path_join(p
, de
->d_name
);
464 r
= blockdev_is_encrypted(q
, depth_left
- 1);
467 if (r
== 0) /* we found one that is not encrypted? then propagate that immediately */
470 found_encrypted
= true;
473 return found_encrypted
;
476 int fd_is_encrypted(int fd
) {
477 char p
[SYS_BLOCK_PATH_MAX(NULL
)];
481 r
= get_block_device_fd(fd
, &devt
);
484 if (r
== 0) /* doesn't have a block device */
487 xsprintf_sys_block_path(p
, NULL
, devt
);
489 return blockdev_is_encrypted(p
, 10 /* safety net: maximum recursion depth */);
492 int path_is_encrypted(const char *path
) {
493 char p
[SYS_BLOCK_PATH_MAX(NULL
)];
497 r
= get_block_device(path
, &devt
);
500 if (r
== 0) /* doesn't have a block device */
503 xsprintf_sys_block_path(p
, NULL
, devt
);
505 return blockdev_is_encrypted(p
, 10 /* safety net: maximum recursion depth */);
508 int fd_get_whole_disk(int fd
, bool backing
, dev_t
*ret
) {
515 r
= fd_get_devnum(fd
, backing
? BLOCK_DEVICE_LOOKUP_BACKING
: 0, &devt
);
519 return block_get_whole_disk(devt
, ret
);
522 int path_get_whole_disk(const char *path
, bool backing
, dev_t
*ret
) {
523 _cleanup_close_
int fd
= -EBADF
;
525 fd
= open(path
, O_CLOEXEC
|O_PATH
);
529 return fd_get_whole_disk(fd
, backing
, ret
);
532 int block_device_add_partition(
543 struct blkpg_partition bp
= {
549 struct blkpg_ioctl_arg ba
= {
550 .op
= BLKPG_ADD_PARTITION
,
552 .datalen
= sizeof(bp
),
555 if (strlen(name
) >= sizeof(bp
.devname
))
558 strcpy(bp
.devname
, name
);
560 return RET_NERRNO(ioctl(fd
, BLKPG
, &ba
));
563 int block_device_remove_partition(
572 struct blkpg_partition bp
= {
576 struct blkpg_ioctl_arg ba
= {
577 .op
= BLKPG_DEL_PARTITION
,
579 .datalen
= sizeof(bp
),
582 if (strlen(name
) >= sizeof(bp
.devname
))
585 strcpy(bp
.devname
, name
);
587 return RET_NERRNO(ioctl(fd
, BLKPG
, &ba
));
590 int block_device_resize_partition(
599 struct blkpg_partition bp
= {
605 struct blkpg_ioctl_arg ba
= {
606 .op
= BLKPG_RESIZE_PARTITION
,
608 .datalen
= sizeof(bp
),
611 return RET_NERRNO(ioctl(fd
, BLKPG
, &ba
));
614 int partition_enumerator_new(sd_device
*dev
, sd_device_enumerator
**ret
) {
615 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
622 /* Refuse invocation on partition block device, insist on "whole" device */
623 r
= block_device_is_whole_disk(dev
);
627 return -ENXIO
; /* return a recognizable error */
629 r
= sd_device_enumerator_new(&e
);
633 r
= sd_device_enumerator_allow_uninitialized(e
);
637 r
= sd_device_enumerator_add_match_parent(e
, dev
);
641 r
= sd_device_get_sysname(dev
, &s
);
645 /* Also add sysname check for safety. Hopefully, this also improves performance. */
646 s
= strjoina(s
, "*");
647 r
= sd_device_enumerator_add_match_sysname(e
, s
);
651 r
= sd_device_enumerator_add_match_subsystem(e
, "block", /* match = */ true);
655 r
= sd_device_enumerator_add_match_property(e
, "DEVTYPE", "partition");
663 int block_device_remove_all_partitions(sd_device
*dev
, int fd
) {
664 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
665 _cleanup_(sd_device_unrefp
) sd_device
*dev_unref
= NULL
;
666 _cleanup_close_
int fd_close
= -EBADF
;
667 bool has_partitions
= false;
671 assert(dev
|| fd
>= 0);
674 r
= block_device_new_from_fd(fd
, 0, &dev_unref
);
681 r
= partition_enumerator_new(dev
, &e
);
686 fd_close
= sd_device_open(dev
, O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
|O_RDONLY
);
693 FOREACH_DEVICE(e
, part
) {
694 const char *v
, *devname
;
697 has_partitions
= true;
699 r
= sd_device_get_devname(part
, &devname
);
703 r
= sd_device_get_property_value(part
, "PARTN", &v
);
707 r
= safe_atoi(v
, &nr
);
711 r
= btrfs_forget_device(devname
);
712 if (r
< 0 && r
!= -ENOENT
)
713 log_debug_errno(r
, "Failed to forget btrfs device %s, ignoring: %m", devname
);
715 r
= block_device_remove_partition(fd
, devname
, nr
);
717 log_debug("Kernel removed partition %s before us, ignoring", devname
);
721 log_debug_errno(r
, "Failed to remove partition %s: %m", devname
);
726 log_debug("Removed partition %s", devname
);
729 return k
< 0 ? k
: has_partitions
;
732 int block_device_has_partitions(sd_device
*dev
) {
733 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
738 /* Checks if the specified device currently has partitions. */
740 r
= partition_enumerator_new(dev
, &e
);
744 return !!sd_device_enumerator_get_device_first(e
);
747 int blockdev_reread_partition_table(sd_device
*dev
) {
748 _cleanup_close_
int fd
= -EBADF
;
752 /* Try to re-read the partition table. This only succeeds if none of the devices is busy. */
754 fd
= sd_device_open(dev
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
);
758 if (flock(fd
, LOCK_EX
|LOCK_NB
) < 0)
761 if (ioctl(fd
, BLKRRPART
, 0) < 0)
767 int blockdev_get_sector_size(int fd
, uint32_t *ret
) {
773 if (ioctl(fd
, BLKSSZGET
, &ssz
) < 0)
775 if (ssz
<= 0) /* make sure the field is initialized */
776 return log_debug_errno(SYNTHETIC_ERRNO(EIO
), "Block device reported invalid sector size %i.", ssz
);
782 int blockdev_get_root(int level
, dev_t
*ret
) {
783 _cleanup_free_
char *p
= NULL
;
787 /* Returns the device node backing the root file system. Traces through
788 * dm-crypt/dm-verity/... Returns > 0 and the devno of the device on success. If there's no block
789 * device (or multiple) returns 0 and a devno of 0. Failure otherwise.
791 * If the root mount has been replaced by some form of volatile file system (overlayfs), the original
792 * root block device node is symlinked in /run/systemd/volatile-root. Let's read that here. */
793 r
= readlink_malloc("/run/systemd/volatile-root", &p
);
794 if (r
== -ENOENT
) { /* volatile-root not found */
795 r
= get_block_device_harder("/", &devno
);
797 return btrfs_log_dev_root(level
, r
, "root file system");
799 return log_full_errno(level
, r
, "Failed to determine block device of root file system: %m");
800 if (r
== 0) { /* Not backed by a single block device. (Could be NFS or so, or could be multi-device RAID or so) */
801 r
= get_block_device_harder("/usr", &devno
);
803 return btrfs_log_dev_root(level
, r
, "/usr");
805 return log_full_errno(level
, r
, "Failed to determine block device of /usr/ file system: %m");
806 if (r
== 0) { /* /usr/ not backed by single block device, either. */
807 log_debug("Neither root nor /usr/ file system are on a (single) block device.");
816 return log_full_errno(level
, r
, "Failed to read symlink /run/systemd/volatile-root: %m");
819 r
= device_path_parse_major_minor(p
, &m
, &devno
);
821 return log_full_errno(level
, r
, "Failed to parse major/minor device node: %m");
823 return log_full_errno(level
, SYNTHETIC_ERRNO(ENOTBLK
), "Volatile root device is of wrong type.");