resize.c revision 1.13 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.13 2015/11/30 19:59:34 christos 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 on %s: ", entry, device_arg);
170 printf("%" PRIu64 " %" PRIu64 "\n", map->map_start, newsize);
171 }
172
173 int
174 cmd_resize(int argc, char *argv[])
175 {
176 char *p;
177 int ch, fd;
178 int64_t human_num;
179
180 while ((ch = getopt(argc, argv, "a:i:s:")) != -1) {
181 switch(ch) {
182 case 'a':
183 if (alignment > 0)
184 usage_resize();
185 if (dehumanize_number(optarg, &human_num) < 0)
186 usage_resize();
187 alignment = human_num;
188 if (alignment < 1)
189 usage_resize();
190 break;
191 case 'i':
192 if (entry > 0)
193 usage_resize();
194 entry = strtoul(optarg, &p, 10);
195 if (*p != 0 || entry < 1)
196 usage_resize();
197 break;
198 case 's':
199 if (sectors > 0 || size > 0)
200 usage_resize();
201 sectors = strtoll(optarg, &p, 10);
202 if (sectors < 1)
203 usage_resize();
204 if (*p == '\0')
205 break;
206 if (*p == 's' || *p == 'S') {
207 if (*(p + 1) == '\0')
208 break;
209 else
210 usage_resize();
211 }
212 if (*p == 'b' || *p == 'B') {
213 if (*(p + 1) == '\0') {
214 size = sectors;
215 sectors = 0;
216 break;
217 } else
218 usage_resize();
219 }
220 if (dehumanize_number(optarg, &human_num) < 0)
221 usage_resize();
222 size = human_num;
223 sectors = 0;
224 break;
225 default:
226 usage_resize();
227 }
228 }
229
230 if (argc == optind)
231 usage_resize();
232
233 if (entry == 0)
234 usage_resize();
235
236 while (optind < argc) {
237 fd = gpt_open(argv[optind++], 0);
238 if (fd == -1)
239 continue;
240
241 if (alignment % secsz != 0) {
242 warnx("Alignment must be a multiple of sector size;");
243 warnx("the sector size for %s is %d bytes.",
244 device_name, secsz);
245 continue;
246 }
247
248 if (size % secsz != 0) {
249 warnx("Size in bytes must be a multiple of sector "
250 "size;");
251 warnx("the sector size for %s is %d bytes.",
252 device_name, secsz);
253 continue;
254 }
255 if (size > 0)
256 sectors = size / secsz;
257
258 resize(fd);
259
260 gpt_close(fd);
261 }
262
263 return 0;
264 }
265