plymouth.encrypt_hook 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/bin/bash
  2. run_hook() {
  3. modprobe -a -q dm-crypt >/dev/null 2>&1
  4. [ "${quiet}" = "y" ] && CSQUIET=">/dev/null"
  5. # Get keyfile if specified
  6. ckeyfile="/crypto_keyfile.bin"
  7. if [ -n "$cryptkey" ]; then
  8. IFS=: read ckdev ckarg1 ckarg2 <<EOF
  9. $cryptkey
  10. EOF
  11. if [ "$ckdev" = "rootfs" ]; then
  12. ckeyfile=$ckarg1
  13. elif resolved=$(resolve_device "${ckdev}" ${rootdelay}); then
  14. case ${ckarg1} in
  15. *[!0-9]*)
  16. # Use a file on the device
  17. # ckarg1 is not numeric: ckarg1=filesystem, ckarg2=path
  18. mkdir /ckey
  19. mount -r -t "$ckarg1" "$resolved" /ckey
  20. dd if="/ckey/$ckarg2" of="$ckeyfile" >/dev/null 2>&1
  21. umount /ckey
  22. ;;
  23. *)
  24. # Read raw data from the block device
  25. # ckarg1 is numeric: ckarg1=offset, ckarg2=length
  26. dd if="$resolved" of="$ckeyfile" bs=1 skip="$ckarg1" count="$ckarg2" >/dev/null 2>&1
  27. ;;
  28. esac
  29. fi
  30. [ ! -f ${ckeyfile} ] && echo "Keyfile could not be opened. Reverting to passphrase."
  31. fi
  32. if [ -n "${cryptdevice}" ]; then
  33. DEPRECATED_CRYPT=0
  34. IFS=: read cryptdev cryptname cryptoptions <<EOF
  35. $cryptdevice
  36. EOF
  37. else
  38. DEPRECATED_CRYPT=1
  39. cryptdev="${root}"
  40. cryptname="root"
  41. fi
  42. warn_deprecated() {
  43. echo "The syntax 'root=${root}' where '${root}' is an encrypted volume is deprecated"
  44. echo "Use 'cryptdevice=${root}:root root=/dev/mapper/root' instead."
  45. }
  46. for cryptopt in ${cryptoptions//,/ }; do
  47. case ${cryptopt} in
  48. allow-discards)
  49. cryptargs="${cryptargs} --allow-discards"
  50. ;;
  51. *)
  52. echo "Encryption option '${cryptopt}' not known, ignoring." >&2
  53. ;;
  54. esac
  55. done
  56. if resolved=$(resolve_device "${cryptdev}" ${rootdelay}); then
  57. if cryptsetup isLuks ${resolved} >/dev/null 2>&1; then
  58. [ ${DEPRECATED_CRYPT} -eq 1 ] && warn_deprecated
  59. dopassphrase=1
  60. # If keyfile exists, try to use that
  61. if [ -f ${ckeyfile} ]; then
  62. if eval cryptsetup --key-file ${ckeyfile} luksOpen ${resolved} ${cryptname} ${cryptargs} ${CSQUIET}; then
  63. dopassphrase=0
  64. else
  65. echo "Invalid keyfile. Reverting to passphrase."
  66. fi
  67. fi
  68. # Ask for a passphrase
  69. if [ ${dopassphrase} -gt 0 ]; then
  70. echo
  71. echo "A password is required to access the ${cryptname} volume:"
  72. plymouth ask-for-password --prompt="Password for ${cryptname} volume" --dont-pause-progress --number-of-tries=5 --command="/sbin/cryptsetup luksOpen --key-file=- ${resolved} ${cryptname} ${cryptargs} ${CSQUIET}"
  73. sleep 2
  74. fi
  75. if [ -e "/dev/mapper/${cryptname}" ]; then
  76. if [ ${DEPRECATED_CRYPT} -eq 1 ]; then
  77. export root="/dev/mapper/root"
  78. fi
  79. else
  80. err "Password succeeded, but ${cryptname} creation failed, aborting..."
  81. exit 1
  82. fi
  83. elif [ -n "${crypto}" ]; then
  84. [ ${DEPRECATED_CRYPT} -eq 1 ] && warn_deprecated
  85. msg "Non-LUKS encrypted device found..."
  86. if echo "$crypto" | awk -F: '{ exit(NF == 5) }'; then
  87. err "Verify parameter format: crypto=hash:cipher:keysize:offset:skip"
  88. err "Non-LUKS decryption not attempted..."
  89. return 1
  90. fi
  91. exe="cryptsetup create $cryptname $resolved $cryptargs"
  92. IFS=: read c_hash c_cipher c_keysize c_offset c_skip <<EOF
  93. $crypto
  94. EOF
  95. [ -n "$c_hash" ] && exe="$exe --hash '$c_hash'"
  96. [ -n "$c_cipher" ] && exe="$exe --cipher '$c_cipher'"
  97. [ -n "$c_keysize" ] && exe="$exe --key-size '$c_keysize'"
  98. [ -n "$c_offset" ] && exe="$exe --offset '$c_offset'"
  99. [ -n "$c_skip" ] && exe="$exe --skip '$c_skip'"
  100. if [ -f "$ckeyfile" ]; then
  101. exe="$exe --key-file $ckeyfile"
  102. else
  103. exe="$exe --verify-passphrase"
  104. echo ""
  105. echo "A password is required to access the ${cryptname} volume:"
  106. fi
  107. eval "$exe $CSQUIET"
  108. if [ $? -ne 0 ]; then
  109. err "Non-LUKS device decryption failed. verify format: "
  110. err " crypto=hash:cipher:keysize:offset:skip"
  111. exit 1
  112. fi
  113. if [ -e "/dev/mapper/${cryptname}" ]; then
  114. if [ ${DEPRECATED_CRYPT} -eq 1 ]; then
  115. export root="/dev/mapper/root"
  116. fi
  117. else
  118. err "Password succeeded, but ${cryptname} creation failed, aborting..."
  119. exit 1
  120. fi
  121. else
  122. err "Failed to open encryption mapping: The device ${cryptdev} is not a LUKS volume and the crypto= paramater was not specified."
  123. fi
  124. fi
  125. rm -f ${ckeyfile}
  126. }