-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathluks.zsh
executable file
·99 lines (93 loc) · 2.08 KB
/
luks.zsh
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
93
94
95
96
97
98
99
#!/usr/bin/zsh
#
# simple luks open/close function for zsh
#
# by Jack (jack@ai4me.de)
#
# place this file in a PATH directory
#
# run with root permissions
#
function usage(){
echo "$0 [-h, --help]"
echo
echo " -h, --help, print usage"
echo
echo " stdin, [1..n] luks open/close"
echo " r reload list"
echo " a abort"
echo
echo "This prints a list of numbered luks volumes and opens a chosen device via cryptsetup."
echo "To open or close a volume enter its number. Any other input will lead to no operation."
}
function main(){
# list all devices
if [ -e /dev/mapper ]; then
a=`ls -dt --color=never /dev/[scm]* /dev/mapper/*|grep -oe "/dev/sd.*" -oe "/dev/mmcblk.*" -oe "/dev/mapper/[^c].*"`
else
a=`ls -dt --color=never /dev/[scm]*|grep -oe "/dev/sd.*" -oe "/dev/mmcblk.*"`
fi
j=0
ldev=()
if [ -e /dev/mapper ]; then
ls -d /dev/mapper/* | grep "/dev/mapper/[^c]*" | while read i; do
j=$((j+1))
lm=`cryptsetup status $i | grep device`
ldev[$j]="$i $lm\n"
done
fi
j=0
dev=()
endev=()
# device vendor
dedev=()
# walk thru devices, check if open, echo
echo $a | while read i;do
cryptsetup isLuks $i
if [ "$?" -ne "1" ];then
j=$((j+1))
m=`echo $ldev|grep $i|grep -oe "/dev/mapper/[A-z0-9]*"`
if [ "$m" != "" ]; then
echo -en "\e[1;32m"
endev[$j]=$m
else
m=$(cat /sys/block/$(basename $i)/device/vendor|grep -o "[A-z]*") > /dev/null 2>&1
dedev[$j]=$m
fi
echo -e "\t$j $i\t\t$m\e[0m"
m=""
dev[$j]=$i
fi
done
echo "(1)"
read b
# no input use 1
if [ "$b" = "" ];then
b=1
fi
# command from user
if [ "$b" = "r" ]; then
main
b="a"
fi
# invalid device number
if ! [[ $(echo {1..$j}) =~ $b ]];then
return
fi
# use pmount to mount or unmount
if [ "$endev[$b]" = "" ];then
num=`ls /dev/mapper/* | grep "lusb" | wc -l`
echo "cryptsetup luksOpen $dev[$b] lusb$num$dedev[$b]";cryptsetup luksOpen $dev[$b] lusb$num$dedev[$b]
else
echo "cryptsetup luksClose $endev[$b]";cryptsetup luksClose $endev[$b]
fi
}
if [ ! "$1" = "" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
usage
return
fi
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
return
fi
main