freedombone-format 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # Makes a USB drive containing a gpg key fragment
  10. #
  11. # License
  12. # =======
  13. #
  14. # Copyright (C) 2015-2018 Bob Mottram <bob@freedombone.net>
  15. #
  16. # This program is free software: you can redistribute it and/or modify
  17. # it under the terms of the GNU Affero General Public License as published by
  18. # the Free Software Foundation, either version 3 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU Affero General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU Affero General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. PROJECT_NAME='freedombone'
  29. export TEXTDOMAIN=${PROJECT_NAME}-format
  30. export TEXTDOMAINDIR="/usr/share/locale"
  31. if [ ! "$1" ]; then
  32. echo $'Specify a drive, such as sdb, sdc, etc'
  33. exit 1
  34. fi
  35. USB_DRIVE_SHORT=${1}
  36. if [[ "$1" == "/dev/"* ]]; then
  37. USB_DRIVE=$1
  38. USB_DRIVE_SHORT=$(echo "$USB_DRIVE" | awk -F '/' '{print $3}' | sed 's|1||g' | sed 's|2||g' | sed 's|3||g')
  39. else
  40. USB_DRIVE=/dev/${1}1
  41. fi
  42. LABEL="${PROJECT_NAME}"
  43. echo $'Partitioning drive'
  44. echo "o
  45. d
  46. 2
  47. d
  48. 1
  49. n
  50. p
  51. 1
  52. a
  53. 1
  54. w
  55. " | fdisk "/dev/${USB_DRIVE_SHORT}";mkfs.ext4 -L "$LABEL" "/dev/${USB_DRIVE_SHORT}1"
  56. echo $"Formatting $USB_DRIVE as LUKS"
  57. if ! cryptsetup -y -v luksFormat "${USB_DRIVE}"; then
  58. echo $"Failed to format $USB_DRIVE as LUKS"
  59. exit 36823
  60. fi
  61. if ! cryptsetup luksOpen "${USB_DRIVE}" encrypted_usb; then
  62. echo $"Failed to open LUKS formatted drive $USB_DRIVE"
  63. exit 37232
  64. fi
  65. if ! mkfs.ext4 /dev/mapper/encrypted_usb -L "$LABEL"; then
  66. cryptsetup luksClose encrypted_usb
  67. echo $'Format of drive $USB_DRIVE failed'
  68. exit 73218
  69. fi
  70. sleep 2
  71. cryptsetup luksClose encrypted_usb
  72. if [ -f /dev/mapper/encrypted_usb ]; then
  73. rm -rf /dev/mapper/encrypted_usb
  74. fi
  75. echo $'Format completed'
  76. exit 0