Skip filesystem shrinking without error if small

This commit is contained in:
Joel Busch 2022-02-28 23:57:26 +01:00
parent 43f1a885cd
commit 2d97ee2195
1 changed files with 25 additions and 25 deletions

View File

@ -332,36 +332,36 @@ fi
minsize=$(cut -d ':' -f 2 <<< "$minsize" | tr -d ' ') minsize=$(cut -d ':' -f 2 <<< "$minsize" | tr -d ' ')
logVariables $LINENO currentsize minsize logVariables $LINENO currentsize minsize
if [[ $currentsize -eq $minsize ]]; then if [[ $currentsize -eq $minsize ]]; then
error $LINENO "Image already shrunk to smallest size" info "Filesystem already shrunk to smallest size. Skipping filesystem shrinking."
exit 11 else
fi #Add some free space to the end of the filesystem
extra_space=$(($currentsize - $minsize))
#Add some free space to the end of the filesystem logVariables $LINENO extra_space
extra_space=$(($currentsize - $minsize)) for space in 5000 1000 100; do
logVariables $LINENO extra_space
for space in 5000 1000 100; do
if [[ $extra_space -gt $space ]]; then if [[ $extra_space -gt $space ]]; then
minsize=$(($minsize + $space)) minsize=$(($minsize + $space))
break break
fi fi
done done
logVariables $LINENO minsize logVariables $LINENO minsize
#Shrink filesystem #Shrink filesystem
info "Shrinking filesystem" info "Shrinking filesystem"
resize2fs -p "$loopback" $minsize resize2fs -p "$loopback" $minsize
rc=$? rc=$?
if (( $rc )); then if (( $rc )); then
error $LINENO "resize2fs failed with rc $rc" error $LINENO "resize2fs failed with rc $rc"
mount "$loopback" "$mountdir" mount "$loopback" "$mountdir"
mv "$mountdir/etc/rc.local.bak" "$mountdir/etc/rc.local" mv "$mountdir/etc/rc.local.bak" "$mountdir/etc/rc.local"
umount "$mountdir" umount "$mountdir"
losetup -d "$loopback" losetup -d "$loopback"
exit 12 exit 12
fi
sleep 1
fi fi
sleep 1
#Shrink partition #Shrink partition
info "Shrinking partition"
partnewsize=$(($minsize * $blocksize)) partnewsize=$(($minsize * $blocksize))
newpartend=$(($partstart + $partnewsize)) newpartend=$(($partstart + $partnewsize))
logVariables $LINENO partnewsize newpartend logVariables $LINENO partnewsize newpartend