[9/9] expand-on-first-boot: Preserve MBR on expansion of GPT disks

Message ID beff349aa8453fab1e13d87fb1bf5367bed41df2.1704632056.git.jan.kiszka@siemens.com
State Superseded, archived
Headers show
Series Add support for BeaglePlay | expand

Commit Message

Jan Kiszka Jan. 7, 2024, 12:54 p.m. UTC
From: Jan Kiszka <jan.kiszka@siemens.com>

In order to keep potential hybrid partition setups intact, preserve the
legacy MBR when a GPT disk is being expanded. This is needed because we
rewrite the partitioning via sfdisk which overwrites the MBR. In the
hypbrid setup, the legacy MBR plays no role regarding the expanded
partition, thus can be simply left untouched.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
---
 .../files/expand-last-partition.sh                     | 10 ++++++++++
 1 file changed, 10 insertions(+)

Patch

diff --git a/meta/recipes-support/expand-on-first-boot/files/expand-last-partition.sh b/meta/recipes-support/expand-on-first-boot/files/expand-last-partition.sh
index 7d658864..e6dbd62e 100755
--- a/meta/recipes-support/expand-on-first-boot/files/expand-last-partition.sh
+++ b/meta/recipes-support/expand-on-first-boot/files/expand-last-partition.sh
@@ -38,6 +38,11 @@  if [ "$DISK_SIZE" -lt "$MINIMAL_SIZE" ]; then
 	exit 0
 fi
 
+IS_GPT="$(sfdisk -d "${BOOT_DEV}" 2>/dev/null | grep -q "label: gpt" && echo 1)"
+if [ "$IS_GPT" = "1" ]; then
+	dd if="${BOOT_DEV}" of=/dev/shm/__mbr__.bak count=1
+fi
+
 LAST_PART="$(sfdisk -d "${BOOT_DEV}" 2>/dev/null | tail -1 | cut -d ' ' -f 1)"
 
 # Transform the partition table as follows:
@@ -54,6 +59,11 @@  sfdisk -d "${BOOT_DEV}" 2>/dev/null | \
 	sed 's|^\('"${LAST_PART}"' .*, \)size=[^,]*, |\1|' | \
 	sfdisk --force "${BOOT_DEV}"
 
+if [ "$IS_GPT" = "1" ]; then
+	dd if=/dev/shm/__mbr__.bak of="${BOOT_DEV}"
+	rm /dev/shm/__mbr__.bak
+fi
+
 # Inform the kernel about the partitioning change
 partx -u "${LAST_PART}"