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