]> git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/wipefs.c
misc-utils: use new xmalloc() wrapper
[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
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 <err.h>
31 #include <string.h>
32 #include <limits.h>
33
34 #include <blkid.h>
35
36 #include "nls.h"
37 #include "xalloc.h"
38 #include "strtosize.h"
39
40 struct wipe_desc {
41 loff_t offset; /* magic string offset */
42 size_t len; /* length of magic string */
43 unsigned char *magic; /* magic string */
44
45 int zap; /* zap this offset? */
46 char *usage; /* raid, filesystem, ... */
47 char *type; /* FS type */
48 char *label; /* FS label */
49 char *uuid; /* FS uuid */
50
51 struct wipe_desc *next;
52 };
53
54 #define WP_MODE_PRETTY 0 /* default */
55 #define WP_MODE_PARSABLE 1
56
57 static void
58 print_pretty(struct wipe_desc *wp, int line)
59 {
60 if (!line) {
61 printf("offset type\n");
62 printf("----------------------------------------------------------------\n");
63 }
64
65 printf("0x%-17jx %s [%s]", wp->offset, wp->type, wp->usage);
66
67 if (wp->label && *wp->label)
68 printf("\n%27s %s", "LABEL:", wp->label);
69 if (wp->uuid)
70 printf("\n%27s %s", "UUID: ", wp->uuid);
71 puts("\n");
72 }
73
74 static void
75 print_parsable(struct wipe_desc *wp, int line)
76 {
77 char enc[256];
78
79 if (!line)
80 printf("# offset,uuid,label,type\n");
81
82 printf("0x%jx,", wp->offset);
83
84 if (wp->uuid) {
85 blkid_encode_string(wp->uuid, enc, sizeof(enc));
86 printf("%s,", enc);
87 } else
88 fputc(',', stdout);
89
90 if (wp->label) {
91 blkid_encode_string(wp->label, enc, sizeof(enc));
92 printf("%s,", enc);
93 } else
94 fputc(',', stdout);
95
96 blkid_encode_string(wp->type, enc, sizeof(enc));
97 printf("%s\n", enc);
98 }
99
100 static void
101 print_all(struct wipe_desc *wp, int mode)
102 {
103 int n = 0;
104
105 while (wp) {
106 switch (mode) {
107 case WP_MODE_PRETTY:
108 print_pretty(wp, n++);
109 break;
110 case WP_MODE_PARSABLE:
111 print_parsable(wp, n++);
112 break;
113 }
114 wp = wp->next;
115 }
116 }
117
118 static struct wipe_desc *
119 add_offset(struct wipe_desc *wp0, loff_t offset, int zap)
120 {
121 struct wipe_desc *wp = wp0;
122
123 while (wp) {
124 if (wp->offset == offset)
125 return wp;
126 wp = wp->next;
127 }
128
129 wp = calloc(1, sizeof(struct wipe_desc));
130 if (!wp)
131 err(EXIT_FAILURE, _("calloc failed"));
132
133 wp->offset = offset;
134 wp->next = wp0;
135 wp->zap = zap;
136 return wp;
137 }
138
139 static inline char *
140 xstrdup(const char *s)
141 {
142 char *x = strdup(s);
143 if (!x)
144 err(EXIT_FAILURE, _("strdup failed"));
145 return x;
146 }
147
148 static struct wipe_desc *
149 get_offset_from_probe(struct wipe_desc *wp, blkid_probe pr, int zap)
150 {
151 const char *off, *type, *usage, *mag;
152 size_t len;
153
154 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) == 0 &&
155 blkid_probe_lookup_value(pr, "SBMAGIC_OFFSET", &off, NULL) == 0 &&
156 blkid_probe_lookup_value(pr, "SBMAGIC", &mag, &len) == 0 &&
157 blkid_probe_lookup_value(pr, "USAGE", &usage, NULL) == 0) {
158
159 loff_t offset = strtoll(off, NULL, 10);
160 const char *p;
161
162 wp = add_offset(wp, offset, zap);
163 if (!wp)
164 return NULL;
165
166 wp->usage = xstrdup(usage);
167 wp->type = xstrdup(type);
168
169 wp->magic = xmalloc(len);
170 memcpy(wp->magic, mag, len);
171 wp->len = len;
172
173 if (blkid_probe_lookup_value(pr, "LABEL", &p, NULL) == 0)
174 wp->label = xstrdup(p);
175
176 if (blkid_probe_lookup_value(pr, "UUID", &p, NULL) == 0)
177 wp->uuid = xstrdup(p);
178 }
179
180 return wp;
181 }
182
183 static struct wipe_desc *
184 read_offsets(struct wipe_desc *wp, const char *fname, int zap)
185 {
186 blkid_probe pr;
187 int rc;
188
189 if (!fname)
190 return NULL;
191
192 pr = blkid_new_probe_from_filename(fname);
193 if (!pr)
194 errx(EXIT_FAILURE, _("error: %s: probing initialization failed"), fname);
195
196 blkid_probe_enable_superblocks(pr, 0); /* enabled by default ;-( */
197
198 blkid_probe_enable_partitions(pr, 1);
199 rc = blkid_do_fullprobe(pr);
200 blkid_probe_enable_partitions(pr, 0);
201
202 if (rc == 0) {
203 const char *type = NULL;
204 blkid_probe_lookup_value(pr, "PTTYPE", &type, NULL);
205 warnx(_("WARNING: %s: appears to contain '%s' "
206 "partition table"), fname, type);
207 }
208
209 blkid_probe_enable_superblocks(pr, 1);
210 blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_MAGIC |
211 BLKID_SUBLKS_TYPE | BLKID_SUBLKS_USAGE |
212 BLKID_SUBLKS_LABEL | BLKID_SUBLKS_UUID);
213
214 while (blkid_do_probe(pr) == 0) {
215 wp = get_offset_from_probe(wp, pr, zap);
216 if (!wp)
217 break;
218 }
219
220 blkid_free_probe(pr);
221 return wp;
222 }
223
224 static int
225 write_all(int fd, const void *buf, size_t count)
226 {
227 while(count) {
228 ssize_t tmp;
229
230 errno = 0;
231 tmp = write(fd, buf, count);
232 if (tmp > 0) {
233 count -= tmp;
234 if (count)
235 buf += tmp;
236 } else if (errno != EINTR && errno != EAGAIN)
237 return -1;
238 }
239 return 0;
240 }
241
242 static int
243 do_wipe_offset(int fd, struct wipe_desc *wp, const char *fname, int noact)
244 {
245 char buf[BUFSIZ];
246 int i;
247 off_t l;
248 size_t len;
249
250 if (!wp->type) {
251 warnx(_("no magic string found at offset "
252 "0x%jx -- ignored"), wp->offset);
253 return 0;
254 }
255
256 l = lseek(fd, wp->offset, SEEK_SET);
257 if (l == (off_t) -1)
258 err(EXIT_FAILURE, _("%s: failed to seek to offset 0x%jx"),
259 fname, wp->offset);
260
261 len = wp->len > sizeof(buf) ? sizeof(buf) : wp->len;
262
263 memset(buf, 0, len);
264 if (noact == 0 && write_all(fd, buf, len))
265 err(EXIT_FAILURE, _("%s: write failed"), fname);
266
267 printf(_("%zd bytes ["), wp->len);
268
269 for (i = 0; i < len; i++) {
270 printf("%02x", wp->magic[i]);
271 if (i + 1 < len)
272 fputc(' ', stdout);
273 }
274
275 printf(_("] erased at offset 0x%jx (%s)\n"), wp->offset, wp->type);
276 return 0;
277 }
278
279 static int
280 do_wipe(struct wipe_desc *wp, const char *fname, int noact)
281 {
282 int fd;
283
284 fd = open(fname, O_WRONLY);
285 if (fd < 0)
286 err(EXIT_FAILURE, _("%s: open failed"), fname);
287
288 while (wp) {
289 if (wp->zap)
290 do_wipe_offset(fd, wp, fname, noact);
291 wp = wp->next;
292 }
293
294 close(fd);
295 return 0;
296 }
297
298 static void
299 free_wipe(struct wipe_desc *wp)
300 {
301 while (wp) {
302 struct wipe_desc *next = wp->next;
303
304 free(wp->usage);
305 free(wp->type);
306 free(wp->magic);
307 free(wp->label);
308 free(wp->uuid);
309 free(wp);
310
311 wp = next;
312 }
313 }
314
315 static loff_t
316 strtoll_offset(const char *str)
317 {
318 uintmax_t sz;
319
320 if (strtosize(str, &sz))
321 errx(EXIT_FAILURE, _("invalid offset value '%s' specified"), str);
322 return sz;
323 }
324
325
326 static void __attribute__((__noreturn__))
327 usage(FILE *out)
328 {
329 fprintf(out, _("Usage: %s [options] <device>\n\nOptions:\n"),
330 program_invocation_short_name);
331
332 fprintf(out, _(
333 " -a, --all wipe all magic strings (BE CAREFUL!)\n"
334 " -h, --help show this help text\n"
335 " -n, --no-act do everything except the actual write() call\n"
336 " -o, --offset <num> offset to erase, in bytes\n"
337 " -p, --parsable print out in parsable instead of printable format\n"));
338
339 fprintf(out, _("\nFor more information see wipefs(8).\n"));
340
341 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
342 }
343
344
345 int
346 main(int argc, char **argv)
347 {
348 struct wipe_desc *wp = NULL;
349 int c, all = 0, has_offset = 0, noact = 0, mode = 0;
350 const char *fname;
351
352 struct option longopts[] = {
353 { "all", 0, 0, 'a' },
354 { "help", 0, 0, 'h' },
355 { "no-act", 0, 0, 'n' },
356 { "offset", 1, 0, 'o' },
357 { "parsable", 0, 0, 'p' },
358 { NULL, 0, 0, 0 }
359 };
360
361 setlocale(LC_ALL, "");
362 bindtextdomain(PACKAGE, LOCALEDIR);
363 textdomain(PACKAGE);
364
365 while ((c = getopt_long(argc, argv, "ahno:p", longopts, NULL)) != -1) {
366 switch(c) {
367 case 'a':
368 all++;
369 break;
370 case 'h':
371 usage(stdout);
372 break;
373 case 'n':
374 noact++;
375 break;
376 case 'o':
377 wp = add_offset(wp, strtoll_offset(optarg), 1);
378 has_offset++;
379 break;
380 case 'p':
381 mode = WP_MODE_PARSABLE;
382 break;
383 default:
384 usage(stderr);
385 break;
386 }
387 }
388
389 if (wp && all)
390 errx(EXIT_FAILURE, _("--offset and --all are mutually exclusive"));
391 if (optind == argc)
392 usage(stderr);
393
394 fname = argv[optind++];
395
396 wp = read_offsets(wp, fname, all);
397
398 if (wp) {
399 if (has_offset || all)
400 do_wipe(wp, fname, noact);
401 else
402 print_all(wp, mode);
403
404 free_wipe(wp);
405 }
406 return EXIT_SUCCESS;
407 }