xref: /netbsd-src/sbin/gpt/resize.c (revision 627f7eb200a4419d89b531d55fccd2ee3ffdcde0)
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.25 2020/05/24 14:42:44 jmcneill Exp $");
37 #endif
38 
39 #include <sys/types.h>
40 
41 #include <err.h>
42 #include <stdbool.h>
43 #include <stddef.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 
49 #include "map.h"
50 #include "gpt.h"
51 #include "gpt_private.h"
52 
53 static int cmd_resize(gpt_t, int, char *[]);
54 
55 static const char *resizehelp[] = {
56 	"[-i index | -b blocknr] [-a alignment] [-s size] [-q]",
57 };
58 
59 struct gpt_cmd c_resize = {
60 	"resize",
61 	cmd_resize,
62 	resizehelp, __arraycount(resizehelp),
63 	GPT_SYNC,
64 };
65 
66 #define usage() gpt_usage(NULL, &c_resize)
67 
68 static int
69 resize(gpt_t gpt, u_int entry, off_t alignment, off_t sectors, off_t size, bool quiet)
70 {
71 	map_t map;
72 	struct gpt_hdr *hdr;
73 	struct gpt_ent *ent;
74 	unsigned int i;
75 	off_t alignsecs, newsize, oldsize;
76 	uint64_t end;
77 
78 
79 	if ((hdr = gpt_hdr(gpt)) == NULL)
80 		return -1;
81 
82 	i = entry - 1;
83 	ent = gpt_ent_primary(gpt, i);
84 	if (gpt_uuid_is_nil(ent->ent_type)) {
85 		gpt_warnx(gpt, "Entry at index %u is unused", entry);
86 		return -1;
87 	}
88 
89 	alignsecs = alignment / gpt->secsz;
90 
91 	for (map = map_first(gpt); map != NULL; map = map->map_next) {
92 		if (entry == map->map_index)
93 			break;
94 	}
95 	if (map == NULL) {
96 		gpt_warnx(gpt, "Could not find map entry corresponding "
97 		    "to index");
98 		return -1;
99 	}
100 
101 	if (sectors > 0 && sectors == map->map_size)
102 		if (alignment == 0 ||
103 		    (alignment > 0 && sectors % alignsecs == 0)) {
104 			/* nothing to do */
105 			if (!quiet)
106 				gpt_warnx(gpt,
107 				    "partition does not need resizing");
108 			return 0;
109 		}
110 
111 	oldsize = map->map_size;
112 	newsize = map_resize(gpt, map, sectors, alignsecs);
113 	if (newsize == -1)
114 		return -1;
115 
116 	if (oldsize == newsize) {
117 		/* Nothing to do */
118 		if (!quiet)
119 			gpt_warnx(gpt,
120 			    "partition does not need resizing");
121 		return 0;
122 	}
123 
124 	end = htole64((uint64_t)(map->map_start + newsize - 1LL));
125 	ent->ent_lba_end = end;
126 
127 	if (gpt_write_primary(gpt) == -1)
128 		return -1;
129 
130 	ent = gpt_ent(gpt->gpt, gpt->lbt, i);
131 	ent->ent_lba_end = end;
132 
133 	if (gpt_write_backup(gpt) == -1)
134 		return -1;
135 
136 	gpt_msg(gpt, "Partition %d resized: %" PRIu64 " %" PRIu64, entry,
137 	    map->map_start, newsize);
138 
139 	return 0;
140 }
141 
142 static int
143 cmd_resize(gpt_t gpt, int argc, char *argv[])
144 {
145 	int ch;
146 	off_t alignment = 0, sectors, start = 0, size = 0;
147 	unsigned int entry = 0;
148 	map_t m;
149 	bool quiet = false;
150 
151 	while ((ch = getopt(argc, argv, GPT_AIS "b:q")) != -1) {
152 		if (ch == 'b')
153 			gpt_human_get(gpt, &start);
154 		else if (ch == 'q')
155 			quiet = true;
156 		else if (gpt_add_ais(gpt, &alignment, &entry, &size, ch) == -1)
157 			return usage();
158 	}
159 
160 	if (argc != optind)
161 		return usage();
162 
163 	if (start > 0) {
164 		for (m = map_first(gpt); m != NULL; m = m->map_next) {
165 			if (m->map_type != MAP_TYPE_GPT_PART ||
166 			    m->map_index < 1)
167 				continue;
168 			if (start != m->map_start)
169 				continue;
170 			entry = m->map_index;
171 			break;
172 		}
173 	}
174 
175 	if ((sectors = gpt_check_ais(gpt, alignment, entry, size)) == -1)
176 		return -1;
177 
178 	return resize(gpt, entry, alignment, sectors, size, quiet);
179 }
180