[parted-devel] [PATCH 1/2] gpt: Check for a relocated partition table

Brian C. Lane bcl at redhat.com
Tue Nov 4 18:55:54 GMT 2025


Other tools, such as gdisk, can relocate the PartitionTableLBA so that
it doesn't interfere with some bootloaders. Parted does not support
using other locations at this time, and will silently rewrite the
partition header using LBA 2, possibly overwriting bootloader data.

This change adds a test to gpt_read that will prompt the user for
confirmation if PartitionTableLBA is not at LBA 2, allowing them to
cancel the operation and protect their bootloader data.

This does not support '--fix' mode because there may be cases where a
user take a disk image, expects to grow it to fit their new disk with
--fix but it also contains a relocated partition table. If this happens
we want to leave the image alone and allow them to use other tools.
---
 gnulib                 |  2 +-
 libparted/labels/gpt.c | 31 +++++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/gnulib b/gnulib
index 480a59b..f4038dc 160000
--- a/gnulib
+++ b/gnulib
@@ -1 +1 @@
-Subproject commit 480a59ba60fa0b43a1cebe218bbcea6a947f1e86
+Subproject commit f4038dcb346fccb58d910e2f0a62c0f45022d2a8
diff --git a/libparted/labels/gpt.c b/libparted/labels/gpt.c
index 780fb70..8c7cb61 100644
--- a/libparted/labels/gpt.c
+++ b/libparted/labels/gpt.c
@@ -1034,6 +1034,22 @@ gpt_read (PedDisk *disk)
             }
         }
 #endif /* !DISCOVER_ONLY */
+
+      // Does the current header point to LBA2? If not, it will be overwritten, possibly
+      // erasing data. Ask the user if this is ok.
+      if (PED_LE64_TO_CPU (primary_gpt->PartitionEntryLBA) != GPT_PRIMARY_PART_TABLE_LBA) {
+          if (ped_exception_throw
+                  (PED_EXCEPTION_ERROR,
+                   (PED_EXCEPTION_YES | PED_EXCEPTION_NO),
+                   _("The Partition Entry Table is not at LBA 2, if you continue it will "
+                     "be relocated to LBA 2, possibly overwriting bootloader data. "
+                     "Continue and overwrite LBA 2?")) != PED_EXCEPTION_YES)
+          {
+              goto error;
+          }
+          write_back = 1;
+      }
+
       pth_free (backup_gpt);
       gpt = primary_gpt;
     }
@@ -1057,6 +1073,21 @@ gpt_read (PedDisk *disk)
           == PED_EXCEPTION_CANCEL)
         goto error_free_gpt;
 
+      // Does the current header point to LBA2? If not, it will be overwritten, possibly
+      // erasing data. Ask the user if this is ok.
+      if (PED_LE64_TO_CPU (primary_gpt->PartitionEntryLBA) != GPT_PRIMARY_PART_TABLE_LBA) {
+          if (ped_exception_throw
+                  (PED_EXCEPTION_ERROR,
+                   (PED_EXCEPTION_YES | PED_EXCEPTION_CANCEL),
+                   _("The Partition Entry Table is not at LBA 2, if you continue it will "
+                     "be relocated to LBA 2, possibly overwriting bootloader data. "
+                     "Continue and overwrite LBA 2?")) != PED_EXCEPTION_YES)
+          {
+              goto error_free_gpt;
+          }
+          write_back = 1;
+      }
+
       gpt = primary_gpt;
     }
   else /* !primary_gpt && backup_gpt */
-- 
2.51.1




More information about the parted-devel mailing list