From 0f1df0db903ba576fd17b08197d3066af7a61e5f Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 18 Nov 2023 23:23:40 +0100 Subject: A lot of changes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- bash/aliases-Zenith | 49 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 13 deletions(-) (limited to 'bash/aliases-Zenith') 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 ' } -- cgit v1.2.3-70-g09d2