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