Home | History | Annotate | Line # | Download | only in gpt
resize.c revision 1.9
      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.9 2014/09/29 20:28:57 christos Exp $");
     37 #endif
     38 
     39 #include <sys/types.h>
     40 
     41 #include <err.h>
     42 #include <stddef.h>
     43 #include <stdio.h>
     44 #include <stdlib.h>
     45 #include <string.h>
     46 #include <unistd.h>
     47 #include <inttypes.h>
     48 
     49 #include "map.h"
     50 #include "gpt.h"
     51 
     52 static off_t alignment, sectors, size;
     53 static unsigned int entry;
     54 
     55 const char resizemsg[] = "resize -i index [-a alignment] [-s size] device ...";
     56 
     57 __dead static void
     58 usage_resize(void)
     59 {
     60 
     61 	fprintf(stderr,
     62 	    "usage: %s %s\n", getprogname(), resizemsg);
     63 	exit(1);
     64 }
     65 
     66 static void
     67 resize(int fd)
     68 {
     69 	uuid_t uuid;
     70 	map_t *gpt, *tpg;
     71 	map_t *tbl, *lbt;
     72 	map_t *map;
     73 	struct gpt_hdr *hdr;
     74 	struct gpt_ent *ent;
     75 	unsigned int i;
     76 	off_t alignsecs, newsize;
     77 
     78 
     79 	gpt = map_find(MAP_TYPE_PRI_GPT_HDR);
     80 	ent = NULL;
     81 	if (gpt == NULL) {
     82 		warnx("%s: error: no primary GPT header; run create or recover",
     83 		    device_name);
     84 		return;
     85 	}
     86 
     87 	tpg = map_find(MAP_TYPE_SEC_GPT_HDR);
     88 	if (tpg == NULL) {
     89 		warnx("%s: error: no secondary GPT header; run recover",
     90 		    device_name);
     91 		return;
     92 	}
     93 
     94 	tbl = map_find(MAP_TYPE_PRI_GPT_TBL);
     95 	lbt = map_find(MAP_TYPE_SEC_GPT_TBL);
     96 	if (tbl == NULL || lbt == NULL) {
     97 		warnx("%s: error: run recover -- trust me", device_name);
     98 		return;
     99 	}
    100 
    101 	hdr = gpt->map_data;
    102 	if (entry > le32toh(hdr->hdr_entries)) {
    103 		warnx("%s: error: index %u out of range (%u max)", device_name,
    104 		    entry, le32toh(hdr->hdr_entries));
    105 		return;
    106 	}
    107 
    108 	i = entry - 1;
    109 	ent = (void*)((char*)tbl->map_data + i *
    110 	    le32toh(hdr->hdr_entsz));
    111 	le_uuid_dec(ent->ent_type, &uuid);
    112 	if (uuid_is_nil(&uuid, NULL)) {
    113 		warnx("%s: error: entry at index %u is unused",
    114 		    device_name, entry);
    115 		return;
    116 	}
    117 
    118 	alignsecs = alignment / secsz;
    119 
    120 	for (map = map_first(); map != NULL; map = map->map_next) {
    121 		if (entry == map->map_index)
    122 			break;
    123 	}
    124 	if (map == NULL) {
    125 		warnx("%s: error: could not find map entry corresponding "
    126 		      "to index", device_name);
    127 		return;
    128 	}
    129 
    130 	if (sectors > 0 && sectors == map->map_size)
    131 		if (alignment == 0 ||
    132 		    (alignment > 0 && sectors % alignsecs == 0)) {
    133 			/* nothing to do */
    134 			warnx("%s: partition does not need resizing",
    135 			    device_name);
    136 			return;
    137 		}
    138 
    139 	newsize = map_resize(map, sectors, alignsecs);
    140 	if (newsize == 0 && alignment > 0) {
    141 		warnx("%s: could not resize partition with alignment "
    142 		      "constraint", device_name);
    143 		return;
    144 	} else if (newsize == 0) {
    145 		warnx("%s: could not resize partition", device_name);
    146 		return;
    147 	}
    148 
    149 	ent->ent_lba_end = htole64(map->map_start + newsize - 1LL);
    150 
    151 	hdr->hdr_crc_table = htole32(crc32(tbl->map_data,
    152 	    le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
    153 	hdr->hdr_crc_self = 0;
    154 	hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
    155 
    156 	gpt_write(fd, gpt);
    157 	gpt_write(fd, tbl);
    158 
    159 	hdr = tpg->map_data;
    160 	ent = (void*)((char*)lbt->map_data + i * le32toh(hdr->hdr_entsz));
    161 
    162 	ent->ent_lba_end = htole64(map->map_start + newsize - 1LL);
    163 
    164 	hdr->hdr_crc_table = htole32(crc32(lbt->map_data,
    165 	    le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
    166 	hdr->hdr_crc_self = 0;
    167 	hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
    168 
    169 	gpt_write(fd, lbt);
    170 	gpt_write(fd, tpg);
    171 
    172 	printf("Partition %d resized, use:\n", entry);
    173 	printf("\tdkctl %s addwedge <wedgename> %" PRIu64 " %" PRIu64
    174 	    " <type>\n", device_arg, map->map_start, newsize);
    175 	printf("to create a wedge for it\n");
    176 }
    177 
    178 int
    179 cmd_resize(int argc, char *argv[])
    180 {
    181 	char *p;
    182 	int ch, fd;
    183 	int64_t human_num;
    184 
    185 	while ((ch = getopt(argc, argv, "a:i:s:")) != -1) {
    186 		switch(ch) {
    187 		case 'a':
    188 			if (alignment > 0)
    189 				usage_resize();
    190 			if (dehumanize_number(optarg, &human_num) < 0)
    191 				usage_resize();
    192 			alignment = human_num;
    193 			if (alignment < 1)
    194 				usage_resize();
    195 			break;
    196 		case 'i':
    197 			if (entry > 0)
    198 				usage_resize();
    199 			entry = strtoul(optarg, &p, 10);
    200 			if (*p != 0 || entry < 1)
    201 				usage_resize();
    202 			break;
    203 		case 's':
    204 			if (sectors > 0 || size > 0)
    205 				usage_resize();
    206 			sectors = strtoll(optarg, &p, 10);
    207 			if (sectors < 1)
    208 				usage_resize();
    209 			if (*p == '\0')
    210 				break;
    211 			if (*p == 's' || *p == 'S') {
    212 				if (*(p + 1) == '\0')
    213 					break;
    214 				else
    215 					usage_resize();
    216 			}
    217 			if (*p == 'b' || *p == 'B') {
    218 				if (*(p + 1) == '\0') {
    219 					size = sectors;
    220 					sectors = 0;
    221 					break;
    222 				} else
    223 					usage_resize();
    224 			}
    225 			if (dehumanize_number(optarg, &human_num) < 0)
    226 				usage_resize();
    227 			size = human_num;
    228 			sectors = 0;
    229 			break;
    230 		default:
    231 			usage_resize();
    232 		}
    233 	}
    234 
    235 	if (argc == optind)
    236 		usage_resize();
    237 
    238 	if (entry == 0)
    239 		usage_resize();
    240 
    241 	while (optind < argc) {
    242 		fd = gpt_open(argv[optind++]);
    243 		if (fd == -1) {
    244 			warn("unable to open device '%s'", device_name);
    245 			continue;
    246 		}
    247 
    248 		if (alignment % secsz != 0) {
    249 			warnx("Alignment must be a multiple of sector size;");
    250 			warnx("the sector size for %s is %d bytes.",
    251 			    device_name, secsz);
    252 			continue;
    253 		}
    254 
    255 		if (size % secsz != 0) {
    256 			warnx("Size in bytes must be a multiple of sector "
    257 			      "size;");
    258 			warnx("the sector size for %s is %d bytes.",
    259 			    device_name, secsz);
    260 			continue;
    261 		}
    262 		if (size > 0)
    263 			sectors = size / secsz;
    264 
    265 		resize(fd);
    266 
    267 		gpt_close(fd);
    268 	}
    269 
    270 	return 0;
    271 }
    272