OCaml 4.14.0 rebuild
[arch-packages.git] / cryptsetup / trunk / hooks-encrypt
blob73bf2e67122420096fb1bd8b928cf65ac999a1fc
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"
72 echo "Encryption option '${cryptopt}' not known, ignoring." >&2
74 esac
75 done
76 set +f
77 IFS="$OLDIFS"
78 unset OLDIFS
80 if resolved=$(resolve_device "${cryptdev}" ${rootdelay}); then
81 if cryptsetup isLuks ${resolved} >/dev/null 2>&1; then
82 [ ${DEPRECATED_CRYPT} -eq 1 ] && warn_deprecated
83 dopassphrase=1
84 # If keyfile exists, try to use that
85 if [ -f ${ckeyfile} ]; then
86 if eval cryptsetup --key-file ${ckeyfile} open --type luks ${resolved} ${cryptname} ${cryptargs} ${CSQUIET}; then
87 dopassphrase=0
88 else
89 echo "Invalid keyfile. Reverting to passphrase."
92 # Ask for a passphrase
93 if [ ${dopassphrase} -gt 0 ]; then
94 echo ""
95 echo "A password is required to access the ${cryptname} volume:"
97 #loop until we get a real password
98 while ! eval cryptsetup open --type luks ${resolved} ${cryptname} ${cryptargs} ${CSQUIET}; do
99 sleep 2;
100 done
102 if [ -e "/dev/mapper/${cryptname}" ]; then
103 if [ ${DEPRECATED_CRYPT} -eq 1 ]; then
104 export root="/dev/mapper/root"
106 else
107 err "Password succeeded, but ${cryptname} creation failed, aborting..."
108 return 1
110 elif [ -n "${crypto}" ]; then
111 [ ${DEPRECATED_CRYPT} -eq 1 ] && warn_deprecated
112 msg "Non-LUKS encrypted device found..."
113 if echo "$crypto" | awk -F: '{ exit(NF == 5) }'; then
114 err "Verify parameter format: crypto=hash:cipher:keysize:offset:skip"
115 err "Non-LUKS decryption not attempted..."
116 return 1
118 exe="cryptsetup open --type plain $resolved $cryptname $cryptargs"
119 IFS=: read c_hash c_cipher c_keysize c_offset c_skip <<EOF
120 $crypto
122 [ -n "$c_hash" ] && exe="$exe --hash '$c_hash'"
123 [ -n "$c_cipher" ] && exe="$exe --cipher '$c_cipher'"
124 [ -n "$c_keysize" ] && exe="$exe --key-size '$c_keysize'"
125 [ -n "$c_offset" ] && exe="$exe --offset '$c_offset'"
126 [ -n "$c_skip" ] && exe="$exe --skip '$c_skip'"
127 if [ -f "$ckeyfile" ]; then
128 exe="$exe --key-file $ckeyfile"
129 else
130 echo ""
131 echo "A password is required to access the ${cryptname} volume:"
133 eval "$exe $CSQUIET"
135 if [ $? -ne 0 ]; then
136 err "Non-LUKS device decryption failed. verify format: "
137 err " crypto=hash:cipher:keysize:offset:skip"
138 return 1
140 if [ -e "/dev/mapper/${cryptname}" ]; then
141 if [ ${DEPRECATED_CRYPT} -eq 1 ]; then
142 export root="/dev/mapper/root"
144 else
145 err "Password succeeded, but ${cryptname} creation failed, aborting..."
146 return 1
148 else
149 err "Failed to open encryption mapping: The device ${cryptdev} is not a LUKS volume and the crypto= paramater was not specified."
152 rm -f ${ckeyfile}
155 # vim: set ft=sh ts=4 sw=4 et: