remove.c revision 1.2 1 /*-
2 * Copyright (c) 2004 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 #include <sys/cdefs.h>
28 #ifdef __FBSDID
29 __FBSDID("$FreeBSD: src/sbin/gpt/remove.c,v 1.10 2006/10/04 18:20:25 marcel Exp $");
30 #endif
31 #ifdef __RCSID
32 __RCSID("$NetBSD: remove.c,v 1.2 2006/10/15 22:36:29 christos Exp $");
33 #endif
34
35 #include <sys/types.h>
36
37 #include <err.h>
38 #include <stddef.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43
44 #include "map.h"
45 #include "gpt.h"
46
47 static int all;
48 static uuid_t type;
49 static off_t block, size;
50 static unsigned int entry;
51
52 static void
53 usage_remove(void)
54 {
55
56 fprintf(stderr,
57 "usage: %s -a device ...\n"
58 " %s [-b lba] [-i index] [-s lba] [-t uuid] device ...\n",
59 getprogname(), getprogname());
60 exit(1);
61 }
62
63 static void
64 rem(int fd)
65 {
66 uuid_t uuid;
67 map_t *gpt, *tpg;
68 map_t *tbl, *lbt;
69 map_t *m;
70 struct gpt_hdr *hdr;
71 struct gpt_ent *ent;
72 unsigned int i;
73
74 gpt = map_find(MAP_TYPE_PRI_GPT_HDR);
75 if (gpt == NULL) {
76 warnx("%s: error: no primary GPT header; run create or recover",
77 device_name);
78 return;
79 }
80
81 tpg = map_find(MAP_TYPE_SEC_GPT_HDR);
82 if (tpg == NULL) {
83 warnx("%s: error: no secondary GPT header; run recover",
84 device_name);
85 return;
86 }
87
88 tbl = map_find(MAP_TYPE_PRI_GPT_TBL);
89 lbt = map_find(MAP_TYPE_SEC_GPT_TBL);
90 if (tbl == NULL || lbt == NULL) {
91 warnx("%s: error: run recover -- trust me", device_name);
92 return;
93 }
94
95 /* Remove all matching entries in the map. */
96 for (m = map_first(); m != NULL; m = m->map_next) {
97 if (m->map_type != MAP_TYPE_GPT_PART || m->map_index < 1)
98 continue;
99 if (entry > 0 && entry != m->map_index)
100 continue;
101 if (block > 0 && block != m->map_start)
102 continue;
103 if (size > 0 && size != m->map_size)
104 continue;
105
106 i = m->map_index - 1;
107
108 hdr = gpt->map_data;
109 ent = (void*)((char*)tbl->map_data + i *
110 le32toh(hdr->hdr_entsz));
111 le_uuid_dec(&ent->ent_type, &uuid);
112 if (!uuid_is_nil(&type, NULL) &&
113 !uuid_equal(&type, &uuid, NULL))
114 continue;
115
116 /* Remove the primary entry by clearing the partition type. */
117 uuid_create_nil((uuid_t *)&ent->ent_type, NULL);
118
119 hdr->hdr_crc_table = htole32(crc32(tbl->map_data,
120 le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
121 hdr->hdr_crc_self = 0;
122 hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
123
124 gpt_write(fd, gpt);
125 gpt_write(fd, tbl);
126
127 hdr = tpg->map_data;
128 ent = (void*)((char*)lbt->map_data + i *
129 le32toh(hdr->hdr_entsz));
130
131 /* Remove the secundary entry. */
132 uuid_create_nil((uuid_t *)&ent->ent_type, NULL);
133
134 hdr->hdr_crc_table = htole32(crc32(lbt->map_data,
135 le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
136 hdr->hdr_crc_self = 0;
137 hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
138
139 gpt_write(fd, lbt);
140 gpt_write(fd, tpg);
141 #ifdef __FreeBSD__
142 printf("%sp%u removed\n", device_name, m->map_index);
143 #endif
144 #ifdef __NetBSD__
145 printf("partition %d removed from %s\n", m->map_index,
146 device_name);
147 #endif
148 }
149 }
150
151 int
152 cmd_remove(int argc, char *argv[])
153 {
154 char *p;
155 int ch, fd;
156
157 /* Get the remove options */
158 while ((ch = getopt(argc, argv, "ab:i:s:t:")) != -1) {
159 switch(ch) {
160 case 'a':
161 if (all > 0)
162 usage_remove();
163 all = 1;
164 break;
165 case 'b':
166 if (block > 0)
167 usage_remove();
168 block = strtoll(optarg, &p, 10);
169 if (*p != 0 || block < 1)
170 usage_remove();
171 break;
172 case 'i':
173 if (entry > 0)
174 usage_remove();
175 entry = strtol(optarg, &p, 10);
176 if (*p != 0 || entry < 1)
177 usage_remove();
178 break;
179 case 's':
180 if (size > 0)
181 usage_remove();
182 size = strtoll(optarg, &p, 10);
183 if (*p != 0 || size < 1)
184 usage_remove();
185 break;
186 case 't':
187 if (!uuid_is_nil(&type, NULL))
188 usage_remove();
189 if (parse_uuid(optarg, &type) != 0)
190 usage_remove();
191 break;
192 default:
193 usage_remove();
194 }
195 }
196
197 if (!all ^
198 (block > 0 || entry > 0 || size > 0 || !uuid_is_nil(&type, NULL)))
199 usage_remove();
200
201 if (argc == optind)
202 usage_remove();
203
204 while (optind < argc) {
205 fd = gpt_open(argv[optind++]);
206 if (fd == -1) {
207 warn("unable to open device '%s'", device_name);
208 continue;
209 }
210
211 rem(fd);
212
213 gpt_close(fd);
214 }
215
216 return (0);
217 }
218