feature: 0.3.2 -> 0.3.3 upgrade (#1958)

* 0.3.2 -> 0.3.3 upgrade script

* fix upgrade

* integrated image

* fix rip-image

* no U option on older rsync

* permissions and cleanup

* fixes

* label fs

* fix progress reporting

* only create rootfs for lite upgrade

* shrink image after creating

* fix for `blue` partitions
This commit is contained in:
Aiden McClelland
2022-11-23 12:58:46 -07:00
parent b40be8c494
commit 0ecd920ad9
8 changed files with 208 additions and 121 deletions

View File

@@ -2,51 +2,12 @@
set -e
function mktmpfifo () {
TMP_PATH=$(mktemp)
rm $TMP_PATH
mkfifo $TMP_PATH
echo $TMP_PATH
}
TMPDIR=$(mktemp -d)
if ! which pv > /dev/null; then
>&2 echo 'This script would like to use `pv` to show a progress indicator, but it is not installed.'
if which apt-get > /dev/null; then
read -p "Install? [y/N]" -n 1 -r
echo
if [[ "$REPLY" =~ ^[Yy]$ ]]; then
sudo apt-get install pv
fi
elif which pacman > /dev/null; then
read -p "Install? [y/N]" -n 1 -r
echo
if [[ "$REPLY" =~ ^[Yy]$ ]]; then
sudo pacman -S pv
fi
elif which brew > /dev/null; then
read -p "Install? [y/N]" -n 1 -r
echo
if [[ "$REPLY" =~ ^[Yy]$ ]]; then
brew install pv
fi
else
>&2 echo 'This script does not recognize what package manager you have available on your system.'
>&2 echo 'Please go install the utility manually if you want progress reporting.'
fi
fi
ROOT_PARTITION=$(readlink -f /dev/disk/by-label/rootfs)
BOOT_PARTITION=$(readlink -f /dev/disk/by-label/boot)
if [[ "$(uname)" == "Darwin" ]]; then
>&2 echo 'OSX not supported'
exit 1
fi
if ! test -e /dev/disk/by-label/green; then
>&2 echo '`green` partition not found'
exit 1
fi
export SOURCE_PARTITION=$(readlink -f /dev/disk/by-label/green)
if [[ "$SOURCE_PARTITION" =~ ^/dev/loop ]]; then
if [[ "$ROOT_PARTITION" =~ ^/dev/loop ]] || [[ "$BOOT_PARTITION" =~ ^/dev/loop ]]; then
>&2 echo 'You are currently ripping from a loop device.'
>&2 echo 'This is probably a mistake, and usually means you failed to detach a .img file.'
read -p "Continue anyway? [y/N]" -n 1 -r
@@ -56,46 +17,35 @@ if [[ "$SOURCE_PARTITION" =~ ^/dev/loop ]]; then
fi
fi
sudo e2fsck -f ${SOURCE_PARTITION}
sudo resize2fs -M ${SOURCE_PARTITION}
export BLOCK_INFO=$(sudo dumpe2fs ${SOURCE_PARTITION})
export BLOCK_COUNT=$(echo "$BLOCK_INFO" | grep "Block count:" | sed 's/Block count:\s\+//g')
export BLOCK_SIZE=$(echo "$BLOCK_INFO" | grep "Block size:" | sed 's/Block size:\s\+//g')
export FS_SIZE=$[$BLOCK_COUNT*$BLOCK_SIZE]
mkdir -p $TMPDIR/source
mkdir -p $TMPDIR/target
echo "Ripping $FS_SIZE bytes from $SOURCE_PARTITION"
if which pv > /dev/null; then
sudo cat ${SOURCE_PARTITION} | head -c $FS_SIZE | pv -s $FS_SIZE | sudo dd of=update.img bs=1M iflag=fullblock oflag=direct conv=fsync 2>/dev/null
else
sudo cat ${SOURCE_PARTITION} | head -c $FS_SIZE | sudo dd of=update.img bs=1M iflag=fullblock oflag=direct conv=fsync
fi
echo Verifying...
export INPUT_HASH=$(mktemp)
export OUTPUT_HASH=$(mktemp)
if which pv > /dev/null; then
export PV_IN=$(mktmpfifo)
fi
sudo cat ${SOURCE_PARTITION} | head -c $FS_SIZE | tee -a $PV_IN | sha256sum > $INPUT_HASH &
export INPUT_CHILD=$!
sudo cat update.img | head -c $FS_SIZE | tee -a $PV_IN | sha256sum > $OUTPUT_HASH &
export OUTPUT_CHILD=$!
if which pv > /dev/null; then
pv -s $[$FS_SIZE*2] < $PV_IN > /dev/null &
fi
wait $INPUT_CHILD $OUTPUT_CHILD
if which pv > /dev/null; then
rm $PV_IN
fi
if ! [[ "$(cat $INPUT_HASH)" == "$(cat $OUTPUT_HASH)" ]]; then
rm $INPUT_HASH $OUTPUT_HASH
>&2 echo Verification Failed
exit 1
fi
rm $INPUT_HASH $OUTPUT_HASH
echo "Verification Succeeded"
rm -f update.img
truncate -s 5000000000 update.img
mkfs.ext4 update.img
e2label update.img rootfs
sudo mount update.img $TMPDIR/target/
sudo e2label update.img red
echo "Image Relabeled to \"red\""
sudo mount $ROOT_PARTITION $TMPDIR/source/
sudo mount $BOOT_PARTITION $TMPDIR/source/current/boot/
sudo rsync -acvAXH --info=progress2 $TMPDIR/source/current/ $TMPDIR/target/
sudo sed -i 's/PARTUUID=[a-f0-9]+/PARTUUID=cb15ae4d/g' $TMPDIR/target/etc/fstab
sudo sed -i 's/PARTUUID=[a-f0-9]+/PARTUUID=cb15ae4d/g' $TMPDIR/target/boot/cmdline.txt
sudo umount $TMPDIR/source/current/boot/
sudo umount $TMPDIR/source/
sudo umount $TMPDIR/target
rm -rf $TMPDIR
sudo e2fsck -f -y update.img
sudo resize2fs -M update.img
BLOCK_INFO=$(sudo dumpe2fs update.img)
BLOCK_COUNT=$(echo "$BLOCK_INFO" | grep "Block count:" | sed 's/Block count:\s\+//g')
BLOCK_SIZE=$(echo "$BLOCK_INFO" | grep "Block size:" | sed 's/Block size:\s\+//g')
FS_SIZE=$[$BLOCK_COUNT*$BLOCK_SIZE]
truncate -s $FS_SIZE update.img
echo "Compressing..."
if which pv > /dev/null; then