From eda1092cc58a57ac753c1c35550b5572737cb170 Mon Sep 17 00:00:00 2001 From: framp Date: Wed, 14 Feb 2018 20:05:03 +0100 Subject: [PATCH] Added cleanup function trap --- pishrink.sh | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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