clevis-luks-regen 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/bin/bash -e
  2. # vim: set ts=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
  3. #
  4. # Copyright (c) 2020 Red Hat, Inc.
  5. # Author: Radovan Sroka <rsroka@redhat.com>
  6. # Author: Sergio Correia <scorreia@redhat.com>
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. . clevis-luks-common-functions
  22. SUMMARY="Regenerate clevis binding"
  23. if [ "${1}" = "--summary" ]; then
  24. echo "${SUMMARY}"
  25. exit 0
  26. fi
  27. usage_and_exit () {
  28. exec >&2
  29. echo "Usage: clevis luks regen [-q] -d DEV -s SLOT"
  30. echo
  31. echo "${SUMMARY}"
  32. echo
  33. echo " -d DEV The LUKS device on which to perform rebinding"
  34. echo
  35. echo " -s SLT The LUKS slot to use"
  36. echo
  37. echo " -q Do not prompt for confirmation"
  38. echo
  39. exit "${1}"
  40. }
  41. QOPT=
  42. while getopts ":hqd:s:" o; do
  43. case "${o}" in
  44. d) DEV="${OPTARG}";;
  45. h) usage_and_exit 0;;
  46. s) SLT="${OPTARG}";;
  47. q) QOPT="-q";;
  48. *) usage_and_exit 1;;
  49. esac
  50. done
  51. if [ -z "${DEV}" ]; then
  52. echo "Did not specify a device!" >&2
  53. exit 1
  54. fi
  55. if [ -z "${SLT}" ]; then
  56. echo "Did not specify a slot!" >&2
  57. exit 1
  58. fi
  59. # Get pin and configuration.
  60. if ! pin_cfg="$(clevis luks list -d "${DEV}" -s "${SLT}")" \
  61. || [ -z "${pin_cfg}" ]; then
  62. exit 1
  63. fi
  64. pin="$(echo "${pin_cfg}" | cut -d' ' -f2)"
  65. cfg="$(echo "${pin_cfg}" | cut -d' ' -f3 | sed -e "s/'//g")"
  66. if [ -z "${pin}" ] || [ -z "${cfg}" ]; then
  67. echo "Invalid pin or configuration" >&2
  68. exit 1
  69. fi
  70. echo "Regenerating binding (device ${DEV}, slot ${SLT}):"
  71. echo "Pin: ${pin}, Config: '${cfg}'"
  72. if [ -z "${QOPT}" ]; then
  73. read -r -p "Do you want to proceed? [ynYN] " ans
  74. [ "${ans}" != "y" ] && [ "${ans}" != "Y" ] && exit 0
  75. fi
  76. if ! clevis_luks_do_bind "${DEV}" "${SLT}" "" "${pin}" "${cfg}" \
  77. "-y" "overwrite"; then
  78. echo "Unable to regenerate binding in ${DEV}:${SLT}" >&2
  79. exit 1
  80. fi
  81. echo "Binding regenerated successfully" >&2