diff --git a/pishrink.sh b/pishrink.sh index 9e57e6b..fe6c2ee 100755 --- a/pishrink.sh +++ b/pishrink.sh @@ -1,5 +1,9 @@ #!/bin/bash +function cleanup() { + losetup -d "$loopback" || true +} + usage() { echo "Usage: $0 [-s] imagefile.img [newimagefile.img]"; exit -1; } should_skip_autoexpand=false @@ -50,6 +54,9 @@ if [ -n "$2" ]; then img="$2" fi +# cleanup at script exit +trap cleanup ERR EXIT + #Gather info beforesize=$(ls -lh "$img" | cut -d ' ' -f 5) parted_output=$(parted -ms "$img" unit B print | tail -n 1) @@ -166,7 +173,6 @@ fi sleep 1 #Shrink partition -losetup -d "$loopback" partnewsize=$(($minsize * $blocksize)) newpartend=$(($partstart + $partnewsize)) parted -s -a minimal "$img" rm $partnum >/dev/null