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