nixos-demo/os/persistence.nix

149 lines
3.9 KiB
Nix
Raw Normal View History

2025-07-12 00:28:21 +02:00
{ config, lib, ... }:
{
environment.persistence = {
persistence = {
enable = lib.mkIf (config.beancloud.persistence.type != "impermanence") false;
persistentStoragePath = "/nix/persistence";
hideMounts = true;
directories = [
"/etc/nixos"
"/var/log"
"/var/lib/bluetooth"
"/var/lib/nixos"
"/var/lib/systemd/coredump"
"/var/lib/systemd/timers"
"/etc/NetworkManager/system-connections"
];
files = [
"/etc/machine-id"
"/etc/ssh/ssh_host_rsa_key"
"/etc/ssh/ssh_host_rsa_key.pub"
"/etc/ssh/ssh_host_ed25519_key"
"/etc/ssh/ssh_host_ed25519_key.pub"
2025-07-12 00:28:21 +02:00
];
};
};
boot.initrd.preLVMCommands =
lib.mkIf
(config.beancloud.persistence.type != "preservation" && config.beancloud.persistence.root == "fs")
''
WAIT_TIME=5
MAX_RETRIES=6
ROOT_DEV="/dev/disk/by-label/root"
ROOT_DIR=/rootmnt"
mountPartition() {
echo "mount the root / partition"
echo "create the $ROOT_DIR folder"
mkdir -p "$ROOT_DIR"
COUNTER=1
while [ ! -e "$ROOT_DEV" ] || [ ! -e "$ROOT_DIR" ]; do
echo "the root partition or the mount folder are not yet available"
sleep $WAIT_TIME
if [ "$COUNTER" -eq "$MAX_RETRIES" ]; then
echo "device check failed"
exit 1
fi
COUNTER="$((COUNTER + 1))"
done
COUNTER=1
until mount "$ROOT_DEV" $ROOT_DIR; do
echo "the root partition has not yet been mounted"
sleep "$WAIT_TIME"
if [ "$COUNTER" -eq "$MAX_RETRIES" ]; then
echo "mount failed"
exit 1
fi
COUNTER="$((COUNTER + 1))"
done
echo "mount done"
}
wipePartition() {
echo "wipe the content from the / partition"
COUNTER=1
until rm -rf "$ROOT_DIR/*"; do
echo "the wipe has been failed"
sleep 5
if [ "$COUNTER" -eq "$MAX_RETRIES" ]; then
echo "wipe failed"
exit 1
fi
COUNTER="$((COUNTER + 1))"
done
echo "wipe done"
}
umountPartition() {
echo "umount the root / partition"
COUNTER=1
until umount "$ROOT_DIR"; do
2025-07-12 00:28:21 +02:00
echo "umount failed"
sleep 5
if [ "$COUNTER" -eq "$MAX_RETRIES" ]; then
echo "umount failed"
exit 1
fi
COUNTER="$((COUNTER + 1))"
done
echo "umount done"
}
loadModules() {
echo "load the ext4 module"
COUNTER=1
until modprobe ext4; do
echo "modprobe ext4 failed"
sleep 5
if [ "$COUNTER" -eq "$MAX_RETRIES" ]; then
echo "modprobe failed"
exit 1
fi
COUNTER="$((COUNTER + 1))"
done
echo "modprobe done"
}
fixPermissions() {
echo "fix /var/empty permissions"
COUNTER=1
until chattr -i -a "$ROOT_DIR/var/empty"; do
echo "chattr /var/empty failed"
sleep 5
if [ "$COUNTER" -eq "$MAX_RETRIES" ]; then
echo "chattr failed"
exit 1
fi
COUNTER="$((COUNTER + 1))"
done
COUNTER=1
until chmod 755 "$ROOT_DIR/var/empty"; do
echo "chmod /var/empty failed"
sleep 5
if [ "$COUNTER" -eq "$MAX_RETRIES" ]; then
echo "chmod failed"
exit 1
fi
COUNTER="$((COUNTER + 1))"
done
echo "permissions fix done"
}
loadModules
mountPartition
fixPermissions
wipePartition
umountPartition
'';
}