clevis-luks-unlock 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. SUMMARY="Unlocks a LUKSv1 volume"
  21. UUID=cb6e8904-81ff-40da-a84a-07ab9ab5715e
  22. function usage() {
  23. echo >&2
  24. echo "Usage: clevis luks unlock -d DEV [-n NAME]" >&2
  25. echo >&2
  26. echo "$SUMMARY": >&2
  27. echo >&2
  28. echo " -d DEV The LUKS device on which to perform unlocking" >&2
  29. echo >&2
  30. echo " -n NAME The name of the unlocked device node" >&2
  31. echo >&2
  32. exit 1
  33. }
  34. if [ $# -eq 1 -a "$1" == "--summary" ]; then
  35. echo "$SUMMARY"
  36. exit 0
  37. fi
  38. while getopts ":d:n:" o; do
  39. case "$o" in
  40. d) DEV=$OPTARG;;
  41. n) NAME=$OPTARG;;
  42. *) usage;;
  43. esac
  44. done
  45. if [ -z "$DEV" ]; then
  46. echo "Did not specify a device!" >&2
  47. usage
  48. fi
  49. NAME=${NAME:-luks-`cryptsetup luksUUID $DEV`}
  50. while read -r slot state uuid; do
  51. [ "$state" != "active" ] && continue
  52. [ "$uuid" != "$UUID" ] && continue
  53. if pt="`luksmeta load -d $DEV -s $slot -u $UUID | clevis decrypt`"; then
  54. echo -n "$pt" | cryptsetup open -d- "$DEV" "$NAME"
  55. exit 0
  56. fi
  57. done <<< $(luksmeta show -d "$DEV")
  58. exit 1