| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 | #!/bin/bash
#
# .---.                  .              .
# |                      |              |
# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
#
#                    Freedom in the Cloud
#
# Makes a USB drive containing a gpg key fragment
#
# License
# =======
#
# Copyright (C) 2015-2016 Bob Mottram <bob@freedombone.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
PROJECT_NAME='freedombone'
export TEXTDOMAIN=${PROJECT_NAME}-format
export TEXTDOMAINDIR="/usr/share/locale"
if [ ! $1 ]; then
   echo $'Specify a drive, such as sdb, sdc, etc'
   exit 1
fi
USB_DRIVE_SHORT=${1}
if [[ "$1" == "/dev/"* ]]; then
    USB_DRIVE=$1
    USB_DRIVE_SHORT=$(echo "$USB_DRIVE" | awk -F '/' '{print $3}' | sed 's|1||g' | sed 's|2||g' | sed 's|3||g')
else
    USB_DRIVE=/dev/${1}1
fi
LABEL="${PROJECT_NAME}"
echo $'Partitioning drive'
echo "o
d
2
d
1
n
p
1
a
1
w
" | fdisk /dev/${USB_DRIVE_SHORT};mkfs.ext4 -L "$LABEL" /dev/${USB_DRIVE_SHORT}1
echo $"Formatting $USB_DRIVE as LUKS"
cryptsetup -y -v luksFormat ${USB_DRIVE}
if [ ! "$?" = "0" ]; then
    echo $"Failed to format $USB_DRIVE as LUKS"
    exit 36823
fi
cryptsetup luksOpen ${USB_DRIVE} encrypted_usb
if [ ! "$?" = "0" ]; then
    echo $"Failed to open LUKS formatted drive $USB_DRIVE"
    exit 37232
fi
mkfs.ext4 /dev/mapper/encrypted_usb -L "$LABEL"
if [ ! "$?" = "0" ]; then
    cryptsetup luksClose encrypted_usb
    echo $'Format of drive $USB_DRIVE failed'
    exit 73218
fi
sleep 2
cryptsetup luksClose encrypted_usb
if [ -f /dev/mapper/encrypted_usb ]; then
    rm -rf /dev/mapper/encrypted_usb
fi
echo $'Format completed'
exit 0
 |