clevis-luks-list 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/bin/bash -e
  2. # vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
  3. #
  4. # Copyright (c) 2017-2019 Red Hat, Inc.
  5. # Author: Javier Martinez Canillas <javierm@redhat.com>
  6. # Author: Sergio Correia <scorreia@redhat.com> - LUKS2 support.
  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="Lists pins bound to a LUKSv1 or LUKSv2 device"
  23. function usage() {
  24. echo >&2
  25. echo "Usage: clevis luks list -d DEV [-s SLT]" >&2
  26. echo >&2
  27. echo "$SUMMARY": >&2
  28. echo >&2
  29. echo " -d DEV The LUKS device to list bound pins" >&2
  30. echo >&2
  31. echo " -s SLOT The slot number to list" >&2
  32. echo >&2
  33. exit 1
  34. }
  35. if [ ${#} -eq 1 ] && [ "${1}" = "--summary" ]; then
  36. echo "${SUMMARY}"
  37. exit 0
  38. fi
  39. while getopts ":d:s:" o; do
  40. case "$o" in
  41. d) DEV=${OPTARG};;
  42. s) SLT=${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 --type luks1 "${DEV}"; then
  51. if ! luksmeta test -d "${DEV}" 2>/dev/null; then
  52. echo "The ${DEV} device is not valid!" >&2
  53. exit 1
  54. fi
  55. fi
  56. if [ -n "${SLT}" ]; then
  57. clevis_luks_read_pins_from_slot "${DEV}" "${SLT}"
  58. else
  59. if ! used_slots=$(clevis_luks_used_slots "${DEV}"); then
  60. echo "No used slots detected for device ${DEV}!" >&2
  61. exit 1
  62. fi
  63. for s in ${used_slots}; do
  64. if ! clevis_luks_read_pins_from_slot "${DEV}" "${s}"; then
  65. continue
  66. fi
  67. done
  68. fi