2 # -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
3 # ex: ts=8 sw=4 sts=4 et filetype=sh
5 PATH=/usr/sbin:/usr/bin:/sbin:/bin
6 NEWROOT=${NEWROOT:-"/sysroot"}
8 # do not ask, if we already have root
9 [ -f $NEWROOT/proc ] && exit 0
11 # check if destination already exists
12 [ -b /dev/mapper/$2 ] && exit 0
14 # we already asked for this device
15 [ -f /tmp/cryptroot-asked-$2 ] && exit 0
17 # load dm_crypt if it is not already loaded
18 [ -d /sys/module/dm_crypt ] || modprobe dm_crypt
20 . /lib/dracut-crypt-lib.sh
22 # default luksname - luks-UUID
25 # fallback to passphrase
28 # if device name is /dev/dm-X, convert to /dev/mapper/name
29 if [ "${1##/dev/dm-}" != "$1" ]; then
30 device="/dev/mapper/$(dmsetup info -c --noheadings -o name "$1")"
35 # TODO: improve to support what cmdline does
36 if [ -f /etc/crypttab ] && getargbool 1 rd.luks.crypttab -n rd_NO_CRYPTTAB; then
37 while read name dev luksfile rest; do
38 # ignore blank lines and comments
39 if [ -z "$name" -o "${name#\#}" != "$name" ]; then
43 # UUID used in crypttab
44 if [ "${dev%%=*}" = "UUID" ]; then
45 if [ "luks-${dev##UUID=}" = "$2" ]; then
50 # path used in crypttab
52 cdev=$(readlink -f $dev)
53 mdev=$(readlink -f $device)
54 if [ "$cdev" = "$mdev" ]; then
67 info "luksOpen $device $luksname $luksfile"
69 if [ -n "$luksfile" -a "$luksfile" != "none" -a -e "$luksfile" ]; then
70 if cryptsetup --key-file "$luksfile" luksOpen "$device" "$luksname"; then
74 while [ -n "$(getarg rd.luks.key)" ]; do
75 if tmp=$(getkey /tmp/luks.keys $device); then
81 info "No key found for $device. Fallback to passphrase mode."
84 info "No key found for $device. Will try $3 time(s) more later."
85 set -- "$1" "$2" "$(($3 - 1))"
87 info "No key found for $device. Will try later."
89 initqueue --unique --onetime --settled \
90 --name cryptroot-ask-$luksname \
91 $(command -v cryptroot-ask) "$@"
96 info "Using '$keypath' on '$keydev'"
97 readkey "$keypath" "$keydev" "$device" \
98 | cryptsetup -d - luksOpen "$device" "$luksname"
105 if [ $ask_passphrase -ne 0 ]; then
106 luks_open="$(command -v cryptsetup) luksOpen"
107 ask_for_password --ply-tries 5 \
108 --ply-cmd "$luks_open -T1 $device $luksname" \
109 --ply-prompt "Password ($device)" \
111 --tty-cmd "$luks_open -T5 $device $luksname"
115 unset device luksname luksfile
117 # mark device as asked
118 >> /tmp/cryptroot-asked-$2