Debian Codegraph @

debian/initramfs/scripts/local-top/cryptroot

raw
1 #!/bin/sh
2
3 PREREQ="cryptroot-prepare"
4
5 #
6 # Standard initramfs preamble
7 #
8 prereqs() {
9 # Make sure that cryptroot is run last in local-top
10 local req script="${0##*/}" dir
11 dir="$DESTDIR/scripts/${CRYPTROOT_STAGE-local-top}"
12 for req in "$dir"/*; do
13 test -x "$req" || continue
14 req="${req##*/}"
15 if [ "$req" != "$script" ]; then
16 printf '%s\n' "$req"
17 fi
18 done
19 }
20
21 case $1 in
22 prereqs)
23 prereqs
24 exit 0
25 ;;
26 esac
27
28 . /scripts/functions
29
30 [ -f /lib/cryptsetup/functions ] || return 0
31 . /lib/cryptsetup/functions
32
33
34 # wait_for_source()
35 # Wait for encrypted $CRYPTTAB_SOURCE . Set $CRYPTTAB_SOURCE
36 # to its normalized device name when it shows up;
37 # return 1 if timeout.
38 wait_for_source() {
39 wait_for_udev 10
40
41 if crypttab_resolve_source; then
42 # the device is here already, no need to loop
43 return 0
44 fi
45
46 # If the source device hasn't shown up yet, give it a little while
47 # to allow for asynchronous device discovery (e.g. USB).
48 #
49 # We also need to take into account RAID or other devices that may
50 # only be available on local-block stage. So, wait 5 seconds upfront,
51 # in local-top; if that fails, end execution relying on local-block
52 # invocations. Allow $ROOTDELAY/4 invocations with 1s sleep times (with
53 # a minimum of 20 invocations), and if after that we still fail, then it's
54 # really time to give-up. Variable $initrd_cnt tracks the re-invocations.
55 #
56 # Part of the lines below has been taken from initramfs-tools
57 # scripts/local's local_device_setup(), as suggested per
58 # https://launchpad.net/bugs/164044 .
59
60 local slumber=5
61 if [ "${CRYPTROOT_STAGE-}" = "local-block" ]; then
62 slumber=1
63 fi
64
65 cryptsetup_message "Waiting for encrypted source device $CRYPTTAB_SOURCE..."
66
67 while [ $slumber -gt 0 ]; do
68 sleep 1
69
70 if crypttab_resolve_source; then
71 wait_for_udev 10
72 return 0
73 fi
74
75 slumber=$(( $slumber - 1 ))
76 done
77 return 1
78 }
79
80 # setup_mapping()
81 # Set up a crypttab(5) mapping defined by $CRYPTTAB_NAME,
82 # $CRYPTTAB_SOURCE, $CRYPTTAB_KEY, $CRYPTTAB_OPTIONS.
83 setup_mapping() {
84 local dev initrd_cnt
85
86 # We control here the number of re-invocations of this script from
87 # local-block - the heuristic is $ROOTDELAY/4, with a minimum of 20.
88
89 if [ -f "$CRYPTROOT_COUNT_FILE" ]; then
90 initrd_cnt="$(cat <"$CRYPTROOT_COUNT_FILE")"
91 else
92 initrd_cnt="${ROOTDELAY:-180}"
93 initrd_cnt=$(( initrd_cnt/4 ))
94 if [ $initrd_cnt -lt 20 ]; then
95 initrd_cnt=20
96 fi
97 echo "$initrd_cnt" >"$CRYPTROOT_COUNT_FILE"
98 fi
99
100 # The same target can be specified multiple times
101 # e.g. root and resume lvs-on-lvm-on-crypto
102 if dm_blkdevname "$CRYPTTAB_NAME" >/dev/null; then
103 return 0
104 fi
105
106 crypttab_parse_options --export --missing-path=fail || return 1
107
108 if ! wait_for_source; then
109 if [ $initrd_cnt -eq 0 ]; then
110 # we've given up
111 if [ -n "$panic" ]; then
112 panic "ALERT! encrypted source device $CRYPTTAB_SOURCE does not exist, can't unlock $CRYPTTAB_NAME."
113 else
114 # let the user fix matters if they can
115 echo " ALERT! encrypted source device $CRYPTTAB_SOURCE does not exist, can't unlock $CRYPTTAB_NAME."
116 echo " Check cryptopts=source= bootarg: cat /proc/cmdline"
117 echo " or missing modules, devices: cat /proc/modules; ls /dev"
118 panic "Dropping to a shell."
119 fi
120 return 1 # can't continue because environment is lost
121 else
122 initrd_cnt=$(( initrd_cnt - 1 ))
123 echo "$initrd_cnt" >"$CRYPTROOT_COUNT_FILE"
124 return 0 # allow some attempts on local-block stage
125 fi
126 fi
127
128 # our `cryptroot-unlock` script searches for cryptsetup processes
129 # with a given CRYPTTAB_NAME it their environment
130 export CRYPTTAB_NAME
131
132 if [ -z "${CRYPTTAB_OPTION_keyscript+x}" ]; then
133 # no keyscript: interactive unlocking, or key file
134
135 if [ "${CRYPTTAB_KEY#/FIXME-initramfs-rootmnt/}" != "$CRYPTTAB_KEY" ]; then
136 # skip the mapping for now if the root FS is not mounted yet
137 sed -rn 's/^\s*[^#[:blank:]]\S*\s+(\S+)\s.*/\1/p' /proc/mounts | grep -Fxq -- "$rootmnt" || return 1
138 # substitute the "/FIXME-initramfs-rootmnt/" prefix by the real root FS mountpoint otherwise
139 CRYPTTAB_KEY="$rootmnt/${CRYPTTAB_KEY#/FIXME-initramfs-rootmnt/}"
140 fi
141
142 if [ "$CRYPTTAB_KEY" != "none" ]; then
143 if [ ! -e "$CRYPTTAB_KEY" ]; then
144 cryptsetup_message "ERROR: Skipping target $CRYPTTAB_NAME: non-existing key file $CRYPTTAB_KEY"
145 return 1
146 fi
147 # try only once if we have a key file
148 CRYPTTAB_OPTION_tries=1
149 fi
150 fi
151
152 local count=0 maxtries="${CRYPTTAB_OPTION_tries:-3}" fstype vg rv
153 while [ $maxtries -le 0 ] || [ $count -lt $maxtries ]; do
154 if [ -z "${CRYPTTAB_OPTION_keyscript+x}" ] && [ "$CRYPTTAB_KEY" != "none" ]; then
155 # unlock via keyfile
156 unlock_mapping "$CRYPTTAB_KEY"
157 else
158 # unlock interactively or via keyscript
159 run_keyscript "$count" | unlock_mapping
160 fi
161 rv=$?
162 count=$(( $count + 1 ))
163
164 if [ $rv -ne 0 ]; then
165 cryptsetup_message "ERROR: $CRYPTTAB_NAME: cryptsetup failed, bad password or options?"
166 sleep 1
167 continue
168 elif ! dev="$(dm_blkdevname "$CRYPTTAB_NAME")"; then
169 cryptsetup_message "ERROR: $CRYPTTAB_NAME: unknown error setting up device mapping"
170 return 1
171 fi
172
173 if ! fstype="$(get_fstype "$dev")" || [ "$fstype" = "unknown" ]; then
174 if [ "$CRYPTTAB_TYPE" != "luks" ]; then
175 # bad password for plain dm-crypt device? or mkfs not run yet?
176 cryptsetup_message "ERROR: $CRYPTTAB_NAME: unknown fstype, bad password or options?"
177 wait_for_udev 10
178 /sbin/cryptsetup remove -- "$CRYPTTAB_NAME"
179 sleep 1
180 continue
181 fi
182 fi
183
184 cryptsetup_message "$CRYPTTAB_NAME: set up successfully"
185 wait_for_udev 10
186 return 0
187 done
188
189 cryptsetup_message "ERROR: $CRYPTTAB_NAME: maximum number of tries exceeded"
190 exit 1
191 }
192
193
194 #######################################################################
195 # Begin real processing
196
197 mkdir -p /cryptroot # might not exist yet if the main system has no crypttab(5)
198
199 # Do we have any kernel boot arguments?
200 if ! grep -qE '^(.*\s)?cryptopts=' /proc/cmdline; then
201 # ensure $TABFILE exists and has a mtime greater than the boot time
202 # (existing $TABFILE is preserved)
203 touch -- "$TABFILE"
204 else
205 # let the read builtin unescape the '\' as GRUB substitutes '\' by '\\' in the cmdline
206 tr ' ' '\n' </proc/cmdline | sed -n 's/^cryptopts=//p' | while IFS= read cryptopts; do
207 # skip empty values (which can be used to disable the initramfs
208 # scripts for a particular boot, cf. #873840)
209 [ -n "$cryptopts" ] || continue
210 unset -v target source key options
211
212 IFS=","
213 for x in $cryptopts; do
214 case "$x" in
215 target=*) target="${x#target=}";;
216 source=*) source="${x#source=}";;
217 key=*) key="${x#key=}";;
218 *) options="${options+$options,}$x";;
219 esac
220 done
221
222 if [ -z "${source:+x}" ]; then
223 cryptsetup_message "ERROR: Missing source= value in kernel parameter cryptopts=$cryptopts"
224 else
225 # preserve mangling
226 printf '%s %s %s %s\n' "${target:-cryptroot}" "$source" "${key:-none}" "${options-}"
227 fi
228 done >"$TABFILE"
229 fi
230
231 # Do we have any settings from the $TABFILE?
232 if [ -s "$TABFILE" ]; then
233 # Create locking directory before invoking cryptsetup(8) to avoid warnings
234 mkdir -pm0700 /run/cryptsetup
235 modprobe -q dm_crypt
236
237 crypttab_foreach_entry setup_mapping
238 fi
239
240 exit 0