03-crypttab.sh 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # shellcheck shell=sh
  2. : "${CRYPTTAB:=/etc/crypttab}"
  3. command -v cryptsetup && [ -f "${CRYPTTAB}" ] || return
  4. msg 'decrypting devices from crypttab'
  5. exec 4<&0; while read -r name dev pass opts err; do
  6. [ "${name##\#*}" ] || continue
  7. [ "${err}" ] && {
  8. error 'invalid crypttab file'
  9. break
  10. }
  11. copts=''
  12. _dev="$(findfs "${dev}")" || {
  13. error 'unable to resolve' "'${dev}'"
  14. continue
  15. }
  16. dev="${_dev}"
  17. # shellcheck disable=2086
  18. { IFS=,; set -- ${opts}; unset IFS; }
  19. for opt; do case "${opt}" in
  20. readonly|read-only) copts="${copts} -r" ;;
  21. header=*) copts="${copts} --${opt}" ;;
  22. tries=*) copts="${copts} -T ${opt##*=}" ;;
  23. discard) copts="${copts} --allow-discards" ;;
  24. noauto) copts=; continue 2 ;;
  25. esac; done
  26. initprint ' %b*%b decrypting%b "%s"...' \
  27. "${__b}${__cya}" "${__blu}" "${__clr}" "${dev}"
  28. # shellcheck disable=2086
  29. case "${pass}" in
  30. none|-|'')
  31. cryptsetup open ${copts} "${dev}" "${name}" <&4
  32. ;;
  33. *)
  34. cryptsetup open ${copts} -d "${pass}" "${dev}" "${name}"
  35. ;;
  36. esac && ok || failed
  37. done < "${CRYPTTAB}"; exec 4>&-