]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/blockdev-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include "alloc-util.h"
7 #include "blockdev-util.h"
8 #include "btrfs-util.h"
9 #include "devnum-util.h"
10 #include "dirent-util.h"
13 #include "missing_magic.h"
14 #include "parse-util.h"
16 int block_get_whole_disk(dev_t d
, dev_t
*ret
) {
17 char p
[SYS_BLOCK_PATH_MAX("/partition")];
18 _cleanup_free_
char *s
= NULL
;
27 /* If it has a queue this is good enough for us */
28 xsprintf_sys_block_path(p
, "/queue", d
);
29 if (access(p
, F_OK
) >= 0) {
36 /* If it is a partition find the originating device */
37 xsprintf_sys_block_path(p
, "/partition", d
);
38 if (access(p
, F_OK
) < 0)
41 /* Get parent dev_t */
42 xsprintf_sys_block_path(p
, "/../dev", d
);
43 r
= read_one_line_file(p
, &s
);
47 r
= parse_devnum(s
, &devt
);
51 /* Only return this if it is really good enough for us. */
52 xsprintf_sys_block_path(p
, "/queue", devt
);
53 if (access(p
, F_OK
) < 0)
60 int get_block_device_fd(int fd
, dev_t
*ret
) {
67 /* Gets the block device directly backing a file system. If the block device is encrypted, returns
68 * the device mapper block device. */
73 if (major(st
.st_dev
) != 0) {
78 r
= btrfs_get_block_device_fd(fd
, ret
);
81 if (r
!= -ENOTTY
) /* not btrfs */
88 int get_block_device(const char *path
, dev_t
*ret
) {
89 _cleanup_close_
int fd
= -1;
94 fd
= open(path
, O_RDONLY
|O_NOFOLLOW
|O_CLOEXEC
);
98 return get_block_device_fd(fd
, ret
);
101 int block_get_originating(dev_t dt
, dev_t
*ret
) {
102 _cleanup_closedir_
DIR *d
= NULL
;
103 _cleanup_free_
char *t
= NULL
;
104 char p
[SYS_BLOCK_PATH_MAX("/slaves")];
105 _cleanup_free_
char *first_found
= NULL
;
110 /* For the specified block device tries to chase it through the layers, in case LUKS-style DM stacking is used,
111 * trying to find the next underlying layer. */
113 xsprintf_sys_block_path(p
, "/slaves", dt
);
118 FOREACH_DIRENT_ALL(de
, d
, return -errno
) {
120 if (dot_or_dot_dot(de
->d_name
))
123 if (!IN_SET(de
->d_type
, DT_LNK
, DT_UNKNOWN
))
127 _cleanup_free_
char *u
= NULL
, *v
= NULL
, *a
= NULL
, *b
= NULL
;
129 /* We found a device backed by multiple other devices. We don't really support
130 * automatic discovery on such setups, with the exception of dm-verity partitions. In
131 * this case there are two backing devices: the data partition and the hash
132 * partition. We are fine with such setups, however, only if both partitions are on
133 * the same physical device. Hence, let's verify this by iterating over every node
134 * in the 'slaves/' directory and comparing them with the first that gets returned by
135 * readdir(), to ensure they all point to the same device. */
137 u
= path_join(p
, de
->d_name
, "../dev");
141 v
= path_join(p
, first_found
, "../dev");
145 r
= read_one_line_file(u
, &a
);
147 return log_debug_errno(r
, "Failed to read %s: %m", u
);
149 r
= read_one_line_file(v
, &b
);
151 return log_debug_errno(r
, "Failed to read %s: %m", v
);
153 /* Check if the parent device is the same. If not, then the two backing devices are on
154 * different physical devices, and we don't support that. */
158 first_found
= strdup(de
->d_name
);
167 q
= strjoina(p
, "/", first_found
, "/dev");
169 r
= read_one_line_file(q
, &t
);
173 r
= parse_devnum(t
, &devt
);
177 if (major(devt
) == 0)
184 int get_block_device_harder_fd(int fd
, dev_t
*ret
) {
190 /* Gets the backing block device for a file system, and handles LUKS encrypted file systems, looking for its
191 * immediate parent, if there is one. */
193 r
= get_block_device_fd(fd
, ret
);
197 r
= block_get_originating(*ret
, ret
);
199 log_debug_errno(r
, "Failed to chase block device, ignoring: %m");
204 int get_block_device_harder(const char *path
, dev_t
*ret
) {
205 _cleanup_close_
int fd
= -1;
210 fd
= open(path
, O_RDONLY
|O_NOFOLLOW
|O_CLOEXEC
);
214 return get_block_device_harder_fd(fd
, ret
);
217 int lock_whole_block_device(dev_t devt
, int operation
) {
218 _cleanup_free_
char *whole_node
= NULL
;
219 _cleanup_close_
int lock_fd
= -1;
223 /* Let's get a BSD file lock on the whole block device, as per: https://systemd.io/BLOCK_DEVICE_LOCKING */
225 r
= block_get_whole_disk(devt
, &whole_devt
);
229 r
= device_path_make_major_minor(S_IFBLK
, whole_devt
, &whole_node
);
233 lock_fd
= open(whole_node
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
237 if (flock(lock_fd
, operation
) < 0)
240 return TAKE_FD(lock_fd
);
243 int blockdev_partscan_enabled(int fd
) {
244 _cleanup_free_
char *p
= NULL
, *buf
= NULL
;
245 unsigned long long ull
;
249 /* Checks if partition scanning is correctly enabled on the block device */
251 if (fstat(fd
, &st
) < 0)
254 if (!S_ISBLK(st
.st_mode
))
257 if (asprintf(&p
, "/sys/dev/block/%u:%u/capability", major(st
.st_rdev
), minor(st
.st_rdev
)) < 0)
260 r
= read_one_line_file(p
, &buf
);
261 if (r
== -ENOENT
) /* If the capability file doesn't exist then we are most likely looking at a
262 * partition block device, not the whole block device. And that means we have no
263 * partition scanning on for it (we do for its parent, but not for the partition
269 r
= safe_atollu_full(buf
, 16, &ull
);
273 #ifndef GENHD_FL_NO_PART_SCAN
274 #define GENHD_FL_NO_PART_SCAN (0x0200)
277 return !FLAGS_SET(ull
, GENHD_FL_NO_PART_SCAN
);
280 static int blockdev_is_encrypted(const char *sysfs_path
, unsigned depth_left
) {
281 _cleanup_free_
char *p
= NULL
, *uuids
= NULL
;
282 _cleanup_closedir_
DIR *d
= NULL
;
283 int r
, found_encrypted
= false;
290 p
= path_join(sysfs_path
, "dm/uuid");
294 r
= read_one_line_file(p
, &uuids
);
299 /* The DM device's uuid attribute is prefixed with "CRYPT-" if this is a dm-crypt device. */
300 if (startswith(uuids
, "CRYPT-"))
304 /* Not a dm-crypt device itself. But maybe it is on top of one? Follow the links in the "slaves/"
308 p
= path_join(sysfs_path
, "slaves");
314 if (errno
== ENOENT
) /* Doesn't have underlying devices */
321 _cleanup_free_
char *q
= NULL
;
325 de
= readdir_no_dot(d
);
330 break; /* No more underlying devices */
333 q
= path_join(p
, de
->d_name
);
337 r
= blockdev_is_encrypted(q
, depth_left
- 1);
340 if (r
== 0) /* we found one that is not encrypted? then propagate that immediately */
343 found_encrypted
= true;
346 return found_encrypted
;
349 int fd_is_encrypted(int fd
) {
350 char p
[SYS_BLOCK_PATH_MAX(NULL
)];
354 r
= get_block_device_fd(fd
, &devt
);
357 if (r
== 0) /* doesn't have a block device */
360 xsprintf_sys_block_path(p
, NULL
, devt
);
362 return blockdev_is_encrypted(p
, 10 /* safety net: maximum recursion depth */);
365 int path_is_encrypted(const char *path
) {
366 char p
[SYS_BLOCK_PATH_MAX(NULL
)];
370 r
= get_block_device(path
, &devt
);
373 if (r
== 0) /* doesn't have a block device */
376 xsprintf_sys_block_path(p
, NULL
, devt
);
378 return blockdev_is_encrypted(p
, 10 /* safety net: maximum recursion depth */);