Change from mirror to snapraid with quirks

This commit is contained in:
Raphaël Numbus
2026-01-25 13:16:46 +01:00
parent daa517a6bf
commit 1c1573c5b2
4 changed files with 96 additions and 119 deletions
+7 -36
View File
@@ -169,10 +169,9 @@ DISK_NAME=()
DISK_TYPE=() DISK_TYPE=()
DISK_HEALTH=() DISK_HEALTH=()
DISK_ID=() DISK_ID=()
DISK_SIZE=()
DISK_SIZE_BYTES=()
for DISK in \$(lsblk -x SIZE -d -n -e 7,11 -o NAME); do for DISK in \$(lsblk -x SIZE -d -n -e 7,11 -o NAME); do
# Disk name and simple path # Disk name and simple path
DISK_DEVPATH+=("/dev/\$DISK") DISK_DEVPATH+=("/dev/\$DISK")
DISK_NAME+=("\$DISK") DISK_NAME+=("\$DISK")
@@ -185,8 +184,8 @@ for DISK in \$(lsblk -x SIZE -d -n -e 7,11 -o NAME); do
elif [[ "\$HDD" -eq 0 ]]; then DISK_TYPE+=("SSD"); elif [[ "\$HDD" -eq 0 ]]; then DISK_TYPE+=("SSD");
else DISK_TYPE+=("Other") else DISK_TYPE+=("Other")
fi fi
# Disk health
# Disk health
if [[ \$(echo "$REMOTE_PASS" | sudo -S smartctl -H /dev/\$DISK 2>/dev/null | grep 'self-assessment' | awk '{print \$6}') == "PASSED" ]]; then if [[ \$(echo "$REMOTE_PASS" | sudo -S smartctl -H /dev/\$DISK 2>/dev/null | grep 'self-assessment' | awk '{print \$6}') == "PASSED" ]]; then
DISK_HEALTH+=("PASSED") DISK_HEALTH+=("PASSED")
else else
@@ -194,9 +193,6 @@ for DISK in \$(lsblk -x SIZE -d -n -e 7,11 -o NAME); do
fi fi
# Disk ID # Disk ID
DISK_ID+=("\$(ls -l /dev/disk/by-id | grep -m1 "../../\$DISK" | awk '{print "/dev/disk/by-id/" \$9}')") DISK_ID+=("\$(ls -l /dev/disk/by-id | grep -m1 "../../\$DISK" | awk '{print "/dev/disk/by-id/" \$9}')")
# Disk size
DISK_SIZE+=("\$(lsblk -x SIZE -d -n -e 7,11 -o SIZE /dev/\$DISK)")
DISK_SIZE_BYTES+=("\$(lsblk -x SIZE -d -n -e 7,11 -b -o SIZE /dev/\$DISK)")
done done
echo "# Hardware detection results on \$(date)" > "${TMPFILE}" echo "# Hardware detection results on \$(date)" > "${TMPFILE}"
@@ -218,9 +214,7 @@ for var in \
DISK_NAME \ DISK_NAME \
DISK_TYPE \ DISK_TYPE \
DISK_HEALTH \ DISK_HEALTH \
DISK_ID \ DISK_ID; do
DISK_SIZE \
DISK_SIZE_BYTES; do
declare -p \${var} | sed 's/^declare /declare -g /' >> "${TMPFILE}" declare -p \${var} | sed 's/^declare /declare -g /' >> "${TMPFILE}"
done done
SSHEND SSHEND
@@ -513,33 +507,8 @@ disks_generation() {
(envsubst < "templates/nix-config/disks/content.nix") >> final-nix-config/etc/nixos/disks/disko.nix (envsubst < "templates/nix-config/disks/content.nix") >> final-nix-config/etc/nixos/disks/disko.nix
sed -i "s|/mnt/content-1|/mnt/data|" final-nix-config/etc/nixos/disks/disko.nix sed -i "s|/mnt/content-1|/mnt/data|" final-nix-config/etc/nixos/disks/disko.nix
# Mirror configuration
elif [[ "$CONTENT_DISK_NUMBER" -eq 1 && "$PARITY_DISK_NUMBER" -eq 1 ]]; then
export CONTENT_DISK_ID="${DATA_DISKS_ID[0]}"
export PARITY_DISK_ID="${DATA_DISKS_ID[1]}"
# Calculate partition size to avoid mdadm size mismatch warning
local SIZE1=0
local SIZE2=0
for k in "${!DISK_ID[@]}"; do
[[ "${DISK_ID[$k]}" == "$CONTENT_DISK_ID" ]] && SIZE1="${DISK_SIZE_BYTES[$k]}"
[[ "${DISK_ID[$k]}" == "$PARITY_DISK_ID" ]] && SIZE2="${DISK_SIZE_BYTES[$k]}"
done
# Fallback to devpath if ID lookup failed
if [[ "$SIZE1" == "0" ]]; then for k in "${!DISK_DEVPATH[@]}"; do [[ "${DISK_DEVPATH[$k]}" == "$CONTENT_DISK_ID" ]] && SIZE1="${DISK_SIZE_BYTES[$k]}"; done; fi
if [[ "$SIZE2" == "0" ]]; then for k in "${!DISK_DEVPATH[@]}"; do [[ "${DISK_DEVPATH[$k]}" == "$PARITY_DISK_ID" ]] && SIZE2="${DISK_SIZE_BYTES[$k]}"; done; fi
local MIN_SIZE=$SIZE1
if [[ "$SIZE2" -lt "$SIZE1" ]]; then MIN_SIZE=$SIZE2; fi
# Subtract 2GB for safety (headers, rounding)
local PART_SIZE_GIB=$(( (MIN_SIZE - 2147483648) / 1024 / 1024 / 1024 ))
export PARTITION_SIZE="${PART_SIZE_GIB}G"
(envsubst < "templates/nix-config/disks/mirror.nix") >> final-nix-config/etc/nixos/disks/disko.nix
# SnapRAID configuration # SnapRAID configuration
elif [[ "$CONTENT_DISK_NUMBER" -gt 1 ]]; then elif [[ ${CONTENT_DISK_NUMBER} -gt 0 && ${PARITY_DISK_NUMBER} -gt 0 ]]; then
sed -i "s|# ./disks/snapraid.nix| ./disks/snapraid.nix|" final-nix-config/etc/nixos/configuration.nix sed -i "s|# ./disks/snapraid.nix| ./disks/snapraid.nix|" final-nix-config/etc/nixos/configuration.nix
j=0 j=0
for i in $(seq 0 $(($CONTENT_DISK_NUMBER - 1))); do for i in $(seq 0 $(($CONTENT_DISK_NUMBER - 1))); do
@@ -570,14 +539,16 @@ disks_generation() {
MOUNT_DEPENDENCIES_STOP+=" \${pkgs.cryptsetup}/bin/cryptsetup close crypted-parity-${j}"$'\n' MOUNT_DEPENDENCIES_STOP+=" \${pkgs.cryptsetup}/bin/cryptsetup close crypted-parity-${j}"$'\n'
done done
echo -e "\n ✅ Generated $PARITY_DISK_NUMBER parity disk configuration(s)." echo -e "\n ✅ Generated $PARITY_DISK_NUMBER parity disk configuration(s)."
[[ ${CONTENT_DISK_NUMBER} -eq 1 && ${PARITY_DISK_NUMBER} ]] && SNAPRAID_CONTENT_FILES+=" \"/mnt/content-0/snapraid.content\""$'\n'
export SNAPRAID_CONTENT_FILES export SNAPRAID_CONTENT_FILES
export SNAPRAID_DATA_DISKS export SNAPRAID_DATA_DISKS
export SNAPRAID_PARITY_FILES export SNAPRAID_PARITY_FILES
export MOUNT_DEPENDENCIES_START export MOUNT_DEPENDENCIES_START
export MOUNT_DEPENDENCIES_STOP export MOUNT_DEPENDENCIES_STOP
envsubst < templates/nix-config/disks/snapraid.nix > final-nix-config/etc/nixos/disks/snapraid.nix
fi fi
envsubst < templates/nix-config/disks/snapraid.nix > final-nix-config/etc/nixos/disks/snapraid.nix
# Close the disko.nix block # Close the disko.nix block
cat <<'EOF' >> final-nix-config/etc/nixos/disks/disko.nix cat <<'EOF' >> final-nix-config/etc/nixos/disks/disko.nix
}; };
+7 -2
View File
@@ -1,4 +1,3 @@
{ lib, ... }:
{ {
disko.devices = { disko.devices = {
# Boot disk LVM configuration # Boot disk LVM configuration
@@ -12,6 +11,12 @@
content.format = "btrfs"; content.format = "btrfs";
content.mountpoint = "/"; content.mountpoint = "/";
}; };
snapraid = {
size = "1G";
content.type = "filesystem";
content.format = "btrfs";
content.mountpoint = "/mnt/content-0";
};
swap = { swap = {
size = "16G"; size = "16G";
content.type = "swap"; content.type = "swap";
@@ -23,7 +28,7 @@
# Boot disk # Boot disk
"boot-1" = { "boot-1" = {
type = "disk"; type = "disk";
device = "${BOOT_DISK_1_ID}"; device = "$BOOT_DISK_1_ID";
content = { content = {
type = "gpt"; type = "gpt";
partitions = { partitions = {
+82 -32
View File
@@ -1,10 +1,9 @@
{ lib, ... }:
{ {
disko.devices = { disko.devices = {
disk = { disk = {
"boot-1" = { boot-1 = {
type = "disk"; type = "disk";
device = "${BOOT_DISK_1_ID}"; device = "$BOOT_DISK_1_ID";
content = { content = {
type = "gpt"; type = "gpt";
partitions = { partitions = {
@@ -12,61 +11,112 @@
size = "1G"; size = "1G";
type = "EF00"; type = "EF00";
content = { content = {
type = "filesystem"; type = "mdraid";
format = "vfat"; name = "boot";
mountpoint = "/boot";
mountOptions = [ "umask=0077" ];
}; };
}; };
crypt_p1 = { luks = {
size = "100%"; size = "100%";
content = { content = {
type = "luks"; type = "luks";
name = "crypted-boot-1"; name = "crypted-boot-1";
settings = { settings = {
keyFile = "/etc/secrets/disks/boot-1";
allowDiscards = true; allowDiscards = true;
keyFile = "/etc/secrets/disks/boot-1";
};
content = {
type = "lvm_pv";
vg = "pool";
}; };
}; };
}; };
}; };
}; };
}; };
"boot-2" = { boot-2 = {
type = "disk"; type = "disk";
device = "${BOOT_DISK_2_ID}"; device = "$BOOT_DISK_2_ID";
content = { content = {
type = "gpt"; type = "gpt";
partitions = { partitions = {
crypt_p2 = { ESP = {
size = "1G";
type = "EF00";
content = {
type = "mdraid";
name = "boot";
};
};
luks = {
size = "100%"; size = "100%";
content = { content = {
type = "luks"; type = "luks";
name = "crypted-boot-2"; name = "crypted-boot-2";
settings = { settings = {
keyFile = "/etc/secrets/disks/boot-2";
allowDiscards = true; allowDiscards = true;
keyFile = "/etc/secrets/disks/boot-2";
}; };
content = { content = {
type = "btrfs"; type = "lvm_pv";
extraArgs = [ vg = "pool";
"-d raid1" };
"/dev/mapper/crypted-boot-1" };
]; };
subvolumes = { };
"/root" = { };
mountpoint = "/"; };
mountOptions = [ };
"rw" mdadm = {
"relatime" boot = {
"ssd" type = "mdadm";
]; level = 1;
}; metadata = "1.2";
"/swap" = { content = {
mountpoint = "none"; type = "filesystem";
swap.size = "16G"; format = "vfat";
}; mountpoint = "/boot";
}; mountOptions = [ "umask=0077" ];
};
};
};
lvm_vg = {
pool = {
type = "lvm_vg";
lvs = {
swap = {
size = "16G";
lvm_type = "mirror";
content = {
type = "swap";
};
};
snapraid = {
size = "1G";
lvm_type = "mirror";
content = {
type = "filesystem";
format = "btrfs";
mountpoint = "/mnt/content-0";
};
};
root = {
size = "100%";
lvm_type = "mirror";
content = {
type = "btrfs";
extraArgs = [ "-f" ];
subvolumes = {
"/rootfs" = {
mountpoint = "/";
mountOptions = [ "compress=zstd" "noatime" ];
};
"/home" = {
mountpoint = "/home";
mountOptions = [ "compress=zstd" ];
};
"/nix" = {
mountpoint = "/nix";
mountOptions = [ "compress=zstd" "noatime" ];
}; };
}; };
}; };
-49
View File
@@ -1,49 +0,0 @@
"content-1" = {
type = "disk";
device = "$CONTENT_DISK_ID";
content = {
type = "gpt";
partitions = {
mdadm = {
size = "$PARTITION_SIZE";
content = {
type = "mdraid";
name = "raid1";
};
};
};
};
};
"parity-1" = {
type = "disk";
device = "$PARITY_DISK_ID";
content = {
type = "gpt";
partitions = {
mdadm = {
size = "$PARTITION_SIZE";
content = {
type = "mdraid";
name = "raid1";
};
};
};
};
};
};
mdadm = {
raid1 = {
type = "mdadm";
level = 1;
metadata = "1.2";
content = {
type = "luks";
name = "crypted-mirror";
settings.keyFile = "/etc/secrets/disks/mirror";
content = {
type = "filesystem";
format = "xfs";
mountpoint = "/mnt/data";
};
};
};