2021-01-29 01:00:40 +01:00
|
|
|
#! /usr/bin/env nix-shell
|
2021-01-29 01:19:47 +01:00
|
|
|
#! nix-shell -i bash -p bash parted cryptsetup btrfs-progs
|
2021-01-29 01:00:40 +01:00
|
|
|
|
|
|
|
set -euo pipefail
|
|
|
|
|
|
|
|
read -p "enter the name of the storage device to partition (e.g. /dev/nvme0n1): " -r
|
|
|
|
drive=$REPLY
|
|
|
|
|
|
|
|
if [[ $drive =~ "nvme" ]]; then
|
|
|
|
boot="${drive}p1"
|
|
|
|
system="${drive}p2"
|
|
|
|
else
|
|
|
|
boot="${drive}1"
|
|
|
|
system="${drive}2"
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo
|
|
|
|
echo "The following layout will be created:"
|
|
|
|
echo "
|
|
|
|
$drive
|
|
|
|
|
|
|
|
|
├── $boot (boot partition, fat32)
|
|
|
|
|
|
2021-01-29 01:25:41 +01:00
|
|
|
└── $system (system partition, LUKS2 + btrfs)
|
2021-01-29 01:00:40 +01:00
|
|
|
|
|
|
|
|
├── @ (mounted as /)
|
|
|
|
| |
|
|
|
|
| ├── /home (mounted @home subvolume)
|
|
|
|
| |
|
|
|
|
| ├── /boot (mounted boot partition)
|
|
|
|
| |
|
2022-01-27 15:03:07 +01:00
|
|
|
| ├── /.swap (mounted @swap subvolume)
|
2021-01-29 01:00:40 +01:00
|
|
|
| |
|
|
|
|
| └── /.snapshots (mounted @snapshots subvolume)
|
|
|
|
|
|
|
|
|
├── @home (mounted as /home)
|
|
|
|
|
|
2022-01-27 15:03:07 +01:00
|
|
|
├── @swap (mounted as /.swap, contains swap file)
|
2021-01-29 01:00:40 +01:00
|
|
|
|
|
|
|
|
└── @snapshots (mounted as /.snapshots)
|
|
|
|
"
|
|
|
|
|
|
|
|
echo
|
|
|
|
echo "WARNING! Continuing will cause $drive to be formatted."
|
|
|
|
read -p "Do you really want to continue? [Y/n]" -n 1 -r
|
|
|
|
if [[ ! $REPLY =~ ^[Yy]$ ]]; then exit 1; fi
|
|
|
|
|
|
|
|
# create partition table
|
|
|
|
parted "$drive" -- mklabel gpt
|
|
|
|
|
|
|
|
# create boot pabootrtition
|
2021-08-14 18:53:56 +02:00
|
|
|
parted "$drive" -- mkpart ESP fat32 1MiB 512MiB
|
|
|
|
parted "$drive" -- set 1 esp on
|
2021-01-29 01:00:40 +01:00
|
|
|
|
|
|
|
# create system partition
|
2021-08-14 18:53:56 +02:00
|
|
|
parted "$drive" -- mkpart primary 512MiB 100%
|
2021-01-29 01:00:40 +01:00
|
|
|
|
|
|
|
# setup LUKS partition
|
|
|
|
cryptsetup -y -v luksFormat "$system"
|
|
|
|
cryptsetup open "$system" enc
|
|
|
|
|
|
|
|
# setup btrfs partition
|
|
|
|
mkfs.btrfs /dev/mapper/enc
|
|
|
|
|
|
|
|
# setup subvolumes
|
|
|
|
mount -t btrfs /dev/mapper/enc /mnt
|
|
|
|
btrfs subvolume create /mnt/@
|
|
|
|
btrfs subvolume create /mnt/@home
|
|
|
|
btrfs subvolume create /mnt/@swap
|
|
|
|
btrfs subvolume create /mnt/@snapshots
|
|
|
|
umount /mnt
|
|
|
|
|
|
|
|
# mount subvolumes
|
|
|
|
mount -o subvol=@,compress-force=zstd,noatime /dev/mapper/enc /mnt
|
|
|
|
mkdir /mnt/home
|
|
|
|
mount -o subvol=@home,compress-force=zstd,noatime /dev/mapper/enc /mnt/home
|
2022-01-27 15:03:07 +01:00
|
|
|
mkdir /mnt/.swap
|
2022-02-09 21:04:01 +01:00
|
|
|
mount -o subvol=@swap,noatime /dev/mapper/enc /mnt/.swap
|
2021-01-29 01:00:40 +01:00
|
|
|
mkdir /mnt/.snapshots
|
|
|
|
mount -o subvol=@snapshots,compress-force=zstd,noatime /dev/mapper/enc /mnt/.snapshots
|
|
|
|
mkdir /mnt/boot
|
|
|
|
mount "$boot" /mnt/boot
|
|
|
|
|
|
|
|
# setup swap file
|
2022-01-27 15:03:07 +01:00
|
|
|
truncate -s 0 /mnt/.swap/swapfile
|
|
|
|
chattr +C /mnt/.swap/swapfile
|
2022-02-09 21:04:01 +01:00
|
|
|
btrfs property set /mnt/.swap/swapfile compression none
|
|
|
|
dd if=/dev/zero of=/mnt/.swap/swapfile bs=1M count=8192 status=progress
|
2022-01-27 15:03:07 +01:00
|
|
|
chmod 600 /mnt/.swap/swapfile
|
|
|
|
mkswap /mnt/.swap/swapfile
|
|
|
|
swapon /mnt/.swap/swapfile
|
2021-01-29 01:00:40 +01:00
|
|
|
|
|
|
|
# generate hardware-configuration.nix
|
|
|
|
nixos-generate-config --root /mnt
|
|
|
|
|
|
|
|
echo
|
|
|
|
echo "Partitions have been created and hardware-configuration.nix has been generated."
|
|
|
|
echo "WARNING: Some hardware-configuration.nix options might need to be set manually:"
|
|
|
|
echo "- add compress-force & noatime options"
|
2022-01-27 15:03:07 +01:00
|
|
|
echo "- add nodatacow option to /.swap"
|
|
|
|
echo '- add "neededForBoot = true;" to /.swap'
|
2021-01-29 01:00:40 +01:00
|
|
|
|