updates for t11 and fsf
[automated-distro-installer] / fsf / open-crypt-luks-keys-loopback
diff --git a/fsf/open-crypt-luks-keys-loopback b/fsf/open-crypt-luks-keys-loopback
new file mode 100755 (executable)
index 0000000..09c85fd
--- /dev/null
@@ -0,0 +1,40 @@
+#!/bin/bash
+
+# usage: $0
+# this script is idempotent
+
+# warning: changes here may affect the close 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
+
+l=$(losetup -j $fs_file | sed -rn 's/^([^ ]+): .*/\1/p' | head -n1 ||:)
+if [[ $l ]]; then
+  echo "$0: skipping losetup due to existing loopback: $l"
+else
+  l=$(losetup -f)
+  m losetup $l $fs_file
+fi
+if cryptsetup status /dev/mapper/$mapper_name &>/dev/null; then
+  echo "$0: skipping cryptsetup due to existing /dev/mapper/$mapper_name"
+else
+  if ! cryptsetup luksOpen $l $mapper_name; then
+    echo "$0: error luksOpen failed. detaching loopback" >&2
+    m losetup -d $l
+    exit 1
+  fi
+fi
+if mountpoint -q /dev/mapper/$mapper_name; then
+  echo "$0: skipping mount /dev/mapper/$mapper_name /mnt2 due to existing mount"
+else
+  m mount /dev/mapper/$mapper_name /mnt2
+fi