Home | History | Annotate | Line # | Download | only in gpt
resize.c revision 1.6
      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.6 2013/12/06 02:31:31 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 (map == NULL) {
    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) {
    138 		warnx("%s: could not resize partition with alignment "
    139 		      "constraint", device_name);
    140 		return;
    141 	} else if (newsize == 0) {
    142 		warnx("%s: could not resize partition", device_name);
    143 		return;
    144 	}
    145 
    146 	ent->ent_lba_end = htole64(map->map_start + newsize - 1LL);
    147 
    148 	hdr->hdr_crc_table = htole32(crc32(tbl->map_data,
    149 	    le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
    150 	hdr->hdr_crc_self = 0;
    151 	hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
    152 
    153 	gpt_write(fd, gpt);
    154 	gpt_write(fd, tbl);
    155 
    156 	hdr = tpg->map_data;
    157 	ent = (void*)((char*)lbt->map_data + i * le32toh(hdr->hdr_entsz));
    158 
    159 	ent->ent_lba_end = htole64(map->map_start + newsize - 1LL);
    160 
    161 	hdr->hdr_crc_table = htole32(crc32(lbt->map_data,
    162 	    le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
    163 	hdr->hdr_crc_self = 0;
    164 	hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
    165 
    166 	gpt_write(fd, lbt);
    167 	gpt_write(fd, tpg);
    168 
    169 	printf("Partition resized, use:\n");
    170 	printf("\tdkctl %s addwedge <wedgename> %" PRIu64 " %" PRIu64
    171 	    " <type>\n", device_arg, map->map_start, newsize);
    172 	printf("to create a wedge for it\n");
    173 }
    174 
    175 int
    176 cmd_resize(int argc, char *argv[])
    177 {
    178 	char *p;
    179 	int ch, fd;
    180 	int64_t human_num;
    181 
    182 	while ((ch = getopt(argc, argv, "a:i:s:")) != -1) {
    183 		switch(ch) {
    184 		case 'a':
    185 			if (alignment > 0)
    186 				usage_resize();
    187 			if (dehumanize_number(optarg, &human_num) < 0)
    188 				usage_resize();
    189 			alignment = human_num;
    190 			if (alignment < 1)
    191 				usage_resize();
    192 			break;
    193 		case 'i':
    194 			if (entry > 0)
    195 				usage_resize();
    196 			entry = strtoul(optarg, &p, 10);
    197 			if (*p != 0 || entry < 1)
    198 				usage_resize();
    199 			break;
    200 		case 's':
    201 			if (size > 0)
    202 				usage_resize();
    203 			size = strtoll(optarg, &p, 10);
    204 			if (*p != 0 || size < 1)
    205 				usage_resize();
    206 			break;
    207 		default:
    208 			usage_resize();
    209 		}
    210 	}
    211 
    212 	if (argc == optind)
    213 		usage_resize();
    214 
    215 	if (entry == 0)
    216 		usage_resize();
    217 
    218 	while (optind < argc) {
    219 		fd = gpt_open(argv[optind++]);
    220 		if (fd == -1) {
    221 			warn("unable to open device '%s'", device_name);
    222 			continue;
    223 		}
    224 
    225 		if (alignment % secsz != 0) {
    226 			warnx("Alignment must be a multiple of sector size; ");
    227 			warnx("the sector size for %s is %d bytes.",
    228 			    device_name, secsz);
    229 			continue;
    230 		}
    231 
    232 		resize(fd);
    233 
    234 		gpt_close(fd);
    235 	}
    236 
    237 	return 0;
    238 }
    239