clevis-luks-unbind 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/bin/bash -e
  2. # vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
  3. #
  4. # Copyright (c) 2017 Red Hat, Inc.
  5. # Author: Javier Martinez Canillas <javierm@redhat.com>
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. SUMMARY="Unbinds a pin bound to a LUKSv1 volume"
  21. UUID=cb6e8904-81ff-40da-a84a-07ab9ab5715e
  22. function usage() {
  23. echo >&2
  24. echo "Usage: clevis luks unbind -d DEV -s SLT" >&2
  25. echo >&2
  26. echo "$SUMMARY": >&2
  27. echo >&2
  28. echo " -d DEV The bound LUKS device" >&2
  29. echo >&2
  30. echo " -s SLOT The LUKSMeta slot number for the pin unbind" >&2
  31. echo >&2
  32. echo " -f Do not ask for confirmation and wipe slot in batch-mode" >&2
  33. echo >&2
  34. exit 1
  35. }
  36. if [ $# -eq 1 -a "$1" == "--summary" ]; then
  37. echo "$SUMMARY"
  38. exit 0
  39. fi
  40. while getopts ":d:s:f" o; do
  41. case "$o" in
  42. f) FRC=-q;;
  43. d) DEV=$OPTARG;;
  44. s) SLT=$OPTARG;;
  45. *) usage;;
  46. esac
  47. done
  48. if [ -z "$DEV" ]; then
  49. echo "Did not specify a device!" >&2
  50. usage
  51. fi
  52. if [ -z "$SLT" ]; then
  53. echo "Did not specify a slot!" >&2
  54. usage
  55. fi
  56. if ! luksmeta test -d $DEV 2>/dev/null; then
  57. echo "The $DEV device is not valid!" >&2
  58. exit 1
  59. fi
  60. read -r slot active uuid <<< $(luksmeta show -d "$DEV" | grep "^$SLT *")
  61. if [ "$uuid" = "empty" ]; then
  62. echo "The LUKSMeta slot $SLT on device $DEV is already empty." >&2
  63. exit 1
  64. fi
  65. if [ "$active" = "active" ]; then
  66. if ! cryptsetup luksKillSlot "$DEV" "$SLT" $FRC; then
  67. echo "LUKSv1 slot $SLT for device $DEV couldn't be deleted"
  68. exit 1
  69. fi
  70. else
  71. echo "LUKSv1 slot $SLT not present on $DEV, only LUKSMeta slot will be cleared." >&2
  72. if [ -z "$FRC" ]; then
  73. echo "The unbind operation will wipe a slot. This operation is unrecoverable." >&2
  74. read -r -p "Do you wish to erase LUKSMeta slot $SLT on $DEV? [ynYN] " ans < /dev/tty
  75. [[ "$ans" =~ ^[yY]$ ]] || exit 0
  76. fi
  77. fi
  78. if ! luksmeta wipe -f -d "$DEV" -u "$UUID" -s "$SLT"; then
  79. echo "LUKSMeta slot $SLT for device $DEV couldn't be deleted"
  80. exit 1
  81. fi
  82. exit 0