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