resizedisk.c revision 1.15 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.15 2015/12/03 21:30:54 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
49 #include "map.h"
50 #include "gpt.h"
51 #include "gpt_private.h"
52
53
54 static int cmd_resizedisk(gpt_t, int, char *[]);
55
56 static const char *resizediskhelp[] = {
57 "[-s size]",
58 };
59
60 struct gpt_cmd c_resizedisk = {
61 "resize",
62 cmd_resizedisk,
63 resizediskhelp, __arraycount(resizediskhelp),
64 0,
65 };
66
67 #define usage() gpt_usage(NULL, &c_resizedisk)
68
69 /*
70 * relocate the secondary GPT based on the following criteria:
71 * - size not specified
72 * - disk has not changed size, do nothing
73 * - disk has grown, relocate secondary
74 * - disk has shrunk, create new secondary
75 * - size specified
76 * - size is larger then disk or same as current location, do nothing
77 * - relocate or create new secondary
78 * - when shrinking, verify that table fits
79 */
80 static int
81 resizedisk(gpt_t gpt, off_t sector, off_t size)
82 {
83 map_t mbrmap;
84 struct gpt_hdr *hdr;
85 struct gpt_ent *ent;
86 struct mbr *mbr;
87 off_t last, oldloc, newloc, lastdata, gpt_size;
88 int i;
89 void *p;
90
91 last = gpt->mediasz / gpt->secsz - 1;
92 lastdata = 0;
93 newloc = 0;
94
95 if (sector > last) {
96 gpt_warnx(gpt, "specified size is larger then the disk");
97 return -1;
98 }
99
100 mbrmap = map_find(gpt, MAP_TYPE_PMBR);
101 if (mbrmap == NULL || mbrmap->map_start != 0) {
102 gpt_warnx(gpt, "No valid PMBR found");
103 return -1;
104 }
105 mbr = mbrmap->map_data;
106
107 gpt->gpt = map_find(gpt, MAP_TYPE_PRI_GPT_HDR);
108 ent = NULL;
109 if (gpt == NULL) {
110 gpt_warnx(gpt, "No primary GPT header; run create or recover");
111 return -1;
112 }
113 hdr = gpt->gpt->map_data;
114 oldloc = (off_t)le64toh((uint64_t)hdr->hdr_lba_alt);
115
116 gpt->tpg = map_find(gpt, MAP_TYPE_SEC_GPT_HDR);
117 if (gpt->tpg == NULL)
118 if (gpt_gpt(gpt, oldloc, 1))
119 gpt->tpg = map_find(gpt, MAP_TYPE_SEC_GPT_HDR);
120
121 gpt->tbl = map_find(gpt, MAP_TYPE_PRI_GPT_TBL);
122 if (gpt->tbl == NULL) {
123 gpt_warnx(gpt, "Run recover");
124 return -1;
125 }
126
127 gpt_size = gpt->tbl->map_size;
128 if (sector == oldloc) {
129 gpt_warnx(gpt, "Device is already the specified size");
130 return 0;
131 }
132 if (sector == 0 && last == oldloc) {
133 gpt_warnx(gpt, "Device hasn't changed size");
134 return 0;
135 }
136
137 for (ent = gpt->tbl->map_data; ent <
138 (struct gpt_ent *)((char *)gpt->tbl->map_data +
139 le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)); ent++) {
140 if (!gpt_uuid_is_nil(ent->ent_type) &&
141 ((off_t)le64toh(ent->ent_lba_end) > lastdata)) {
142 lastdata = (off_t)le64toh((uint64_t)ent->ent_lba_end);
143 }
144 }
145 if (sector - gpt_size <= lastdata) {
146 gpt_warnx(gpt, "Not enough space at %" PRIu64
147 " for secondary GPT table", sector);
148 return -1;
149 }
150 if (last - gpt_size <= lastdata) {
151 gpt_warnx(gpt, "Not enough space for new secondary GPT table");
152 return -1;
153 }
154
155 if (sector > oldloc)
156 newloc = sector;
157 if (sector > 0 && sector < oldloc && last >= oldloc)
158 newloc = sector;
159 if (sector == 0 && last > oldloc)
160 newloc = last;
161 if (newloc > 0) {
162 if (gpt->tpg == NULL) {
163 gpt_warnx(gpt, "No secondary GPT header; run recover");
164 return -1;
165 }
166 if (gpt->lbt == NULL) {
167 gpt_warnx(gpt, "Run recover");
168 return -1;
169 }
170 gpt->tpg->map_start = newloc;
171 gpt->lbt->map_start = newloc - gpt_size;
172 } else {
173 if (sector > 0)
174 newloc = sector;
175 else
176 newloc = last;
177
178 if ((p = calloc(1, gpt->secsz)) == NULL) {
179 gpt_warn(gpt, "Error allocating secondary GPT header");
180 return -1;
181 }
182
183 gpt->tpg = map_add(gpt, newloc, 1LL, MAP_TYPE_SEC_GPT_HDR, p, 1);
184 if (gpt->tpg == NULL) {
185 gpt_warn(gpt, "Error adding secondary GPT header");
186 return -1;
187 }
188
189 gpt->lbt = map_add(gpt, newloc - gpt_size, gpt_size,
190 MAP_TYPE_SEC_GPT_TBL, gpt->tbl->map_data, 0);
191 if (gpt->lbt == NULL) {
192 gpt_warn(gpt, "Error adding secondary GPT table");
193 return -1;
194 }
195 memcpy(gpt->tpg->map_data, gpt->gpt->map_data, gpt->secsz);
196 }
197
198 hdr = gpt->gpt->map_data;
199 hdr->hdr_lba_alt = (uint64_t)gpt->tpg->map_start;
200 hdr->hdr_crc_self = 0;
201 hdr->hdr_lba_end = htole64((uint64_t)(gpt->lbt->map_start - 1));
202 hdr->hdr_crc_self =
203 htole32(crc32(gpt->gpt->map_data, GPT_HDR_SIZE));
204 gpt_write(gpt, gpt->gpt);
205
206 hdr = gpt->tpg->map_data;
207 hdr->hdr_lba_self = htole64((uint64_t)gpt->tpg->map_start);
208 hdr->hdr_lba_alt = htole64((uint64_t)gpt->gpt->map_start);
209 hdr->hdr_lba_end = htole64((uint64_t)(gpt->lbt->map_start - 1));
210 hdr->hdr_lba_table = htole64((uint64_t)gpt->lbt->map_start);
211
212 if (gpt_write_backup(gpt) == -1)
213 return -1;
214
215 for (i = 0; i < 4; i++)
216 if (mbr->mbr_part[0].part_typ == MBR_PTYPE_PMBR)
217 break;
218 if (i == 4) {
219 gpt_warnx(gpt, "No valid PMBR partition found");
220 return -1;
221 }
222 if (last > 0xffffffff) {
223 mbr->mbr_part[0].part_size_lo = htole16(0xffff);
224 mbr->mbr_part[0].part_size_hi = htole16(0xffff);
225 } else {
226 mbr->mbr_part[0].part_size_lo = htole16((uint16_t)last);
227 mbr->mbr_part[0].part_size_hi = htole16((uint16_t)(last >> 16));
228 }
229 if (gpt_write(gpt, mbrmap) == -1) {
230 gpt_warnx(gpt, "Error writing PMBR");
231 return -1;
232 }
233
234 return 0;
235 }
236
237 static int
238 cmd_resizedisk(gpt_t gpt, int argc, char *argv[])
239 {
240 int ch;
241 off_t sector, size = 0;
242
243 while ((ch = getopt(argc, argv, "s:")) != -1) {
244 switch(ch) {
245 case 's':
246 if (gpt_add_ais(gpt, NULL, NULL, &size, ch) == -1)
247 return -1;
248 break;
249 default:
250 return usage();
251 }
252 }
253
254 if (argc != optind)
255 return usage();
256
257 if ((sector = gpt_check_ais(gpt, 0, (u_int)~0, size)) == -1)
258 return -1;
259
260 return resizedisk(gpt, sector, size);
261 }
262