]> git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/wipefs.c
e6c417ce725bdf3a0789614b697eba826fd51211
[thirdparty/util-linux.git] / misc-utils / wipefs.c
1 /*
2 * wipefs - utility to wipe filesystems from device
3 *
4 * Copyright (C) 2009 Red Hat, Inc. All rights reserved.
5 * Written by Karel Zak <kzak@redhat.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it would 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.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <ctype.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <getopt.h>
30 #include <string.h>
31 #include <limits.h>
32 #include <libgen.h>
33
34 #include <blkid.h>
35
36 #include "nls.h"
37 #include "xalloc.h"
38 #include "strutils.h"
39 #include "all-io.h"
40 #include "match.h"
41 #include "c.h"
42 #include "closestream.h"
43 #include "optutils.h"
44 #include "blkdev.h"
45
46 struct wipe_desc {
47 loff_t offset; /* magic string offset */
48 size_t len; /* length of magic string */
49 unsigned char *magic; /* magic string */
50
51 char *usage; /* raid, filesystem, ... */
52 char *type; /* FS type */
53 char *label; /* FS label */
54 char *uuid; /* FS uuid */
55
56 struct wipe_desc *next;
57
58 unsigned int zap : 1,
59 on_disk : 1,
60 is_parttable : 1;
61
62 };
63
64 enum {
65 WP_MODE_PRETTY, /* default */
66 WP_MODE_PARSABLE
67 };
68
69 enum {
70 WP_FL_NOACT = (1 << 1),
71 WP_FL_ALL = (1 << 2),
72 WP_FL_QUIET = (1 << 3),
73 WP_FL_BACKUP = (1 << 4),
74 WP_FL_FORCE = (1 << 5)
75 };
76
77 static const char *type_pattern;
78
79 static void
80 print_pretty(struct wipe_desc *wp, int line)
81 {
82 if (!line) {
83 printf("offset type\n");
84 printf("----------------------------------------------------------------\n");
85 }
86
87 printf("0x%-17jx %s [%s]", (intmax_t)wp->offset, wp->type, _(wp->usage));
88
89 if (wp->label && *wp->label)
90 printf("\n%27s %s", "LABEL:", wp->label);
91 if (wp->uuid)
92 printf("\n%27s %s", "UUID: ", wp->uuid);
93 puts("\n");
94 }
95
96 static void
97 print_parsable(struct wipe_desc *wp, int line)
98 {
99 char enc[256];
100
101 if (!line)
102 printf("# offset,uuid,label,type\n");
103
104 printf("0x%jx,", (intmax_t)wp->offset);
105
106 if (wp->uuid) {
107 blkid_encode_string(wp->uuid, enc, sizeof(enc));
108 printf("%s,", enc);
109 } else
110 fputc(',', stdout);
111
112 if (wp->label) {
113 blkid_encode_string(wp->label, enc, sizeof(enc));
114 printf("%s,", enc);
115 } else
116 fputc(',', stdout);
117
118 blkid_encode_string(wp->type, enc, sizeof(enc));
119 printf("%s\n", enc);
120 }
121
122 static void
123 print_all(struct wipe_desc *wp, int mode)
124 {
125 int n = 0;
126
127 while (wp) {
128 switch (mode) {
129 case WP_MODE_PRETTY:
130 print_pretty(wp, n++);
131 break;
132 case WP_MODE_PARSABLE:
133 print_parsable(wp, n++);
134 break;
135 default:
136 abort();
137 }
138 wp = wp->next;
139 }
140 }
141
142 static struct wipe_desc *
143 add_offset(struct wipe_desc *wp0, loff_t offset, int zap)
144 {
145 struct wipe_desc *wp = wp0;
146
147 while (wp) {
148 if (wp->offset == offset)
149 return wp;
150 wp = wp->next;
151 }
152
153 wp = xcalloc(1, sizeof(struct wipe_desc));
154 wp->offset = offset;
155 wp->next = wp0;
156 wp->zap = zap ? 1 : 0;
157 return wp;
158 }
159
160 static struct wipe_desc *
161 clone_offset(struct wipe_desc *wp0)
162 {
163 struct wipe_desc *wp = NULL;
164
165 while(wp0) {
166 wp = add_offset(wp, wp0->offset, wp0->zap);
167 wp0 = wp0->next;
168 }
169
170 return wp;
171 }
172
173 static struct wipe_desc *
174 get_desc_for_probe(struct wipe_desc *wp, blkid_probe pr)
175 {
176 const char *off, *type, *mag, *p, *usage = NULL;
177 size_t len;
178 loff_t offset;
179 int rc, ispt = 0;
180
181 /* superblocks */
182 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) == 0) {
183 rc = blkid_probe_lookup_value(pr, "SBMAGIC_OFFSET", &off, NULL);
184 if (!rc)
185 rc = blkid_probe_lookup_value(pr, "SBMAGIC", &mag, &len);
186 if (rc)
187 return wp;
188
189 /* partitions */
190 } else if (blkid_probe_lookup_value(pr, "PTTYPE", &type, NULL) == 0) {
191 rc = blkid_probe_lookup_value(pr, "PTMAGIC_OFFSET", &off, NULL);
192 if (!rc)
193 rc = blkid_probe_lookup_value(pr, "PTMAGIC", &mag, &len);
194 if (rc)
195 return wp;
196 usage = N_("partition table");
197 ispt = 1;
198 } else
199 return wp;
200
201 if (type_pattern && !match_fstype(type, type_pattern))
202 return wp;
203
204 offset = strtoll(off, NULL, 10);
205
206 wp = add_offset(wp, offset, 0);
207 if (!wp)
208 return NULL;
209
210 if (usage || blkid_probe_lookup_value(pr, "USAGE", &usage, NULL) == 0)
211 wp->usage = xstrdup(usage);
212
213 wp->type = xstrdup(type);
214 wp->on_disk = 1;
215 wp->is_parttable = ispt ? 1 : 0;
216
217 wp->magic = xmalloc(len);
218 memcpy(wp->magic, mag, len);
219 wp->len = len;
220
221 if (blkid_probe_lookup_value(pr, "LABEL", &p, NULL) == 0)
222 wp->label = xstrdup(p);
223
224 if (blkid_probe_lookup_value(pr, "UUID", &p, NULL) == 0)
225 wp->uuid = xstrdup(p);
226
227 return wp;
228 }
229
230 static blkid_probe
231 new_probe(const char *devname, int mode)
232 {
233 blkid_probe pr = NULL;
234
235 if (!devname)
236 return NULL;
237
238 if (mode) {
239 int fd = open(devname, mode);
240 if (fd < 0)
241 goto error;
242
243 pr = blkid_new_probe();
244 if (!pr || blkid_probe_set_device(pr, fd, 0, 0) != 0) {
245 close(fd);
246 goto error;
247 }
248 } else
249 pr = blkid_new_probe_from_filename(devname);
250
251 if (!pr)
252 goto error;
253
254 blkid_probe_enable_superblocks(pr, 1);
255 blkid_probe_set_superblocks_flags(pr,
256 BLKID_SUBLKS_MAGIC | /* return magic string and offset */
257 BLKID_SUBLKS_TYPE | /* return superblock type */
258 BLKID_SUBLKS_USAGE | /* return USAGE= */
259 BLKID_SUBLKS_LABEL | /* return LABEL= */
260 BLKID_SUBLKS_UUID | /* return UUID= */
261 BLKID_SUBLKS_BADCSUM); /* accept bad checksums */
262
263 blkid_probe_enable_partitions(pr, 1);
264 blkid_probe_set_partitions_flags(pr, BLKID_PARTS_MAGIC |
265 BLKID_PARTS_FORCE_GPT);
266 return pr;
267 error:
268 blkid_free_probe(pr);
269 err(EXIT_FAILURE, _("error: %s: probing initialization failed"), devname);
270 }
271
272 static struct wipe_desc *
273 read_offsets(struct wipe_desc *wp, const char *devname)
274 {
275 blkid_probe pr = new_probe(devname, 0);
276
277 if (!pr)
278 return NULL;
279
280 while (blkid_do_probe(pr) == 0) {
281 wp = get_desc_for_probe(wp, pr);
282 if (!wp)
283 break;
284 }
285
286 blkid_free_probe(pr);
287 return wp;
288 }
289
290 static void
291 free_wipe(struct wipe_desc *wp)
292 {
293 while (wp) {
294 struct wipe_desc *next = wp->next;
295
296 free(wp->usage);
297 free(wp->type);
298 free(wp->magic);
299 free(wp->label);
300 free(wp->uuid);
301 free(wp);
302
303 wp = next;
304 }
305 }
306
307 static void do_wipe_real(blkid_probe pr, const char *devname,
308 struct wipe_desc *w, int flags)
309 {
310 size_t i;
311
312 if (blkid_do_wipe(pr, (flags & WP_FL_NOACT) != 0))
313 warn(_("%s: failed to erase %s magic string at offset 0x%08jx"),
314 devname, w->type, (intmax_t)w->offset);
315
316 if (flags & WP_FL_QUIET)
317 return;
318
319 printf(P_("%s: %zd byte was erased at offset 0x%08jx (%s): ",
320 "%s: %zd bytes were erased at offset 0x%08jx (%s): ",
321 w->len),
322 devname, w->len, (intmax_t)w->offset, w->type);
323
324 for (i = 0; i < w->len; i++) {
325 printf("%02x", w->magic[i]);
326 if (i + 1 < w->len)
327 fputc(' ', stdout);
328 }
329 putchar('\n');
330 }
331
332 static void do_backup(struct wipe_desc *wp, const char *base)
333 {
334 char *fname = NULL;
335 int fd;
336
337 xasprintf(&fname, "%s0x%08jx.bak", base, (intmax_t)wp->offset);
338
339 fd = open(fname, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
340 if (fd < 0)
341 goto err;
342 if (write_all(fd, wp->magic, wp->len) != 0)
343 goto err;
344 close(fd);
345 free(fname);
346 return;
347 err:
348 err(EXIT_FAILURE, _("%s: failed to create a signature backup"), fname);
349 }
350
351 #ifdef BLKRRPART
352 static void rereadpt(int fd, const char *devname)
353 {
354 struct stat st;
355
356 if (fstat(fd, &st) || !S_ISBLK(st.st_mode))
357 return;
358
359 errno = 0;
360 ioctl(fd, BLKRRPART);
361 printf(_("%s: calling ioctl to re-read partition table: %m\n"), devname);
362 }
363 #endif
364
365 static struct wipe_desc *
366 do_wipe(struct wipe_desc *wp, const char *devname, int flags)
367 {
368 int mode = O_RDWR, reread = 0, need_force = 0;
369 blkid_probe pr;
370 struct wipe_desc *w, *wp0;
371 int zap = (flags & WP_FL_ALL) ? 1 : wp->zap;
372 char *backup = NULL;
373
374 if (!(flags & WP_FL_FORCE))
375 mode |= O_EXCL;
376 pr = new_probe(devname, mode);
377 if (!pr)
378 return NULL;
379
380 if (zap && (flags & WP_FL_BACKUP)) {
381 const char *home = getenv ("HOME");
382 char *tmp = xstrdup(devname);
383
384 if (!home)
385 errx(EXIT_FAILURE, _("failed to create a signature backup, $HOME undefined"));
386 xasprintf (&backup, "%s/wipefs-%s-", home, basename(tmp));
387 free(tmp);
388 }
389
390 wp0 = clone_offset(wp);
391
392 while (blkid_do_probe(pr) == 0) {
393 wp = get_desc_for_probe(wp, pr);
394 if (!wp)
395 break;
396
397 /* Check if offset is in provided list */
398 w = wp0;
399 while(w && w->offset != wp->offset)
400 w = w->next;
401 if (wp0 && !w)
402 continue;
403
404 /* Mark done if found in provided list */
405 if (w)
406 w->on_disk = wp->on_disk;
407
408 if (!wp->on_disk)
409 continue;
410
411 if (!(flags & WP_FL_FORCE)
412 && wp->is_parttable
413 && !blkid_probe_is_wholedisk(pr)) {
414 warnx(_("%s: ignoring nested \"%s\" partition table "
415 "on non-whole disk device"), devname, wp->type);
416 need_force = 1;
417 continue;
418 }
419
420 if (zap) {
421 if (backup)
422 do_backup(wp, backup);
423 do_wipe_real(pr, devname, wp, flags);
424 if (wp->is_parttable)
425 reread = 1;
426 }
427 }
428
429 for (w = wp0; w != NULL; w = w->next) {
430 if (!w->on_disk && !(flags & WP_FL_QUIET))
431 warnx(_("%s: offset 0x%jx not found"), devname, (uintmax_t)w->offset);
432 }
433
434 if (need_force)
435 warnx(_("Use the --force option to force erase."));
436
437 fsync(blkid_probe_get_fd(pr));
438
439 #ifdef BLKRRPART
440 if (reread && (mode & O_EXCL))
441 rereadpt(blkid_probe_get_fd(pr), devname);
442 #endif
443
444 close(blkid_probe_get_fd(pr));
445 blkid_free_probe(pr);
446 free_wipe(wp0);
447 free(backup);
448
449 return wp;
450 }
451
452
453 static void __attribute__((__noreturn__))
454 usage(FILE *out)
455 {
456 fputs(USAGE_HEADER, out);
457 fprintf(out,
458 _(" %s [options] <device>\n"), program_invocation_short_name);
459
460 fputs(USAGE_SEPARATOR, out);
461 fputs(_("Wipe signatures from a device.\n"), out);
462
463 fputs(USAGE_OPTIONS, out);
464 fputs(_(" -a, --all wipe all magic strings (BE CAREFUL!)\n"
465 " -b, --backup create a signature backup in $HOME\n"
466 " -f, --force force erasure\n"
467 " -h, --help show this help text\n"
468 " -n, --no-act do everything except the actual write() call\n"
469 " -o, --offset <num> offset to erase, in bytes\n"
470 " -p, --parsable print out in parsable instead of printable format\n"
471 " -q, --quiet suppress output messages\n"
472 " -t, --types <list> limit the set of filesystem, RAIDs or partition tables\n"
473 " -V, --version output version information and exit\n"), out);
474
475 fprintf(out, USAGE_MAN_TAIL("wipefs(8)"));
476
477 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
478 }
479
480
481 int
482 main(int argc, char **argv)
483 {
484 struct wipe_desc *wp0 = NULL, *wp;
485 int c, has_offset = 0, flags = 0;
486 int mode = WP_MODE_PRETTY;
487
488 static const struct option longopts[] = {
489 { "all", 0, 0, 'a' },
490 { "backup", 0, 0, 'b' },
491 { "force", 0, 0, 'f' },
492 { "help", 0, 0, 'h' },
493 { "no-act", 0, 0, 'n' },
494 { "offset", 1, 0, 'o' },
495 { "parsable", 0, 0, 'p' },
496 { "quiet", 0, 0, 'q' },
497 { "types", 1, 0, 't' },
498 { "version", 0, 0, 'V' },
499 { NULL, 0, 0, 0 }
500 };
501
502 static const ul_excl_t excl[] = { /* rows and cols in in ASCII order */
503 { 'a','o' },
504 { 0 }
505 };
506 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
507
508 setlocale(LC_ALL, "");
509 bindtextdomain(PACKAGE, LOCALEDIR);
510 textdomain(PACKAGE);
511 atexit(close_stdout);
512
513 while ((c = getopt_long(argc, argv, "abfhno:pqt:V", longopts, NULL)) != -1) {
514
515 err_exclusive_options(c, longopts, excl, excl_st);
516
517 switch(c) {
518 case 'a':
519 flags |= WP_FL_ALL;
520 break;
521 case 'b':
522 flags |= WP_FL_BACKUP;
523 break;
524 case 'f':
525 flags |= WP_FL_FORCE;
526 break;
527 case 'h':
528 usage(stdout);
529 break;
530 case 'n':
531 flags |= WP_FL_NOACT;
532 break;
533 case 'o':
534 wp0 = add_offset(wp0, strtosize_or_err(optarg,
535 _("invalid offset argument")), 1);
536 has_offset++;
537 break;
538 case 'p':
539 mode = WP_MODE_PARSABLE;
540 break;
541 case 'q':
542 flags |= WP_FL_QUIET;
543 break;
544 case 't':
545 type_pattern = optarg;
546 break;
547 case 'V':
548 printf(UTIL_LINUX_VERSION);
549 return EXIT_SUCCESS;
550 default:
551 errtryhelp(EXIT_FAILURE);
552 }
553 }
554
555 if (optind == argc)
556 usage(stderr);
557
558 if ((flags & WP_FL_BACKUP) && !((flags & WP_FL_ALL) || has_offset))
559 warnx(_("The --backup option is meaningless in this context"));
560
561 if (!(flags & WP_FL_ALL) && !has_offset) {
562 /*
563 * Print only
564 */
565 while (optind < argc) {
566 wp0 = read_offsets(NULL, argv[optind++]);
567 if (wp0)
568 print_all(wp0, mode);
569 free_wipe(wp0);
570 }
571 } else {
572 /*
573 * Erase
574 */
575 while (optind < argc) {
576 wp = clone_offset(wp0);
577 wp = do_wipe(wp, argv[optind++], flags);
578 free_wipe(wp);
579 }
580 }
581
582 return EXIT_SUCCESS;
583 }