db-move: moved firefox-i18n from [testing] to [extra] (any)
[arch-packages.git] / cryptsetup / repos / core-x86_64 / hooks-encrypt
blob031dbb55370b06ea4562fd3e019f1a0d260eee42
1 #!/usr/bin/ash
3 run_hook() {
4 modprobe -a -q dm-crypt >/dev/null 2>&1
5 [ "${quiet}" = "y" ] && CSQUIET=">/dev/null"
7 # Get keyfile if specified
8 ckeyfile="/crypto_keyfile.bin"
9 if [ -n "$cryptkey" ]; then
10 IFS=: read ckdev ckarg1 ckarg2 <<EOF
11 $cryptkey
12 EOF
14 if [ "$ckdev" = "rootfs" ]; then
15 ckeyfile=$ckarg1
16 elif resolved=$(resolve_device "${ckdev}" ${rootdelay}); then
17 case ${ckarg1} in
18 *[!0-9]*)
19 # Use a file on the device
20 # ckarg1 is not numeric: ckarg1=filesystem, ckarg2=path
21 mkdir /ckey
22 mount -r -t "$ckarg1" "$resolved" /ckey
23 dd if="/ckey/$ckarg2" of="$ckeyfile" >/dev/null 2>&1
24 umount /ckey
27 # Read raw data from the block device
28 # ckarg1 is numeric: ckarg1=offset, ckarg2=length
29 dd if="$resolved" of="$ckeyfile" bs=1 skip="$ckarg1" count="$ckarg2" >/dev/null 2>&1
31 esac
33 [ ! -f ${ckeyfile} ] && echo "Keyfile could not be opened. Reverting to passphrase."
36 if [ -n "${cryptdevice}" ]; then
37 DEPRECATED_CRYPT=0
38 IFS=: read cryptdev cryptname cryptoptions <<EOF
39 $cryptdevice
40 EOF
41 else
42 DEPRECATED_CRYPT=1
43 cryptdev="${root}"
44 cryptname="root"
47 # This may happen if third party hooks do the crypt setup
48 if [ -b "/dev/mapper/${cryptname}" ]; then
49 echo "Device ${cryptname} already exists, not doing any crypt setup."
50 return 0
53 warn_deprecated() {
54 echo "The syntax 'root=${root}' where '${root}' is an encrypted volume is deprecated"
55 echo "Use 'cryptdevice=${root}:root root=/dev/mapper/root' instead."
58 set -f
59 OLDIFS="$IFS"; IFS=,
60 for cryptopt in ${cryptoptions}; do
61 case ${cryptopt} in
62 allow-discards|discard)
63 cryptargs="${cryptargs} --allow-discards"
65 no-read-workqueue|perf-no_read_workqueue)
66 cryptargs="${cryptargs} --perf-no_read_workqueue"
68 no-write-workqueue|perf-no_write_workqueue)
69 cryptargs="${cryptargs} --perf-no_write_workqueue"
71 sector-size=*)
72 cryptargs="${cryptargs} --sector-size ${cryptopt#*=}"
75 echo "Encryption option '${cryptopt}' not known, ignoring." >&2
77 esac
78 done
79 set +f
80 IFS="$OLDIFS"
81 unset OLDIFS
83 if resolved=$(resolve_device "${cryptdev}" ${rootdelay}); then
84 if cryptsetup isLuks ${resolved} >/dev/null 2>&1; then
85 [ ${DEPRECATED_CRYPT} -eq 1 ] && warn_deprecated
86 dopassphrase=1
87 # If keyfile exists, try to use that
88 if [ -f ${ckeyfile} ]; then
89 if eval cryptsetup --key-file ${ckeyfile} open --type luks ${resolved} ${cryptname} ${cryptargs} ${CSQUIET}; then
90 dopassphrase=0
91 else
92 echo "Invalid keyfile. Reverting to passphrase."
95 # Ask for a passphrase
96 if [ ${dopassphrase} -gt 0 ]; then
97 if command -v plymouth >/dev/null 2>&1 && plymouth --ping 2>/dev/null; then
98 plymouth ask-for-password \
99 --prompt="A password is required to access the ${cryptname} volume" \
100 --command="cryptsetup open --type luks --key-file=- ${resolved} ${cryptname} ${cryptargs} ${CSQUIET}"
101 else
102 echo ""
103 echo "A password is required to access the ${cryptname} volume:"
105 #loop until we get a real password
106 while ! eval cryptsetup open --type luks ${resolved} ${cryptname} ${cryptargs} ${CSQUIET}; do
107 sleep 2;
108 done
111 if [ -e "/dev/mapper/${cryptname}" ]; then
112 if [ ${DEPRECATED_CRYPT} -eq 1 ]; then
113 export root="/dev/mapper/root"
115 else
116 err "Password succeeded, but ${cryptname} creation failed, aborting..."
117 return 1
119 elif [ -n "${crypto}" ]; then
120 [ ${DEPRECATED_CRYPT} -eq 1 ] && warn_deprecated
121 msg "Non-LUKS encrypted device found..."
122 if echo "$crypto" | awk -F: '{ exit(NF == 5) }'; then
123 err "Verify parameter format: crypto=hash:cipher:keysize:offset:skip"
124 err "Non-LUKS decryption not attempted..."
125 return 1
127 exe="cryptsetup open --type plain $resolved $cryptname $cryptargs"
128 IFS=: read c_hash c_cipher c_keysize c_offset c_skip <<EOF
129 $crypto
131 [ -n "$c_hash" ] && exe="$exe --hash '$c_hash'"
132 [ -n "$c_cipher" ] && exe="$exe --cipher '$c_cipher'"
133 [ -n "$c_keysize" ] && exe="$exe --key-size '$c_keysize'"
134 [ -n "$c_offset" ] && exe="$exe --offset '$c_offset'"
135 [ -n "$c_skip" ] && exe="$exe --skip '$c_skip'"
136 if [ -f "$ckeyfile" ]; then
137 exe="$exe --key-file $ckeyfile"
138 else
139 echo ""
140 echo "A password is required to access the ${cryptname} volume:"
142 eval "$exe $CSQUIET"
144 if [ $? -ne 0 ]; then
145 err "Non-LUKS device decryption failed. verify format: "
146 err " crypto=hash:cipher:keysize:offset:skip"
147 return 1
149 if [ -e "/dev/mapper/${cryptname}" ]; then
150 if [ ${DEPRECATED_CRYPT} -eq 1 ]; then
151 export root="/dev/mapper/root"
153 else
154 err "Password succeeded, but ${cryptname} creation failed, aborting..."
155 return 1
157 else
158 err "Failed to open encryption mapping: The device ${cryptdev} is not a LUKS volume and the crypto= paramater was not specified."
161 rm -f ${ckeyfile}
164 # vim: set ft=sh ts=4 sw=4 et: