Added cleanup function trap
This commit is contained in:
parent
83e72eeec3
commit
eda1092cc5
|
@ -1,5 +1,9 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
|
function cleanup() {
|
||||||
|
losetup -d "$loopback" || true
|
||||||
|
}
|
||||||
|
|
||||||
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 +54,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 +173,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
|
||||||
|
|
Loading…
Reference in New Issue