]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/blockdev-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
9 #include <sys/statfs.h>
11 #include "alloc-util.h"
12 #include "blockdev-util.h"
13 #include "btrfs-util.h"
14 #include "dirent-util.h"
18 #include "stat-util.h"
20 int block_get_whole_disk(dev_t d
, dev_t
*ret
) {
21 char p
[SYS_BLOCK_PATH_MAX("/partition")];
22 _cleanup_free_
char *s
= NULL
;
28 /* If it has a queue this is good enough for us */
29 xsprintf_sys_block_path(p
, "/queue", d
);
30 if (access(p
, F_OK
) >= 0) {
35 /* If it is a partition find the originating device */
36 xsprintf_sys_block_path(p
, "/partition", d
);
37 if (access(p
, F_OK
) < 0)
40 /* Get parent dev_t */
41 xsprintf_sys_block_path(p
, "/../dev", d
);
42 r
= read_one_line_file(p
, &s
);
46 r
= sscanf(s
, "%u:%u", &m
, &n
);
50 /* Only return this if it is really good enough for us. */
51 xsprintf_sys_block_path(p
, "/queue", makedev(m
, n
));
52 if (access(p
, F_OK
) < 0)
59 int get_block_device(const char *path
, dev_t
*dev
) {
66 /* Get's the block device directly backing a file system. If
67 * the block device is encrypted, returns the device mapper
73 if (major(st
.st_dev
) != 0) {
78 if (statfs(path
, &sfs
) < 0)
81 if (F_TYPE_EQUAL(sfs
.f_type
, BTRFS_SUPER_MAGIC
))
82 return btrfs_get_block_device(path
, dev
);
88 int block_get_originating(dev_t dt
, dev_t
*ret
) {
89 _cleanup_closedir_
DIR *d
= NULL
;
90 _cleanup_free_
char *t
= NULL
;
91 char p
[SYS_BLOCK_PATH_MAX("/slaves")];
92 struct dirent
*de
, *found
= NULL
;
97 /* For the specified block device tries to chase it through the layers, in case LUKS-style DM stacking is used,
98 * trying to find the next underlying layer. */
100 xsprintf_sys_block_path(p
, "/slaves", dt
);
105 FOREACH_DIRENT_ALL(de
, d
, return -errno
) {
107 if (dot_or_dot_dot(de
->d_name
))
110 if (!IN_SET(de
->d_type
, DT_LNK
, DT_UNKNOWN
))
114 _cleanup_free_
char *u
= NULL
, *v
= NULL
, *a
= NULL
, *b
= NULL
;
116 /* We found a device backed by multiple other devices. We don't really support automatic
117 * discovery on such setups, with the exception of dm-verity partitions. In this case there are
118 * two backing devices: the data partition and the hash partition. We are fine with such
119 * setups, however, only if both partitions are on the same physical device. Hence, let's
122 u
= strjoin(p
, "/", de
->d_name
, "/../dev");
126 v
= strjoin(p
, "/", found
->d_name
, "/../dev");
130 r
= read_one_line_file(u
, &a
);
132 return log_debug_errno(r
, "Failed to read %s: %m", u
);
134 r
= read_one_line_file(v
, &b
);
136 return log_debug_errno(r
, "Failed to read %s: %m", v
);
138 /* Check if the parent device is the same. If not, then the two backing devices are on
139 * different physical devices, and we don't support that. */
150 q
= strjoina(p
, "/", found
->d_name
, "/dev");
152 r
= read_one_line_file(q
, &t
);
156 if (sscanf(t
, "%u:%u", &maj
, &min
) != 2)
162 *ret
= makedev(maj
, min
);
166 int get_block_device_harder(const char *path
, dev_t
*ret
) {
172 /* Gets the backing block device for a file system, and handles LUKS encrypted file systems, looking for its
173 * immediate parent, if there is one. */
175 r
= get_block_device(path
, ret
);
179 r
= block_get_originating(*ret
, ret
);
181 log_debug_errno(r
, "Failed to chase block device '%s', ignoring: %m", path
);