Home | History | Annotate | Line # | Download | only in gpt
resizedisk.c revision 1.1
      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: resizedisk.c,v 1.1 2014/09/23 07:47:54 jnemeth Exp $");
     33 #endif
     34 
     35 #include <sys/bootblock.h>
     36 #include <sys/types.h>
     37 
     38 #include <err.h>
     39 #include <stddef.h>
     40 #include <stdio.h>
     41 #include <stdlib.h>
     42 #include <string.h>
     43 #include <unistd.h>
     44 #include <inttypes.h>
     45 
     46 #include "map.h"
     47 #include "gpt.h"
     48 
     49 static uint64_t sector, size;
     50 
     51 const char resizediskmsg[] = "resizedisk [-s size] device ...";
     52 
     53 __dead static void
     54 usage_resizedisk(void)
     55 {
     56 
     57 	fprintf(stderr,
     58 	    "usage: %s %s\n", getprogname(), resizediskmsg);
     59 	exit(1);
     60 }
     61 
     62 /*
     63  * relocate the secondary GPT based on the following criteria:
     64  * - size not specified
     65  *   - disk has not changed size, do nothing
     66  *   - disk has grown, relocate secondary
     67  *   - disk has shrunk, create new secondary
     68  * - size specified
     69  *   - size is larger then disk or same as current location, do nothing
     70  *   - relocate or create new secondary
     71  * - when shrinking, verify that table fits
     72  */
     73 static void
     74 resizedisk(int fd)
     75 {
     76 	uuid_t uuid;
     77 	map_t *gpt, *tpg;
     78 	map_t *tbl, *lbt;
     79 	map_t *mbrmap;
     80 	struct gpt_hdr *hdr;
     81 	struct gpt_ent *ent;
     82 	struct mbr *mbr;
     83 	uint64_t last, oldloc, newloc, lastdata, gpt_size;
     84 	int i;
     85 
     86 	last = mediasz / secsz - 1;
     87 	lastdata = 0;
     88 	newloc = 0;
     89 
     90 	if (sector > last) {
     91 		warnx("%s: specified size is larger then the disk",
     92 		    device_name);
     93 		return;
     94 	}
     95 
     96         mbrmap = map_find(MAP_TYPE_PMBR);
     97         if (mbrmap == NULL || mbrmap->map_start != 0) {
     98                 warnx("%s: error: no valid Protective MBR found", device_name);
     99                 return;
    100         }
    101         mbr = mbrmap->map_data;
    102 
    103 	gpt = map_find(MAP_TYPE_PRI_GPT_HDR);
    104 	ent = NULL;
    105 	if (gpt == NULL) {
    106 		warnx("%s: error: no primary GPT header; run create or recover",
    107 		    device_name);
    108 		return;
    109 	}
    110 	hdr = gpt->map_data;
    111 	oldloc = le64toh(hdr->hdr_lba_alt);
    112 
    113 	tpg = map_find(MAP_TYPE_SEC_GPT_HDR);
    114 	if (tpg == NULL)
    115 		if (gpt_gpt(fd, oldloc, 1))
    116 			tpg = map_find(MAP_TYPE_SEC_GPT_HDR);
    117 
    118 	tbl = map_find(MAP_TYPE_PRI_GPT_TBL);
    119 	lbt = map_find(MAP_TYPE_SEC_GPT_TBL);
    120 	if (tbl == NULL) {
    121 		warnx("%s: error: run recover -- trust me", device_name);
    122 		return;
    123 	}
    124 
    125 	gpt_size = tbl->map_size;
    126 	if (sector == oldloc) {
    127 		warnx("%s: device is already the specified size", device_name);
    128 		return;
    129 	}
    130 	if (sector == 0 && last == oldloc) {
    131 		warnx("%s: device hasn't changed size", device_name);
    132 		return;
    133 	}
    134 
    135 	for (ent = tbl->map_data; ent <
    136 	    (struct gpt_ent *)((char *)tbl->map_data +
    137 	    le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)); ent++) {
    138 		le_uuid_dec(ent->ent_type, &uuid);
    139 		if (!uuid_is_nil(&uuid, NULL) &&
    140 		    (le64toh(ent->ent_lba_end) > lastdata)) {
    141 			lastdata = le64toh(ent->ent_lba_end);
    142 		}
    143 	}
    144 	if (sector - gpt_size <= lastdata) {
    145 		warnx("%s: not enough space at %lu for secondary GPT table",
    146 		    device_name, sector);
    147 		return;
    148 	}
    149 	if (last - gpt_size <= lastdata) {
    150 		warnx("%s: not enough space for new secondary GPT table",
    151 		    device_name);
    152 		return;
    153 	}
    154 
    155 	if (sector > oldloc)
    156 		newloc = sector;
    157 	if (sector > 0 && sector < oldloc && last >= oldloc)
    158 		newloc = sector;
    159 	if (sector == 0 && last > oldloc)
    160 		newloc = last;
    161 	if (newloc > 0) {
    162 		if (tpg == NULL) {
    163 			warnx("%s: error: no secondary GPT header; run recover",
    164 			    device_name);
    165 			return;
    166 		}
    167 		if (lbt == NULL) {
    168 			warnx("%s: error: run recover -- trust me",
    169 			    device_name);
    170 			return;
    171 		}
    172 		tpg->map_start = newloc;
    173 		lbt->map_start = newloc - gpt_size;
    174 	} else {
    175 		if (sector > 0)
    176 			newloc = sector;
    177 		else
    178 			newloc = last;
    179 		tpg = map_add(newloc, 1LL, MAP_TYPE_SEC_GPT_HDR,
    180 		    calloc(1, secsz));
    181 		lbt = map_add(newloc - gpt_size, gpt_size, MAP_TYPE_SEC_GPT_TBL,
    182 		    tbl->map_data);
    183 		memcpy(tpg->map_data, gpt->map_data, secsz);
    184 	}
    185 
    186 	hdr = gpt->map_data;
    187 	hdr->hdr_lba_alt = tpg->map_start;
    188 	hdr->hdr_crc_self = 0;
    189 	hdr->hdr_crc_self =
    190 	    htole32(crc32(gpt->map_data, GPT_HDR_SIZE));
    191 	gpt_write(fd, gpt);
    192 
    193 	hdr = tpg->map_data;
    194 	hdr->hdr_lba_self = htole64(tpg->map_start);
    195 	hdr->hdr_lba_alt = htole64(gpt->map_start);
    196 	hdr->hdr_lba_table = htole64(lbt->map_start);
    197 	hdr->hdr_crc_self = 0;
    198 	hdr->hdr_crc_self =
    199 	    htole32(crc32(tpg->map_data, GPT_HDR_SIZE));
    200 	gpt_write(fd, lbt);
    201 	gpt_write(fd, tpg);
    202 
    203 	for (i = 0; i < 4; i++)
    204 		if (mbr->mbr_part[0].part_typ == MBR_PTYPE_PMBR)
    205 			break;
    206 	if (i == 4) {
    207 		warnx("%s: no valid PMBR partition found", device_name);
    208 		return;
    209 	}
    210 	if (last > 0xffffffff) {
    211 		mbr->mbr_part[0].part_size_lo = htole16(0xffff);
    212 		mbr->mbr_part[0].part_size_hi = htole16(0xffff);
    213 	} else {
    214 		mbr->mbr_part[0].part_size_lo = htole16(last);
    215 		mbr->mbr_part[0].part_size_hi = htole16(last >> 16);
    216 	}
    217 	gpt_write(fd, mbrmap);
    218 
    219 	return;
    220 }
    221 
    222 int
    223 cmd_resizedisk(int argc, char *argv[])
    224 {
    225 	char *p;
    226 	int ch, fd;
    227 	int64_t human_num;
    228 
    229 	while ((ch = getopt(argc, argv, "s:")) != -1) {
    230 		switch(ch) {
    231 		case 's':
    232 			if (sector > 0 || size > 0)
    233 				usage_resizedisk();
    234 			sector = strtoll(optarg, &p, 10);
    235 			if (sector < 1)
    236 				usage_resizedisk();
    237 			if (*p == '\0')
    238 				break;
    239 			if (*p == 's' || *p == 'S') {
    240 				if (*(p + 1) == '\0')
    241 					break;
    242 				else
    243 					usage_resizedisk();
    244 			}
    245 			if (*p == 'b' || *p == 'B') {
    246 				if (*(p + 1) == '\0') {
    247 					size = sector;
    248 					sector = 0;
    249 					break;
    250 				} else
    251 					usage_resizedisk();
    252 			}
    253 			if (dehumanize_number(optarg, &human_num) < 0)
    254 				usage_resizedisk();
    255 			size = human_num;
    256 			sector = 0;
    257 			break;
    258 		default:
    259 			usage_resizedisk();
    260 		}
    261 	}
    262 
    263 	if (argc == optind)
    264 		usage_resizedisk();
    265 
    266 	while (optind < argc) {
    267 		fd = gpt_open(argv[optind++]);
    268 		if (fd == -1) {
    269 			warn("unable to open device '%s'", device_name);
    270 			continue;
    271 		}
    272 
    273 		if (size % secsz != 0) {
    274 			warnx("Size in bytes must be a multiple of sector "
    275 			      "size;");
    276 			warnx("the sector size for %s is %d bytes.",
    277 			    device_name, secsz);
    278 			continue;
    279 		}
    280 		if (size > 0)
    281 			sector = size / secsz - 1;
    282 
    283 		resizedisk(fd);
    284 
    285 		gpt_close(fd);
    286 	}
    287 
    288 	return 0;
    289 }
    290