Home | History | Annotate | Line # | Download | only in gpt
resizedisk.c revision 1.13
      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: resizedisk.c,v 1.13 2015/12/03 02:02:43 christos Exp $");
     37 #endif
     38 
     39 #include <sys/bootblock.h>
     40 #include <sys/types.h>
     41 
     42 #include <err.h>
     43 #include <stddef.h>
     44 #include <stdio.h>
     45 #include <stdlib.h>
     46 #include <string.h>
     47 #include <unistd.h>
     48 
     49 #include "map.h"
     50 #include "gpt.h"
     51 #include "gpt_private.h"
     52 
     53 
     54 static int cmd_resizedisk(gpt_t, int, char *[]);
     55 
     56 static const char *resizediskhelp[] = {
     57 	"[-s size]",
     58 };
     59 
     60 struct gpt_cmd c_resizedisk = {
     61 	"resize",
     62 	cmd_resizedisk,
     63 	resizediskhelp, __arraycount(resizediskhelp),
     64 	0,
     65 };
     66 
     67 #define usage() gpt_usage(NULL, &c_resizedisk)
     68 
     69 /*
     70  * relocate the secondary GPT based on the following criteria:
     71  * - size not specified
     72  *   - disk has not changed size, do nothing
     73  *   - disk has grown, relocate secondary
     74  *   - disk has shrunk, create new secondary
     75  * - size specified
     76  *   - size is larger then disk or same as current location, do nothing
     77  *   - relocate or create new secondary
     78  * - when shrinking, verify that table fits
     79  */
     80 static int
     81 resizedisk(gpt_t gpt, off_t sector, off_t size)
     82 {
     83 	map_t mbrmap;
     84 	struct gpt_hdr *hdr;
     85 	struct gpt_ent *ent;
     86 	struct mbr *mbr;
     87 	off_t last, oldloc, newloc, lastdata, gpt_size;
     88 	int i;
     89 	void *p;
     90 
     91 	last = gpt->mediasz / gpt->secsz - 1;
     92 	lastdata = 0;
     93 	newloc = 0;
     94 
     95 	if (sector > last) {
     96 		gpt_warnx(gpt, "specified size is larger then the disk");
     97 		return -1;
     98 	}
     99 
    100         mbrmap = map_find(gpt, MAP_TYPE_PMBR);
    101         if (mbrmap == NULL || mbrmap->map_start != 0) {
    102                 gpt_warnx(gpt, "No valid PMBR found");
    103                 return -1;
    104         }
    105         mbr = mbrmap->map_data;
    106 
    107 	gpt->gpt = map_find(gpt, MAP_TYPE_PRI_GPT_HDR);
    108 	ent = NULL;
    109 	if (gpt == NULL) {
    110 		gpt_warnx(gpt, "No primary GPT header; run create or recover");
    111 		return -1;
    112 	}
    113 	hdr = gpt->gpt->map_data;
    114 	oldloc = (off_t)le64toh((uint64_t)hdr->hdr_lba_alt);
    115 
    116 	gpt->tpg = map_find(gpt, MAP_TYPE_SEC_GPT_HDR);
    117 	if (gpt->tpg == NULL)
    118 		if (gpt_gpt(gpt, oldloc, 1))
    119 			gpt->tpg = map_find(gpt, MAP_TYPE_SEC_GPT_HDR);
    120 
    121 	gpt->tbl = map_find(gpt, MAP_TYPE_PRI_GPT_TBL);
    122 	if (gpt->tbl == NULL) {
    123 		gpt_warnx(gpt, "Run recover");
    124 		return -1;
    125 	}
    126 
    127 	gpt_size = gpt->tbl->map_size;
    128 	if (sector == oldloc) {
    129 		gpt_warnx(gpt, "Device is already the specified size");
    130 		return 0;
    131 	}
    132 	if (sector == 0 && last == oldloc) {
    133 		gpt_warnx(gpt, "Device hasn't changed size");
    134 		return 0;
    135 	}
    136 
    137 	for (ent = gpt->tbl->map_data; ent <
    138 	    (struct gpt_ent *)((char *)gpt->tbl->map_data +
    139 	    le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)); ent++) {
    140 		if (!gpt_uuid_is_nil(ent->ent_type) &&
    141 		    ((off_t)le64toh(ent->ent_lba_end) > lastdata)) {
    142 			lastdata = (off_t)le64toh((uint64_t)ent->ent_lba_end);
    143 		}
    144 	}
    145 	if (sector - gpt_size <= lastdata) {
    146 		gpt_warnx(gpt, "Not enough space at %" PRIu64
    147 		    " for secondary GPT table", sector);
    148 		return -1;
    149 	}
    150 	if (last - gpt_size <= lastdata) {
    151 		gpt_warnx(gpt, "Not enough space for new secondary GPT table");
    152 		return -1;
    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 (gpt->tpg == NULL) {
    163 			gpt_warnx(gpt, "No secondary GPT header; run recover");
    164 			return -1;
    165 		}
    166 		if (gpt->lbt == NULL) {
    167 			gpt_warnx(gpt, "Run recover");
    168 			return -1;
    169 		}
    170 		gpt->tpg->map_start = newloc;
    171 		gpt->lbt->map_start = newloc - gpt_size;
    172 	} else {
    173 		if (sector > 0)
    174 			newloc = sector;
    175 		else
    176 			newloc = last;
    177 		if ((p = calloc(1, gpt->secsz)) == NULL) {
    178 			gpt_warn(gpt, "Error allocating secondary GPT");
    179 			return -1;
    180 		}
    181 		if (gpt->lbt == NULL) {
    182 			gpt_warn(gpt, "Error adding secondary GPT");
    183 			return -1;
    184 		}
    185 		gpt->tpg = map_add(gpt, newloc, 1LL, MAP_TYPE_SEC_GPT_HDR, p);
    186 		if (gpt->lbt == NULL) {
    187 			gpt_warn(gpt, "Error adding secondary GPT");
    188 			return -1;
    189 		}
    190 		// XXX: map add with non-allocated memory
    191 		gpt->lbt = map_add(gpt, newloc - gpt_size, gpt_size,
    192 		    MAP_TYPE_SEC_GPT_TBL, gpt->tbl->map_data);
    193 		if (gpt->lbt == NULL) {
    194 			gpt_warn(gpt, "Error adding secondary GPT table");
    195 			return -1;
    196 		}
    197 		memcpy(gpt->tpg->map_data, gpt->gpt->map_data, gpt->secsz);
    198 	}
    199 
    200 	hdr = gpt->gpt->map_data;
    201 	hdr->hdr_lba_alt = (uint64_t)gpt->tpg->map_start;
    202 	hdr->hdr_crc_self = 0;
    203 	hdr->hdr_lba_end = htole64((uint64_t)(gpt->lbt->map_start - 1));
    204 	hdr->hdr_crc_self =
    205 	    htole32(crc32(gpt->gpt->map_data, GPT_HDR_SIZE));
    206 	gpt_write(gpt, gpt->gpt);
    207 
    208 	hdr = gpt->tpg->map_data;
    209 	hdr->hdr_lba_self = htole64((uint64_t)gpt->tpg->map_start);
    210 	hdr->hdr_lba_alt = htole64((uint64_t)gpt->gpt->map_start);
    211 	hdr->hdr_lba_end = htole64((uint64_t)(gpt->lbt->map_start - 1));
    212 	hdr->hdr_lba_table = htole64((uint64_t)gpt->lbt->map_start);
    213 
    214 	if (gpt_write_backup(gpt) == -1)
    215 		return -1;
    216 
    217 	for (i = 0; i < 4; i++)
    218 		if (mbr->mbr_part[0].part_typ == MBR_PTYPE_PMBR)
    219 			break;
    220 	if (i == 4) {
    221 		gpt_warnx(gpt, "No valid PMBR partition found");
    222 		return -1;
    223 	}
    224 	if (last > 0xffffffff) {
    225 		mbr->mbr_part[0].part_size_lo = htole16(0xffff);
    226 		mbr->mbr_part[0].part_size_hi = htole16(0xffff);
    227 	} else {
    228 		mbr->mbr_part[0].part_size_lo = htole16((uint16_t)last);
    229 		mbr->mbr_part[0].part_size_hi = htole16((uint16_t)(last >> 16));
    230 	}
    231 	if (gpt_write(gpt, mbrmap) == -1) {
    232 		gpt_warnx(gpt, "Error writing PMBR");
    233 		return -1;
    234 	}
    235 
    236 	return 0;
    237 }
    238 
    239 static int
    240 cmd_resizedisk(gpt_t gpt, int argc, char *argv[])
    241 {
    242 	int ch;
    243 	off_t sector, size = 0;
    244 
    245 	while ((ch = getopt(argc, argv, "s:")) != -1) {
    246 		switch(ch) {
    247 		case 's':
    248 			if (gpt_add_ais(gpt, NULL, NULL, &size, ch) == -1)
    249 				return -1;
    250 			break;
    251 		default:
    252 			return usage();
    253 		}
    254 	}
    255 
    256 	if (argc != optind)
    257 		return usage();
    258 
    259 	if ((sector = gpt_check_ais(gpt, 0, (u_int)~0, size)) == -1)
    260 		return -1;
    261 
    262 	return resizedisk(gpt, sector, size);
    263 }
    264