Standardize system disk on LVM-on-LUKS for snapshot support.

Add dedicated parity disk and correct data disk mountpoints.
Resolve various Nix syntax errors in disk templates.
Set data disk filesystem to XFS for better large-file performance.
This commit is contained in:
Raphaël Billet
2025-11-18 22:35:09 +01:00
parent c666160b4e
commit 6827785db7
12 changed files with 733 additions and 174 deletions
+139 -64
View File
@@ -1,17 +1,6 @@
#!/bin/bash
install_prerun_action() {
echo -e "\n\n ➡️ On the target host : start the computer and boot into the NixOS iso.\n Launch a console and set up a new user password."
SETUP_ANSWER="$(gum input --placeholder "Type 'done' when you have finished.")"
if [[ "$SETUP_ANSWER" == "done" ]]; then
:
else
echo " Aborting - you did not type 'done'."
exit 1
fi
}
update_prerun_action() {
echo -e "\n\n ➡️ On the target host : make sure the NixOS installation you want to update is up-and-running, accessible with SSH."
prerun_action() {
echo -e "$1"
SETUP_ANSWER="$(gum input --placeholder "Type 'done' when you have finished.")"
if [[ "$SETUP_ANSWER" == "done" ]]; then
:
@@ -76,6 +65,56 @@ necessary_credentials_with_config() {
fi
}
hardware_detection() {
echo -e "\n\n ➡️ Please provide the password of the target host :"
ssh-copy-id -i extra-files/home/numbus-admin/.ssh/id_ed25519.pub nixos@$TARGET_HOST
ssh_to_host() {
ssh -i extra-files/home/numbus-admin/.ssh/id_ed25519 nixos@$TARGET_HOST "$1"
}
echo -e "\n\n 🔎 Detecting graphics card on target host..."
VGA_INFO=$(ssh_to_host "lspci -nn | grep -i 'vga'")
if echo "$VGA_INFO" | grep -iq "intel"; then
echo -e " ✅ Intel graphics card detected."
TARGET_GRAPHICS="true"
elif echo "$VGA_INFO" | grep -iq "amd"; then
echo -e " ✅ AMD graphics card detected."
TARGET_GRAPHICS="true"
elif echo "$VGA_INFO" | grep -iq "nvidia"; then
echo -e " ✅ NVIDIA graphics card detected."
TARGET_GRAPHICS="true"
else
echo -e " ️ No dedicated graphics card detected."
TARGET_GRAPHICS="false"
fi
echo -e "\n\n 🔎 Detecting transconding acceleration on target host..."
if ssh_to_host "ls /dev/dri/renderD128"; then
echo -e " ✅ Transcoding capable card detected."
TARGET_GRAPHICS_RENDERER="true"
else
echo -e " ️ No transcoding capable card detected."
TARGET_GRAPHICS_RENDERER="false"
fi
echo -e "\n\n 🔎 Detecting USB Google Coral TPU on target host..."
if ssh_to_host "lsusb | grep -iq 'google'"; then
echo -e " ✅ USB Google Coral TPU detected."
TARGET_USB_CORAL="true"
else
echo -e " ️ No USB Google Coral TPU detected."
TARGET_USB_CORAL="false"
fi
echo -e "\n\n 🔎 Detecting Zigbee coordinator on target host..."
if ssh_to_host "ls /dev/serial/by-id/ | grep -i 'zigbee'"; then
echo -e " ✅ Zigbee device found in /dev/serial/by-id/."
TARGET_ZIGBEE_DEVICE=$(ssh_to_host "ls /dev/serial/by-id/ | grep -i 'zigbee'")
TARGET_ZIGBEE="true"
else
echo -e " ️ No Zigbee device found."
TARGET_ZIGBEE="false"
fi
}
files_generation() {
echo -e "\n\n ✅ Generating necessary folder tree..."
mkdir -p extra-files/home/numbus-admin/.ssh/
@@ -190,57 +229,11 @@ files_generation() {
nix shell nixpkgs#mosquitto -c mosquitto_passwd -b extra-files/mnt/config-storage/hass/mqtt/config/password.txt $HOME_ASSISTANT_MQTT_USER $HOME_ASSISTANT_MQTT_PASSWORD
}
hardware_detection() {
echo -e "\n\n ➡️ Please provide the password of the target host :"
ssh-copy-id -i extra-files/home/numbus-admin/.ssh/id_ed25519.pub nixos@$TARGET_HOST
disk_config_generation() {
ssh_to_host() {
ssh -i extra-files/home/numbus-admin/.ssh/id_ed25519 nixos@$TARGET_HOST "$1"
}
echo -e "\n\n 🔎 Detecting graphics card on target host..."
VGA_INFO=$(ssh_to_host "lspci -nn | grep -i 'vga'")
if echo "$VGA_INFO" | grep -iq "intel"; then
echo -e " ✅ Intel graphics card detected."
TARGET_GRAPHICS="true"
elif echo "$VGA_INFO" | grep -iq "amd"; then
echo -e " ✅ AMD graphics card detected."
TARGET_GRAPHICS="true"
elif echo "$VGA_INFO" | grep -iq "nvidia"; then
echo -e " ✅ NVIDIA graphics card detected."
TARGET_GRAPHICS="true"
else
echo -e " ️ No dedicated graphics card detected."
TARGET_GRAPHICS="false"
fi
echo -e "\n\n 🔎 Detecting transconding acceleration on target host..."
if ssh_to_host "ls /dev/dri/renderD128"; then
echo -e " ✅ Transcoding capable card detected."
TARGET_GRAPHICS_RENDERER="true"
else
echo -e " ️ No transcoding capable card detected."
TARGET_GRAPHICS_RENDERER="false"
fi
echo -e "\n\n 🔎 Detecting USB Google Coral TPU on target host..."
if ssh_to_host "lsusb | grep -iq 'google'"; then
echo -e " ✅ USB Google Coral TPU detected."
TARGET_USB_CORAL="true"
else
echo -e " ️ No USB Google Coral TPU detected."
TARGET_USB_CORAL="false"
fi
echo -e "\n\n 🔎 Detecting Zigbee coordinator on target host..."
if ssh_to_host "ls /dev/serial/by-id/ | grep -i 'zigbee'"; then
echo -e " ✅ Zigbee device found in /dev/serial/by-id/."
TARGET_ZIGBEE_DEVICE=$(ssh_to_host "ls /dev/serial/by-id/ | grep -i 'zigbee'")
TARGET_ZIGBEE="true"
else
echo -e " ️ No Zigbee device found."
TARGET_ZIGBEE="false"
fi
}
disk_config_generation() {
echo -e "\n\n ⚠️ WARNING: you will choose the disks you want to install NixOS on."
echo -e " !! PLEASE MAKE SURE YOU BACKED UP ANY IMPORTANT DATA !!"
echo -e " !! ALL DATA WILL BE WIPED ON THE DISKS YOU CHOOSE !!"
@@ -254,8 +247,86 @@ disk_config_generation() {
exit 1
fi
sleep 10
echo -e "\n\n 🔎 Fetching disks from target host..."
DISK_JSON=$(ssh_to_host "lsblk -d --json -o NAME,ROTA,SIZE,PATH")
BY_ID_RAW=$(ssh_to_host "ls -l /dev/disk/by-id/")
if [ -z "$DISK_JSON" ]; then
echo " ❌ Could not find any disks on the target host. Aborting."
exit 1
fi
declare -A BY_ID_MAP
while read -r line; do
if [[ "$line" =~ "-> ../../"(.*)$ ]]; then
dev_name="${BASH_REMATCH[1]}"
by_id_path="/dev/disk/by-id/$(echo "$line" | awk '{print $9}')"
if [[ ! -v "BY_ID_MAP[$dev_name]" && ! "$by_id_path" =~ -part ]]; then
BY_ID_MAP["$dev_name"]="$by_id_path"
fi
fi
done <<< "$BY_ID_RAW"
declare -A DISK_MAP
declare -a DISK_OPTIONS
while read -r name type size; do
by_id=${BY_ID_MAP[$name]}
if [ -z "$by_id" ]; then continue; fi
option=$(printf "%-8s %-5s %-8s (%s)" "$name" "$type" "$size" "$by_id")
DISK_OPTIONS+=("$option")
DISK_MAP["$option"]="$by_id"
done < <(echo "$DISK_JSON" | jq -r '.blockdevices[] | "\(.name) \(if .name | test("^nvme") then "NVMe" else (if .rota == "0" then "SSD" else "HDD" end) end) \(.size)"')
echo -e "\n\n ➡️ Please choose one (stripe) or two (mirror) disks for your NixOS boot installation:"
mapfile -t SELECTED_BOOT_OPTIONS < <(gum choose --limit 2 "${DISK_OPTIONS[@]}")
if [ ${#SELECTED_BOOT_OPTIONS[@]} -eq 0 ]; then
echo " ❌ No boot disk selected. Aborting."
exit 1
fi
NUMBER_OF_BOOT_DISKS=${#SELECTED_BOOT_OPTIONS[@]}
BOOT_DISK_1=${DISK_MAP["${SELECTED_BOOT_OPTIONS[0]}"]}
if [ "$NUMBER_OF_BOOT_DISKS" -eq 2 ]; then
BOOT_DISK_2=${DISK_MAP["${SELECTED_BOOT_OPTIONS[1]}"]}
fi
REMAINING_DISKS=()
for option in "${DISK_OPTIONS[@]}"; do
is_boot_disk=false
for selected in "${SELECTED_BOOT_OPTIONS[@]}"; do
if [[ "$option" == "$selected" ]]; then
is_boot_disk=true
break
fi
done
if ! $is_boot_disk; then
REMAINING_DISKS+=("$option")
fi
done
if [ ${#REMAINING_DISKS[@]} -gt 0 ]; then
echo -e "\n\n ➡️ Please choose your data disks (up to 4):"
mapfile -t SELECTED_DATA_OPTIONS < <(gum choose --limit 4 "${REMAINING_DISKS[@]}")
NUMBER_OF_DATA_DISKS=${#SELECTED_DATA_OPTIONS[@]}
for i in $(seq 0 $(($NUMBER_OF_DATA_DISKS - 1))); do
declare "DATA_DISK_$(($i + 1))"="${DISK_MAP["${SELECTED_DATA_OPTIONS[$i]}"]}"
done
else
echo -e "\n\n ️ No remaining disks available for data storage."
NUMBER_OF_DATA_DISKS=0
fi
DISK_CONFIG_TEMPLATE="config-files/disks/boot-${NUMBER_OF_BOOT_DISKS}-data-${NUMBER_OF_DATA_DISKS}.nix"
if [[ -f "$DISK_CONFIG_TEMPLATE" ]]; then
echo -e "\n\n ✅ Generating disk configuration from template: $DISK_CONFIG_TEMPLATE"
envsubst < "$DISK_CONFIG_TEMPLATE" > disk-config.nix
else
echo -e "\n\n ❌ Error: No disk configuration template found for $NUMBER_OF_BOOT_DISKS boot disk(s) and $NUMBER_OF_DATA_DISKS data disk(s)."
echo " Looked for: $DISK_CONFIG_TEMPLATE"
exit 1
}
deploy() {
@@ -322,17 +393,21 @@ echo $ACTION_ANSWER
if [[ "$ACTION_ANSWER" == "[1] 🌐 Deploy NixOS on a remote machine" ]]; then
echo -e "\n ➡️ Proceeding with deployment…"
install_prerun_action
prerun_action "\n\n ➡️ On the target host : start the computer and boot into the NixOS iso.\n Launch a console and set up a new user password."
necessary_credentials
hardware_detection
files_generation
disk_config_generation
elif [[ "$ACTION_ANSWER" == "[2] 💽 Deploy NixOS on a remote machine with a file configuration" ]]; then
echo -e "\n ➡️ Proceeding with deployment using a config file…"
install_prerun_action
prerun_action "\n\n ➡️ On the target host : start the computer and boot into the NixOS iso.\n Launch a console and set up a new user password."
necessary_credentials_with_config
hardware_detection
files_generation
disk_config_generation
elif [[ "$ACTION_ANSWER" == "[3] 🛠️ Update a NixOS remote machine" ]]; then
echo -e "\n ➡️ Proceeding with update…"
update_prerun_action
prerun_action "\n\n ➡️ On the target host : make sure the NixOS installation you want to update is up-and-running, accessible with SSH."
nixos_update
else
echo "Aborting - you did not type '1, 2 or 3'."