xref: /netbsd-src/sbin/gpt/resize.c (revision 084befafcbe60823bb62e923a1dfc7fa094d2b41)
10a080583Sjnemeth /*-
20a080583Sjnemeth  * Copyright (c) 2002 Marcel Moolenaar
30a080583Sjnemeth  * All rights reserved.
40a080583Sjnemeth  *
50a080583Sjnemeth  * Redistribution and use in source and binary forms, with or without
60a080583Sjnemeth  * modification, are permitted provided that the following conditions
70a080583Sjnemeth  * are met:
80a080583Sjnemeth  *
90a080583Sjnemeth  * 1. Redistributions of source code must retain the above copyright
100a080583Sjnemeth  *    notice, this list of conditions and the following disclaimer.
110a080583Sjnemeth  * 2. Redistributions in binary form must reproduce the above copyright
120a080583Sjnemeth  *    notice, this list of conditions and the following disclaimer in the
130a080583Sjnemeth  *    documentation and/or other materials provided with the distribution.
140a080583Sjnemeth  *
150a080583Sjnemeth  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
160a080583Sjnemeth  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
170a080583Sjnemeth  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
180a080583Sjnemeth  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
190a080583Sjnemeth  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
200a080583Sjnemeth  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
210a080583Sjnemeth  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
220a080583Sjnemeth  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
230a080583Sjnemeth  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
240a080583Sjnemeth  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
250a080583Sjnemeth  */
260a080583Sjnemeth 
27a50708a1Schristos #if HAVE_NBTOOL_CONFIG_H
28a50708a1Schristos #include "nbtool_config.h"
29a50708a1Schristos #endif
30a50708a1Schristos 
310a080583Sjnemeth #include <sys/cdefs.h>
320a080583Sjnemeth #ifdef __FBSDID
330a080583Sjnemeth __FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $");
340a080583Sjnemeth #endif
350a080583Sjnemeth #ifdef __RCSID
36*084befafSjmcneill __RCSID("$NetBSD: resize.c,v 1.25 2020/05/24 14:42:44 jmcneill Exp $");
370a080583Sjnemeth #endif
380a080583Sjnemeth 
390a080583Sjnemeth #include <sys/types.h>
400a080583Sjnemeth 
410a080583Sjnemeth #include <err.h>
42*084befafSjmcneill #include <stdbool.h>
430a080583Sjnemeth #include <stddef.h>
440a080583Sjnemeth #include <stdio.h>
450a080583Sjnemeth #include <stdlib.h>
460a080583Sjnemeth #include <string.h>
470a080583Sjnemeth #include <unistd.h>
480a080583Sjnemeth 
490a080583Sjnemeth #include "map.h"
500a080583Sjnemeth #include "gpt.h"
510b43d398Schristos #include "gpt_private.h"
520a080583Sjnemeth 
538ca93e46Schristos static int cmd_resize(gpt_t, int, char *[]);
540a080583Sjnemeth 
558ca93e46Schristos static const char *resizehelp[] = {
56*084befafSjmcneill 	"[-i index | -b blocknr] [-a alignment] [-s size] [-q]",
578ca93e46Schristos };
580a080583Sjnemeth 
598ca93e46Schristos struct gpt_cmd c_resize = {
608ca93e46Schristos 	"resize",
618ca93e46Schristos 	cmd_resize,
628ca93e46Schristos 	resizehelp, __arraycount(resizehelp),
63aa3b5bb2Sjnemeth 	GPT_SYNC,
648ca93e46Schristos };
658ca93e46Schristos 
668ca93e46Schristos #define usage() gpt_usage(NULL, &c_resize)
670a080583Sjnemeth 
680b43d398Schristos static int
resize(gpt_t gpt,u_int entry,off_t alignment,off_t sectors,off_t size,bool quiet)69*084befafSjmcneill resize(gpt_t gpt, u_int entry, off_t alignment, off_t sectors, off_t size, bool quiet)
700a080583Sjnemeth {
710b43d398Schristos 	map_t map;
720a080583Sjnemeth 	struct gpt_hdr *hdr;
730a080583Sjnemeth 	struct gpt_ent *ent;
740a080583Sjnemeth 	unsigned int i;
75*084befafSjmcneill 	off_t alignsecs, newsize, oldsize;
765c1ccc6eSchristos 	uint64_t end;
770a080583Sjnemeth 
780a080583Sjnemeth 
790b43d398Schristos 	if ((hdr = gpt_hdr(gpt)) == NULL)
800b43d398Schristos 		return -1;
810b43d398Schristos 
820a080583Sjnemeth 	i = entry - 1;
830b43d398Schristos 	ent = gpt_ent_primary(gpt, i);
8421c34dbbSchristos 	if (gpt_uuid_is_nil(ent->ent_type)) {
850b43d398Schristos 		gpt_warnx(gpt, "Entry at index %u is unused", entry);
860b43d398Schristos 		return -1;
870a080583Sjnemeth 	}
880a080583Sjnemeth 
890b43d398Schristos 	alignsecs = alignment / gpt->secsz;
900a080583Sjnemeth 
910b43d398Schristos 	for (map = map_first(gpt); map != NULL; map = map->map_next) {
920a080583Sjnemeth 		if (entry == map->map_index)
930a080583Sjnemeth 			break;
940a080583Sjnemeth 	}
95bb7996edSchristos 	if (map == NULL) {
960b43d398Schristos 		gpt_warnx(gpt, "Could not find map entry corresponding "
970b43d398Schristos 		    "to index");
980b43d398Schristos 		return -1;
990a080583Sjnemeth 	}
1000a080583Sjnemeth 
1012098c850Sjnemeth 	if (sectors > 0 && sectors == map->map_size)
1020a080583Sjnemeth 		if (alignment == 0 ||
1032098c850Sjnemeth 		    (alignment > 0 && sectors % alignsecs == 0)) {
1040a080583Sjnemeth 			/* nothing to do */
105*084befafSjmcneill 			if (!quiet)
106*084befafSjmcneill 				gpt_warnx(gpt,
107*084befafSjmcneill 				    "partition does not need resizing");
1080b43d398Schristos 			return 0;
1090a080583Sjnemeth 		}
1100a080583Sjnemeth 
111*084befafSjmcneill 	oldsize = map->map_size;
1120b43d398Schristos 	newsize = map_resize(gpt, map, sectors, alignsecs);
113c458b37cSchristos 	if (newsize == -1)
1140b43d398Schristos 		return -1;
1150a080583Sjnemeth 
116*084befafSjmcneill 	if (oldsize == newsize) {
117*084befafSjmcneill 		/* Nothing to do */
118*084befafSjmcneill 		if (!quiet)
119*084befafSjmcneill 			gpt_warnx(gpt,
120*084befafSjmcneill 			    "partition does not need resizing");
121*084befafSjmcneill 		return 0;
122*084befafSjmcneill 	}
123*084befafSjmcneill 
1245c1ccc6eSchristos 	end = htole64((uint64_t)(map->map_start + newsize - 1LL));
1255c1ccc6eSchristos 	ent->ent_lba_end = end;
1260a080583Sjnemeth 
1270b43d398Schristos 	if (gpt_write_primary(gpt) == -1)
1280b43d398Schristos 		return -1;
1290a080583Sjnemeth 
1300b43d398Schristos 	ent = gpt_ent(gpt->gpt, gpt->lbt, i);
1315c1ccc6eSchristos 	ent->ent_lba_end = end;
1320a080583Sjnemeth 
1330b43d398Schristos 	if (gpt_write_backup(gpt) == -1)
1340b43d398Schristos 		return -1;
1350a080583Sjnemeth 
136ffec5f5cSchristos 	gpt_msg(gpt, "Partition %d resized: %" PRIu64 " %" PRIu64, entry,
13768bc3825Schristos 	    map->map_start, newsize);
1380b43d398Schristos 
1390b43d398Schristos 	return 0;
1400a080583Sjnemeth }
1410a080583Sjnemeth 
1428ca93e46Schristos static int
cmd_resize(gpt_t gpt,int argc,char * argv[])1430b43d398Schristos cmd_resize(gpt_t gpt, int argc, char *argv[])
1440a080583Sjnemeth {
1450b43d398Schristos 	int ch;
146f2b09ed0Smartin 	off_t alignment = 0, sectors, start = 0, size = 0;
147da0a3b4cSchristos 	unsigned int entry = 0;
148f2b09ed0Smartin 	map_t m;
149*084befafSjmcneill 	bool quiet = false;
1500a080583Sjnemeth 
151*084befafSjmcneill 	while ((ch = getopt(argc, argv, GPT_AIS "b:q")) != -1) {
152f2b09ed0Smartin 		if (ch == 'b')
153f2b09ed0Smartin 			gpt_human_get(gpt, &start);
154*084befafSjmcneill 		else if (ch == 'q')
155*084befafSjmcneill 			quiet = true;
156f2b09ed0Smartin 		else if (gpt_add_ais(gpt, &alignment, &entry, &size, ch) == -1)
1578ca93e46Schristos 			return usage();
1580a080583Sjnemeth 	}
1590a080583Sjnemeth 
1600b43d398Schristos 	if (argc != optind)
1618ca93e46Schristos 		return usage();
1620a080583Sjnemeth 
163f2b09ed0Smartin 	if (start > 0) {
164f2b09ed0Smartin 		for (m = map_first(gpt); m != NULL; m = m->map_next) {
165f2b09ed0Smartin 			if (m->map_type != MAP_TYPE_GPT_PART ||
166f2b09ed0Smartin 			    m->map_index < 1)
167f2b09ed0Smartin 				continue;
168f2b09ed0Smartin 			if (start != m->map_start)
169f2b09ed0Smartin 				continue;
170f2b09ed0Smartin 			entry = m->map_index;
171f2b09ed0Smartin 			break;
172f2b09ed0Smartin 		}
173f2b09ed0Smartin 	}
174f2b09ed0Smartin 
175bbb4a8abSchristos 	if ((sectors = gpt_check_ais(gpt, alignment, entry, size)) == -1)
1760b43d398Schristos 		return -1;
1770a080583Sjnemeth 
178*084befafSjmcneill 	return resize(gpt, entry, alignment, sectors, size, quiet);
1790a080583Sjnemeth }
180