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
losetup -d "$loopback"
fi
if [ "$debug" = true ]; then
local old_owner=$(stat -c %u:%g "$src")
chown "$old_owner" "$LOGFILE"
fi
}
function logVariables() {
@ -299,6 +294,7 @@ if (( $rc )); then
info "Possibly invalid image. Run 'parted $img unit B print' manually to investigate"
exit 8
fi
# Determine the last partition number and its starting block.
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')"
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
parttype="logical"
fi
# Now mount the last partition as a loopback device..
loopback="$(losetup -f --show -o "$partstart" "$img")"
# Wait 3 seconds to ensure loopback is ready.
sleep 3
@ -358,10 +355,8 @@ fi
minsize=$(cut -d ':' -f 2 <<< "$minsize" | tr -d ' ')
logVariables $LINENO currentsize minsize
if [[ $currentsize -eq $minsize ]]; then
error $LINENO "Image already shrunk to smallest size"
exit 11
fi
info "Image already shrunk to smallest size"
else
#Add some free space to the end of the filesystem
targetsize=$(($minsize + $extraspace * 1024**2 / $blocksize))
if [ $targetsize -ge $currentsize ]; then
@ -421,9 +416,11 @@ if (( $rc )); then
error $LINENO "trunate failed with rc $rc"
exit 16
fi
fi
# handle compression
if [[ -n $ziptool ]]; then
info "Compressing"
options=""
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]}"
@ -453,4 +450,5 @@ fi
aftersize=$(ls -lh "$img" | cut -d ' ' -f 5)
logVariables $LINENO aftersize
info "Shrunk $img from $beforesize to $aftersize"
info "Shrunk/compressed $img from $beforesize to $aftersize"
ls -lh "$img"