freedombone-format 2.3KB

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