]> git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/wipefs.c
58a643e54f5bfdec36783bf27cf42e19e37347e7
[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)) {
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 return NULL;
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, 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, 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, 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 static void rereadpt(int fd, const char *devname)
352 {
353 #ifdef BLKRRPART
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 #endif
363 }
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 if (!home)
383 errx(EXIT_FAILURE, _("failed to create a signature backup, $HOME undefined"));
384 xasprintf (&backup, "%s/wipefs-%s-", home, basename(devname));
385 }
386
387 wp0 = clone_offset(wp);
388
389 while (blkid_do_probe(pr) == 0) {
390 wp = get_desc_for_probe(wp, pr);
391 if (!wp)
392 break;
393
394 /* Check if offset is in provided list */
395 w = wp0;
396 while(w && w->offset != wp->offset)
397 w = w->next;
398 if (wp0 && !w)
399 continue;
400
401 /* Mark done if found in provided list */
402 if (w)
403 w->on_disk = wp->on_disk;
404
405 if (!wp->on_disk)
406 continue;
407
408 if (!(flags & WP_FL_FORCE)
409 && wp->is_parttable
410 && !blkid_probe_is_wholedisk(pr)) {
411 warnx(_("%s: ignoring nested \"%s\" partition table "
412 "on non-whole disk device"), devname, wp->type);
413 need_force = 1;
414 continue;
415 }
416
417 if (zap) {
418 if (backup)
419 do_backup(wp, backup);
420 do_wipe_real(pr, devname, wp, flags);
421 if (wp->is_parttable)
422 reread = 1;
423 }
424 }
425
426 for (w = wp0; w != NULL; w = w->next) {
427 if (!w->on_disk && !(flags & WP_FL_QUIET))
428 warnx(_("%s: offset 0x%jx not found"), devname, w->offset);
429 }
430
431 if (need_force)
432 warnx(_("Use the --force option to force erase."));
433
434 fsync(blkid_probe_get_fd(pr));
435
436 if (reread && (mode & O_EXCL))
437 rereadpt(blkid_probe_get_fd(pr), devname);
438
439 close(blkid_probe_get_fd(pr));
440 blkid_free_probe(pr);
441 free_wipe(wp0);
442 free(backup);
443
444 return wp;
445 }
446
447
448 static void __attribute__((__noreturn__))
449 usage(FILE *out)
450 {
451 fputs(_("\nUsage:\n"), out);
452 fprintf(out,
453 _(" %s [options] <device>\n"), program_invocation_short_name);
454
455 fputs(_("\nOptions:\n"), out);
456 fputs(_(" -a, --all wipe all magic strings (BE CAREFUL!)\n"
457 " -b, --backup create a signature backup in $HOME\n"
458 " -f, --force force erasure\n"
459 " -h, --help show this help text\n"
460 " -n, --no-act do everything except the actual write() call\n"
461 " -o, --offset <num> offset to erase, in bytes\n"
462 " -p, --parsable print out in parsable instead of printable format\n"
463 " -q, --quiet suppress output messages\n"
464 " -t, --types <list> limit the set of filesystem, RAIDs or partition tables\n"
465 " -V, --version output version information and exit\n"), out);
466
467 fprintf(out, _("\nFor more information see wipefs(8).\n"));
468
469 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
470 }
471
472
473 int
474 main(int argc, char **argv)
475 {
476 struct wipe_desc *wp0 = NULL, *wp;
477 int c, has_offset = 0, flags = 0;
478 int mode = WP_MODE_PRETTY;
479
480 static const struct option longopts[] = {
481 { "all", 0, 0, 'a' },
482 { "backup", 0, 0, 'b' },
483 { "force", 0, 0, 'f' },
484 { "help", 0, 0, 'h' },
485 { "no-act", 0, 0, 'n' },
486 { "offset", 1, 0, 'o' },
487 { "parsable", 0, 0, 'p' },
488 { "quiet", 0, 0, 'q' },
489 { "types", 1, 0, 't' },
490 { "version", 0, 0, 'V' },
491 { NULL, 0, 0, 0 }
492 };
493
494 static const ul_excl_t excl[] = { /* rows and cols in in ASCII order */
495 { 'a','o' },
496 { 0 }
497 };
498 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
499
500 setlocale(LC_ALL, "");
501 bindtextdomain(PACKAGE, LOCALEDIR);
502 textdomain(PACKAGE);
503 atexit(close_stdout);
504
505 while ((c = getopt_long(argc, argv, "afhno:pqt:V", longopts, NULL)) != -1) {
506
507 err_exclusive_options(c, longopts, excl, excl_st);
508
509 switch(c) {
510 case 'a':
511 flags |= WP_FL_ALL;
512 break;
513 case 'b':
514 flags |= WP_FL_BACKUP;
515 break;
516 case 'f':
517 flags |= WP_FL_FORCE;
518 break;
519 case 'h':
520 usage(stdout);
521 break;
522 case 'n':
523 flags |= WP_FL_NOACT;
524 break;
525 case 'o':
526 wp0 = add_offset(wp0, strtosize_or_err(optarg,
527 _("invalid offset argument")), 1);
528 has_offset++;
529 break;
530 case 'p':
531 mode = WP_MODE_PARSABLE;
532 break;
533 case 'q':
534 flags |= WP_FL_QUIET;
535 break;
536 case 't':
537 type_pattern = optarg;
538 break;
539 case 'V':
540 printf(UTIL_LINUX_VERSION);
541 return EXIT_SUCCESS;
542 default:
543 usage(stderr);
544 break;
545 }
546 }
547
548 if (optind == argc)
549 usage(stderr);
550
551 if ((flags & WP_FL_BACKUP) && !((flags & WP_FL_ALL) || has_offset))
552 warnx(_("The --backup option is meaningless in this context"));
553
554 if (!(flags & WP_FL_ALL) && !has_offset) {
555 /*
556 * Print only
557 */
558 while (optind < argc) {
559 wp0 = read_offsets(NULL, argv[optind++]);
560 if (wp0)
561 print_all(wp0, mode);
562 free_wipe(wp0);
563 }
564 } else {
565 /*
566 * Erase
567 */
568 while (optind < argc) {
569 wp = clone_offset(wp0);
570 wp = do_wipe(wp, argv[optind++], flags);
571 free_wipe(wp);
572 }
573 }
574
575 return EXIT_SUCCESS;
576 }