resize.c revision 1.5 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 #include <sys/cdefs.h>
28 #ifdef __FBSDID
29 __FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $");
30 #endif
31 #ifdef __RCSID
32 __RCSID("$NetBSD: resize.c,v 1.5 2013/11/28 01:37:14 jnemeth Exp $");
33 #endif
34
35 #include <sys/types.h>
36
37 #include <err.h>
38 #include <stddef.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <inttypes.h>
44
45 #include "map.h"
46 #include "gpt.h"
47
48 static off_t alignment, size;
49 static unsigned int entry;
50
51 const char resizemsg[] = "resize -i index [-a alignment] [-s sectors] "
52 "device ...";
53
54 __dead static void
55 usage_resize(void)
56 {
57
58 fprintf(stderr,
59 "usage: %s %s\n", getprogname(), resizemsg);
60 exit(1);
61 }
62
63 static void
64 resize(int fd)
65 {
66 uuid_t uuid;
67 map_t *gpt, *tpg;
68 map_t *tbl, *lbt;
69 map_t *map;
70 struct gpt_hdr *hdr;
71 struct gpt_ent *ent;
72 unsigned int i;
73 off_t alignsecs, newsize;
74
75
76 gpt = map_find(MAP_TYPE_PRI_GPT_HDR);
77 ent = NULL;
78 if (gpt == NULL) {
79 warnx("%s: error: no primary GPT header; run create or recover",
80 device_name);
81 return;
82 }
83
84 tpg = map_find(MAP_TYPE_SEC_GPT_HDR);
85 if (tpg == NULL) {
86 warnx("%s: error: no secondary GPT header; run recover",
87 device_name);
88 return;
89 }
90
91 tbl = map_find(MAP_TYPE_PRI_GPT_TBL);
92 lbt = map_find(MAP_TYPE_SEC_GPT_TBL);
93 if (tbl == NULL || lbt == NULL) {
94 warnx("%s: error: run recover -- trust me", device_name);
95 return;
96 }
97
98 hdr = gpt->map_data;
99 if (entry > le32toh(hdr->hdr_entries)) {
100 warnx("%s: error: index %u out of range (%u max)", device_name,
101 entry, le32toh(hdr->hdr_entries));
102 return;
103 }
104
105 i = entry - 1;
106 ent = (void*)((char*)tbl->map_data + i *
107 le32toh(hdr->hdr_entsz));
108 le_uuid_dec(ent->ent_type, &uuid);
109 if (uuid_is_nil(&uuid, NULL)) {
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 (size > 0 && size == map->map_size)
128 if (alignment == 0 ||
129 (alignment > 0 && size % 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, size, alignsecs);
137 if (newsize == 0 && alignment > 0 && size > 0) {
138 warnx("%s: could not resize partition with alignment "
139 "constraint", device_name);
140 newsize = map_resize(map, size, 0);
141 }
142 if (newsize == 0) {
143 warnx("%s: could not resize partition", device_name);
144 return;
145 }
146
147 ent->ent_lba_end = htole64(map->map_start + newsize - 1LL);
148
149 hdr->hdr_crc_table = htole32(crc32(tbl->map_data,
150 le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
151 hdr->hdr_crc_self = 0;
152 hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
153
154 gpt_write(fd, gpt);
155 gpt_write(fd, tbl);
156
157 hdr = tpg->map_data;
158 ent = (void*)((char*)lbt->map_data + i * le32toh(hdr->hdr_entsz));
159
160 ent->ent_lba_end = htole64(map->map_start + newsize - 1LL);
161
162 hdr->hdr_crc_table = htole32(crc32(lbt->map_data,
163 le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
164 hdr->hdr_crc_self = 0;
165 hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
166
167 gpt_write(fd, lbt);
168 gpt_write(fd, tpg);
169
170 printf("Partition resized, use:\n");
171 printf("\tdkctl %s addwedge <wedgename> %" PRIu64 " %" PRIu64
172 " <type>\n", device_arg, map->map_start, newsize);
173 printf("to create a wedge for it\n");
174 }
175
176 int
177 cmd_resize(int argc, char *argv[])
178 {
179 char *p;
180 int ch, fd;
181 int64_t human_num;
182
183 while ((ch = getopt(argc, argv, "a:i:s:")) != -1) {
184 switch(ch) {
185 case 'a':
186 if (alignment > 0)
187 usage_resize();
188 if (dehumanize_number(optarg, &human_num) < 0)
189 usage_resize();
190 alignment = human_num;
191 if (alignment < 1)
192 usage_resize();
193 break;
194 case 'i':
195 if (entry > 0)
196 usage_resize();
197 entry = strtoul(optarg, &p, 10);
198 if (*p != 0 || entry < 1)
199 usage_resize();
200 break;
201 case 's':
202 if (size > 0)
203 usage_resize();
204 size = strtoll(optarg, &p, 10);
205 if (*p != 0 || size < 1)
206 usage_resize();
207 break;
208 default:
209 usage_resize();
210 }
211 }
212
213 if (argc == optind)
214 usage_resize();
215
216 if (entry == 0)
217 usage_resize();
218
219 while (optind < argc) {
220 fd = gpt_open(argv[optind++]);
221 if (fd == -1) {
222 warn("unable to open device '%s'", device_name);
223 continue;
224 }
225
226 if (alignment % secsz != 0) {
227 warnx("Alignment must be a multiple of sector size; ");
228 warnx("the sector size for %s is %d bytes.",
229 device_name, secsz);
230 continue;
231 }
232
233 resize(fd);
234
235 gpt_close(fd);
236 }
237
238 return 0;
239 }
240