-
Notifications
You must be signed in to change notification settings - Fork 0
/
cryptopen
50 lines (43 loc) · 1.15 KB
/
cryptopen
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
45
46
47
48
#!/usr/bin/env bash
# About: Open and mount a crypted device by just specifying the target directory
# Location: /usr/local/bin/
# Usage:
# $ cryptopen /home/dir
# gets dirs, device names etc automatically from
# /etc/fstab:
# /dev/mapper/cryptdir /home/dir ext3 noauto,defaults 0 0
# /etc/crypttab:
# cryptdir /dev/disk/by-id/ata-Hitachi_HDT731110SLC219_STF389MS33DSGK-part1 - noauto,nofail,luks
#
# $ alias m="cryptopen /home/m"
#
readonly FSTABPATH=/etc/fstab
readonly CRYPTTABPATH=/etc/crypttab
readonly DIR=$1
# I avoid grep to avoid mistaken matches
found=0
while IFS=" " read -r fdev fpath fremainder
do
if [ "${DIR}" = "${fpath}" ]
then
name=${fdev#/dev/mapper/}
while IFS=" " read -r cname cdev cpass copts cremainder
do
if [ "${name}" = "${cname}" ]
then
found=1
break 2
# read confuses cryptsetup, so i put it at the end of the script
fi
done < "${CRYPTTABPATH}"
fi
done < "${FSTABPATH}"
if [ "${found}" -ne 1 ]
then
echo "Not found: ${DIR}"
exit 1
fi
echo "open ${cdev} ${name}"
/sbin/cryptsetup open "${cdev}" "${name}" \
&& /sbin/e2fsck -v -y "${fdev}" \
&& /bin/mount "${fdev}" "${DIR}"