summaryrefslogtreecommitdiff
path: root/mount
diff options
context:
space:
mode:
authorErich Eckner <git@eckner.net>2017-07-03 22:22:18 +0200
committerErich Eckner <git@eckner.net>2017-07-03 22:22:18 +0200
commit7c1dacb6659cf0eff6ff683555c837cc45032561 (patch)
tree980b6e7c6815957f869dcc2c04da63b40051b372 /mount
downloadmount-usb-disk-7c1dacb6659cf0eff6ff683555c837cc45032561.tar.xz
Initial commitHEADmaster
Diffstat (limited to 'mount')
-rwxr-xr-xmount67
1 files changed, 67 insertions, 0 deletions
diff --git a/mount b/mount
new file mode 100755
index 0000000..3d8884a
--- /dev/null
+++ b/mount
@@ -0,0 +1,67 @@
+#!/bin/bash
+
+set -e
+
+base_dir="$(readlink -f "${0%/*}")"
+
+if ! mountpoint -q "${base_dir}"; then
+ echo 'Komisch, das Skript liegt nicht, wo es sein sollte.'
+ exit 1
+fi
+
+if ! [ -d "${base_dir%1}2" ]; then
+ echo 'Neben dem Verzeichnis des Skriptes sollte es noch ein zweites geben.'
+ exit
+fi
+
+if mountpoint -q "${base_dir%1}2"; then
+ mount=false
+else
+ mount=true
+fi
+
+device="$(
+ mount | \
+ grep "^\S\+1 on ${base_dir} " | \
+ awk '{print $1}' | \
+ sed 's|1$||'
+)"
+
+if ! [ -b "${device}" ] || \
+ ! [ -b "${device}1" ] || \
+ ! [ -b "${device}2" ]; then
+ echo 'Komisch, das Skript befindet sich nicht auf der Partition, wo es sein sollte.'
+ exit 1
+fi
+
+crypt_device="usb-disk-$(
+ sha512sum "$0" | \
+ head -c16
+)"
+
+tmp_dir="$(mktemp -d)"
+trap 'rm -rf --one-file-system "${tmp_dir}"' EXIT
+
+if ${mount}; then
+
+ mkfifo "${tmp_dir}/fifo"
+
+ gpg -o - -d "/mnt/usb/usbhd-sdg1/key_$(whoami).gpg" > \
+ /dev/null
+
+ gpg -o - -d "/mnt/usb/usbhd-sdg1/key_$(whoami).gpg" > \
+ "${tmp_dir}/fifo" &
+
+ su -c '
+ cryptsetup luksOpen "'"${device}2"'" "'"${crypt_device}"'" --key-file "'"${tmp_dir}/fifo"'"
+ mount "/dev/mapper/'"${crypt_device}"'" "'"${base_dir%1}2"'"
+ '
+
+else
+
+ su -c '
+ umount -l "'"${base_dir%1}2"'"
+ cryptsetup luksClose "'"${crypt_device}"'"
+ '
+
+fi