Module Name: src Committed By: mlelstv Date: Sun Nov 20 11:57:02 UTC 2022
Modified Files: src/sbin/gpt: destroy.c gpt.c gpt.h resizedisk.c Log Message: Fix destroying and moving GPT header also for truncated/extended images. To generate a diff of this commit: cvs rdiff -u -r1.13 -r1.14 src/sbin/gpt/destroy.c cvs rdiff -u -r1.82 -r1.83 src/sbin/gpt/gpt.c cvs rdiff -u -r1.43 -r1.44 src/sbin/gpt/gpt.h cvs rdiff -u -r1.18 -r1.19 src/sbin/gpt/resizedisk.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/sbin/gpt/destroy.c diff -u src/sbin/gpt/destroy.c:1.13 src/sbin/gpt/destroy.c:1.14 --- src/sbin/gpt/destroy.c:1.13 Wed Oct 16 19:03:53 2019 +++ src/sbin/gpt/destroy.c Sun Nov 20 11:57:02 2022 @@ -33,7 +33,7 @@ __FBSDID("$FreeBSD: src/sbin/gpt/destroy.c,v 1.6 2005/08/31 01:47:19 marcel Exp $"); #endif #ifdef __RCSID -__RCSID("$NetBSD: destroy.c,v 1.13 2019/10/16 19:03:53 jnemeth Exp $"); +__RCSID("$NetBSD: destroy.c,v 1.14 2022/11/20 11:57:02 mlelstv Exp $"); #endif #include <sys/types.h> @@ -59,7 +59,7 @@ struct gpt_cmd c_destroy = { "destroy", cmd_destroy, destroyhelp, __arraycount(destroyhelp), - GPT_SYNC, + GPT_OPTGPT | GPT_SYNC, }; #define usage() gpt_usage(NULL, &c_destroy) Index: src/sbin/gpt/gpt.c diff -u src/sbin/gpt/gpt.c:1.82 src/sbin/gpt/gpt.c:1.83 --- src/sbin/gpt/gpt.c:1.82 Sun May 24 18:42:20 2020 +++ src/sbin/gpt/gpt.c Sun Nov 20 11:57:02 2022 @@ -35,7 +35,7 @@ __FBSDID("$FreeBSD: src/sbin/gpt/gpt.c,v 1.16 2006/07/07 02:44:23 marcel Exp $"); #endif #ifdef __RCSID -__RCSID("$NetBSD: gpt.c,v 1.82 2020/05/24 18:42:20 jmcneill Exp $"); +__RCSID("$NetBSD: gpt.c,v 1.83 2022/11/20 11:57:02 mlelstv Exp $"); #endif #include <sys/param.h> @@ -574,8 +574,25 @@ gpt_open(const char *dev, int flags, int goto close; if ((found = gpt_gpt(gpt, 1LL, 1)) == -1) goto close; - if (gpt_gpt(gpt, devsz - 1LL, found) == -1) - goto close; + + if (found) { + struct map *map; + struct gpt_hdr *hdr; + + /* + * read secondary GPT from position stored in primary header + * when possible + */ + map = map_find(gpt, MAP_TYPE_PRI_GPT_HDR); + hdr = map ? map->map_data : NULL; + if (hdr && hdr->hdr_lba_alt > 0 && hdr->hdr_lba_alt < (uint64_t)devsz) { + if (gpt_gpt(gpt, (off_t)hdr->hdr_lba_alt, found) == -1) + goto close; + } + } else { + if (gpt_gpt(gpt, devsz - 1LL, found) == -1) + goto close; + } return gpt; Index: src/sbin/gpt/gpt.h diff -u src/sbin/gpt/gpt.h:1.43 src/sbin/gpt/gpt.h:1.44 --- src/sbin/gpt/gpt.h:1.43 Sat Jul 16 12:57:14 2022 +++ src/sbin/gpt/gpt.h Sun Nov 20 11:57:02 2022 @@ -1,4 +1,4 @@ -/* $NetBSD: gpt.h,v 1.43 2022/07/16 12:57:14 mlelstv Exp $ */ +/* $NetBSD: gpt.h,v 1.44 2022/11/20 11:57:02 mlelstv Exp $ */ /*- * Copyright (c) 2002 Marcel Moolenaar @@ -85,6 +85,7 @@ gpt_t gpt_open(const char *, int, int, o #define GPT_TIMESTAMP 0x20 #define GPT_SYNC 0x40 #define GPT_HYBRID 0x80 +#define GPT_OPTGPT 0x4000 #define GPT_OPTDEV 0x8000 void* gpt_read(gpt_t, off_t, size_t); Index: src/sbin/gpt/resizedisk.c diff -u src/sbin/gpt/resizedisk.c:1.18 src/sbin/gpt/resizedisk.c:1.19 --- src/sbin/gpt/resizedisk.c:1.18 Sun May 24 14:42:44 2020 +++ src/sbin/gpt/resizedisk.c Sun Nov 20 11:57:02 2022 @@ -33,7 +33,7 @@ __FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $"); #endif #ifdef __RCSID -__RCSID("$NetBSD: resizedisk.c,v 1.18 2020/05/24 14:42:44 jmcneill Exp $"); +__RCSID("$NetBSD: resizedisk.c,v 1.19 2022/11/20 11:57:02 mlelstv Exp $"); #endif #include <sys/bootblock.h> @@ -62,7 +62,7 @@ struct gpt_cmd c_resizedisk = { "resizedisk", cmd_resizedisk, resizediskhelp, __arraycount(resizediskhelp), - 0, + GPT_OPTGPT, }; #define usage() gpt_usage(NULL, &c_resizedisk) @@ -123,25 +123,9 @@ resizedisk(gpt_t gpt, off_t sector, off_ gpt->tpg = map_find(gpt, MAP_TYPE_SEC_GPT_HDR); 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; - } - } - 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"); - return -1; - } + if (gpt->tpg == NULL || gpt->lbt == NULL) + gpt_warnx(gpt, "No secondary GPT table"); gpt_size = gpt->tbl->map_size; if (sector == oldloc) { @@ -153,7 +137,8 @@ resizedisk(gpt_t gpt, off_t sector, off_ if (sector == 0 && last == oldloc) { if (!quiet) gpt_warnx(gpt, "Device hasn't changed size"); - return 0; + if (gpt->tpg != NULL && gpt->lbt != NULL) + return 0; } for (ent = gpt->tbl->map_data; ent < @@ -183,18 +168,14 @@ resizedisk(gpt_t gpt, off_t sector, off_ if (sector == 0 && last > oldloc) newloc = last; - if (newloc > 0) { - if (gpt->tpg == NULL) { - gpt_warnx(gpt, "No secondary GPT header; run recover"); - return -1; - } - if (gpt->lbt == NULL) { - gpt_warnx(gpt, "Run recover"); - return -1; - } + if (newloc > 0 && gpt->tpg != NULL && gpt->lbt != NULL) { + if (!quiet) + gpt_msg(gpt, "Moving secondary GPT header"); gpt->tpg->map_start = newloc; gpt->lbt->map_start = newloc - gpt_size; } else { + if (!quiet) + gpt_msg(gpt, "Creating new secondary GPT header"); if (sector > 0) newloc = sector; else