]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/blockdev-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
6 #include "blockdev-util.h"
7 #include "btrfs-util.h"
8 #include "dirent-util.h"
11 #include "missing_magic.h"
12 #include "parse-util.h"
13 #include "stat-util.h"
15 int block_get_whole_disk(dev_t d
, dev_t
*ret
) {
16 char p
[SYS_BLOCK_PATH_MAX("/partition")];
17 _cleanup_free_
char *s
= NULL
;
26 /* If it has a queue this is good enough for us */
27 xsprintf_sys_block_path(p
, "/queue", d
);
28 if (access(p
, F_OK
) >= 0) {
33 /* If it is a partition find the originating device */
34 xsprintf_sys_block_path(p
, "/partition", d
);
35 if (access(p
, F_OK
) < 0)
38 /* Get parent dev_t */
39 xsprintf_sys_block_path(p
, "/../dev", d
);
40 r
= read_one_line_file(p
, &s
);
44 r
= parse_dev(s
, &devt
);
48 /* Only return this if it is really good enough for us. */
49 xsprintf_sys_block_path(p
, "/queue", devt
);
50 if (access(p
, F_OK
) < 0)
57 int get_block_device(const char *path
, dev_t
*ret
) {
58 _cleanup_close_
int fd
= -1;
65 /* Gets the block device directly backing a file system. If the block device is encrypted, returns
66 * the device mapper block device. */
68 fd
= open(path
, O_NOFOLLOW
|O_CLOEXEC
);
75 if (major(st
.st_dev
) != 0) {
80 r
= btrfs_get_block_device_fd(fd
, ret
);
83 if (r
!= -ENOTTY
) /* not btrfs */
90 int block_get_originating(dev_t dt
, dev_t
*ret
) {
91 _cleanup_closedir_
DIR *d
= NULL
;
92 _cleanup_free_
char *t
= NULL
;
93 char p
[SYS_BLOCK_PATH_MAX("/slaves")];
94 struct dirent
*de
, *found
= NULL
;
99 /* For the specified block device tries to chase it through the layers, in case LUKS-style DM stacking is used,
100 * trying to find the next underlying layer. */
102 xsprintf_sys_block_path(p
, "/slaves", dt
);
107 FOREACH_DIRENT_ALL(de
, d
, return -errno
) {
109 if (dot_or_dot_dot(de
->d_name
))
112 if (!IN_SET(de
->d_type
, DT_LNK
, DT_UNKNOWN
))
116 _cleanup_free_
char *u
= NULL
, *v
= NULL
, *a
= NULL
, *b
= NULL
;
118 /* We found a device backed by multiple other devices. We don't really support automatic
119 * discovery on such setups, with the exception of dm-verity partitions. In this case there are
120 * two backing devices: the data partition and the hash partition. We are fine with such
121 * setups, however, only if both partitions are on the same physical device. Hence, let's
124 u
= path_join(p
, de
->d_name
, "../dev");
128 v
= path_join(p
, found
->d_name
, "../dev");
132 r
= read_one_line_file(u
, &a
);
134 return log_debug_errno(r
, "Failed to read %s: %m", u
);
136 r
= read_one_line_file(v
, &b
);
138 return log_debug_errno(r
, "Failed to read %s: %m", v
);
140 /* Check if the parent device is the same. If not, then the two backing devices are on
141 * different physical devices, and we don't support that. */
152 q
= strjoina(p
, "/", found
->d_name
, "/dev");
154 r
= read_one_line_file(q
, &t
);
158 r
= parse_dev(t
, &devt
);
162 if (major(devt
) == 0)
169 int get_block_device_harder(const char *path
, dev_t
*ret
) {
175 /* Gets the backing block device for a file system, and handles LUKS encrypted file systems, looking for its
176 * immediate parent, if there is one. */
178 r
= get_block_device(path
, ret
);
182 r
= block_get_originating(*ret
, ret
);
184 log_debug_errno(r
, "Failed to chase block device '%s', ignoring: %m", path
);