Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
freedombone-format 2.94 KiB
#!/bin/bash
#  _____               _           _
# |   __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# |   __|  _| -_| -_| . | . |     | . | . |   | -_|
# |__|  |_| |___|___|___|___|_|_|_|___|___|_|_|___|
#
#                              Freedom in the Cloud
#
# Makes a USB drive containing a gpg key fragment
#
# License
# =======
#
# Copyright (C) 2015-2019 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"

source "/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-backup"

format_progress_file=/root/.format_progress.txt

if [ ! "$1" ]; then
    echo $'Specify a drive, such as sdb, sdc, etc'
    echo $'Or "simple" to try to autodetect the backup drive'
    exit 1
fi

echo -n '1' > $format_progress_file

simple_format=
drive_device="$1"
if [[ "$drive_device" == 'simple' ]]; then
    drive_device=$(detect_connected_drives)
    if [[ "$drive_device" != 'sd'* ]]; then
        echo $'Drive not detected'
        exit 2
    fi
    simple_format=1
fi

echo -n '2' > $format_progress_file

USB_DRIVE_SHORT=${drive_device}
if [[ "$drive_device" == "/dev/"* ]]; then
    USB_DRIVE=${drive_device}
    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/${drive_device}1
fi

echo -n '3' > $format_progress_file

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 -n '4' > $format_progress_file

if [ ! $simple_format ]; then
    echo $"Formatting $USB_DRIVE as LUKS"
    if ! cryptsetup -y -v luksFormat "${USB_DRIVE}"; then
        echo $"Failed to format $USB_DRIVE as LUKS"
        exit 36
    fi
    if ! cryptsetup open --type luks "${USB_DRIVE}" encrypted_usb; then
        echo $"Failed to open LUKS formatted drive $USB_DRIVE"
        exit 37
    fi
    if ! mkfs.ext4 /dev/mapper/encrypted_usb -L "$LABEL"; then
        cryptsetup close encrypted_usb
        echo $'Format of drive $USB_DRIVE failed'
        exit 73
    fi
    sleep 2
    cryptsetup close encrypted_usb
    if [ -f /dev/mapper/encrypted_usb ]; then
        rm -rf /dev/mapper/encrypted_usb
    fi
fi

echo -n '5' > $format_progress_file

echo $'Format completed'
exit 0