From 4816680ba6499d87ade9c356198acdc2df3849d1 Mon Sep 17 00:00:00 2001 From: Andres Salomon Date: Thu, 1 Jul 2021 21:54:29 -0400 Subject: [PATCH] resizerootfs: switch from using sfdisk to parted for resizing partitions sfdisk is a bit crusty - it doesn't understand gpt partition tables very well, for example. By switching to parted, we can handle gpt issues (which may be useful in the future, and is definitely useful for other boards), and we no longer have to hardcode that 4M alignment workaround. Parted will tell us the free space at the end of the disk. Because we're already using partprobe, there's no additional dependencies needed. --- rootfs/etc/initramfs-tools/hooks/rpi-resizerootfs | 2 +- .../scripts/local-bottom/rpi-resizerootfs | 15 +++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/rootfs/etc/initramfs-tools/hooks/rpi-resizerootfs b/rootfs/etc/initramfs-tools/hooks/rpi-resizerootfs index f20edb2..f4776aa 100755 --- a/rootfs/etc/initramfs-tools/hooks/rpi-resizerootfs +++ b/rootfs/etc/initramfs-tools/hooks/rpi-resizerootfs @@ -18,6 +18,6 @@ esac #copy_exec /usr/bin/tail copy_exec /sbin/blkid copy_exec /bin/lsblk -copy_exec /sbin/sfdisk +copy_exec /sbin/parted copy_exec /sbin/partprobe copy_exec /sbin/resize2fs diff --git a/rootfs/etc/initramfs-tools/scripts/local-bottom/rpi-resizerootfs b/rootfs/etc/initramfs-tools/scripts/local-bottom/rpi-resizerootfs index 8bfdae5..09bf36f 100755 --- a/rootfs/etc/initramfs-tools/scripts/local-bottom/rpi-resizerootfs +++ b/rootfs/etc/initramfs-tools/scripts/local-bottom/rpi-resizerootfs @@ -18,9 +18,14 @@ rootpart=$(realpath $ROOT) rootpart_nr=$(blkid -sPART_ENTRY_NUMBER -o value -p $rootpart) rootdev="/dev/$(lsblk -no pkname "$rootpart")" -# Check if there's free space on the device (note: we align the first -# partition at 4MB, so there's always at least 3MB free) -free_space="$(sfdisk -qF $rootdev | tail -n1 | grep -v [^0-9]3M)" +# Parted will detect if the GPT label is messed up and fix it +# automatically, we just need to tell it to do so. +parted -s $rootdev print 2>&1 | grep -z "fix the GPT" && { + echo "Fix" | parted ---pretend-input-tty $rootdev print +} + +# Check if there's free space at the end of the device +free_space="$(parted -m -s $rootdev print free | tail -n1 | grep free)" if test -z "$free_space"; then # Great, we already resized; nothing left to do! exit 0 @@ -32,9 +37,7 @@ log_begin_msg "$0 resizing $ROOT" umount "${rootmnt}" # Expand the partition size to fill the entire device -sfdisk -f $rootdev -N $rootpart_nr <