Skip shrinking if the image reached minimum size.

This commit is contained in:
Phuong Vu 2022-04-28 07:10:56 +07:00
parent 540cd52832
commit a8643bac88
1 changed files with 60 additions and 62 deletions

View File

@ -31,11 +31,6 @@ function cleanup() {
if losetup "$loopback" &>/dev/null; then if losetup "$loopback" &>/dev/null; then
losetup -d "$loopback" losetup -d "$loopback"
fi fi
if [ "$debug" = true ]; then
local old_owner=$(stat -c %u:%g "$src")
chown "$old_owner" "$LOGFILE"
fi
} }
function logVariables() { function logVariables() {
@ -299,6 +294,7 @@ if (( $rc )); then
info "Possibly invalid image. Run 'parted $img unit B print' manually to investigate" info "Possibly invalid image. Run 'parted $img unit B print' manually to investigate"
exit 8 exit 8
fi fi
# Determine the last partition number and its starting block.
partnum="$(echo "$parted_output" | tail -n 1 | cut -d ':' -f 1)" partnum="$(echo "$parted_output" | tail -n 1 | cut -d ':' -f 1)"
partstart="$(echo "$parted_output" | tail -n 1 | cut -d ':' -f 2 | tr -d 'B')" partstart="$(echo "$parted_output" | tail -n 1 | cut -d ':' -f 2 | tr -d 'B')"
if [ -z "$(parted -s "$img" unit B print | grep "$partstart" | grep logical)" ]; then if [ -z "$(parted -s "$img" unit B print | grep "$partstart" | grep logical)" ]; then
@ -306,6 +302,7 @@ if [ -z "$(parted -s "$img" unit B print | grep "$partstart" | grep logical)" ];
else else
parttype="logical" parttype="logical"
fi fi
# Now mount the last partition as a loopback device..
loopback="$(losetup -f --show -o "$partstart" "$img")" loopback="$(losetup -f --show -o "$partstart" "$img")"
# Wait 3 seconds to ensure loopback is ready. # Wait 3 seconds to ensure loopback is ready.
sleep 3 sleep 3
@ -358,72 +355,72 @@ 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 "Image already shrunk to smallest size"
exit 11 else
fi #Add some free space to the end of the filesystem
targetsize=$(($minsize + $extraspace * 1024**2 / $blocksize))
#Add some free space to the end of the filesystem if [ $targetsize -ge $currentsize ]; then
targetsize=$(($minsize + $extraspace * 1024**2 / $blocksize))
if [ $targetsize -ge $currentsize ]; then
info "Target size ($targetsize) too large, force to current size minus 1" info "Target size ($targetsize) too large, force to current size minus 1"
let minsize=$currentsize-1 let minsize=$currentsize-1
else else
minsize=$targetsize minsize=$targetsize
fi fi
logVariables $LINENO targetsize currentsize minsize logVariables $LINENO targetsize currentsize 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 fi
sleep 1 sleep 1
#Shrink partition #Shrink partition
partnewsize=$(($minsize * $blocksize)) partnewsize=$(($minsize * $blocksize))
newpartend=$(($partstart + $partnewsize)) newpartend=$(($partstart + $partnewsize))
logVariables $LINENO partnewsize newpartend logVariables $LINENO partnewsize newpartend
parted -s -a minimal "$img" rm "$partnum" parted -s -a minimal "$img" rm "$partnum"
rc=$? rc=$?
if (( $rc )); then if (( $rc )); then
error $LINENO "parted failed with rc $rc" error $LINENO "parted failed with rc $rc"
exit 13 exit 13
fi fi
parted -s "$img" unit B mkpart "$parttype" "$partstart" "$newpartend" parted -s "$img" unit B mkpart "$parttype" "$partstart" "$newpartend"
rc=$? rc=$?
if (( $rc )); then if (( $rc )); then
error $LINENO "parted failed with rc $rc" error $LINENO "parted failed with rc $rc"
exit 14 exit 14
fi fi
#Truncate the file #Truncate the file
info "Shrinking image" info "Shrinking image"
endresult=$(parted -ms "$img" unit B print free) endresult=$(parted -ms "$img" unit B print free)
rc=$? rc=$?
if (( $rc )); then if (( $rc )); then
error $LINENO "parted failed with rc $rc" error $LINENO "parted failed with rc $rc"
exit 15 exit 15
fi fi
endresult=$(tail -1 <<< "$endresult" | cut -d ':' -f 2 | tr -d 'B') endresult=$(tail -1 <<< "$endresult" | cut -d ':' -f 2 | tr -d 'B')
logVariables $LINENO endresult logVariables $LINENO endresult
truncate -s "$endresult" "$img" truncate -s "$endresult" "$img"
rc=$? rc=$?
if (( $rc )); then if (( $rc )); then
error $LINENO "trunate failed with rc $rc" error $LINENO "trunate failed with rc $rc"
exit 16 exit 16
fi
fi fi
# handle compression # handle compression
if [[ -n $ziptool ]]; then if [[ -n $ziptool ]]; then
info "Compressing"
options="" options=""
envVarname="${MYNAME^^}_${ziptool^^}" # PISHRINK_GZIP or PISHRINK_XZ environment variables allow to override all options for gzip or xz envVarname="${MYNAME^^}_${ziptool^^}" # PISHRINK_GZIP or PISHRINK_XZ environment variables allow to override all options for gzip or xz
[[ $parallel == true ]] && options="${ZIP_PARALLEL_OPTIONS[$ziptool]}" [[ $parallel == true ]] && options="${ZIP_PARALLEL_OPTIONS[$ziptool]}"
@ -453,4 +450,5 @@ fi
aftersize=$(ls -lh "$img" | cut -d ' ' -f 5) aftersize=$(ls -lh "$img" | cut -d ' ' -f 5)
logVariables $LINENO aftersize logVariables $LINENO aftersize
info "Shrunk $img from $beforesize to $aftersize" info "Shrunk/compressed $img from $beforesize to $aftersize"
ls -lh "$img"