From e42c7066d7389808a3922330d01e1bf58207f8ff Mon Sep 17 00:00:00 2001 From: Tomaz Zaman Date: Mon, 10 Aug 2026 23:47:28 +0200 Subject: [PATCH 32/77] layerscape: fix first-boot backup GPT write The first-boot script used 'blockdev --getsz' to size the device, but blockdev is not in the busybox image - so the size came out empty, the backup-GPT write was silently skipped, and the uci-default still exited 0 and self-deleted, so it never retried. Use /sys/block/mmcblk0/size (always present, no tool), make the write idempotent (skip if the EFI PART header is already at the last LBA), and exit non-zero to retry next boot until the backup GPT is confirmed - instead of self-deleting on a silent skip. Co-Authored-By: Claude Fable 5 --- .../10-mono-gateway-expand-rootfs | 55 ++++++++++--------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/target/linux/layerscape/base-files/etc/uci-defaults/10-mono-gateway-expand-rootfs b/target/linux/layerscape/base-files/etc/uci-defaults/10-mono-gateway-expand-rootfs index 814defb48b..fa66a82414 100644 --- a/target/linux/layerscape/base-files/etc/uci-defaults/10-mono-gateway-expand-rootfs +++ b/target/linux/layerscape/base-files/etc/uci-defaults/10-mono-gateway-expand-rootfs @@ -4,12 +4,11 @@ # inside a partition already sized to the full eMMC (the on-device GPT # is never rewritten by an upgrade, and its entry array sits in the # first 4 KiB clear of the boot firmware; see mono_gpt.py). -# 2. Write the backup GPT to the end of the device. The flashed image is -# trimmed and carries only the primary GPT, so the secondary copy is -# laid down here from the blob shipped in the boot partition. This makes -# the on-disk table complete so partition tools do not "repair" it. -# uci-defaults run once and self-delete on exit 0; both steps are idempotent -# so a re-run after sysupgrade is harmless. +# 2. Write the backup GPT to the end of the device from the blob shipped in +# the boot partition, so the on-disk table is complete and partition +# tools do not "repair" it. +# uci-defaults self-delete on exit 0; this script exits NON-zero (retries +# next boot) until the backup GPT is confirmed present, and is idempotent. . /lib/functions.sh case "$(board_name)" in @@ -18,33 +17,35 @@ mono,gateway-dk|mono,gateway-dk-sdboot) ;; esac disk=/dev/mmcblk0 +sysfs=/sys/block/mmcblk0/size # size in 512-byte sectors (no tools) rc=0 -# 1. grow rootfs +# 1. grow rootfs (no-op once full) root_dev=$(readlink -f /dev/root 2>/dev/null) [ -b "$root_dev" ] || root_dev=/dev/mmcblk0p2 -if ! resize2fs "$root_dev" 2>&1 | logger -t mono-firstboot; then - logger -t mono-firstboot "resize2fs failed on $root_dev" - rc=1 -fi +resize2fs "$root_dev" 2>&1 | logger -t mono-firstboot -# 2. lay down the backup GPT from the blob in the boot partition -if [ -b /dev/mmcblk0p1 ]; then - mkdir -p /tmp/bootp - if mount -t ext4 -o ro /dev/mmcblk0p1 /tmp/bootp 2>/dev/null; then - if [ -f /tmp/bootp/boot/backup-gpt.bin ]; then - sectors=$(blockdev --getsz "$disk" 2>/dev/null) - if [ -n "$sectors" ]; then - dd if=/tmp/bootp/boot/backup-gpt.bin of="$disk" \ - bs=512 seek=$((sectors - 33)) conv=fsync 2>/dev/null \ - && logger -t mono-firstboot "backup GPT written at sector $((sectors - 33))" \ - || { logger -t mono-firstboot "backup GPT write failed"; rc=1; } - fi - fi - umount /tmp/bootp 2>/dev/null +# 2. backup GPT +sectors=$(cat "$sysfs" 2>/dev/null) +if [ -z "$sectors" ]; then + logger -t mono-firstboot "no $sysfs, deferring backup GPT" + rc=1 +elif [ "$(dd if="$disk" bs=512 skip=$((sectors - 1)) count=1 2>/dev/null | head -c 8)" = "EFI PART" ]; then + : # already present +elif [ -b /dev/mmcblk0p1 ] && mkdir -p /tmp/bootp && \ + mount -t ext4 -o ro /dev/mmcblk0p1 /tmp/bootp 2>/dev/null; then + if [ -f /tmp/bootp/boot/backup-gpt.bin ] && \ + dd if=/tmp/bootp/boot/backup-gpt.bin of="$disk" \ + bs=512 seek=$((sectors - 33)) conv=fsync 2>/dev/null; then + logger -t mono-firstboot "backup GPT written at sector $((sectors - 33))" + else + logger -t mono-firstboot "backup GPT write failed" + rc=1 fi - rmdir /tmp/bootp 2>/dev/null + umount /tmp/bootp 2>/dev/null +else + logger -t mono-firstboot "cannot mount boot partition, deferring backup GPT" + rc=1 fi -# Non-zero keeps this script for a retry next boot. exit $rc -- 2.47.3