aboutsummaryrefslogblamecommitdiffstats
path: root/bash/aliases-Zenith
blob: e4e74227021d99d3cbee219092a7aa6fa4e049f1 (plain) (tree)
1
2
3


                      




















                                                                                                                                   


                 














                                                                                                                          
                 
 
# -*- shell-script -*-

mount-crypt () {
	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 \
		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
		'
}