2 * Linux block devices helpers
4 * Copyright (C) 2018-2023 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2018-2023 Ondrej Kozina
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "cryptsetup.h"
24 #ifdef HAVE_SYS_SYSMACROS_H
25 # include <sys/sysmacros.h> /* for major, minor */
27 #include <uuid/uuid.h>
29 #define UUID_LEN 37 /* 36 + \0, libuuid ... */
31 static int dm_prepare_uuid(const char *type, const char *uuid, char *buf, size_t buflen)
33 char *ptr, uuid2[UUID_LEN] = {0};
37 /* Remove '-' chars */
39 if (uuid_parse(uuid, uu) < 0) {
40 log_dbg("Requested UUID %s has invalid format.", uuid);
44 for (ptr = uuid2, i = 0; i < UUID_LEN; i++)
51 if (snprintf(buf, buflen, DM_UUID_PREFIX "%s%s%s%s",
52 type ?: "", type ? "-" : "",
53 uuid2[0] ? uuid2 : "", uuid2[0] ? "-" : "") < 0)
59 /* return number of holders in general, if matched dm_uuid prefix it's returned via dm_name */
60 /* negative value is error */
61 static int lookup_holder_dm_name(const char *dm_uuid, dev_t devno, char **r_dm_name)
64 char dm_subpath[PATH_MAX], data_dev_dir[PATH_MAX], uuid[DM_UUID_LEN], dm_name[PATH_MAX] = {};
67 int dmfd, fd, len, r = 0; /* not found */
73 len = snprintf(data_dev_dir, PATH_MAX, "/sys/dev/block/%u:%u/holders", major(devno), minor(devno));
74 if (len < 0 || len >= PATH_MAX)
77 if (!(dir = opendir(data_dev_dir)))
78 /* map ENOTDIR to ENOENT we'll handle both errors same */
79 return errno == ENOTDIR ? -ENOENT : -errno;
81 while (r != 1 && (entry = readdir(dir))) {
82 if (entry->d_name[0] == '.' ||
83 !strncmp(entry->d_name, "..", 2))
86 /* there's a holder */
89 /* we already have a dm_name, just count remaining holders */
93 len = snprintf(dm_subpath, PATH_MAX, "%s/%s", entry->d_name, "dm");
94 if (len < 0 || len >= PATH_MAX) {
99 /* looking for dm-X/dm directory, symlinks are fine */
100 dmfd = openat(dirfd(dir), dm_subpath, O_DIRECTORY | O_RDONLY);
104 fd = openat(dmfd, "uuid", O_RDONLY);
110 if (fstat(fd, &st) || !S_ISREG(st.st_mode)) {
116 /* reads binary data */
117 s = read_buffer(fd, uuid, sizeof(uuid) - 1);
119 uuid[s > 0 ? s : 0] = '\0';
120 if (!strncmp(uuid, dm_uuid, strlen(dm_uuid)))
121 log_dbg("Found candidate device %s", entry->d_name);
127 fd = openat(dmfd, "name", O_RDONLY);
133 if (fstat(fd, &st) || !S_ISREG(st.st_mode)) {
139 /* reads binary data */
140 s = read_buffer(fd, dm_name, sizeof(dm_name));
145 log_dbg("Found dm device %s", dm_name);
146 if (!(*r_dm_name = strdup(dm_name)))
156 int tools_lookup_crypt_device(struct crypt_device *cd, const char *type,
157 const char *data_device_path, char **r_name)
161 char dev_uuid[DM_UUID_LEN + DM_BY_ID_PREFIX_LEN] = DM_BY_ID_PREFIX;
163 if (!dm_prepare_uuid(type, crypt_get_uuid(cd), dev_uuid + DM_BY_ID_PREFIX_LEN, DM_UUID_LEN))
166 c = strrchr(dev_uuid, '-');
173 log_dbg("Looking for any dm device with prefix: %s", dev_uuid);
175 if (stat(data_device_path, &st) < 0)
178 if (!S_ISBLK(st.st_mode))
181 return lookup_holder_dm_name(dev_uuid + DM_BY_ID_PREFIX_LEN, st.st_rdev, r_name);
184 static void report_partition(const char *value, const char *device, bool batch_mode)
187 log_dbg("Device %s already contains a '%s' partition signature.", device, value);
189 log_std(_("WARNING: Device %s already contains a '%s' partition signature.\n"), device, value);
192 static void report_superblock(const char *value, const char *device, bool batch_mode)
195 log_dbg("Device %s already contains a '%s' superblock signature.", device, value);
197 log_std(_("WARNING: Device %s already contains a '%s' superblock signature.\n"), device, value);
200 int tools_detect_signatures(const char *device, tools_probe_filter_info filter,
201 size_t *count,bool batch_mode)
205 struct blkid_handle *h;
213 if (!blk_supported()) {
214 log_dbg("Blkid support disabled.");
218 if ((r = blk_init_by_path(&h, device))) {
219 log_err(_("Failed to initialize device signature probes."));
224 case PRB_FILTER_LUKS:
225 if (blk_superblocks_filter_luks(h)) {
230 case PRB_FILTER_NONE:
231 blk_set_chains_for_full_print(h);
234 blk_set_chains_for_fast_detection(h);
235 if (blk_superblocks_only_luks(h)) {
241 while ((pr = blk_probe(h)) < PRB_EMPTY) {
242 if (blk_is_partition(h))
243 report_partition(blk_get_partition_type(h), device, batch_mode);
244 else if (blk_is_superblock(h))
245 report_superblock(blk_get_superblock_type(h), device, batch_mode);
247 log_dbg("Internal tools_detect_signatures() error.");
261 int tools_wipe_all_signatures(const char *path, bool exclusive, bool only_luks)
266 struct blkid_handle *h = NULL;
268 if (!blk_supported()) {
269 log_dbg("Blkid support disabled.");
273 if (stat(path, &st)) {
274 log_err(_("Failed to stat device %s."), path);
279 if (S_ISBLK(st.st_mode) && exclusive)
282 /* better than opening regular file with O_EXCL (undefined) */
283 /* coverity[toctou] */
284 fd = open(path, flags); /* lgtm[cpp/toctou-race-condition] */
287 log_err(_("Cannot exclusively open %s, device in use."), path);
289 log_err(_("Failed to open file %s in read/write mode."), path);
293 if ((r = blk_init_by_fd(&h, fd))) {
294 log_err(_("Failed to initialize device signature probes."));
299 blk_set_chains_for_wipes(h);
300 if (only_luks && (r = blk_superblocks_only_luks(h))) {
305 while ((pr = blk_probe(h)) < PRB_EMPTY) {
306 if (blk_is_partition(h))
307 log_verbose(_("Existing '%s' partition signature on device %s will be wiped."),
308 blk_get_partition_type(h), path);
309 if (blk_is_superblock(h))
310 log_verbose(_("Existing '%s' superblock signature on device %s will be wiped."),
311 blk_get_superblock_type(h), path);
312 if (blk_do_wipe(h) || fsync(fd)) {
313 log_err(_("Failed to wipe device signature."));
319 if (pr != PRB_EMPTY) {
320 log_err(_("Failed to probe device %s for a signature."), path);
329 int tools_superblock_block_size(const char *device, char *sb_name, size_t sb_name_len, unsigned *r_block_size)
331 struct blkid_handle *h;
335 if (!r_block_size || !sb_name || sb_name_len < 1)
338 if (!blk_supported()) {
339 log_dbg("Blkid support disabled.");
343 if ((r = blk_init_by_path(&h, device))) {
344 log_err(_("Failed to initialize device signature probes."));
348 blk_set_chains_for_superblocks(h);
350 switch (blk_probe(h)) {
352 *r_block_size = blk_get_block_size(h);
353 if (!*r_block_size) /* same as not-found */
356 if (!(name = blk_get_superblock_type(h))) {
361 /* we don't mind truncating */
362 strncpy(sb_name, name, sb_name_len - 1);
363 sb_name[sb_name_len-1] = '\0';
365 log_dbg("Detected superblock %s on device %s (block size: %u).", sb_name, device, *r_block_size);
379 bool tools_blkid_supported(void)
381 return blk_supported() != 0;