resize.c revision 1.15 1 1.1 jnemeth /*-
2 1.1 jnemeth * Copyright (c) 2002 Marcel Moolenaar
3 1.1 jnemeth * All rights reserved.
4 1.1 jnemeth *
5 1.1 jnemeth * Redistribution and use in source and binary forms, with or without
6 1.1 jnemeth * modification, are permitted provided that the following conditions
7 1.1 jnemeth * are met:
8 1.1 jnemeth *
9 1.1 jnemeth * 1. Redistributions of source code must retain the above copyright
10 1.1 jnemeth * notice, this list of conditions and the following disclaimer.
11 1.1 jnemeth * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 jnemeth * notice, this list of conditions and the following disclaimer in the
13 1.1 jnemeth * documentation and/or other materials provided with the distribution.
14 1.1 jnemeth *
15 1.1 jnemeth * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 jnemeth * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 1.1 jnemeth * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 1.1 jnemeth * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 1.1 jnemeth * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 1.1 jnemeth * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 1.1 jnemeth * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 1.1 jnemeth * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 1.1 jnemeth * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 1.1 jnemeth * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 1.1 jnemeth */
26 1.1 jnemeth
27 1.9 christos #if HAVE_NBTOOL_CONFIG_H
28 1.9 christos #include "nbtool_config.h"
29 1.9 christos #endif
30 1.9 christos
31 1.1 jnemeth #include <sys/cdefs.h>
32 1.1 jnemeth #ifdef __FBSDID
33 1.1 jnemeth __FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $");
34 1.1 jnemeth #endif
35 1.1 jnemeth #ifdef __RCSID
36 1.15 christos __RCSID("$NetBSD: resize.c,v 1.15 2015/12/01 09:05:33 christos Exp $");
37 1.1 jnemeth #endif
38 1.1 jnemeth
39 1.1 jnemeth #include <sys/types.h>
40 1.1 jnemeth
41 1.1 jnemeth #include <err.h>
42 1.1 jnemeth #include <stddef.h>
43 1.1 jnemeth #include <stdio.h>
44 1.1 jnemeth #include <stdlib.h>
45 1.1 jnemeth #include <string.h>
46 1.1 jnemeth #include <unistd.h>
47 1.1 jnemeth
48 1.1 jnemeth #include "map.h"
49 1.1 jnemeth #include "gpt.h"
50 1.15 christos #include "gpt_private.h"
51 1.1 jnemeth
52 1.7 jnemeth static off_t alignment, sectors, size;
53 1.1 jnemeth static unsigned int entry;
54 1.1 jnemeth
55 1.15 christos const char resizemsg[] = "resize -i index [-a alignment] [-s size]";
56 1.1 jnemeth
57 1.15 christos static int
58 1.1 jnemeth usage_resize(void)
59 1.1 jnemeth {
60 1.1 jnemeth
61 1.1 jnemeth fprintf(stderr,
62 1.1 jnemeth "usage: %s %s\n", getprogname(), resizemsg);
63 1.15 christos return -1;
64 1.1 jnemeth }
65 1.1 jnemeth
66 1.15 christos static int
67 1.15 christos resize(gpt_t gpt)
68 1.1 jnemeth {
69 1.15 christos map_t map;
70 1.1 jnemeth struct gpt_hdr *hdr;
71 1.1 jnemeth struct gpt_ent *ent;
72 1.1 jnemeth unsigned int i;
73 1.1 jnemeth off_t alignsecs, newsize;
74 1.1 jnemeth
75 1.1 jnemeth
76 1.15 christos if ((hdr = gpt_hdr(gpt)) == NULL)
77 1.15 christos return -1;
78 1.15 christos
79 1.1 jnemeth ent = NULL;
80 1.1 jnemeth
81 1.1 jnemeth i = entry - 1;
82 1.15 christos ent = gpt_ent_primary(gpt, i);
83 1.11 christos if (gpt_uuid_is_nil(ent->ent_type)) {
84 1.15 christos gpt_warnx(gpt, "Entry at index %u is unused", entry);
85 1.15 christos return -1;
86 1.1 jnemeth }
87 1.1 jnemeth
88 1.15 christos alignsecs = alignment / gpt->secsz;
89 1.1 jnemeth
90 1.15 christos for (map = map_first(gpt); map != NULL; map = map->map_next) {
91 1.1 jnemeth if (entry == map->map_index)
92 1.1 jnemeth break;
93 1.1 jnemeth }
94 1.4 christos if (map == NULL) {
95 1.15 christos gpt_warnx(gpt, "Could not find map entry corresponding "
96 1.15 christos "to index");
97 1.15 christos return -1;
98 1.1 jnemeth }
99 1.1 jnemeth
100 1.7 jnemeth if (sectors > 0 && sectors == map->map_size)
101 1.1 jnemeth if (alignment == 0 ||
102 1.7 jnemeth (alignment > 0 && sectors % alignsecs == 0)) {
103 1.1 jnemeth /* nothing to do */
104 1.15 christos gpt_warnx(gpt, "partition does not need resizing");
105 1.15 christos return 0;
106 1.1 jnemeth }
107 1.1 jnemeth
108 1.15 christos newsize = map_resize(gpt, map, sectors, alignsecs);
109 1.6 jnemeth if (newsize == 0 && alignment > 0) {
110 1.15 christos gpt_warnx(gpt, "Could not resize partition with alignment "
111 1.15 christos "constraint");
112 1.15 christos return -1;
113 1.6 jnemeth } else if (newsize == 0) {
114 1.15 christos gpt_warnx(gpt, "Could not resize partition");
115 1.15 christos return -1;
116 1.1 jnemeth }
117 1.1 jnemeth
118 1.1 jnemeth ent->ent_lba_end = htole64(map->map_start + newsize - 1LL);
119 1.1 jnemeth
120 1.15 christos if (gpt_write_primary(gpt) == -1)
121 1.15 christos return -1;
122 1.1 jnemeth
123 1.15 christos ent = gpt_ent(gpt->gpt, gpt->lbt, i);
124 1.1 jnemeth ent->ent_lba_end = htole64(map->map_start + newsize - 1LL);
125 1.1 jnemeth
126 1.15 christos if (gpt_write_backup(gpt) == -1)
127 1.15 christos return -1;
128 1.1 jnemeth
129 1.15 christos gpt_msg(gpt, "Partition %d resized: %" PRIu64 " %" PRIu64 "\n", entry,
130 1.15 christos map->map_start, newsize);
131 1.1 jnemeth
132 1.15 christos return 0;
133 1.1 jnemeth }
134 1.1 jnemeth
135 1.1 jnemeth int
136 1.15 christos cmd_resize(gpt_t gpt, int argc, char *argv[])
137 1.1 jnemeth {
138 1.1 jnemeth char *p;
139 1.15 christos int ch;
140 1.1 jnemeth int64_t human_num;
141 1.1 jnemeth
142 1.1 jnemeth while ((ch = getopt(argc, argv, "a:i:s:")) != -1) {
143 1.1 jnemeth switch(ch) {
144 1.1 jnemeth case 'a':
145 1.1 jnemeth if (alignment > 0)
146 1.15 christos return usage_resize();
147 1.1 jnemeth if (dehumanize_number(optarg, &human_num) < 0)
148 1.15 christos return usage_resize();
149 1.1 jnemeth alignment = human_num;
150 1.5 jnemeth if (alignment < 1)
151 1.15 christos return usage_resize();
152 1.1 jnemeth break;
153 1.1 jnemeth case 'i':
154 1.1 jnemeth if (entry > 0)
155 1.15 christos return usage_resize();
156 1.1 jnemeth entry = strtoul(optarg, &p, 10);
157 1.1 jnemeth if (*p != 0 || entry < 1)
158 1.15 christos return usage_resize();
159 1.1 jnemeth break;
160 1.1 jnemeth case 's':
161 1.7 jnemeth if (sectors > 0 || size > 0)
162 1.15 christos return usage_resize();
163 1.7 jnemeth sectors = strtoll(optarg, &p, 10);
164 1.7 jnemeth if (sectors < 1)
165 1.15 christos return usage_resize();
166 1.7 jnemeth if (*p == '\0')
167 1.7 jnemeth break;
168 1.7 jnemeth if (*p == 's' || *p == 'S') {
169 1.7 jnemeth if (*(p + 1) == '\0')
170 1.7 jnemeth break;
171 1.7 jnemeth else
172 1.15 christos return usage_resize();
173 1.7 jnemeth }
174 1.7 jnemeth if (*p == 'b' || *p == 'B') {
175 1.7 jnemeth if (*(p + 1) == '\0') {
176 1.7 jnemeth size = sectors;
177 1.7 jnemeth sectors = 0;
178 1.7 jnemeth break;
179 1.7 jnemeth } else
180 1.15 christos return usage_resize();
181 1.7 jnemeth }
182 1.7 jnemeth if (dehumanize_number(optarg, &human_num) < 0)
183 1.15 christos return usage_resize();
184 1.7 jnemeth size = human_num;
185 1.7 jnemeth sectors = 0;
186 1.1 jnemeth break;
187 1.1 jnemeth default:
188 1.15 christos return usage_resize();
189 1.1 jnemeth }
190 1.1 jnemeth }
191 1.1 jnemeth
192 1.15 christos if (argc != optind)
193 1.15 christos return usage_resize();
194 1.1 jnemeth
195 1.1 jnemeth if (entry == 0)
196 1.15 christos return usage_resize();
197 1.7 jnemeth
198 1.15 christos if ((sectors = gpt_check(gpt, alignment, size)) == -1)
199 1.15 christos return -1;
200 1.1 jnemeth
201 1.15 christos return resize(gpt);
202 1.1 jnemeth }
203