fix: persist fstab to survive reboots

Enhance k3s VM startup script with improved disk formatting checks and persistence
This commit is contained in:
jon brookes 2025-09-30 11:56:55 +01:00
parent 4a8ae2d1ff
commit 31af97ced8

View file

@ -1,13 +1,18 @@
#!/bin/bash #!/bin/bash
# ensure only run once
if [[ -f /etc/startup_was_launched ]]; then exit 0; fi
touch /etc/startup_was_launched
# Format the k3s disk if not already formatted # Format the k3s disk if not already formatted
# This command creates an ext4 filesystem on the specified # This creates an ext4 filesystem on the specified
# disk with no reserved space for root, forces the operation, # disk with no reserved space for root, forces the operation,
# fully initializes inode tables and the journal, and enables # fully initializes inode tables and the journal, and enables
# discard/TRIM for better performance on SSDs or # discard/TRIM for better performance on SSDs or
# thin-provisioned storage. # thin-provisioned storage.
if ! lsblk | grep -q "/mnt/disks/k3s"; then if ! lsblk | grep -q "/var/lib/rancher/k3s"; then
mkfs.ext4 -m 0 -F -E lazy_itable_init=0,lazy_journal_init=0,discard /dev/disk/by-id/google-k3s-disk mkfs.ext4 -m 0 -F -E lazy_itable_init=0,lazy_journal_init=0,discard /dev/disk/by-id/google-k3s-disk
mkdir -p /var/lib/rancher/k3s mkdir -p /var/lib/rancher/k3s
mount -o discard,defaults /dev/disk/by-id/google-k3s-disk /var/lib/rancher/k3s mount -o discard,defaults /dev/disk/by-id/google-k3s-disk /var/lib/rancher/k3s
@ -26,12 +31,15 @@ fi
# Similarly, a disk named app-data-disk will appear as /dev/ # Similarly, a disk named app-data-disk will appear as /dev/
# disk/by-id/google-app-data-disk. # disk/by-id/google-app-data-disk.
# Add to /etc/fstab for persistence (only if not already present)
if ! grep -q "/var/lib/rancher/k3s" /etc/fstab; then
echo "/dev/disk/by-id/google-k3s-disk /var/lib/rancher/k3s ext4 defaults,discard 0 0" >> /etc/fstab
fi
if ! grep -q "/mnt/disks/app-data" /etc/fstab; then
echo "/dev/disk/by-id/google-app-data-disk /mnt/disks/app-data ext4 defaults,discard 0 0" >> /etc/fstab
fi
# ensure only run once
if [[ -f /etc/startup_was_launched ]]; then exit 0; fi
touch /etc/startup_was_launched
# apt install # apt install
apt update apt update
apt install -y ncdu htop apt install -y ncdu htop