xref: /netbsd-src/sbin/gpt/resize.c (revision 4391d5e9d4f291db41e3b3ba26a01b5e51364aae)
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.2 2013/11/22 03:50:05 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 (entry != map->map_index) {
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 #ifdef __FreeBSD__
171 	printf("%sp%u resized\n", device_name, i + 1);
172 #endif
173 #ifdef __NetBSD__
174 	printf("Partition resized, use:\n");
175 	printf("\tdkctl %s addwedge <wedgename> %" PRIu64 " %" PRIu64
176 	    " <type>\n", device_arg, map->map_start, newsize);
177 	printf("to create a wedge for it\n");
178 #endif
179 }
180 
181 int
182 cmd_resize(int argc, char *argv[])
183 {
184 	char *p;
185 	int ch, fd;
186 	int64_t human_num;
187 
188 	while ((ch = getopt(argc, argv, "a:i:s:")) != -1) {
189 		switch(ch) {
190 		case 'a':
191 			if (alignment > 0)
192 				usage_resize();
193 			if (dehumanize_number(optarg, &human_num) < 0)
194 				usage_resize();
195 			alignment = human_num;
196 			break;
197 		case 'i':
198 			if (entry > 0)
199 				usage_resize();
200 			entry = strtoul(optarg, &p, 10);
201 			if (*p != 0 || entry < 1)
202 				usage_resize();
203 			break;
204 		case 's':
205 			if (size > 0)
206 				usage_resize();
207 			size = strtoll(optarg, &p, 10);
208 			if (*p != 0 || size < 1)
209 				usage_resize();
210 			break;
211 		default:
212 			usage_resize();
213 		}
214 	}
215 
216 	if (argc == optind)
217 		usage_resize();
218 
219 	if (entry == 0)
220 		usage_resize();
221 
222 	while (optind < argc) {
223 		fd = gpt_open(argv[optind++]);
224 		if (fd == -1) {
225 			warn("unable to open device '%s'", device_name);
226 			continue;
227 		}
228 
229 		if (alignment % secsz != 0) {
230 			warnx("Alignment must be a multiple of sector size; ");
231 			warnx("the sector size for %s is %d bytes.",
232 			    device_name, secsz);
233 			continue;
234 		}
235 
236 		resize(fd);
237 
238 		gpt_close(fd);
239 	}
240 
241 	return 0;
242 }
243