run_qemu.sh: use an exit trap for reporting the result and cleanup

This will prevent any leftover files in case of a sudden error or
ctrl+c.
This commit is contained in:
Johannes 'josch' Schauer 2018-12-27 14:14:01 +01:00
parent 97d273aaf6
commit fa3719eaeb
Signed by untrusted user: josch
GPG key ID: F2CBA5C78FBD83E1

View file

@ -5,6 +5,24 @@ set -eu
cachedir="./shared/cache" cachedir="./shared/cache"
tmpdir="$(mktemp -d)" tmpdir="$(mktemp -d)"
cleanup() {
rv=$?
rm -f "$tmpdir/debian-unstable-overlay.qcow"
[ -e "$tmpdir" ] && rmdir "$tmpdir"
if [ -e shared/result.txt ]; then
head --lines=-1 shared/result.txt
res="$(tail --lines=1 shared/result.txt)"
rm shared/result.txt
if [ "$res" -ne 0 ]; then
# this might possibly overwrite another non-zero rv
rv=1
fi
fi
exit $rv
}
trap cleanup INT TERM EXIT
# the path to debian-unstable.qcow must be absolute or otherwise qemu will # the path to debian-unstable.qcow must be absolute or otherwise qemu will
# look for the path relative to debian-unstable-overlay.qcow # look for the path relative to debian-unstable-overlay.qcow
qemu-img create -f qcow2 -b "$(realpath $cachedir)/debian-unstable.qcow" "$tmpdir/debian-unstable-overlay.qcow" qemu-img create -f qcow2 -b "$(realpath $cachedir)/debian-unstable.qcow" "$tmpdir/debian-unstable-overlay.qcow"
@ -18,10 +36,3 @@ qemu-system-x86_64 $KVM -m 512M -nographic \
-serial unix:/tmp/ttyS1,server,nowait \ -serial unix:/tmp/ttyS1,server,nowait \
-virtfs local,id=mmdebstrap,path="$(pwd)/shared",security_model=none,mount_tag=mmdebstrap \ -virtfs local,id=mmdebstrap,path="$(pwd)/shared",security_model=none,mount_tag=mmdebstrap \
-drive file="$tmpdir/debian-unstable-overlay.qcow",cache=unsafe,index=0 -drive file="$tmpdir/debian-unstable-overlay.qcow",cache=unsafe,index=0
head --lines=-1 shared/result.txt
if [ "$(tail --lines=1 shared/result.txt)" -ne 0 ]; then
echo "test.sh failed"
exit 1
fi
rm "$tmpdir/debian-unstable-overlay.qcow" shared/result.txt
rmdir "$tmpdir"