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