From e09d410b1f6aca75cf0b89b1ab123eda5289b480 Mon Sep 17 00:00:00 2001 From: David Costa <33375428+d-costa@users.noreply.github.com> Date: Thu, 4 Dec 2025 11:45:47 +0000 Subject: [PATCH 1/2] fix: mount disk using cloud init Signed-off-by: David Costa <33375428+d-costa@users.noreply.github.com> --- examples/secure-env-vars/main.tf | 2 +- ...point.sh.tftpl => startup-script.sh.tftpl} | 10 +++++++++- main.tf | 19 +++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) rename examples/secure-env-vars/{custom-entrypoint.sh.tftpl => startup-script.sh.tftpl} (79%) diff --git a/examples/secure-env-vars/main.tf b/examples/secure-env-vars/main.tf index e72520c..3336ec3 100644 --- a/examples/secure-env-vars/main.tf +++ b/examples/secure-env-vars/main.tf @@ -60,7 +60,7 @@ module "atlantis" { command = ["/home/atlantis/custom-entrypoint.sh"] args = ["server"] - startup_script = templatefile("${path.module}/custom-entrypoint.sh.tftpl", { + startup_script = templatefile("${path.module}/startup-script.sh.tftpl", { cloud_sdk_version = "455.0.0" app_key_secret_name = local.secret_names.app_key app_id_secret_name = local.secret_names.app_id diff --git a/examples/secure-env-vars/custom-entrypoint.sh.tftpl b/examples/secure-env-vars/startup-script.sh.tftpl similarity index 79% rename from examples/secure-env-vars/custom-entrypoint.sh.tftpl rename to examples/secure-env-vars/startup-script.sh.tftpl index 324988c..bbb7cab 100644 --- a/examples/secure-env-vars/custom-entrypoint.sh.tftpl +++ b/examples/secure-env-vars/startup-script.sh.tftpl @@ -2,7 +2,15 @@ set -e -mkdir -p ${mount_folder} +# cloud-init executes after the startup script, so we need to wait until the disk is mounted +timeout 1m bash -c 'until /bin/findmnt -t ext4 -T ${mount_folder}' + +# if timeout failed, print msg +if [ $? -ne 0 ]; then + echo "Error: Disk not mounted at ${mount_folder} within timeout period" + echo "Please rerun this script: 'google_metadata_script_runner startup'" +fi + chown 100 ${mount_folder} cat <<'EOF' > "${mount_folder}/${entrypoint_filename}" #!/bin/bash diff --git a/main.tf b/main.tf index cc73570..e467007 100644 --- a/main.tf +++ b/main.tf @@ -38,6 +38,25 @@ data "cloudinit_config" "config" { gzip = false base64_encode = false + part { + # https://docs.cloud.google.com/compute/docs/disks/format-mount-disk-linux + # https://docs.cloud.google.com/container-optimized-os/docs/concepts/disks-and-filesystem#mounting_and_formatting_disks + filename = "bootcmd" + content_type = "text/cloud-config" + merge_type = "list(append)+dict(no_replace, recurse_list)+str()" + content = yamlencode({ + bootcmd = [ + # Format disk if not already formatted + "/sbin/blkid /dev/disk/by-id/google-${local.atlantis_persistent_disk_name} --probe --match-types ext4 || mkfs.ext4 -m 0 -E lazy_itable_init=0,lazy_journal_init=0,discard /dev/disk/by-id/google-${local.atlantis_persistent_disk_name}", + # Check and repair filesystem + "/sbin/fsck.ext4 -tvy /dev/disk/by-id/google-${local.atlantis_persistent_disk_name}", + # Create mount point and mount the disk + "mkdir -p ${local.atlantis_disk_mount_path}", + "mount -t ext4 -o discard,defaults /dev/disk/by-id/google-${local.atlantis_persistent_disk_name} ${local.atlantis_disk_mount_path}" + ] + }) + } + part { filename = "runcmda" content_type = "text/cloud-config" From 953ff556b948abdc5209bb053de17e06629669f7 Mon Sep 17 00:00:00 2001 From: David Costa <33375428+d-costa@users.noreply.github.com> Date: Mon, 6 Apr 2026 11:27:29 +0100 Subject: [PATCH 2/2] Update examples/secure-env-vars/startup-script.sh.tftpl Co-authored-by: Karolis Pocius <10368398+kpocius@users.noreply.github.com> Signed-off-by: David Costa <33375428+d-costa@users.noreply.github.com> --- examples/secure-env-vars/startup-script.sh.tftpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/secure-env-vars/startup-script.sh.tftpl b/examples/secure-env-vars/startup-script.sh.tftpl index bbb7cab..1e724af 100644 --- a/examples/secure-env-vars/startup-script.sh.tftpl +++ b/examples/secure-env-vars/startup-script.sh.tftpl @@ -3,7 +3,7 @@ set -e # cloud-init executes after the startup script, so we need to wait until the disk is mounted -timeout 1m bash -c 'until /bin/findmnt -t ext4 -T ${mount_folder}' +timeout 1m bash -c "until /bin/findmnt -t ext4 -T ${mount_folder}; do sleep 1; done" # if timeout failed, print msg if [ $? -ne 0 ]; then