Merge pull request #55 from framps/master

Added cleanup of loop device in case of execution errors
Closes #54
This commit is contained in:
Drew Bonasera 2018-03-30 13:58:52 -04:00 committed by GitHub
commit fd6c0b1ba4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 1 deletions

View File

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