#!/bin/bash # usage: $0 # this script is idempotent # warning: changes here may affect the open version of this script if ! test "$BASH_VERSION"; then echo "error: shell is not bash" >&2; exit 1; fi shopt -s inherit_errexit 2>/dev/null ||: # ignore fail in bash < 4.4 set -eE -o pipefail trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" exit status: $?, PIPESTATUS: ${PIPESTATUS[*]}" >&2' ERR m() { printf "%s\n" "$*"; "$@"; } fs_file=/root/crypt-luks-keys-loopback mapper_name=crypt-luks-keys-loopback if mountpoint /mnt2 &>/dev/null; then m umount /mnt2 fi if cryptsetup status /dev/mapper/$mapper_name &>/dev/null; then m cryptsetup luksClose /dev/mapper/$mapper_name; then fi l=$(losetup -l --noheadings | awk '$6 ~ /\/'$mapper_name'$/ {print $1}') if [[ $l ]]; then m losetup -d $l else echo "$0: warning: no loopback device found" >&2 fi