diff --git a/gcloud/tf/scripts/k3s-vm-startup.sh b/gcloud/tf/scripts/k3s-vm-startup.sh index 3d8a496..665aed3 100644 --- a/gcloud/tf/scripts/k3s-vm-startup.sh +++ b/gcloud/tf/scripts/k3s-vm-startup.sh @@ -1,13 +1,18 @@ #!/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 -# 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, # fully initializes inode tables and the journal, and enables # discard/TRIM for better performance on SSDs or # 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 mkdir -p /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/ # 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 update apt install -y ncdu htop