2 # TODO this one needs some work to work with lots of different
6 /sbin/modprobe -a -q dm-crypt >/dev/null 2>&1
7 if [ -e "/sys/class/misc/device-mapper" ]; then
8 if [ ! -e "/dev/mapper/control" ]; then
9 /bin/mknod "/dev/mapper/control" c $(cat /sys/class/misc/device-mapper/dev | sed 's|:| |')
11 [ "${quiet}" = "y" ] && CSQUIET=">/dev/null"
13 # Get keyfile if specified
14 ckeyfile="/crypto_keyfile.bin"
15 if [ "x${cryptkey}" != "x" ]; then
16 ckdev="$(echo "${cryptkey}" | cut -d: -f1)"
17 ckarg1="$(echo "${cryptkey}" | cut -d: -f2)"
18 ckarg2="$(echo "${cryptkey}" | cut -d: -f3)"
20 if poll_device "${ckdev}" ${rootdelay}; then
23 # Use a file on the device
24 # ckarg1 is not numeric: ckarg1=filesystem, ckarg2=path
26 mount -r -t ${ckarg1} ${ckdev} /ckey
27 dd if=/ckey/${ckarg2} of=${ckeyfile} >/dev/null 2>&1
31 # Read raw data from the block device
32 # ckarg1 is numeric: ckarg1=offset, ckarg2=length
33 dd if=${ckdev} of=${ckeyfile} bs=1 skip=${ckarg1} count=${ckarg2} >/dev/null 2>&1
38 [ ! -f ${ckeyfile} ] && echo "Keyfile could not be opened. Reverting to passphrase."
41 if [ -n "${cryptdevice}" ]; then
43 cryptdev="$(echo "${cryptdevice}" | cut -d: -f1)"
44 cryptname="$(echo "${cryptdevice}" | cut -d: -f2)"
51 echo ${cryptdev} > /.cryptdev
52 echo ${cryptname} > /.cryptname
55 echo "The syntax 'root=${root}' where '${root}' is an encrypted volume is deprecated"
56 echo "Use 'cryptdevice=${root}:root root=/dev/mapper/root' instead."
59 if poll_device "${cryptdev}" ${rootdelay}; then
60 if /sbin/cryptsetup isLuks ${cryptdev} >/dev/null 2>&1; then
61 [ ${DEPRECATED_CRYPT} -eq 1 ] && warn_deprecated
63 # If keyfile exists, try to use that
64 if [ -f ${ckeyfile} ]; then
65 if eval /sbin/cryptsetup --key-file ${ckeyfile} luksOpen ${cryptdev} ${cryptname} ${CSQUIET}; then
68 echo "Invalid keyfile. Reverting to passphrase."
71 # Ask for a passphrase
72 if [ ${dopassphrase} -gt 0 ]; then
74 echo "A password is required to access the ${cryptname} volume:"
76 #loop until we get a real password
77 while ! eval /sbin/cryptsetup luksOpen ${cryptdev} ${cryptname} ${CSQUIET}; do
78 if [ -f /.done ]; then
87 /sbin/ip addr flush dev "$device"
88 /sbin/ip link set down "$device"
90 if [ -f /.done ]; then
94 if [ -f /.cryptdev ]; then
98 if [ -f /.cryptname ]; then
105 if [ -f /var/log/lastlog ]; then
109 if [ -f /var/run/dropbear.pid ]; then
110 rm /var/run/dropbear.pid
114 if [ -e "/dev/mapper/${cryptname}" ]; then
115 if [ ${DEPRECATED_CRYPT} -eq 1 ]; then
116 export root="/dev/mapper/root"
119 err "Password succeeded, but ${cryptname} creation failed, aborting..."
122 elif [ -n "${crypto}" ]; then
123 [ ${DEPRECATED_CRYPT} -eq 1 ] && warn_deprecated
124 msg "Non-LUKS encrypted device found..."
126 if [ $# -ne 5 ]; then
127 err "Verify parameter format: crypto=hash:cipher:keysize:offset:skip"
128 err "Non-LUKS decryption not attempted..."
132 exe="/sbin/cryptsetup create ${cryptname} ${cryptdev}"
133 tmp=$(echo "${crypto}" | cut -d: -f1)
134 [ -n "${tmp}" ] && exe="${exe} --hash \"${tmp}\""
135 tmp=$(echo "${crypto}" | cut -d: -f2)
136 [ -n "${tmp}" ] && exe="${exe} --cipher \"${tmp}\""
137 tmp=$(echo "${crypto}" | cut -d: -f3)
138 [ -n "${tmp}" ] && exe="${exe} --key-size \"${tmp}\""
139 tmp=$(echo "${crypto}" | cut -d: -f4)
140 [ -n "${tmp}" ] && exe="${exe} --offset \"${tmp}\""
141 tmp=$(echo "${crypto}" | cut -d: -f5)
142 [ -n "${tmp}" ] && exe="${exe} --skip \"${tmp}\""
144 if [ -f ${ckeyfile} ]; then
145 exe="${exe} --key-file ${ckeyfile}"
147 exe="${exe} --verify-passphrase"
149 echo "A password is required to access the ${cryptname} volume:"
152 eval "${exe} ${CSQUIET}"
154 if [ $? -ne 0 ]; then
155 err "Non-LUKS device decryption failed. verify format: "
156 err " crypto=hash:cipher:keysize:offset:skip"
160 if [ -e "/dev/mapper/${cryptname}" ]; then
161 if [ ${DEPRECATED_CRYPT} -eq 1 ]; then
162 export root="/dev/mapper/root"
165 err "Password succeeded, but ${cryptname} creation failed, aborting..."
169 err "Failed to open encryption mapping: The device ${cryptdev} is not a LUKS volume and the crypto= paramater was not specified."