clevis-luks-unlock 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/bin/bash -e
  2. # vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
  3. #
  4. # Copyright (c) 2016 Red Hat, Inc.
  5. # Author: Nathaniel McCallum <npmccallum@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. . clevis-luks-common-functions
  21. SUMMARY="Unlocks a LUKS volume"
  22. function usage() {
  23. exec >&2
  24. echo
  25. echo "Usage: clevis luks unlock -d DEV [-n NAME]"
  26. echo
  27. echo "$SUMMARY":
  28. echo
  29. echo " -d DEV The LUKS device on which to perform unlocking"
  30. echo
  31. echo " -n NAME The name of the unlocked device node"
  32. echo
  33. exit 2
  34. }
  35. if [ $# -eq 1 ] && [ "$1" == "--summary" ]; then
  36. echo "$SUMMARY"
  37. exit 0
  38. fi
  39. while getopts ":d:n:" o; do
  40. case "$o" in
  41. d) DEV="$OPTARG";;
  42. n) NAME="$OPTARG";;
  43. *) usage;;
  44. esac
  45. done
  46. if [ -z "$DEV" ]; then
  47. echo "Did not specify a device!" >&2
  48. usage
  49. fi
  50. if ! cryptsetup isLuks "$DEV"; then
  51. echo "$DEV is not a LUKS device!" >&2
  52. exit 1
  53. fi
  54. NAME="${NAME:-luks-"$(cryptsetup luksUUID "$DEV")"}"
  55. if ! pt=$(clevis_luks_unlock_device "${DEV}"); then
  56. echo "${DEV} could not be opened." >&2
  57. exit 1
  58. fi
  59. cryptsetup open -d- "${DEV}" "${NAME}" < <(echo -n "${pt}")