blob: 9e8e1e7d0065968b0326c4a0bf026abed46ab8e3 (
plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
#!/bin/bash
#
# #BINDIR#/cryptfs: mount/umount encrypted partitions
#
set -e
if [ $# -eq 1 ] && [ "x$1" = 'x-u' ]; then
unmount=true
elif [ $# -eq 0 ]; then
unmount=false
else
>&2 printf 'Usage: %s [-u]\n' "$0"
exit 1
fi
give_all_parent_dirs() {
local odir
local dir
odir="$1"
odir="/${odir#/}"
dir="${odir%/*}"
while [ "${dir}" != '' ]; do
printf '%s %s\n' "${odir}" "${dir}"
dir="${dir%/*}"
done
}
inner_fs_to_mountorder() {
{
echo "${fstab}" | \
grep "^$1 " | \
awk '{print "x " $3}'
echo "${fstab}" | \
sed -n '
s|^\(\S\+\) \S\+ \(\S\+\)\( .*\)\?$|\1 \2|
T
p
' | \
while read -r order dir; do
give_all_parent_dirs "${dir}" | \
awk '{print "'"${order}"' " $1 " " $2}'
done | \
sort -k3,3 -k2r,2 | \
uniq -f2 | \
cut -d' ' -f1,3
} | \
sort -k2r,2 -k1,1 | \
uniq -f1 -d | \
cut -d' ' -f1
}
mountpoint_to_file() {
{
echo "${fstab}" | \
awk '{print $1 " " $3}'
give_all_parent_dirs "$1" | \
awk '{print "x " $2}'
} | \
sort -k2r,2 -k1,1 | \
uniq -f1 -d | \
cut -d' ' -f1 | \
head -n1
}
mountorder_to_device() {
{
printf 'x %s\n' "$1"
echo "${fstab}" | \
awk '{print $1 " " $2}' | \
sort -k2,2 -u
} | \
sort -k2,2 -k1,1 | \
uniq -f1 -d | \
cut -d' ' -f1
}
crypttab=$(
sed '
s|^\s*#CONF_PREFIXES#\s*||
/^\s*#/d
/^\s*$/d
s/\s\+/ /g
' '#ETCDIR#/crypttab' | \
sort -u | \
sed '
s|UUID=|#UUIDDIR#/|
s/\s\+/ /g
s/^ //
'
)
fstab=$(
sed '
s|^\s*#CONF_PREFIXES#\s*||
/^\s*#/d
/^\s*\S\+\s\+[^\/[:space:]]/d
/^\s*$/d
s/\s\+/ /g
' '#ETCDIR#/fstab' | \
sort -u | \
cat -n | \
sed '
s|UUID=|#UUIDDIR#/|
s/\s\+/ /g
s/^ //
s/ \+$//
'
)
mount_details() {
echo "${fstab}" | \
grep "^$2 " | \
while read -r _ source dest fs opts _; do
case "$1" in
'mp')
printf '%s\n' "${dest}"
;;
'dev')
printf '%s\n' "${source}"
;;
'fs')
printf '%s\n' "${fs}"
;;
'all')
printf -- '-t %s ' "${fs}"
if [ -n "${opts}" ] && \
[ "${opts}" != 'defaults' ]; then
printf -- '-o %s ' "${opts}"
fi
printf '%s %s\n' "${source}" "${dest}"
;;
*)
>&2 printf 'unknown mount_details "%s"\n' "$1"
exit 42
;;
esac
done
}
is_mounted() {
mountpoint -q "$(mount_details mp "$1")"
}
is_unlocked() {
cryptsetup status "$1" >/dev/null 2>&1
}
do_mount() {
local inner_fs
if is_mounted "$1"; then
return
fi
inner_fs=$(inner_fs_to_mountorder "$1")
case "$(mount_details fs "$1")" in
'ext'*)
e2fsck -p $(mount_details dev "$1")
;;
'fuse.sshfs'|'fuse.ftps')
local max_wait
local host
max_wait=$(($(date +%s)+120))
host=$(
mount_details dev "$1" | \
sed 's>^\([^:@]*@\)\?\([^:@]\+\):\S*$>\2>'
)
while [ $(date +%s) -lt ${max_wait} ] && \
! #PING6# -c1 "${host}" >/dev/null 2>&1 && \
! ping -c1 "${host}" >/dev/null 2>&1; do
>&2 printf ','
sleep 1
done
;;
esac
if [ -n "${inner_fs}" ]; then
do_unmount -l "${inner_fs}"
fi
mount $(mount_details all "$1")
if [ -n "${inner_fs}" ]; then
do_mount "${inner_fs}"
fi
}
do_unmount() {
local inner_fs
local lazy
if [ "x$1" = 'x-l' ]; then
lazy='-l'
shift
else
lazy=''
fi
if ! is_mounted "$1"; then
return
fi
inner_fs=$(inner_fs_to_mountorder "$1")
if [ -n "${inner_fs}" ]; then
do_unmount -l "${inner_fs}"
fi
umount ${lazy} $(mount_details mp "$1")
if [ -n "${inner_fs}" ]; then
do_mount "${inner_fs}"
fi
}
do_crypt_close() {
if is_unlocked "$1"; then
cryptsetup luksClose "$1"
fi
}
do_crypt_open() {
if ! is_unlocked "$1"; then
cryptsetup luksOpen "$2" "$1" --key-file="$3"
fi
}
echo "${crypttab}" | \
grep -vxF '' | \
while read -r name raw key; do
key="${key%% *}"
key_mount=$(mountpoint_to_file "${key}")
mount_order=$(mountorder_to_device "#MAPDIR#/${name}")
if ${unmount}; then
do_unmount "${mount_order}"
do_crypt_close "${name}"
else
do_mount "${key_mount}"
do_crypt_open "${name}" "${raw}" "${key}"
do_unmount -l "${key_mount}"
do_mount "${mount_order}"
fi
done
# End of file
|