From eda1092cc58a57ac753c1c35550b5572737cb170 Mon Sep 17 00:00:00 2001 From: framp Date: Wed, 14 Feb 2018 20:05:03 +0100 Subject: [PATCH 1/3] 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 From 67add2e6135978e9e599b888d41cadc1e58bbf5c Mon Sep 17 00:00:00 2001 From: framp Date: Wed, 14 Feb 2018 20:05:03 +0100 Subject: [PATCH 2/3] Added cleanup function trap --- pishrink.sh | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pishrink.sh b/pishrink.sh index 9e57e6b..4cbc518 100755 --- a/pishrink.sh +++ b/pishrink.sh @@ -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 From 8aae06a4b20b1638237e4a5442a26ebb3fe6a3b1 Mon Sep 17 00:00:00 2001 From: framp Date: Sat, 24 Mar 2018 12:01:59 +0100 Subject: [PATCH 3/3] Modified cleanup --- pishrink.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pishrink.sh b/pishrink.sh index 4cbc518..1d68900 100755 --- a/pishrink.sh +++ b/pishrink.sh @@ -1,7 +1,9 @@ #!/bin/bash function cleanup() { - [[ -n $loopback ]] && losetup -d "$loopback" + if losetup $loopback &>/dev/null; then + losetup -d "$loopback" + fi } usage() { echo "Usage: $0 [-s] imagefile.img [newimagefile.img]"; exit -1; }