nixfiles/extras/mk-raspberry-ext-drive.sh
2023-09-13 19:26:50 +01:00

66 lines
1.5 KiB
Bash

#! @bash@/bin/sh
set -e
# e.g /dev/nvme0n1
DRIVE_PATH=$1
KEY_FILE=$2
TEMP_MOUNTPOINT=$3
if echo "$DRIVE_PATH" | grep -q "[0-9]$"; then
PARTITION_SEPARATOR="p"
else
PARTITION_SEPARATOR=""
fi
if [ -z "$DRIVE_PATH" ]; then
echo "Please specify a path to device as first argument"
exit 1
fi
if [ -z "$KEY_FILE" ]; then
echo "Please specify a key file to use"
exit 1
fi
if [ -z "$TEMP_MOUNTPOINT" ]; then
echo "Please specify a temp mountpoint to use"
exit 1
fi
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit
fi
# encrypted partition label
ENCRYPTED_LABEL=@ENCRYPTED_LABEL@
# unencrypted filesystem label
UNENCRYPTED_LABEL=@UNENCRYPTED_LABEL@
echo "Wiping Partitions..."
@util-linux@/bin/wipefs --all ${DRIVE_PATH}
echo "Creating Encrypted Partition"
@cryptsetup@/bin/cryptsetup luksFormat "${DRIVE_PATH}" --key-file "${KEY_FILE}" --label "${ENCRYPTED_LABEL}"
echo "Opening Encrypted Partition"
@cryptsetup@/bin/cryptsetup open "${DRIVE_PATH}" "mk-raspberry-ext-drive" --key-file "${KEY_FILE}"
echo "Formatting Encrypted Filesystem"
@btrfs-progs@/bin/mkfs.btrfs -L "${UNENCRYPTED_LABEL}" /dev/mapper/mk-raspberry-ext-drive
echo "Mounting Partition"
mount -t btrfs /dev/mapper/mk-raspberry-ext-drive "$TEMP_MOUNTPOINT"
echo "Creating Folders"
mkdir "$TEMP_MOUNTPOINT/backups"
mkdir "$TEMP_MOUNTPOINT/storage"
mkdir "$TEMP_MOUNTPOINT/extras"
echo "Unmounting"
umount "$TEMP_MOUNTPOINT"
echo "Closing mapper device"
@cryptsetup@/bin/cryptsetup close "mk-raspberry-ext-drive"