Debian Codegraph @

debian/cryptdisks-functions

raw
1 #
2 # This file is for inclusion with
3 # . /lib/cryptsetup/cryptdisks-functions
4 # and should not be executed directly.
5
6 PATH="/usr/sbin:/usr/bin:/sbin:/bin"
7 CRYPTDISKS_ENABLE="Yes"
8
9 #set -x
10
11 # Sanity check #1
12 [ -x /sbin/cryptsetup ] || exit 0
13
14 . /lib/lsb/init-functions
15 . /lib/cryptsetup/functions
16
17 if [ -r /etc/default/cryptdisks ]; then
18 . /etc/default/cryptdisks
19 fi
20
21 MOUNT="$CRYPTDISKS_MOUNT"
22
23
24 # do_start()
25 # Unlock all devices in the crypttab(5)
26 do_start() {
27 [ -s "$TABFILE" ] || return 0
28
29 # Create locking directory before invoking cryptsetup(8) to avoid warnings
30 mkdir -pm0700 /run/cryptsetup
31 modprobe -qb dm-mod || true
32 modprobe -qb dm-crypt || true
33 dmsetup mknodes >/dev/null 2>&1 || true
34
35 if [ "$INITSTATE" != "init" ]; then
36 log_action_begin_msg "Starting $INITSTATE crypto disks"
37 fi
38 mount_fs
39
40 crypttab_foreach_entry _do_start_callback
41
42 umount_fs
43 log_action_end_msg 0
44 }
45 _do_start_callback() {
46 setup_mapping || log_action_end_msg $?
47 }
48
49 # mount_fs()
50 # Premounts file systems
51 mount_fs() {
52 local point
53 MOUNTED=""
54
55 for point in $MOUNT; do
56 if mount "$point" >/dev/null; then
57 MOUNTED="$MOUNTED $point"
58 fi
59 done
60 }
61
62 # Postunmounts file systems
63 umount_fs() {
64 local point
65
66 for point in $MOUNTED; do
67 umount "$point" >/dev/null
68 done
69 }
70
71 # setup_mapping()
72 # Set up a crypttab(5) mapping defined by $CRYPTTAB_NAME,
73 # $CRYPTTAB_SOURCE, $CRYPTTAB_KEY, $CRYPTTAB_OPTIONS.
74 setup_mapping() {
75 if dm_blkdevname "$CRYPTTAB_NAME" >/dev/null; then
76 device_msg "running"
77 return 0
78 fi
79
80 local loud="${DEFAULT_LOUD:-}"
81 crypttab_parse_options --export --missing-path=fail || return 1
82 if [ -n "${CRYPTTAB_OPTION_quiet+x}" ]; then
83 loud="no"
84 elif [ -n "${CRYPTTAB_OPTION_loud+x}" ]; then
85 loud="yes"
86 fi
87
88 if [ -z "${FORCE_START-}" ]; then
89 if [ "$INITSTATE" = "early" -a -n "${CRYPTTAB_OPTION_noearly+x}" ] ||
90 [ "$INITSTATE" != "manual" -a -n "${CRYPTTAB_OPTION_noauto+x}" ]; then
91 device_msg "ignored"
92 return 0
93 fi
94 fi
95
96 if [ -z "${CRYPTTAB_OPTION_keyscript+x}" ] && [ "$CRYPTTAB_KEY" != "none" ]; then
97 if ! crypttab_key_check; then
98 device_msg "invalid key"
99 return 1
100 fi
101 CRYPTTAB_OPTION_tries=1
102 fi
103
104 if ! crypttab_resolve_source; then
105 if [ "$loud" = "yes" ]; then
106 device_msg "skipped, device $CRYPTTAB_SOURCE does not exist"
107 fi
108 return 1
109 fi
110 device_msg "starting"
111
112 local offset_bytes=""
113 if [ -n "${CRYPTTAB_OPTION_offset+x}" ] && [ ${#CRYPTTAB_OPTION_offset} -le 7 ] && [ $CRYPTTAB_OPTION_offset -lt 4194304 ]; then
114 # silently ignore large offset values which might cause the multiplication to overflow...
115 offset_bytes=$((CRYPTTAB_OPTION_offset * 512))
116 fi
117
118 local out tmpdev
119 if [ "$CRYPTTAB_TYPE" != "luks" ] && [ "$CRYPTTAB_TYPE" != "bitlk" ]; then
120 # fail if the device has a filesystem and the disk encryption format doesn't
121 # verify the key digest (unlike LUKS); unless it's swap, otherwise people can't
122 # easily convert an existing plainttext swap partition to an encrypted one
123 if ! out="$(/lib/cryptsetup/checks/un_blkid "$CRYPTTAB_SOURCE" "" ${CRYPTTAB_OPTION_offset+"$offset_bytes"} 2>/dev/null)" &&
124 ! /lib/cryptsetup/checks/blkid "$CRYPTTAB_SOURCE" swap ${CRYPTTAB_OPTION_offset+"$offset_bytes"} >/dev/null; then
125 log_warning_msg "$CRYPTTAB_NAME: the precheck for '$CRYPTTAB_SOURCE' failed: $out"
126 return 1
127 fi
128 fi
129
130 local count=0 maxtries="${CRYPTTAB_OPTION_tries:-3}" fstype rv
131 local target="$CRYPTTAB_NAME"
132 CRYPTTAB_NAME="${CRYPTTAB_NAME}_unformatted" # XXX potential conflict
133 while [ $maxtries -le 0 ] || [ $count -lt $maxtries ]; do
134 if [ -z "${CRYPTTAB_OPTION_keyscript+x}" ] && [ "$CRYPTTAB_KEY" != "none" ]; then
135 # unlock via keyfile
136 unlock_mapping "$CRYPTTAB_KEY"
137 else
138 # unlock interactively or via keyscript
139 CRYPTTAB_NAME="$target" run_keyscript "$count" | unlock_mapping
140 fi
141 rv=$?
142 count=$(( $count + 1 ))
143
144 if [ $rv -ne 0 ] || ! tmpdev="$(dm_blkdevname "$CRYPTTAB_NAME")"; then
145 continue
146 fi
147 if [ -n "${CRYPTTAB_OPTION_check+x}" ] && \
148 ! "$CRYPTTAB_OPTION_check" "$tmpdev" ${CRYPTTAB_OPTION_checkargs+"$CRYPTTAB_OPTION_checkargs"}; then
149 log_warning_msg "$target: the check for '$CRYPTTAB_NAME' failed"
150 cryptsetup remove -- "$CRYPTTAB_NAME"
151 continue
152 fi
153 if [ "${CRYPTTAB_OPTION_swap+x}" ]; then
154 if out="$(/lib/cryptsetup/checks/un_blkid "$tmpdev" "" ${CRYPTTAB_OPTION_offset+"$offset_bytes"} 2>/dev/null)" ||
155 /lib/cryptsetup/checks/blkid "$tmpdev" swap ${CRYPTTAB_OPTION_offset+"$offset_bytes"} >/dev/null 2>&1; then
156 mkswap "$tmpdev" >/dev/null 2>&1
157 else
158 log_warning_msg "$target: the check for '$CRYPTTAB_NAME' failed. $CRYPTTAB_NAME contains data: $out"
159 cryptsetup remove -- "$CRYPTTAB_NAME"
160 return 1
161 fi
162 elif [ "${CRYPTTAB_OPTION_tmp+x}" ]; then
163 local tmpdir="$(mktemp --tmpdir="/run/cryptsetup" --directory)" rv=0
164 if ! mkfs -t "$CRYPTTAB_OPTION_tmp" -q "$tmpdev" >/dev/null 2>&1 ||
165 ! mount -t "$CRYPTTAB_OPTION_tmp" "$tmpdev" "$tmpdir" ||
166 ! chmod 1777 "$tmpdir"; then
167 rv=1
168 fi
169 umount "$tmpdir" || true
170 rmdir "$tmpdir" || true
171 [ $rv -eq 0 ] || return $rv
172 fi
173 if command -v udevadm >/dev/null 2>&1; then
174 udevadm settle
175 fi
176 dmsetup rename -- "$CRYPTTAB_NAME" "$target"
177 device_msg "$target" "started"
178 return 0
179 done
180 device_msg "$target" "failed"
181 return 1
182 }
183
184 # Removes all mappings in crypttab, except the ones holding the root
185 # file system or /usr
186 do_stop() {
187 local devno_rootfs devno_usr
188 dmsetup mknodes
189 log_action_begin_msg "Stopping $INITSTATE crypto disks"
190
191 devno_rootfs="$(get_mnt_devno /)" || devno_rootfs=""
192 devno_usr="$(get_mnt_devno /usr)" || devno_usr=""
193
194 crypttab_foreach_entry _do_stop_callback
195 log_action_end_msg 0
196 }
197 _do_stop_callback() {
198 local skip="n" devno rv=0
199
200 # traverse the device tree for each crypttab(5) entry and mark / and
201 # /usr holders as skipped. that's suboptimal but we can't use
202 # mapped device names as they might contain any character other than
203 # NUL. shouldn't be much overhead anyway as the device tree is
204 # likely not that long
205 foreach_cryptdev _do_stop_skipped $devno_rootfs $devno_usr
206 [ "$skip" = "n" ] || return $rv
207
208 if devno="$(dmsetup info -c --noheadings -o devno -- "$CRYPTTAB_NAME" 2>/dev/null)" && [ -n "$devno" ]; then
209 foreach_cryptdev --reverse _do_stop_remove "$devno" || rv=$? # try to remove slave devices first
210 fi
211 return $rv
212 }
213 _do_stop_skipped() {
214 if [ "$1" = "$CRYPTTAB_NAME" ]; then
215 skip="y"
216 fi
217 }
218 _do_stop_remove() {
219 local name="$1" i rv=0
220 for i in 1 2 4 8 16 32; do
221 remove_mapping "$name" 3<&- && break || rv=$?
222 if [ $rv -eq 1 ] || [ $rv -eq 2 -a $i -gt 16 ]; then
223 log_action_end_msg $rv
224 break
225 fi
226 log_action_cont_msg "$name busy..."
227 sleep $i
228 done
229 }
230
231 # device_msg([$name], $message)
232 # Convenience function to handle $VERBOSE
233 device_msg() {
234 local name message
235 if [ $# -eq 1 ]; then
236 name="$CRYPTTAB_NAME"
237 message="$1"
238 else
239 name="$1"
240 message="$2"
241 fi
242
243 if [ "$VERBOSE" != "no" ]; then
244 log_action_cont_msg "$name ($message)"
245 fi
246 }
247
248 # remove_mapping($target)
249 # Remove mapping $target
250 remove_mapping() {
251 local CRYPTTAB_NAME="$1"
252
253 if ! dm_blkdevname "$CRYPTTAB_NAME" >/dev/null; then
254 device_msg "stopped"
255 return 0
256 fi
257
258 if [ "$(dmsetup info --noheadings -c -o subsystem -- "$CRYPTTAB_NAME")" != "CRYPT" ]; then
259 device_msg "error"
260 return 1
261 fi
262
263 local opencount="$(dmsetup info -c --noheadings -o open -- "$CRYPTTAB_NAME" 2>/dev/null || true)"
264 if [ -z "$opencount" ]; then
265 device_msg "error"
266 return 1
267 elif [ "$opencount" != "0" ]; then
268 device_msg "busy"
269 if [ "$INITSTATE" = "early" ] || [ "$INITSTATE" = "manual" ]; then
270 return 1
271 elif [ "$INITSTATE" = "remaining" ]; then
272 return 2
273 fi
274 return 0
275 fi
276
277 if cryptsetup remove -- "$CRYPTTAB_NAME"; then
278 device_msg "stopping"
279 return 0
280 else
281 device_msg "error"
282 return 1
283 fi
284 }
285
286 # vim: set filetype=sh :