2014-09-23 11:47:54 +04:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2002 Marcel Moolenaar
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2014-09-30 00:28:57 +04:00
|
|
|
#if HAVE_NBTOOL_CONFIG_H
|
|
|
|
#include "nbtool_config.h"
|
|
|
|
#endif
|
|
|
|
|
2014-09-23 11:47:54 +04:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#ifdef __FBSDID
|
|
|
|
__FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $");
|
|
|
|
#endif
|
|
|
|
#ifdef __RCSID
|
2015-12-05 00:39:18 +03:00
|
|
|
__RCSID("$NetBSD: resizedisk.c,v 1.17 2015/12/04 21:39:18 christos Exp $");
|
2014-09-23 11:47:54 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/bootblock.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "map.h"
|
|
|
|
#include "gpt.h"
|
2015-12-01 12:05:33 +03:00
|
|
|
#include "gpt_private.h"
|
2014-09-23 11:47:54 +04:00
|
|
|
|
|
|
|
|
2015-12-01 19:32:19 +03:00
|
|
|
static int cmd_resizedisk(gpt_t, int, char *[]);
|
2014-09-23 11:47:54 +04:00
|
|
|
|
2015-12-01 19:32:19 +03:00
|
|
|
static const char *resizediskhelp[] = {
|
2015-12-03 04:07:28 +03:00
|
|
|
"[-s size]",
|
2015-12-01 19:32:19 +03:00
|
|
|
};
|
2014-09-23 11:47:54 +04:00
|
|
|
|
2015-12-01 19:32:19 +03:00
|
|
|
struct gpt_cmd c_resizedisk = {
|
2015-12-04 19:46:24 +03:00
|
|
|
"resizedisk",
|
2015-12-01 19:32:19 +03:00
|
|
|
cmd_resizedisk,
|
|
|
|
resizediskhelp, __arraycount(resizediskhelp),
|
|
|
|
0,
|
|
|
|
};
|
|
|
|
|
|
|
|
#define usage() gpt_usage(NULL, &c_resizedisk)
|
2014-09-23 11:47:54 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* relocate the secondary GPT based on the following criteria:
|
|
|
|
* - size not specified
|
|
|
|
* - disk has not changed size, do nothing
|
|
|
|
* - disk has grown, relocate secondary
|
|
|
|
* - disk has shrunk, create new secondary
|
|
|
|
* - size specified
|
|
|
|
* - size is larger then disk or same as current location, do nothing
|
|
|
|
* - relocate or create new secondary
|
|
|
|
* - when shrinking, verify that table fits
|
|
|
|
*/
|
2015-12-01 12:05:33 +03:00
|
|
|
static int
|
2015-12-03 04:07:28 +03:00
|
|
|
resizedisk(gpt_t gpt, off_t sector, off_t size)
|
2014-09-23 11:47:54 +04:00
|
|
|
{
|
2015-12-01 12:05:33 +03:00
|
|
|
map_t mbrmap;
|
2014-09-23 11:47:54 +04:00
|
|
|
struct gpt_hdr *hdr;
|
|
|
|
struct gpt_ent *ent;
|
|
|
|
struct mbr *mbr;
|
2015-12-01 12:05:33 +03:00
|
|
|
off_t last, oldloc, newloc, lastdata, gpt_size;
|
2014-09-23 11:47:54 +04:00
|
|
|
int i;
|
|
|
|
|
2015-12-01 12:05:33 +03:00
|
|
|
last = gpt->mediasz / gpt->secsz - 1;
|
2014-09-23 11:47:54 +04:00
|
|
|
lastdata = 0;
|
|
|
|
newloc = 0;
|
|
|
|
|
|
|
|
if (sector > last) {
|
2015-12-04 19:46:24 +03:00
|
|
|
gpt_warnx(gpt, "specified number of sectors %jd"
|
|
|
|
" is larger then the disk %jd", (uintmax_t)sector,
|
|
|
|
(uintmax_t)last);
|
2015-12-01 12:05:33 +03:00
|
|
|
return -1;
|
2014-09-23 11:47:54 +04:00
|
|
|
}
|
|
|
|
|
2015-12-01 12:05:33 +03:00
|
|
|
mbrmap = map_find(gpt, MAP_TYPE_PMBR);
|
2014-09-23 11:47:54 +04:00
|
|
|
if (mbrmap == NULL || mbrmap->map_start != 0) {
|
2015-12-01 12:05:33 +03:00
|
|
|
gpt_warnx(gpt, "No valid PMBR found");
|
|
|
|
return -1;
|
2014-09-23 11:47:54 +04:00
|
|
|
}
|
|
|
|
mbr = mbrmap->map_data;
|
|
|
|
|
2015-12-01 12:05:33 +03:00
|
|
|
gpt->gpt = map_find(gpt, MAP_TYPE_PRI_GPT_HDR);
|
2014-09-23 11:47:54 +04:00
|
|
|
if (gpt == NULL) {
|
2015-12-01 12:05:33 +03:00
|
|
|
gpt_warnx(gpt, "No primary GPT header; run create or recover");
|
|
|
|
return -1;
|
2014-09-23 11:47:54 +04:00
|
|
|
}
|
2015-12-04 19:46:24 +03:00
|
|
|
|
|
|
|
gpt->tbl = map_find(gpt, MAP_TYPE_PRI_GPT_TBL);
|
|
|
|
if (gpt->tbl == NULL) {
|
|
|
|
gpt_warnx(gpt, "No primary GPT table; Run recover");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-12-01 12:05:33 +03:00
|
|
|
hdr = gpt->gpt->map_data;
|
2015-12-03 05:02:43 +03:00
|
|
|
oldloc = (off_t)le64toh((uint64_t)hdr->hdr_lba_alt);
|
2014-09-23 11:47:54 +04:00
|
|
|
|
2015-12-01 12:05:33 +03:00
|
|
|
gpt->tpg = map_find(gpt, MAP_TYPE_SEC_GPT_HDR);
|
2015-12-04 19:46:24 +03:00
|
|
|
gpt->lbt = map_find(gpt, MAP_TYPE_SEC_GPT_TBL);
|
|
|
|
if (gpt->tpg == NULL || gpt->lbt == NULL) {
|
|
|
|
if (gpt_gpt(gpt, oldloc, 1) == -1) {
|
|
|
|
gpt_warnx(gpt,
|
|
|
|
"Error reading backup GPT information at %#jx",
|
|
|
|
oldloc);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2014-09-23 11:47:54 +04:00
|
|
|
|
2015-12-04 19:46:24 +03:00
|
|
|
gpt->tpg = map_find(gpt, MAP_TYPE_SEC_GPT_HDR);
|
|
|
|
if (gpt->tpg == NULL) {
|
|
|
|
gpt_warnx(gpt, "No secondary GPT header; Run recover");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
gpt->lbt = map_find(gpt, MAP_TYPE_SEC_GPT_TBL);
|
|
|
|
if (gpt->lbt == NULL) {
|
|
|
|
gpt_warnx(gpt, "No secondary GPT table; Run recover");
|
2015-12-01 12:05:33 +03:00
|
|
|
return -1;
|
2014-09-23 11:47:54 +04:00
|
|
|
}
|
|
|
|
|
2015-12-01 12:05:33 +03:00
|
|
|
gpt_size = gpt->tbl->map_size;
|
2014-09-23 11:47:54 +04:00
|
|
|
if (sector == oldloc) {
|
2015-12-01 12:05:33 +03:00
|
|
|
gpt_warnx(gpt, "Device is already the specified size");
|
|
|
|
return 0;
|
2014-09-23 11:47:54 +04:00
|
|
|
}
|
2015-12-04 19:46:24 +03:00
|
|
|
|
2014-09-23 11:47:54 +04:00
|
|
|
if (sector == 0 && last == oldloc) {
|
2015-12-01 12:05:33 +03:00
|
|
|
gpt_warnx(gpt, "Device hasn't changed size");
|
|
|
|
return 0;
|
2014-09-23 11:47:54 +04:00
|
|
|
}
|
|
|
|
|
2015-12-01 12:05:33 +03:00
|
|
|
for (ent = gpt->tbl->map_data; ent <
|
|
|
|
(struct gpt_ent *)((char *)gpt->tbl->map_data +
|
2014-09-23 11:47:54 +04:00
|
|
|
le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)); ent++) {
|
2014-09-30 21:59:59 +04:00
|
|
|
if (!gpt_uuid_is_nil(ent->ent_type) &&
|
2015-12-01 12:05:33 +03:00
|
|
|
((off_t)le64toh(ent->ent_lba_end) > lastdata)) {
|
2015-12-03 05:02:43 +03:00
|
|
|
lastdata = (off_t)le64toh((uint64_t)ent->ent_lba_end);
|
2014-09-23 11:47:54 +04:00
|
|
|
}
|
|
|
|
}
|
2015-12-04 19:46:24 +03:00
|
|
|
|
2014-09-23 11:47:54 +04:00
|
|
|
if (sector - gpt_size <= lastdata) {
|
2015-12-01 12:05:33 +03:00
|
|
|
gpt_warnx(gpt, "Not enough space at %" PRIu64
|
|
|
|
" for secondary GPT table", sector);
|
|
|
|
return -1;
|
2014-09-23 11:47:54 +04:00
|
|
|
}
|
2015-12-04 19:46:24 +03:00
|
|
|
|
2014-09-23 11:47:54 +04:00
|
|
|
if (last - gpt_size <= lastdata) {
|
2015-12-01 12:05:33 +03:00
|
|
|
gpt_warnx(gpt, "Not enough space for new secondary GPT table");
|
|
|
|
return -1;
|
2014-09-23 11:47:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sector > oldloc)
|
|
|
|
newloc = sector;
|
|
|
|
if (sector > 0 && sector < oldloc && last >= oldloc)
|
|
|
|
newloc = sector;
|
|
|
|
if (sector == 0 && last > oldloc)
|
|
|
|
newloc = last;
|
2015-12-04 19:46:24 +03:00
|
|
|
|
2014-09-23 11:47:54 +04:00
|
|
|
if (newloc > 0) {
|
2015-12-01 12:05:33 +03:00
|
|
|
if (gpt->tpg == NULL) {
|
|
|
|
gpt_warnx(gpt, "No secondary GPT header; run recover");
|
|
|
|
return -1;
|
2014-09-23 11:47:54 +04:00
|
|
|
}
|
2015-12-01 12:05:33 +03:00
|
|
|
if (gpt->lbt == NULL) {
|
|
|
|
gpt_warnx(gpt, "Run recover");
|
|
|
|
return -1;
|
2014-09-23 11:47:54 +04:00
|
|
|
}
|
2015-12-01 12:05:33 +03:00
|
|
|
gpt->tpg->map_start = newloc;
|
|
|
|
gpt->lbt->map_start = newloc - gpt_size;
|
2014-09-23 11:47:54 +04:00
|
|
|
} else {
|
|
|
|
if (sector > 0)
|
|
|
|
newloc = sector;
|
|
|
|
else
|
|
|
|
newloc = last;
|
2015-12-03 05:09:20 +03:00
|
|
|
|
2015-12-04 19:46:24 +03:00
|
|
|
if (gpt_add_hdr(gpt, MAP_TYPE_SEC_GPT_HDR, newloc) == -1)
|
2015-12-02 23:09:54 +03:00
|
|
|
return -1;
|
2015-12-03 05:09:20 +03:00
|
|
|
|
2015-12-01 12:05:33 +03:00
|
|
|
gpt->lbt = map_add(gpt, newloc - gpt_size, gpt_size,
|
2015-12-04 00:30:54 +03:00
|
|
|
MAP_TYPE_SEC_GPT_TBL, gpt->tbl->map_data, 0);
|
2015-12-02 23:09:54 +03:00
|
|
|
if (gpt->lbt == NULL) {
|
|
|
|
gpt_warn(gpt, "Error adding secondary GPT table");
|
|
|
|
return -1;
|
|
|
|
}
|
2015-12-01 12:05:33 +03:00
|
|
|
memcpy(gpt->tpg->map_data, gpt->gpt->map_data, gpt->secsz);
|
2014-09-23 11:47:54 +04:00
|
|
|
}
|
|
|
|
|
2015-12-01 12:05:33 +03:00
|
|
|
hdr = gpt->gpt->map_data;
|
2015-12-03 05:02:43 +03:00
|
|
|
hdr->hdr_lba_alt = (uint64_t)gpt->tpg->map_start;
|
2014-09-23 11:47:54 +04:00
|
|
|
hdr->hdr_crc_self = 0;
|
2015-12-03 05:02:43 +03:00
|
|
|
hdr->hdr_lba_end = htole64((uint64_t)(gpt->lbt->map_start - 1));
|
2014-09-23 11:47:54 +04:00
|
|
|
hdr->hdr_crc_self =
|
2015-12-01 12:05:33 +03:00
|
|
|
htole32(crc32(gpt->gpt->map_data, GPT_HDR_SIZE));
|
|
|
|
gpt_write(gpt, gpt->gpt);
|
2014-09-23 11:47:54 +04:00
|
|
|
|
2015-12-01 12:05:33 +03:00
|
|
|
hdr = gpt->tpg->map_data;
|
2015-12-03 05:02:43 +03:00
|
|
|
hdr->hdr_lba_self = htole64((uint64_t)gpt->tpg->map_start);
|
|
|
|
hdr->hdr_lba_alt = htole64((uint64_t)gpt->gpt->map_start);
|
|
|
|
hdr->hdr_lba_end = htole64((uint64_t)(gpt->lbt->map_start - 1));
|
|
|
|
hdr->hdr_lba_table = htole64((uint64_t)gpt->lbt->map_start);
|
2015-12-01 12:05:33 +03:00
|
|
|
|
|
|
|
if (gpt_write_backup(gpt) == -1)
|
|
|
|
return -1;
|
2014-09-23 11:47:54 +04:00
|
|
|
|
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
if (mbr->mbr_part[0].part_typ == MBR_PTYPE_PMBR)
|
|
|
|
break;
|
|
|
|
if (i == 4) {
|
2015-12-01 12:05:33 +03:00
|
|
|
gpt_warnx(gpt, "No valid PMBR partition found");
|
|
|
|
return -1;
|
2014-09-23 11:47:54 +04:00
|
|
|
}
|
|
|
|
if (last > 0xffffffff) {
|
|
|
|
mbr->mbr_part[0].part_size_lo = htole16(0xffff);
|
|
|
|
mbr->mbr_part[0].part_size_hi = htole16(0xffff);
|
|
|
|
} else {
|
2015-12-03 05:02:43 +03:00
|
|
|
mbr->mbr_part[0].part_size_lo = htole16((uint16_t)last);
|
|
|
|
mbr->mbr_part[0].part_size_hi = htole16((uint16_t)(last >> 16));
|
2014-09-23 11:47:54 +04:00
|
|
|
}
|
2015-12-01 12:05:33 +03:00
|
|
|
if (gpt_write(gpt, mbrmap) == -1) {
|
|
|
|
gpt_warnx(gpt, "Error writing PMBR");
|
|
|
|
return -1;
|
|
|
|
}
|
2014-09-23 11:47:54 +04:00
|
|
|
|
2015-12-01 12:05:33 +03:00
|
|
|
return 0;
|
2014-09-23 11:47:54 +04:00
|
|
|
}
|
|
|
|
|
2015-12-01 19:32:19 +03:00
|
|
|
static int
|
2015-12-01 12:05:33 +03:00
|
|
|
cmd_resizedisk(gpt_t gpt, int argc, char *argv[])
|
2014-09-23 11:47:54 +04:00
|
|
|
{
|
2015-12-01 12:05:33 +03:00
|
|
|
int ch;
|
2015-12-04 19:46:24 +03:00
|
|
|
off_t sector, size = gpt->mediasz;
|
2014-09-23 11:47:54 +04:00
|
|
|
|
|
|
|
while ((ch = getopt(argc, argv, "s:")) != -1) {
|
|
|
|
switch(ch) {
|
|
|
|
case 's':
|
2015-12-01 22:25:24 +03:00
|
|
|
if (gpt_add_ais(gpt, NULL, NULL, &size, ch) == -1)
|
|
|
|
return -1;
|
2014-09-23 11:47:54 +04:00
|
|
|
break;
|
|
|
|
default:
|
2015-12-01 19:32:19 +03:00
|
|
|
return usage();
|
2014-09-23 11:47:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-01 12:05:33 +03:00
|
|
|
if (argc != optind)
|
2015-12-01 19:32:19 +03:00
|
|
|
return usage();
|
2014-09-23 11:47:54 +04:00
|
|
|
|
2015-12-03 05:02:43 +03:00
|
|
|
if ((sector = gpt_check_ais(gpt, 0, (u_int)~0, size)) == -1)
|
2015-12-01 12:05:33 +03:00
|
|
|
return -1;
|
2014-09-23 11:47:54 +04:00
|
|
|
|
2015-12-04 19:46:24 +03:00
|
|
|
if (--sector == 0) {
|
2015-12-05 00:39:18 +03:00
|
|
|
gpt_warnx(gpt, "New size %ju too small", (uintmax_t)size);
|
2015-12-04 19:46:24 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-12-03 04:07:28 +03:00
|
|
|
return resizedisk(gpt, sector, size);
|
2014-09-23 11:47:54 +04:00
|
|
|
}
|