resizedisk.c revision 1.3 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/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $");
34 #endif
35 #ifdef __RCSID
36 __RCSID("$NetBSD: resizedisk.c,v 1.3 2014/09/29 20:28:57 christos Exp $");
37 #endif
38
39 #include <sys/bootblock.h>
40 #include <sys/types.h>
41
42 #include <err.h>
43 #include <stddef.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <inttypes.h>
49
50 #include "map.h"
51 #include "gpt.h"
52
53 static uint64_t sector, size;
54
55 const char resizediskmsg[] = "resizedisk [-s size] device ...";
56
57 __dead static void
58 usage_resizedisk(void)
59 {
60
61 fprintf(stderr,
62 "usage: %s %s\n", getprogname(), resizediskmsg);
63 exit(1);
64 }
65
66 /*
67 * relocate the secondary GPT based on the following criteria:
68 * - size not specified
69 * - disk has not changed size, do nothing
70 * - disk has grown, relocate secondary
71 * - disk has shrunk, create new secondary
72 * - size specified
73 * - size is larger then disk or same as current location, do nothing
74 * - relocate or create new secondary
75 * - when shrinking, verify that table fits
76 */
77 static void
78 resizedisk(int fd)
79 {
80 uuid_t uuid;
81 map_t *gpt, *tpg;
82 map_t *tbl, *lbt;
83 map_t *mbrmap;
84 struct gpt_hdr *hdr;
85 struct gpt_ent *ent;
86 struct mbr *mbr;
87 uint64_t last, oldloc, newloc, lastdata, gpt_size;
88 int i;
89
90 last = mediasz / secsz - 1;
91 lastdata = 0;
92 newloc = 0;
93
94 if (sector > last) {
95 warnx("%s: specified size is larger then the disk",
96 device_name);
97 return;
98 }
99
100 mbrmap = map_find(MAP_TYPE_PMBR);
101 if (mbrmap == NULL || mbrmap->map_start != 0) {
102 warnx("%s: error: no valid Protective MBR found", device_name);
103 return;
104 }
105 mbr = mbrmap->map_data;
106
107 gpt = map_find(MAP_TYPE_PRI_GPT_HDR);
108 ent = NULL;
109 if (gpt == NULL) {
110 warnx("%s: error: no primary GPT header; run create or recover",
111 device_name);
112 return;
113 }
114 hdr = gpt->map_data;
115 oldloc = le64toh(hdr->hdr_lba_alt);
116
117 tpg = map_find(MAP_TYPE_SEC_GPT_HDR);
118 if (tpg == NULL)
119 if (gpt_gpt(fd, oldloc, 1))
120 tpg = map_find(MAP_TYPE_SEC_GPT_HDR);
121
122 tbl = map_find(MAP_TYPE_PRI_GPT_TBL);
123 lbt = map_find(MAP_TYPE_SEC_GPT_TBL);
124 if (tbl == NULL) {
125 warnx("%s: error: run recover -- trust me", device_name);
126 return;
127 }
128
129 gpt_size = tbl->map_size;
130 if (sector == oldloc) {
131 warnx("%s: device is already the specified size", device_name);
132 return;
133 }
134 if (sector == 0 && last == oldloc) {
135 warnx("%s: device hasn't changed size", device_name);
136 return;
137 }
138
139 for (ent = tbl->map_data; ent <
140 (struct gpt_ent *)((char *)tbl->map_data +
141 le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)); ent++) {
142 le_uuid_dec(ent->ent_type, &uuid);
143 if (!uuid_is_nil(&uuid, NULL) &&
144 (le64toh(ent->ent_lba_end) > lastdata)) {
145 lastdata = le64toh(ent->ent_lba_end);
146 }
147 }
148 if (sector - gpt_size <= lastdata) {
149 warnx("%s: not enough space at %" PRIu64
150 " for secondary GPT table", device_name, sector);
151 return;
152 }
153 if (last - gpt_size <= lastdata) {
154 warnx("%s: not enough space for new secondary GPT table",
155 device_name);
156 return;
157 }
158
159 if (sector > oldloc)
160 newloc = sector;
161 if (sector > 0 && sector < oldloc && last >= oldloc)
162 newloc = sector;
163 if (sector == 0 && last > oldloc)
164 newloc = last;
165 if (newloc > 0) {
166 if (tpg == NULL) {
167 warnx("%s: error: no secondary GPT header; run recover",
168 device_name);
169 return;
170 }
171 if (lbt == NULL) {
172 warnx("%s: error: run recover -- trust me",
173 device_name);
174 return;
175 }
176 tpg->map_start = newloc;
177 lbt->map_start = newloc - gpt_size;
178 } else {
179 if (sector > 0)
180 newloc = sector;
181 else
182 newloc = last;
183 tpg = map_add(newloc, 1LL, MAP_TYPE_SEC_GPT_HDR,
184 calloc(1, secsz));
185 lbt = map_add(newloc - gpt_size, gpt_size, MAP_TYPE_SEC_GPT_TBL,
186 tbl->map_data);
187 memcpy(tpg->map_data, gpt->map_data, secsz);
188 }
189
190 hdr = gpt->map_data;
191 hdr->hdr_lba_alt = tpg->map_start;
192 hdr->hdr_crc_self = 0;
193 hdr->hdr_crc_self =
194 htole32(crc32(gpt->map_data, GPT_HDR_SIZE));
195 gpt_write(fd, gpt);
196
197 hdr = tpg->map_data;
198 hdr->hdr_lba_self = htole64(tpg->map_start);
199 hdr->hdr_lba_alt = htole64(gpt->map_start);
200 hdr->hdr_lba_table = htole64(lbt->map_start);
201 hdr->hdr_crc_self = 0;
202 hdr->hdr_crc_self =
203 htole32(crc32(tpg->map_data, GPT_HDR_SIZE));
204 gpt_write(fd, lbt);
205 gpt_write(fd, tpg);
206
207 for (i = 0; i < 4; i++)
208 if (mbr->mbr_part[0].part_typ == MBR_PTYPE_PMBR)
209 break;
210 if (i == 4) {
211 warnx("%s: no valid PMBR partition found", device_name);
212 return;
213 }
214 if (last > 0xffffffff) {
215 mbr->mbr_part[0].part_size_lo = htole16(0xffff);
216 mbr->mbr_part[0].part_size_hi = htole16(0xffff);
217 } else {
218 mbr->mbr_part[0].part_size_lo = htole16(last);
219 mbr->mbr_part[0].part_size_hi = htole16(last >> 16);
220 }
221 gpt_write(fd, mbrmap);
222
223 return;
224 }
225
226 int
227 cmd_resizedisk(int argc, char *argv[])
228 {
229 char *p;
230 int ch, fd;
231 int64_t human_num;
232
233 while ((ch = getopt(argc, argv, "s:")) != -1) {
234 switch(ch) {
235 case 's':
236 if (sector > 0 || size > 0)
237 usage_resizedisk();
238 sector = strtoll(optarg, &p, 10);
239 if (sector < 1)
240 usage_resizedisk();
241 if (*p == '\0')
242 break;
243 if (*p == 's' || *p == 'S') {
244 if (*(p + 1) == '\0')
245 break;
246 else
247 usage_resizedisk();
248 }
249 if (*p == 'b' || *p == 'B') {
250 if (*(p + 1) == '\0') {
251 size = sector;
252 sector = 0;
253 break;
254 } else
255 usage_resizedisk();
256 }
257 if (dehumanize_number(optarg, &human_num) < 0)
258 usage_resizedisk();
259 size = human_num;
260 sector = 0;
261 break;
262 default:
263 usage_resizedisk();
264 }
265 }
266
267 if (argc == optind)
268 usage_resizedisk();
269
270 while (optind < argc) {
271 fd = gpt_open(argv[optind++]);
272 if (fd == -1) {
273 warn("unable to open device '%s'", device_name);
274 continue;
275 }
276
277 if (size % secsz != 0) {
278 warnx("Size in bytes must be a multiple of sector "
279 "size;");
280 warnx("the sector size for %s is %d bytes.",
281 device_name, secsz);
282 continue;
283 }
284 if (size > 0)
285 sector = size / secsz - 1;
286
287 resizedisk(fd);
288
289 gpt_close(fd);
290 }
291
292 return 0;
293 }
294