Merge pull request #55 from framps/master
Added cleanup of loop device in case of execution errors Closes #54
This commit is contained in:
commit
fd6c0b1ba4
10
pishrink.sh
10
pishrink.sh
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue