Skip to content
Claude edited this page Nov 6, 2026 · 5 revisions

The scripts in this project write exactly one piece of instance metadata: a key named user-data holding a cloud-config document. Container-Optimized OS reads it on every boot and uses it to mount the data disk, bind the containers to that disk, and install the daily update-reboot timer. Nothing else in the deployment is stored in metadata.

Scope: what the user-data key contains, and why each part is there. Installation, Migrating to a Data Disk and Upgrading Container-Optimized OS tell you when to apply it. This page does not repeat their steps.

Why the configuration lives in metadata

Container-Optimized OS is an immutable image. /etc is a tmpfs overlay that is rebuilt from the image on every boot, so a systemd unit copied into /etc/systemd/system works until the machine restarts and then disappears. /mnt/disks is tmpfs as well, so even the mount point has to be recreated each time.

Instance metadata is the part of the machine that does persist, because it lives in GCE rather than on the disk. cloud-init reads user-data on every boot and reapplies whatever it declares. That is why the mount and the timer are written as a cloud-config rather than installed onto the filesystem.

Two consequences follow. Changing the running system by hand does not survive a reboot. And changing the metadata does not affect the running system until you reboot or rerun cloud-init.

What the scripts write

migrate-to-data-disk.sh and upgrade-cos.sh generate the same document from the same helper, so both produce identical metadata for the same disk name, mount point and reboot time.

Section Runs Contents
bootcmd every boot, before the rest of cloud-init fsck the data disk, create the mount point, mount it
write_files every boot six scripts under /var/lib/bwgc, a login script under /etc/profile.d, five systemd units under /etc/systemd/system
runcmd every boot reload systemd, start the stack, enable the supervisor and update-reboot timers, link the deployment into each home

Print the exact document for your own instance rather than reading it here:

./utilities/migrate-to-data-disk.sh --print-cloud-config

bootcmd

fsck.ext4 -tvy /dev/disk/by-id/google-bwgc-data
mkdir -p /mnt/disks/bwgc
mount -t ext4 -o discard,defaults /dev/disk/by-id/google-bwgc-data /mnt/disks/bwgc

The disk is addressed by its device name under /dev/disk/by-id/, not by /dev/sdX, because kernel device order is not stable across boots. mkdir is not redundant: /mnt/disks is tmpfs and is empty again on every boot.

write_files

Twelve files, rewritten every boot because those under /etc would otherwise be lost:

  • /var/lib/bwgc/compose.sh runs docker compose against the deployment on the data disk. COS ships no compose binary, so it comes from the docker:cli image.
  • /var/lib/bwgc/start-stack.sh brings the vault up once the disk is mounted.
  • /var/lib/bwgc/supervise-stack.sh restarts anything that has since stopped.
  • /var/lib/bwgc/make-private.sh closes the deployment directory and .env to other local users. The start and supervise scripts run it before every start.
  • /var/lib/bwgc/cos-update-reboot.sh asks update_engine_client whether an OS update has been staged, and reboots only if one has.
  • /var/lib/bwgc/link-homes.sh points ~/bitwarden_gcloud at the deployment in every home present at boot.
  • /etc/profile.d/bwgc-link.sh does the same for a home created later, the first time its user logs in, and loads the docker-compose shim in interactive shells.
  • /etc/systemd/system/bwgc.service runs the start script, and stops the stack on shutdown.
  • /etc/systemd/system/bwgc-supervise.service and .timer run the supervisor every five minutes.
  • /etc/systemd/system/cos-update-reboot.service and .timer run the update check daily.

All three .service units hand their script to /bin/sh rather than putting the path in ExecStart directly. /var is mounted noexec on Container-Optimized OS, so ExecStart=/var/lib/bwgc/cos-update-reboot.sh fails with status=203/EXEC even when the file is present and marked executable. Handing the path to an interpreter reads the file instead of executing it, which noexec permits.

runcmd

systemctl daemon-reload
systemctl enable --now bwgc.service
systemctl enable --now bwgc-supervise.timer
systemctl enable --now cos-update-reboot.timer
/bin/sh /var/lib/bwgc/link-homes.sh

runcmd is the first stage that runs after write_files, so the units exist by then. It also runs after bootcmd, so the data disk is already mounted when the stack starts.

Who starts the containers

docker.service belongs to cos-critical.target and starts roughly twenty seconds before cloud-init mounts the data disk. Any container Docker starts by itself in that window is created while the mount point is still an empty directory on the boot disk.

Docker materialises a missing bind-mount source as a directory, which gives two failure modes:

  • Containers that bind a file, such as backup mounting .env or proxy mounting caddy/Caddyfile, fail to start, because a directory cannot be mounted onto a file. They fail to start, with not a directory in the daemon's error.
  • Containers that bind a directory start normally and read the empty directory on the boot disk instead of the vault. Nothing reports an error, so the vault appears to be up while serving nothing.

The second failure is silent, so the stack is not started by Docker.

The restart policy decides who starts the stack

Docker starts a container at daemon start according to its restart policy. Measured on Container-Optimized OS 129:

Policy Started by the daemon at boot
always yes
unless-stopped yes
on-failure yes
no no

Only no keeps the daemon out of it. docker-compose.yml therefore reads its policy from a variable:

restart: ${BWGC_RESTART_POLICY:-always}

backup is the one exception, defaulting to on-failure. The table above shows that the daemon starts it either way.

A deployment that never sets the variable keeps the behaviour it has always had. migrate-to-data-disk.sh sets BWGC_RESTART_POLICY=no in .env once the data disk exists, because from that point cloud-init owns startup.

Do not set BWGC_RESTART_POLICY=no by hand on a deployment without this metadata. Nothing would start the stack at all.

What starts it instead

bwgc.service runs after the mount and recreates the stack:

compose down --remove-orphans
compose up -d

It tears down first rather than calling up alone. up leaves an already-running container alone, and Docker resolves a bind source when the container starts, so a container that came up on the wrong path stays there. COS enables live-restore, so restarting the daemon does not correct it either. Only recreating the container does.

Because no gives up Docker's own crash recovery, bwgc-supervise.timer runs compose up -d every five minutes. That leaves running containers untouched and starts anything that has stopped. A crashed container is therefore back within five minutes rather than immediately.

A boot now looks like this:

21s  docker.service started, no containers
30s  cloud-init mounts the data disk
32s  bwgc.service starts the stack
39s  the stack up (seven containers on the Caddy path, four on a tunnel)

Reading the metadata on a running instance

gcloud compute instances describe $INSTANCE --zone $ZONE --format='value(metadata.items.user-data)'

Confirm cloud-init applied it:

gcloud compute ssh $INSTANCE --zone $ZONE --command 'findmnt -no SOURCE,TARGET /mnt/disks/bwgc; systemctl is-enabled cos-update-reboot.timer'

Confirm the stack was started by cloud-init and not by Docker:

gcloud compute ssh $INSTANCE --zone $ZONE --command 'systemctl is-active bwgc.service bwgc-supervise.timer; sudo journalctl -b -u bwgc.service'

A healthy boot logs bwgc: recreating the stack against /mnt/disks/bwgc and finishes without error.

Changing it

Regenerate the document and apply it as a whole. Do not hand-edit the value in the console: the scripts assume they own the key, and the next run overwrites whatever is there.

./utilities/migrate-to-data-disk.sh --print-cloud-config > cloud-config.yaml
gcloud compute instances add-metadata $INSTANCE --zone $ZONE --metadata-from-file user-data=cloud-config.yaml

add-metadata replaces the whole user-data key rather than merging into it. Save the current value first if you have edits worth keeping:

gcloud compute instances describe $INSTANCE --zone $ZONE --format='value(metadata.items.user-data)' > user-data.backup

The new metadata takes effect at the next boot. Reboot from inside the guest:

gcloud compute ssh $INSTANCE --zone $ZONE --command 'sync; sudo systemctl reboot'

The connection drops and the command exits non-zero. That is the reboot working.

Do not use gcloud compute instances reset here. It is a power cycle: it neither flushes the page cache nor unmounts, so a file written seconds earlier reaches the disk truncated and the fsck in bootcmd zero-fills the tail. That corrupted a vault's .env on a real run. stop then start is graceful but releases the instance's ephemeral external address; an in-guest reboot keeps it.

Removing it

Removing the key leaves the instance without the data-disk mount. What happens next depends on BWGC_RESTART_POLICY in .env. Set to no, nothing starts the stack at all and the vault does not come back. Unset, Docker starts the containers itself against empty directories on the boot disk, and the vault comes up without its database. The data on the disk is untouched either way. Remove it only when you are also moving the deployment back onto the boot disk.

gcloud compute instances remove-metadata $INSTANCE --zone $ZONE --keys user-data

What is not in metadata

Instance metadata is readable by anyone with compute.instances.get on the project, and by any process on the instance that can reach the metadata server. No secrets are written to it. .env, the backup encryption key, the admin token and the database all stay on the data disk. The cloud-config refers to the disk by name and never carries its contents.

user-data is also a different key from startup-script. A deployment that used a startup script for reboots still has that key, and it still runs. See Automatic Restarts for removing it.

Clone this wiki locally