-
Notifications
You must be signed in to change notification settings - Fork 11
/
mount_encrypted
executable file
·92 lines (79 loc) · 1.76 KB
/
mount_encrypted
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/bash
help() {
echo "usage: mount_encrypted [options] image [mountpoint]"
echo
echo " see also umount_encrypted"
echo
echo " if mountpoint is not provided mounts under /media/image"
echo
echo " --ro mount read only"
echo
exit 255
}
[ "$1" == "--help" ] || [ "$1" == "" ] && help
[ "$1" == "--ro" ] && shift && RO="-o ro"
exit_err() {
exit 1
}
unmkdir() {
rmdir "$MNT"
exit_err
}
unlosetup() {
losetup -d $LOOP
unmkdir
}
uncryptsetup() {
cryptsetup remove $DEV_NAME
unlosetup
}
IMG="$1"
DEV_NAME=$( realpath $IMG | tr -c '[:alnum:]' '_' )
if [ "$2" == "" ]; then
# create mount point from image name
# - replace forward slashes and '..' with underscores
MNT=`echo "$IMG" | tr / _ | sed 's/\.\./__/g'`
MNT="/media/$MNT"
if [ -d "$MNT" ]; then
echo "directory '$MNT' already exists ---"
if [ "$( ls $MNT )" == "" ]; then
echo "... and is empty, so we don't mind"
else
echo "--- and is NOT empty"
exit_err
fi
else
if ! mkdir $MNT; then
exit_err
fi
fi
else
MNT="$2"
fi
LOOP=`losetup -f`
if ! losetup $LOOP "$IMG"; then
unmkdir
else
cyptsetup_command="cryptsetup create -c aes $DEV_NAME $LOOP"
crypt_setup=unknown
crypto_dev_status="$( cryptsetup status $DEV_NAME $LOOP )"
if [ "$?" == "0" ]; then
echo "no need to execute '$cyptsetup_command' - $DEV_NAME $LOOP already exists"
crypt_setup=ok
else
echo "cryptsetup create -c aes $DEV_NAME $LOOP"
if ! cryptsetup create -c aes $DEV_NAME $LOOP; then
unlosetup
crypt_setup=not_ok
else
crypt_setup=ok
fi
fi
if [ "$crypt_setup" == "ok" ]; then
if ! mount $RO /dev/mapper/$DEV_NAME "$MNT"; then
uncryptsetup
else
echo "image is mounted under $MNT"
fi
fi
fi