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