resize.c revision 1.8.6.1 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: resize.c,v 1.8.6.1 2015/06/02 19:49:38 snj Exp $");
37 #endif
38
39 #include <sys/types.h>
40
41 #include <err.h>
42 #include <stddef.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47
48 #include "map.h"
49 #include "gpt.h"
50
51 static off_t alignment, sectors, size;
52 static unsigned int entry;
53
54 const char resizemsg[] = "resize -i index [-a alignment] [-s size] device ...";
55
56 __dead static void
57 usage_resize(void)
58 {
59
60 fprintf(stderr,
61 "usage: %s %s\n", getprogname(), resizemsg);
62 exit(1);
63 }
64
65 static void
66 resize(int fd)
67 {
68 map_t *gpt, *tpg;
69 map_t *tbl, *lbt;
70 map_t *map;
71 struct gpt_hdr *hdr;
72 struct gpt_ent *ent;
73 unsigned int i;
74 off_t alignsecs, newsize;
75
76
77 gpt = map_find(MAP_TYPE_PRI_GPT_HDR);
78 ent = NULL;
79 if (gpt == NULL) {
80 warnx("%s: error: no primary GPT header; run create or recover",
81 device_name);
82 return;
83 }
84
85 tpg = map_find(MAP_TYPE_SEC_GPT_HDR);
86 if (tpg == NULL) {
87 warnx("%s: error: no secondary GPT header; run recover",
88 device_name);
89 return;
90 }
91
92 tbl = map_find(MAP_TYPE_PRI_GPT_TBL);
93 lbt = map_find(MAP_TYPE_SEC_GPT_TBL);
94 if (tbl == NULL || lbt == NULL) {
95 warnx("%s: error: run recover -- trust me", device_name);
96 return;
97 }
98
99 hdr = gpt->map_data;
100 if (entry > le32toh(hdr->hdr_entries)) {
101 warnx("%s: error: index %u out of range (%u max)", device_name,
102 entry, le32toh(hdr->hdr_entries));
103 return;
104 }
105
106 i = entry - 1;
107 ent = (void*)((char*)tbl->map_data + i *
108 le32toh(hdr->hdr_entsz));
109 if (gpt_uuid_is_nil(ent->ent_type)) {
110 warnx("%s: error: entry at index %u is unused",
111 device_name, entry);
112 return;
113 }
114
115 alignsecs = alignment / secsz;
116
117 for (map = map_first(); map != NULL; map = map->map_next) {
118 if (entry == map->map_index)
119 break;
120 }
121 if (map == NULL) {
122 warnx("%s: error: could not find map entry corresponding "
123 "to index", device_name);
124 return;
125 }
126
127 if (sectors > 0 && sectors == map->map_size)
128 if (alignment == 0 ||
129 (alignment > 0 && sectors % alignsecs == 0)) {
130 /* nothing to do */
131 warnx("%s: partition does not need resizing",
132 device_name);
133 return;
134 }
135
136 newsize = map_resize(map, sectors, alignsecs);
137 if (newsize == 0 && alignment > 0) {
138 warnx("%s: could not resize partition with alignment "
139 "constraint", device_name);
140 return;
141 } else if (newsize == 0) {
142 warnx("%s: could not resize partition", device_name);
143 return;
144 }
145
146 ent->ent_lba_end = htole64(map->map_start + newsize - 1LL);
147
148 hdr->hdr_crc_table = htole32(crc32(tbl->map_data,
149 le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
150 hdr->hdr_crc_self = 0;
151 hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
152
153 gpt_write(fd, gpt);
154 gpt_write(fd, tbl);
155
156 hdr = tpg->map_data;
157 ent = (void*)((char*)lbt->map_data + i * le32toh(hdr->hdr_entsz));
158
159 ent->ent_lba_end = htole64(map->map_start + newsize - 1LL);
160
161 hdr->hdr_crc_table = htole32(crc32(lbt->map_data,
162 le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
163 hdr->hdr_crc_self = 0;
164 hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
165
166 gpt_write(fd, lbt);
167 gpt_write(fd, tpg);
168
169 printf("Partition %d resized, use:\n", entry);
170 printf("\tdkctl %s addwedge <wedgename> %" PRIu64 " %" PRIu64
171 " <type>\n", device_arg, map->map_start, newsize);
172 printf("to create a wedge for it\n");
173 }
174
175 int
176 cmd_resize(int argc, char *argv[])
177 {
178 char *p;
179 int ch, fd;
180 int64_t human_num;
181
182 while ((ch = getopt(argc, argv, "a:i:s:")) != -1) {
183 switch(ch) {
184 case 'a':
185 if (alignment > 0)
186 usage_resize();
187 if (dehumanize_number(optarg, &human_num) < 0)
188 usage_resize();
189 alignment = human_num;
190 if (alignment < 1)
191 usage_resize();
192 break;
193 case 'i':
194 if (entry > 0)
195 usage_resize();
196 entry = strtoul(optarg, &p, 10);
197 if (*p != 0 || entry < 1)
198 usage_resize();
199 break;
200 case 's':
201 if (sectors > 0 || size > 0)
202 usage_resize();
203 sectors = strtoll(optarg, &p, 10);
204 if (sectors < 1)
205 usage_resize();
206 if (*p == '\0')
207 break;
208 if (*p == 's' || *p == 'S') {
209 if (*(p + 1) == '\0')
210 break;
211 else
212 usage_resize();
213 }
214 if (*p == 'b' || *p == 'B') {
215 if (*(p + 1) == '\0') {
216 size = sectors;
217 sectors = 0;
218 break;
219 } else
220 usage_resize();
221 }
222 if (dehumanize_number(optarg, &human_num) < 0)
223 usage_resize();
224 size = human_num;
225 sectors = 0;
226 break;
227 default:
228 usage_resize();
229 }
230 }
231
232 if (argc == optind)
233 usage_resize();
234
235 if (entry == 0)
236 usage_resize();
237
238 while (optind < argc) {
239 fd = gpt_open(argv[optind++]);
240 if (fd == -1) {
241 warn("unable to open device '%s'", device_name);
242 continue;
243 }
244
245 if (alignment % secsz != 0) {
246 warnx("Alignment must be a multiple of sector size;");
247 warnx("the sector size for %s is %d bytes.",
248 device_name, secsz);
249 continue;
250 }
251
252 if (size % secsz != 0) {
253 warnx("Size in bytes must be a multiple of sector "
254 "size;");
255 warnx("the sector size for %s is %d bytes.",
256 device_name, secsz);
257 continue;
258 }
259 if (size > 0)
260 sectors = size / secsz;
261
262 resize(fd);
263
264 gpt_close(fd);
265 }
266
267 return 0;
268 }
269