hooks-encrypt 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/usr/bin/ash
  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. # This may happen if third party hooks do the crypt setup
  43. if [ -b "/dev/mapper/${cryptname}" ]; then
  44. echo "Device ${cryptname} already exists, not doing any crypt setup."
  45. return 0
  46. fi
  47. warn_deprecated() {
  48. echo "The syntax 'root=${root}' where '${root}' is an encrypted volume is deprecated"
  49. echo "Use 'cryptdevice=${root}:root root=/dev/mapper/root' instead."
  50. }
  51. for cryptopt in ${cryptoptions//,/ }; do
  52. case ${cryptopt} in
  53. allow-discards)
  54. cryptargs="${cryptargs} --allow-discards"
  55. ;;
  56. *)
  57. echo "Encryption option '${cryptopt}' not known, ignoring." >&2
  58. ;;
  59. esac
  60. done
  61. if resolved=$(resolve_device "${cryptdev}" ${rootdelay}); then
  62. if cryptsetup isLuks ${resolved} >/dev/null 2>&1; then
  63. [ ${DEPRECATED_CRYPT} -eq 1 ] && warn_deprecated
  64. dopassphrase=1
  65. # If keyfile exists, try to use that
  66. if [ -f ${ckeyfile} ]; then
  67. if eval cryptsetup --key-file ${ckeyfile} open --type luks ${resolved} ${cryptname} ${cryptargs} ${CSQUIET}; then
  68. dopassphrase=0
  69. else
  70. echo "Invalid keyfile. Reverting to passphrase."
  71. fi
  72. fi
  73. # Ask for a passphrase
  74. if [ ${dopassphrase} -gt 0 ]; then
  75. echo ""
  76. echo "A password is required to access the ${cryptname} volume:"
  77. #loop until we get a real password
  78. while ! eval cryptsetup open --type luks ${resolved} ${cryptname} ${cryptargs} ${CSQUIET}; do
  79. sleep 2;
  80. done
  81. fi
  82. if [ -e "/dev/mapper/${cryptname}" ]; then
  83. if [ ${DEPRECATED_CRYPT} -eq 1 ]; then
  84. export root="/dev/mapper/root"
  85. fi
  86. else
  87. err "Password succeeded, but ${cryptname} creation failed, aborting..."
  88. return 1
  89. fi
  90. elif [ -n "${crypto}" ]; then
  91. [ ${DEPRECATED_CRYPT} -eq 1 ] && warn_deprecated
  92. msg "Non-LUKS encrypted device found..."
  93. if echo "$crypto" | awk -F: '{ exit(NF == 5) }'; then
  94. err "Verify parameter format: crypto=hash:cipher:keysize:offset:skip"
  95. err "Non-LUKS decryption not attempted..."
  96. return 1
  97. fi
  98. exe="cryptsetup open --type plain $resolved $cryptname $cryptargs"
  99. IFS=: read c_hash c_cipher c_keysize c_offset c_skip <<EOF
  100. $crypto
  101. EOF
  102. [ -n "$c_hash" ] && exe="$exe --hash '$c_hash'"
  103. [ -n "$c_cipher" ] && exe="$exe --cipher '$c_cipher'"
  104. [ -n "$c_keysize" ] && exe="$exe --key-size '$c_keysize'"
  105. [ -n "$c_offset" ] && exe="$exe --offset '$c_offset'"
  106. [ -n "$c_skip" ] && exe="$exe --skip '$c_skip'"
  107. if [ -f "$ckeyfile" ]; then
  108. exe="$exe --key-file $ckeyfile"
  109. else
  110. echo ""
  111. echo "A password is required to access the ${cryptname} volume:"
  112. fi
  113. eval "$exe $CSQUIET"
  114. if [ $? -ne 0 ]; then
  115. err "Non-LUKS device decryption failed. verify format: "
  116. err " crypto=hash:cipher:keysize:offset:skip"
  117. return 1
  118. fi
  119. if [ -e "/dev/mapper/${cryptname}" ]; then
  120. if [ ${DEPRECATED_CRYPT} -eq 1 ]; then
  121. export root="/dev/mapper/root"
  122. fi
  123. else
  124. err "Password succeeded, but ${cryptname} creation failed, aborting..."
  125. return 1
  126. fi
  127. else
  128. err "Failed to open encryption mapping: The device ${cryptdev} is not a LUKS volume and the crypto= paramater was not specified."
  129. fi
  130. fi
  131. rm -f ${ckeyfile}
  132. }
  133. # vim: set ft=sh ts=4 sw=4 et: