]>
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
);
87 int get_block_device_harder(const char *path
, dev_t
*dev
) {
88 _cleanup_closedir_
DIR *d
= NULL
;
89 _cleanup_free_
char *t
= NULL
;
90 char p
[SYS_BLOCK_PATH_MAX("/slaves")];
91 struct dirent
*de
, *found
= NULL
;
100 /* Gets the backing block device for a file system, and
101 * handles LUKS encrypted file systems, looking for its
102 * immediate parent, if there is one. */
104 r
= get_block_device(path
, &dt
);
108 xsprintf_sys_block_path(p
, "/slaves", dt
);
117 FOREACH_DIRENT_ALL(de
, d
, return -errno
) {
119 if (dot_or_dot_dot(de
->d_name
))
122 if (!IN_SET(de
->d_type
, DT_LNK
, DT_UNKNOWN
))
126 _cleanup_free_
char *u
= NULL
, *v
= NULL
, *a
= NULL
, *b
= NULL
;
128 /* We found a device backed by multiple other devices. We don't really support automatic
129 * discovery on such setups, with the exception of dm-verity partitions. In this case there are
130 * two backing devices: the data partition and the hash partition. We are fine with such
131 * setups, however, only if both partitions are on the same physical device. Hence, let's
134 u
= strjoin(p
, "/", de
->d_name
, "/../dev");
138 v
= strjoin(p
, "/", found
->d_name
, "/../dev");
142 r
= read_one_line_file(u
, &a
);
144 log_debug_errno(r
, "Failed to read %s: %m", u
);
148 r
= read_one_line_file(v
, &b
);
150 log_debug_errno(r
, "Failed to read %s: %m", v
);
154 /* Check if the parent device is the same. If not, then the two backing devices are on
155 * different physical devices, and we don't support that. */
166 q
= strjoina(p
, "/", found
->d_name
, "/dev");
168 r
= read_one_line_file(q
, &t
);
174 if (sscanf(t
, "%u:%u", &maj
, &min
) != 2)
180 *dev
= makedev(maj
, min
);