diff options
Diffstat (limited to '')
-rw-r--r-- | bash/aliases-Zenith | 49 |
1 files changed, 36 insertions, 13 deletions
diff --git a/bash/aliases-Zenith b/bash/aliases-Zenith index 73db885..e4e7422 100644 --- a/bash/aliases-Zenith +++ b/bash/aliases-Zenith @@ -1,21 +1,44 @@ # -*- shell-script -*- mount-crypt () { - gpg --decrypt /media/Secondary/.crypt/crypt.key | \ - asroot env XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR}" CRYPT_DIR=~/Crypt sh -ec ' - device="$(losetup --show -f -P /media/Secondary/.crypt/crypt.img)" - printf "%s\n" "${device}" > "${XDG_RUNTIME_DIR}/CryptCrypt" - cryptsetup -d - open "${device}" CryptCrypt - mkdir -p "${CRYPT_DIR}" - mount /dev/mapper/CryptCrypt "${CRYPT_DIR}" - ' + if mountpoint -q -- ~/Crypt; then + printf '%s\n' 'mount-crypt: already mounted' >&2 + elif test -e /dev/mapper/CryptCrypt; then + asroot env CRYPT_DIR=~/Crypt sh -ec \ + 'mount -- /dev/mapper/CryptCrypt "${CRYPT_DIR}"' + else + gpg --decrypt /media/Secondary/.crypt/crypt.key | \ + key2root -k CryptCrypt env \ + CRYPT_IMG=/media/Secondary/.crypt/crypt.img \ + CRYPT_DIR=~/Crypt \ + CRYPT_DEV="$(losetup | sed 's/\s\+/ /g' | cut -d ' ' -f 1,6 | \ + sed -n 's#\(/[^ ]*\) '"$(realpath -- /media/Secondary/.crypt/crypt.img)"'$#\1#p' | sed 1q)" \ + sh -ec ' + if test -z "${CRYPT_DEV}"; then + CRYPT_DEV="$(losetup --show -f -P "${CRYPT_IMG}")" + fi + cryptsetup -d - open "${CRYPT_DEV}" CryptCrypt + mkdir -p -- "${CRYPT_DIR}" + mount -- /dev/mapper/CryptCrypt "${CRYPT_DIR}" + ' + fi } umount-crypt () { - asroot env XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR}" CRYPT_DIR=~/Crypt sh -c ' - umount "${CRYPT_DIR}" - rmdir "${CRYPT_DIR}" - cryptsetup close CryptCrypt - losetup --detach "$(cat -- "${XDG_RUNTIME_DIR}/CryptCrypt")" + asroot env \ + CRYPT_DIR=~/Crypt \ + CRYPT_DEV="$(losetup | sed 's/\s\+/ /g' | cut -d ' ' -f 1,6 | \ + sed -n 's#\(/[^ ]*\) '"$(realpath -- /media/Secondary/.crypt/crypt.img)"'$#\1#p' | sed 1q)" \ + sh -c ' + if mountpoint -q -- "${CRYPT_DIR}"; then + umount -- "${CRYPT_DIR}" + fi + rmdir -- "${CRYPT_DIR}" || : + if test -e /dev/mapper/CryptCrypt; then + cryptsetup close CryptCrypt + fi + if test -n "${CRYPT_DEV}"; then + losetup -d "${CRYPT_DEV}" + fi ' } |