destroy.c revision 1.6 1 /*-
2 * Copyright (c) 2002 Marcel Moolenaar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #if HAVE_NBTOOL_CONFIG_H
28 #include "nbtool_config.h"
29 #endif
30
31 #include <sys/cdefs.h>
32 #ifdef __FBSDID
33 __FBSDID("$FreeBSD: src/sbin/gpt/destroy.c,v 1.6 2005/08/31 01:47:19 marcel Exp $");
34 #endif
35 #ifdef __RCSID
36 __RCSID("$NetBSD: destroy.c,v 1.6 2014/09/29 21:04:34 christos Exp $");
37 #endif
38
39 #include <sys/types.h>
40
41 #include <err.h>
42 #include <stddef.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47
48 #include "map.h"
49 #include "gpt.h"
50
51 static int recoverable;
52
53 const char destroymsg[] = "destroy [-r] device ...";
54
55 __dead static void
56 usage_destroy(void)
57 {
58
59 fprintf(stderr,
60 "usage: %s %s\n", getprogname(), destroymsg);
61 exit(1);
62 }
63
64 static void
65 destroy(int fd)
66 {
67 map_t *pri_hdr, *sec_hdr;
68
69 pri_hdr = map_find(MAP_TYPE_PRI_GPT_HDR);
70 sec_hdr = map_find(MAP_TYPE_SEC_GPT_HDR);
71
72 if (pri_hdr == NULL && sec_hdr == NULL) {
73 warnx("%s: error: device doesn't contain a GPT", device_name);
74 return;
75 }
76
77 if (recoverable && sec_hdr == NULL) {
78 warnx("%s: error: recoverability not possible", device_name);
79 return;
80 }
81
82 if (pri_hdr != NULL) {
83 memset(pri_hdr->map_data, 0, secsz);
84 gpt_write(fd, pri_hdr);
85 }
86
87 if (!recoverable && sec_hdr != NULL) {
88 memset(sec_hdr->map_data, 0, secsz);
89 gpt_write(fd, sec_hdr);
90 }
91 }
92
93 int
94 cmd_destroy(int argc, char *argv[])
95 {
96 int ch, fd;
97
98 while ((ch = getopt(argc, argv, "r")) != -1) {
99 switch(ch) {
100 case 'r':
101 recoverable = 1;
102 break;
103 default:
104 usage_destroy();
105 }
106 }
107
108 if (argc == optind)
109 usage_destroy();
110
111 while (optind < argc) {
112 fd = gpt_open(argv[optind++]);
113 if (fd == -1) {
114 warn("unable to open device '%s'", device_name);
115 continue;
116 }
117
118 destroy(fd);
119
120 gpt_close(fd);
121 }
122
123 return (0);
124 }
125