Skip to content

Instantly share code, notes, and snippets.

@cyxou
Created May 21, 2022 20:29
Show Gist options
  • Save cyxou/551da8553abce1feb50d6a62e23460d9 to your computer and use it in GitHub Desktop.
Save cyxou/551da8553abce1feb50d6a62e23460d9 to your computer and use it in GitHub Desktop.
#! /usr/bin/env nix-shell
#! nix-shell -i bash -p bash parted cryptsetup btrfs-progs
# Steps for this script are taken from this article:
# https://dzone.com/articles/nixos-native-flake-deployment-with-luks-and-lvm
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"
root="${drive}p2"
else
boot="${drive}1"
root="${drive}2"
fi
echo
echo "The following layout will be created:"
echo "
$drive disk
├─$boot part /boot
└─$root part
└─crypt crypt
├─vg-swap lvm [SWAP]
└─vg-nixos lvm /
"
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
echo "Create partition table:"
parted -a opt --script $drive \
mklabel gpt \
mkpart primary fat32 0% 512MiB \
mkpart primary 512MiB 100% \
set 1 esp on \
name 1 boot \
set 2 lvm on \
name 2 root
echo "Create encrypted partition:"
cryptsetup luksFormat /dev/disk/by-partlabel/root
echo "Open encrypted partition:"
cryptsetup luksOpen /dev/disk/by-partlabel/root root
echo "Create a physical volume from /dev/mapper/root:"
pvcreate /dev/mapper/root
echo "Create a volume group from /dev/mapper/root:"
vgcreate vg /dev/mapper/root
echo "Create a 16GB logical volume on volume group vg that will be used for swap partition:"
lvcreate -L 16G -n swap vg
echo "Create a logical volume on the volume group vg using all the rest of the storage space that will be used for the root partition:"
lvcreate -l '100%FREE' -n root vg
echo "Showing what we got:"
lvdisplay
echo "Formatting the boot partition with FAT32:"
mkfs.fat -F 32 -n boot /dev/disk/by-partlabel/boot
echo "Formatting the root partition with EXT4 file system:"
mkfs.ext4 -L root /dev/vg/root
echo "Formatting the swap partition:"
mkswap -L swap /dev/vg/swap
echo "Mounting Newly Created Partitions and Swap Activation:"
mount /dev/disk/by-label/root /mnt
mkdir -p /mnt/boot
mount /dev/disk/by-label/boot /mnt/boot
swapon /dev/vg/swap
swapon -s
echo "Partitioning done! You may now proceed further with NixOS installation"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment