Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sbin/gpt Fix destroying and moving GPT header also for trunc...



details:   https://anonhg.NetBSD.org/src/rev/ed24252293ad
branches:  trunk
changeset: 372385:ed24252293ad
user:      mlelstv <mlelstv%NetBSD.org@localhost>
date:      Sun Nov 20 11:57:02 2022 +0000

description:
Fix destroying and moving GPT header also for truncated/extended
images.

diffstat:

 sbin/gpt/destroy.c    |   4 ++--
 sbin/gpt/gpt.c        |  23 ++++++++++++++++++++---
 sbin/gpt/gpt.h        |   3 ++-
 sbin/gpt/resizedisk.c |  41 +++++++++++------------------------------
 4 files changed, 35 insertions(+), 36 deletions(-)

diffs (161 lines):

diff -r 7fe179d38e22 -r ed24252293ad sbin/gpt/destroy.c
--- a/sbin/gpt/destroy.c        Sat Nov 19 22:09:21 2022 +0000
+++ b/sbin/gpt/destroy.c        Sun Nov 20 11:57:02 2022 +0000
@@ -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 @@
        "destroy",
        cmd_destroy,
        destroyhelp, __arraycount(destroyhelp),
-       GPT_SYNC,
+       GPT_OPTGPT | GPT_SYNC,
 };
 
 #define usage() gpt_usage(NULL, &c_destroy)
diff -r 7fe179d38e22 -r ed24252293ad sbin/gpt/gpt.c
--- a/sbin/gpt/gpt.c    Sat Nov 19 22:09:21 2022 +0000
+++ b/sbin/gpt/gpt.c    Sun Nov 20 11:57:02 2022 +0000
@@ -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 @@
                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;
 
diff -r 7fe179d38e22 -r ed24252293ad sbin/gpt/gpt.h
--- a/sbin/gpt/gpt.h    Sat Nov 19 22:09:21 2022 +0000
+++ b/sbin/gpt/gpt.h    Sun Nov 20 11:57:02 2022 +0000
@@ -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 @@
 #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);
diff -r 7fe179d38e22 -r ed24252293ad sbin/gpt/resizedisk.c
--- a/sbin/gpt/resizedisk.c     Sat Nov 19 22:09:21 2022 +0000
+++ b/sbin/gpt/resizedisk.c     Sun Nov 20 11:57:02 2022 +0000
@@ -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 @@
        "resizedisk",
        cmd_resizedisk,
        resizediskhelp, __arraycount(resizediskhelp),
-       0,
+       GPT_OPTGPT,
 };
 
 #define usage() gpt_usage(NULL, &c_resizedisk)
@@ -123,25 +123,9 @@
 
        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 @@
        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 @@
        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



Home | Main Index | Thread Index | Old Index