Added cleanup function trap

This commit is contained in:
framp 2018-02-14 20:05:03 +01:00
parent 83e72eeec3
commit 67add2e613
1 changed files with 7 additions and 1 deletions

View File

@ -1,5 +1,9 @@
#!/bin/bash
function cleanup() {
[[ -n $loopback ]] && losetup -d "$loopback"
}
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