2 * blkid probe utilities
4 * Copyright (C) 2018-2020 Red Hat, Inc. All rights reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "utils_blkid.h"
31 #include <blkid/blkid.h>
32 /* make bad checksums flag optional */
33 #ifndef BLKID_SUBLKS_BADCSUM
34 #define BLKID_SUBLKS_BADCSUM 0
40 #ifndef HAVE_BLKID_WIPE
41 static size_t crypt_getpagesize(void)
43 long r = sysconf(_SC_PAGESIZE);
44 return r <= 0 ? 4096 : (size_t)r;
49 void blk_set_chains_for_wipes(struct blkid_handle *h)
52 blkid_probe_enable_partitions(h->pr, 1);
53 blkid_probe_set_partitions_flags(h->pr, 0
54 #ifdef HAVE_BLKID_WIPE
59 blkid_probe_enable_superblocks(h->pr, 1);
60 blkid_probe_set_superblocks_flags(h->pr, BLKID_SUBLKS_LABEL |
64 BLKID_SUBLKS_VERSION |
66 BLKID_SUBLKS_BADCSUM);
70 void blk_set_chains_for_full_print(struct blkid_handle *h)
72 blk_set_chains_for_wipes(h);
75 void blk_set_chains_for_fast_detection(struct blkid_handle *h)
78 blkid_probe_enable_partitions(h->pr, 1);
79 blkid_probe_set_partitions_flags(h->pr, 0);
81 blkid_probe_enable_superblocks(h->pr, 1);
82 blkid_probe_set_superblocks_flags(h->pr, BLKID_SUBLKS_TYPE);
86 int blk_init_by_path(struct blkid_handle **h, const char *path)
90 struct blkid_handle *tmp = malloc(sizeof(*tmp));
96 tmp->pr = blkid_new_probe_from_filename(path);
109 int blk_init_by_fd(struct blkid_handle **h, int fd)
113 struct blkid_handle *tmp = malloc(sizeof(*tmp));
117 tmp->pr = blkid_new_probe();
123 if (blkid_probe_set_device(tmp->pr, fd, 0, 0)) {
124 blkid_free_probe(tmp->pr);
138 int blk_superblocks_filter_luks(struct blkid_handle *h)
142 char luks[] = "crypto_LUKS";
143 char *luks_filter[] = {
147 r = blkid_probe_filter_superblocks_type(h->pr, BLKID_FLTR_NOTIN, luks_filter);
152 blk_probe_status blk_probe(struct blkid_handle *h)
154 blk_probe_status pr = PRB_FAIL;
156 int r = blkid_do_probe(h->pr);
166 blk_probe_status blk_safeprobe(struct blkid_handle *h)
170 r = blkid_do_safeprobe(h->pr);
174 return PRB_AMBIGUOUS;
184 int blk_is_partition(struct blkid_handle *h)
188 r = blkid_probe_has_value(h->pr, "PTTYPE");
193 int blk_is_superblock(struct blkid_handle *h)
197 r = blkid_probe_has_value(h->pr, "TYPE");
202 const char *blk_get_partition_type(struct blkid_handle *h)
204 const char *value = NULL;
206 (void) blkid_probe_lookup_value(h->pr, "PTTYPE", &value, NULL);
211 const char *blk_get_superblock_type(struct blkid_handle *h)
213 const char *value = NULL;
215 (void) blkid_probe_lookup_value(h->pr, "TYPE", &value, NULL);
220 void blk_free(struct blkid_handle *h)
227 blkid_free_probe(h->pr);
234 #ifndef HAVE_BLKID_WIPE
235 static int blk_step_back(struct blkid_handle *h)
237 #ifdef HAVE_BLKID_STEP_BACK
238 return blkid_probe_step_back(h->pr);
240 blkid_reset_probe(h->pr);
241 blkid_probe_set_device(h->pr, h->fd, 0, 0);
245 #endif /* not HAVE_BLKID_WIPE */
246 #endif /* HAVE_BLKID */
248 int blk_do_wipe(struct blkid_handle *h)
251 #ifdef HAVE_BLKID_WIPE
252 return blkid_do_wipe(h->pr, 0);
258 size_t alignment, len, bsize = blkid_probe_get_sectorsize(h->pr);
260 if (h->fd < 0 || !bsize)
263 if (blk_is_partition(h)) {
264 if (blkid_probe_lookup_value(h->pr, "PTMAGIC_OFFSET", &offset, NULL))
266 if (blkid_probe_lookup_value(h->pr, "PTMAGIC", NULL, &len))
268 } else if (blk_is_superblock(h)) {
269 if (blkid_probe_lookup_value(h->pr, "SBMAGIC_OFFSET", &offset, NULL))
271 if (blkid_probe_lookup_value(h->pr, "SBMAGIC", NULL, &len))
276 alignment = crypt_getpagesize();
278 if (posix_memalign(&buf, alignment, len))
282 offset_val = strtoll(offset, NULL, 10);
284 /* TODO: missing crypt_wipe_fd() */
285 ret = write_lseek_blockwise(h->fd, bsize, alignment, buf, len, offset_val);
290 if ((size_t)ret == len) {
297 #else /* HAVE_BLKID */
302 int blk_supported(void)
311 off_t blk_get_offset(struct blkid_handle *h)
313 off_t offset_value = -1;
316 if (blk_is_superblock(h)) {
317 if (!blkid_probe_lookup_value(h->pr, "SBMAGIC_OFFSET", &offset, NULL))
318 offset_value = strtoll(offset, NULL, 10);
319 } else if (blk_is_partition(h) && !blkid_probe_lookup_value(h->pr, "PTMAGIC_OFFSET", &offset, NULL))
320 offset_value = strtoll(offset, NULL, 10);