From 4bd86f8de7eeb673085b6a135f8438a425879da4 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Sat, 22 Jun 2024 23:32:57 +0200 Subject: [PATCH] Generate partitioning scripts using `nix` --- profiles/DerGeret/Arch/partition.sh | 33 ------- profiles/DerGeret/Arch/setup.sh | 13 --- profiles/ManuSurface/Arch/partition.nix | 9 ++ profiles/ManuSurface/Arch/setup.sh | 2 - scripts/Arch/OS/setup.sh | 10 +-- scripts/Common/OS/partition.sh | 110 +----------------------- 6 files changed, 15 insertions(+), 162 deletions(-) delete mode 100755 profiles/DerGeret/Arch/partition.sh create mode 100644 profiles/ManuSurface/Arch/partition.nix diff --git a/profiles/DerGeret/Arch/partition.sh b/profiles/DerGeret/Arch/partition.sh deleted file mode 100755 index 60752352..00000000 --- a/profiles/DerGeret/Arch/partition.sh +++ /dev/null @@ -1,33 +0,0 @@ -#!/bin/bash -function dualbootPartition() { - { - # Delete placeholders for swap and Linux partition - echo "d"; - echo "2"; - - echo "d"; - echo "3"; - - # Create swap partition - echo "n"; - echo ""; - echo ""; - echo "+${SWAP_SIZE}G"; - echo "y"; - echo "t"; - echo "2"; - echo "19"; - - # Fill remaining space with Linux partition - echo "n"; - echo ""; - echo ""; - echo ""; - echo "y"; - echo "t"; - echo "3"; - echo "23"; - - echo "w"; - } | fdisk "$OS_DISK"; -} diff --git a/profiles/DerGeret/Arch/setup.sh b/profiles/DerGeret/Arch/setup.sh index f0b4167b..f15d519e 100755 --- a/profiles/DerGeret/Arch/setup.sh +++ b/profiles/DerGeret/Arch/setup.sh @@ -2,19 +2,6 @@ pushd "${BASH_SOURCE%/*}" > /dev/null; . "./partition.sh"; -extraMounts() -{ - disks=($(bash -c "echo $OS_DISK*")); - mount --mkdir -o force -t ntfs3 "${disks[-1]}" "$ARCH_MOUNT_ROOT/mnt/win"; -} - -function initializePartitions() { - dualbootPartition; -} - -SKIP_FORMAT_EFI=1 \ -ARCH_MOUNT_ROOT="/mnt" \ -ARCH_DISK=nvme0n1 \ ARCH_HOSTNAME="der-geret" \ USER_DISPLAYNAME="Manuel Thalmann" \ . "../../../scripts/Arch/OS/setup.sh"; diff --git a/profiles/ManuSurface/Arch/partition.nix b/profiles/ManuSurface/Arch/partition.nix new file mode 100644 index 00000000..895b6aeb --- /dev/null +++ b/profiles/ManuSurface/Arch/partition.nix @@ -0,0 +1,9 @@ +{ ... }: { + imports = [ + ../../../scripts/Arch/OS/partition.nix + ]; + + config = { + partition.os.deviceName = "nvme0n1"; + }; +} diff --git a/profiles/ManuSurface/Arch/setup.sh b/profiles/ManuSurface/Arch/setup.sh index 0a0bfc08..aa69dc47 100755 --- a/profiles/ManuSurface/Arch/setup.sh +++ b/profiles/ManuSurface/Arch/setup.sh @@ -1,8 +1,6 @@ #!/bin/bash -e pushd "${BASH_SOURCE%/*}" > /dev/null; -ARCH_MOUNT_ROOT="/mnt" \ -ARCH_DISK=nvme0n1 \ ARCH_HOSTNAME="manu-surface" \ USER_DISPLAYNAME="Manuel Thalmann" \ . "../../../scripts/Arch/OS/setup.sh"; diff --git a/scripts/Arch/OS/setup.sh b/scripts/Arch/OS/setup.sh index deafa1bc..e7707a9b 100755 --- a/scripts/Arch/OS/setup.sh +++ b/scripts/Arch/OS/setup.sh @@ -1,10 +1,10 @@ #!/bin/bash -e pushd "${BASH_SOURCE%/*}" > /dev/null; +PARTITION_MODULE="${PARTITION_MODULE:-"./partition.nix"}"; ARCH_TIMEZONE="${ARCH_TIMEZONE:-"Europe/Zurich"}"; -ARCH_DISK="${ARCH_DISK}"; ARCH_HOSTNAME="${ARCH_HOSTNAME}"; -ARCH_MOUNT_ROOT="${ARCH_MOUNT_ROOT:-"/mnt"}"; +ARCH_MOUNT_ROOT="${ARCH_MOUNT_ROOT:-"$(../../eval-module.sh "$PARTITION_MODULE" partition.rootDir --raw)"}"; ARCH_LANG="${ARCH_LANG:-"en_US.UTF-8"}"; ARCH_KEYMAP="${ARCH_KEYMAP:-"de_CH-latin1"}"; ARCH_X11_KEYMAP="${ARCH_X11_KEYMAP:-"ch"}"; @@ -22,10 +22,8 @@ tempDir="$tempRoot/$relativeDir"; loadkeys "$ARCH_KEYMAP"; timedatectl set-timezone "$ARCH_TIMEZONE"; -OS_MOUNT_ROOT="${ARCH_MOUNT_ROOT}"; -OS_DISK="${ARCH_DISK}" \ -OS_LABEL="Arch" \ - . "../../Common/OS/partition.sh"; + +. "../../Common/OS/partition.sh"; pacman-key --init; pacman-key --populate; diff --git a/scripts/Common/OS/partition.sh b/scripts/Common/OS/partition.sh index 128ddfd9..5dd19564 100755 --- a/scripts/Common/OS/partition.sh +++ b/scripts/Common/OS/partition.sh @@ -1,109 +1,3 @@ #!/bin/bash -pushd "${BASH_SOURCE%/*}" > /dev/null; - -source "../Scripts/hooks.sh"; -source "../Scripts/is-truthy.sh"; -source "../Scripts/choose-disk.sh"; -OS_MOUNT_ROOT="${OS_ROOT:-"/mnt"}"; -OS_DISK="${OS_DISK}"; -BOOT_LABEL="${BOOT_LABEL:-Boot}"; -OS_LABEL="${OS_LABEL:-Linux}"; -SWAP_LABEL="${SWAP_LABEL:-Swap}"; -BOOT_MOUNTPOINT="${BOOT_MOUNTPOINT:-"/boot"}"; -SKIP_FORMAT_EFI="${SKIP_FORMAT_EFI}"; -memory="$(cat /proc/meminfo | awk -F " " '/^MemTotal/ { print $2 }')"; -swapSize="$(echo "$memory" | awk '{ print int((($1 / 1024 / 1024) * 0.75) + 0.5)}')"; - -if [ ! -z "$OS_DISK" ] && [ ! -e "$OS_DISK" ] -then - OS_DISK="/dev/${OS_DISK}" -fi; - -if [ -z "$OS_DISK" ] || [ ! -e "$OS_DISK" ] -then - chooseDisk OS_DISK "Which disk do you wish to format for installing the OS?"; -fi; - -function partitionDisk() { - { - echo "g"; - - echo "n"; - echo ""; - echo ""; - echo "+1G"; - echo "y"; - echo "t"; - echo "1"; - - echo "n"; - echo ""; - echo ""; - echo "+${SWAP_SIZE}G"; - echo "y"; - echo "t"; - echo ""; - echo "19"; - - echo "n"; - echo ""; - echo ""; - echo ""; - echo "y"; - echo "t"; - echo ""; - echo "23"; - - echo "w"; - } | fdisk "$OS_DISK"; -} - -OS_DISK="${OS_DISK}" \ -SWAP_SIZE="$swapSize" \ - runHook initializePartitions \ - partitionDisk; - -while true -do - disks=($(bash -c "echo $OS_DISK*")); - [ "${#disks[@]}" -ge 4 ] && break; -done; - -bootDisk="${BOOT_DISK:-"${disks[1]}"}"; -swapDisk="${SWAP_DISK:-"${disks[2]}"}"; -linuxDisk="${LINUX_DISK:-"${disks[3]}"}"; -labels=("$SWAP_LABEL" "$OS_LABEL"); - -if ! isTruthy "$SKIP_FORMAT_EFI" -then - mkfs.fat -F 32 -n "$BOOT_LABEL" "$bootDisk"; - labels+=("$BOOT_LABEL"); -fi - -mkswap -L "$SWAP_LABEL" "$swapDisk"; -mkfs.ext4 -L "$OS_LABEL" -F "$linuxDisk"; - -for label in ${labels[*]}; -do - while true; - do - [ -b "/dev/disk/by-label/$label" ] && break; - done; -done - -mount "/dev/disk/by-label/$OS_LABEL" "$OS_MOUNT_ROOT"; - -if ! isTruthy "$SKIP_FORMAT_EFI" -then - bootDisk="/dev/disk/by-label/$BOOT_LABEL"; -fi - -mount --mkdir "$bootDisk" "$OS_MOUNT_ROOT$BOOT_MOUNTPOINT"; -swapon "/dev/disk/by-label/$SWAP_LABEL"; - -if command -v extraMounts -then - extraMounts; -fi; - -popd > /dev/null; +PARTITION_MODULE="${PARTITION_MODULE:-"./partition.nix"}"; +eval "$(../../eval-module.sh "$PARTITION_MODULE" partition.script --raw)";