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,10 +355,8 @@ 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 #Add some free space to the end of the filesystem
targetsize=$(($minsize + $extraspace * 1024**2 / $blocksize)) targetsize=$(($minsize + $extraspace * 1024**2 / $blocksize))
if [ $targetsize -ge $currentsize ]; then if [ $targetsize -ge $currentsize ]; then
@ -421,9 +416,11 @@ 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"