aboutsummaryrefslogtreecommitdiffstats
path: root/bash/aliases-Zenith
blob: e4e74227021d99d3cbee219092a7aa6fa4e049f1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# -*- 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
		'
}