nixfiles/extras/mk-raspberry-ext-drive.nix

69 lines
1.7 KiB
Nix
Raw Normal View History

2023-09-13 19:26:50 +01:00
{
util-linux,
cryptsetup,
btrfs-progs,
2023-09-18 03:56:58 +01:00
writeShellApplication,
2023-09-13 19:26:50 +01:00
}: let
2023-09-20 18:17:50 +01:00
externalDriveData = import ../data/drives/raspberryExternalDrive.nix;
2024-03-10 18:29:49 +00:00
in
writeShellApplication {
name = "mk-raspberry-ext-drive";
runtimeInputs = [
util-linux
cryptsetup
btrfs-progs
];
text = ''
if [ -z "''${1-}" ]; then
echo "Please specify a path to device as first argument"
exit 1
fi
2023-09-18 03:56:58 +01:00
2024-03-10 18:29:49 +00:00
DRIVE_PATH=$1
2023-09-18 03:56:58 +01:00
2024-03-10 18:29:49 +00:00
if [ -z "''${2-}" ]; then
echo "Please specify a key file to use"
exit 1
fi
2023-09-18 03:56:58 +01:00
2024-03-10 18:29:49 +00:00
KEY_FILE=$2
2023-09-18 03:56:58 +01:00
2024-03-10 18:29:49 +00:00
if [ -z "''${3-}" ]; then
echo "Please specify a temp mountpoint to use"
exit 1
fi
2023-09-18 03:56:58 +01:00
2024-03-10 18:29:49 +00:00
TEMP_MOUNTPOINT=$3
2023-09-18 03:56:58 +01:00
2024-03-10 18:29:49 +00:00
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit
fi
echo "Wiping Partitions..."
wipefs --all "$DRIVE_PATH"
2023-09-18 03:56:58 +01:00
2024-03-10 18:29:49 +00:00
echo "Creating Encrypted Partition"
cryptsetup luksFormat "$DRIVE_PATH" --key-file "$KEY_FILE" --label "${externalDriveData.encryptedLabel}"
2023-09-18 03:56:58 +01:00
2024-03-10 18:29:49 +00:00
echo "Opening Encrypted Partition"
cryptsetup open "$DRIVE_PATH" "mk-raspberry-ext-drive" --key-file "$KEY_FILE"
2023-09-18 03:56:58 +01:00
2024-03-10 18:29:49 +00:00
echo "Formatting Encrypted Filesystem"
mkfs.btrfs -L "${externalDriveData.unencryptedLabel}" /dev/mapper/mk-raspberry-ext-drive
2023-09-18 03:56:58 +01:00
2024-03-10 18:29:49 +00:00
echo "Mounting Partition"
mount -t btrfs /dev/mapper/mk-raspberry-ext-drive "$TEMP_MOUNTPOINT"
2023-09-18 03:56:58 +01:00
2024-03-10 18:29:49 +00:00
echo "Creating Folders"
mkdir "$TEMP_MOUNTPOINT/backups"
mkdir "$TEMP_MOUNTPOINT/storage"
mkdir "$TEMP_MOUNTPOINT/extras"
2023-09-18 03:56:58 +01:00
2024-03-10 18:29:49 +00:00
echo "Unmounting"
umount "$TEMP_MOUNTPOINT"
2023-09-18 03:56:58 +01:00
2024-03-10 18:29:49 +00:00
echo "Closing mapper device"
cryptsetup close "mk-raspberry-ext-drive"
'';
}