Skip to content

Upgrading Container Optimized OS

Claude edited this page Nov 6, 2026 · 29 revisions

Moves your vault to a newer Container-Optimized OS milestone by building a new instance and moving the data disk to it.¹

Everything on this page runs in Cloud Shell.² Verify each step rather than assuming success, and delete nothing until the vault serves from the new instance.

Just finished the migration? Your session is set up. Start at Part 1.


Which page do you need?

gcloud compute ssh $INSTANCE --zone $ZONE --command 'df -h /mnt/disks/bwgc'
Result Go to
A filesystem is listed You are in the right place. Continue below.
No such file or directory Migrating to a Data Disk first, once. It ends by sending you back here.³

Before you start

Work through Cloud Shell Setup. It leaves you in ~/bwgc with the scripts downloaded and INSTANCE and ZONE exported.

If Cloud Shell has timed out since, return to the script directory and re-export the two variables. Run the script from ~/bwgc, because it downloads the backup beside wherever it runs:

cd ~/bwgc
export INSTANCE=bitwarden
export ZONE=us-central1-a

Part 1. Confirm you need this

Step 1. Find what your instance is running

export MILESTONE=$(gcloud compute ssh $INSTANCE --zone $ZONE --command 'grep ^VERSION_ID= /etc/os-release' 2>/dev/null | cut -d= -f2 | tr -d '\r')
export SRC_IMAGE=$(gcloud compute ssh $INSTANCE --zone $ZONE --command 'curl -s -H "Metadata-Flavor: Google" http://metadata.google.internal/computeMetadata/v1/instance/image' 2>/dev/null | sed 's|.*/||' | tr -d '\r')
echo "milestone $MILESTONE, built from $SRC_IMAGE"
milestone 109, built from cos-stable-109-17800-0-51

If either value is empty, the SSH connection failed. Check the instance is running:

gcloud compute instances describe $INSTANCE --zone $ZONE --format='value(status)'

Step 2. Check whether your milestone is still supported

gcloud compute images describe-from-family cos-$MILESTONE-lts --project cos-cloud --format='value(name)'
Result Meaning
An image name Still supported. Upgrading is optional.
NOT_FOUND Support has ended, or you are on a beta or dev milestone. Upgrade either way.
Telling the two NOT_FOUND cases apart (optional, slow)

This walks every image in cos-cloud and takes about a minute:

gcloud compute images list --project cos-cloud --show-deprecated --format="value(family)" | grep -cx "cos-$MILESTONE-lts"

A non-zero count means the family existed and has been withdrawn. Zero means it never existed: you are on a beta or dev milestone.

What the image itself says (optional)
gcloud compute images describe $SRC_IMAGE --project cos-cloud --format='value(family,deprecated.state)'
cos-stable	DEPRECATED

A blank second field is what a live LTS milestone returns. DEPRECATED only means a newer build exists, which is true of almost every image eventually, so it does not answer the question; Step 2's check does.⁴

Step 3. See which milestones are available

gcloud compute images list --project cos-cloud --format="value(family)" | grep -E '^cos-[0-9]+-lts$' | sort -u -t- -k2 -n
cos-117-lts
cos-121-lts
cos-125-lts
cos-129-lts

The highest is where you are going. The script picks it automatically.⁵


Part 2. Upgrade

Step 4. Start the upgrade

./upgrade-cos.sh --instance $INSTANCE --zone $ZONE

Step 5. Confirm the plan

=== Resolving the target milestone
newest live LTS family: cos-129-lts
data disk read from bitwarden: bwgc-data

=== Plan
  from        bitwarden   (COS milestone 109)
  to          bitwarden-129  (cos-129-lts, 10GB pd-standard)
  data disk   bwgc-data  ->  /mnt/disks/bwgc
  zone        us-central1-a

  bitwarden and its boot disk are DELETED before the replacement is built.
  bwgc-data is detached first and keeps your vault. There is no way back
  to the old milestone afterwards.
Proceed? [y/N]

Type y.

Step 6. Note where your backup landed

backup pulled to /home/you/bwgc/bwgc-backups/bw_backup_....aes256
KEEP THIS until the new instance is confirmed working from a real client.
It is encrypted with BACKUP_ENCRYPTION_KEY from your .env. Without that key it
is useless -- record it now.

Keep this file. It is your rollback. If the script said it is NOT encrypted, it is a readable copy of your vault: keep it off shared machines and delete it once the new instance is confirmed.

Step 7. Answer the prompts

Stay at the keyboard. Every prompt reads [y/N]: type y to go on. Pressing Enter aborts.

Prompt When Answer
Proceed? immediately Already answered in Step 5.
Continue with no network tags? vault still serving n, and add the http-server and https-server tags first. Answer y only on the Cloudflare Tunnel path.⁶
Delete ... now, before the replacement exists? vault already stopped, data disk detached y.⁷

Other prompts may appear, such as Continue without a local copy? and Continue and let the external IP change?. Read each one. Answering n costs nothing until the delete prompt; from there on the vault is down.

If you answered n at the delete prompt, the old instance is shut down with the data disk detached. Nothing is lost, but re-running will not fix it.⁸ Put it back by hand:

gcloud compute instances attach-disk $INSTANCE --disk bwgc-data --device-name bwgc-data --zone $ZONE
gcloud compute instances start $INSTANCE --zone $ZONE

Confirm df -h /mnt/disks/bwgc and docker ps, then start again from Step 4 when ready.

After the delete prompt the script builds the replacement with the data disk attached, on the same external IP.⁹

Step 8. Point at the new instance

export INSTANCE=${INSTANCE}-129

Use the milestone number the script reported.


Part 3. Verify

Step 9. Check the instance

gcloud compute ssh $INSTANCE --zone $ZONE --command 'grep -E "VERSION|BUILD_ID" /etc/os-release; docker --version; df -h /mnt/disks/bwgc; lsblk -o NAME,SIZE,TYPE | head -4'

You want the new milestone, a newer Docker, and the data disk mounted.

Step 10. Check the containers

gcloud compute ssh $INSTANCE --zone $ZONE --command 'docker ps -a --format "{{.Names}}\t{{.Status}}"'

Every service Up, and bitwarden showing healthy.

Step 11. Check fail2ban

gcloud compute ssh $INSTANCE --zone $ZONE --command 'docker exec fail2ban fail2ban-client status'

Jails should be listed. A count of 0 means fail2ban is protecting nothing.

Step 12. Check backups work on the new host

gcloud compute ssh $INSTANCE --zone $ZONE --command 'docker exec backup ash /backup.sh local'
gcloud compute ssh $INSTANCE --zone $ZONE --command 'ls -lt /mnt/disks/bwgc/bitwarden_gcloud/bitwarden/backups/ | head -3'

The newest bw_backup_ entry should carry today's timestamp and a non-zero size.

Step 13. Check the vault from outside

export DOMAIN=$(gcloud compute ssh $INSTANCE --zone $ZONE --command 'grep -E "^DOMAIN=" /mnt/disks/bwgc/bitwarden_gcloud/.env' 2>/dev/null | cut -d= -f2 | tr -d '\r"'"'"'')
echo "domain $DOMAIN"
domain vault.example.com

The vault answers:

curl -sI https://$DOMAIN/api/version | head -1
HTTP/2 200

The security headers reach subpaths, not just the root:

curl -sI https://$DOMAIN/admin | grep -i x-frame-options
x-frame-options: DENY

Step 14. Log in from a real client

Do this before deleting anything. Check your entries and attachments are there.¹⁰

Step 15. Look for anything still billing

gcloud compute disks list --filter='-users:*'
gcloud compute addresses list --filter='status!=IN_USE'

Both are usually empty, and then you are done. Anything listed bills: a disk counts against your 30 GB, and an unattached address bills on its own. Delete an address you do not need, taking its name from the listing:¹¹

gcloud compute addresses delete NAME-FROM-THE-LISTING-ABOVE --region ${ZONE%-*}

Rollback

To go back to the previous milestone, build another instance on that family. Give it an explicit name:¹²

./upgrade-cos.sh --instance $INSTANCE --zone $ZONE --new-instance bitwarden-rollback --image-family cos-125-lts

This is the same one-way swap run again, with the same prompts and the same backup: answer Step 7's prompts, and work through Steps 9 to 15 afterwards. The data disk is never rewritten by the upgrade, so it carries your vault either way.


If the data disk itself is lost

This is the only path that uses the downloaded archive. Read it through before starting.

1. Stop the old stack and free the container names

Only on a host that has been through the migration:¹³

gcloud compute ssh $INSTANCE --zone $ZONE --command 'sudo systemctl stop bwgc-supervise.timer bwgc.service'
gcloud compute ssh $INSTANCE --zone $ZONE --command 'docker rm -f proxy bitwarden backup fail2ban ddns countryblock cloudflared watchtower 2>/dev/null; true'

This destroys nothing: the vault lived on the data disk, which is already gone.

2. Send one archive up from Cloud Shell

List the archives and pick one. Do not glob; there is usually more than one:

ls -t ~/bwgc/bwgc-backups/
gcloud compute scp ~/bwgc/bwgc-backups/bw_backup_YYYY-MM-DD-HHMMSS.tar.gz.aes256 $INSTANCE:~/ --zone $ZONE

An unencrypted archive has no .aes256 suffix. instance-<name>.json in that directory is a configuration dump, not a backup.

3. Restore into a directory of its own

Connect, and stay in this session through step 6:

gcloud compute ssh $INSTANCE --zone $ZONE

On the instance:¹⁴

git clone https://github.com/dadatuputi/bitwarden_gcloud.git ~/bwgc-restore
cd ~/bwgc-restore
git checkout -q "$(git tag -l 'v*' --sort=-v:refname | head -1)" && git describe --tags
mkdir -p bitwarden/backups
mv ~/bw_backup_* bitwarden/backups/

Recover .env from the archive. Encrypted:

openssl enc -d -aes256 -salt -pbkdf2 -pass pass:'YOUR-KEY' \
  -in bitwarden/backups/bw_backup_YYYY-MM-DD-HHMMSS.tar.gz.aes256 | tar xzf - .env

Unencrypted:

tar xzf bitwarden/backups/bw_backup_YYYY-MM-DD-HHMMSS.tar.gz .env

If the archive has no .env, copy .env.template to .env and fill it in. At least set DOMAIN and EMAIL, or the stack will not start.¹⁵

4. Run the restore, with the vault stopped

Start only the backup container, and keep --no-deps:¹⁶

docker-compose up -d --no-deps backup
docker exec -it -e RESTORE_FORCE=true backup backup restore /data/backups/bw_backup_YYYY-MM-DD-HHMMSS.tar.gz.aes256

Drop .aes256 from the filename if the archive is unencrypted.¹⁷ Check the result:

ls -l bitwarden/db.sqlite3 bitwarden/.env.restored

5. Put .env in place and bring the stack up

cp bitwarden/.env.restored .env
docker-compose up -d

Log in and confirm your entries and attachments.¹⁸

6. Adopt the restored deployment

Point the familiar path at it. This removes only the symlink:

sudo rm -rf ~/bitwarden_gcloud && ln -s ~/bwgc-restore ~/bitwarden_gcloud

Remove the old deployment's leftover network and certificate volume:

docker network rm bitwarden_gcloud_default 2>/dev/null
docker volume rm bitwarden_gcloud_caddycerts 2>/dev/null

7. Get back onto a data disk

Leave the instance shell. The rest runs in Cloud Shell:

exit

First, make sure the old disk is really gone. If a disk with your old name (default bwgc-data) still exists, do not reattach or reuse it.¹⁹ Look at it read-only instead:

gcloud compute instances attach-disk $INSTANCE --disk bwgc-data --device-name bwgc-data --zone $ZONE --mode ro
gcloud compute ssh $INSTANCE --zone $ZONE --command 'sudo mkdir -p /mnt/check && sudo mount -o ro /dev/disk/by-id/google-bwgc-data /mnt/check && ls -l /mnt/check/bitwarden_gcloud/bitwarden/db.sqlite3'

If that database is newer than your archive, stop here: detach it and run the upgrade against that disk instead.

Only if the old disk is gone, create a new one with the same name:

export DATA_DISK=bwgc-data
gcloud compute disks create $DATA_DISK --size 15GB --type pd-standard --zone $ZONE
gcloud compute instances attach-disk $INSTANCE --disk $DATA_DISK --device-name $DATA_DISK --zone $ZONE

Then run the migration again:

./migrate-to-data-disk.sh --instance $INSTANCE --zone $ZONE --disk-name $DATA_DISK

Answer y at Replace the user-data metadata? [y/N]. This prompt is not optional here.²⁰ If gcloud instead says No change requested; skipping update, that is also correct.²¹

8. Clean up the restore directory

Once step 7 has finished and the vault works from /mnt/disks/bwgc:²²

docker volume rm bwgc-restore_caddycerts 2>/dev/null
sudo rm -rf ~/bwgc-restore

Reference

Free tier

The Always Free tier covers 30 GB of standard persistent disk and one e2-micro in us-west1, us-central1 or us-east1.

  • Only pd-standard is free. pd-balanced and pd-ssd charge from the first gigabyte. Both scripts pass --type=pd-standard.
  • Snapshots are never free. Keeping "a snapshot of the last good image" is the one intuitive approach that generates a bill.
  • The instance allowance is by time, not by instance. Two e2-micro instances running at once draw the same pool down at twice the rate, so --overlap costs instance-hours as well as disk. The default deletes first and avoids both.

Sizing

within 3 GB of filling the default size?
  no  -> use the default (15 GB)
  yes -> room to expand?
           yes -> expand to the ceiling, with a warning
           no  -> stop, and offer --force

The ceiling is what the allowance leaves beside your boot disk, which the script reads from the instance. On a 10 GB boot disk it is 20 GB:

Vault Data disk Result
up to 11 GB 15 GB 25 GB total, 5 GB spare
12–17 GB 20 GB prompts first, no margin left
18 GB or more stops --force --disk-size <size> to accept the charge

A 30 GB boot disk, from the old instructions, leaves no free-tier room for a data disk; this upgrade ends that by rebuilding the boot disk at 10 GB.²³

Options

migrate-to-data-disk.sh
  --instance NAME       required
  --zone ZONE           required
  --disk-name NAME      default bwgc-data
  --disk-size SIZE      override the automatic size
  --mount PATH          default /mnt/disks/bwgc
  --reboot-time HH:MM   OS update reboot window, default 06:00
  --print-cloud-config  print the generated cloud-config and exit
  --yes                 skip prompts
  --force               size past the free tier and accept the charge
upgrade-cos.sh
  --instance NAME       required
  --zone ZONE           required
  --new-instance NAME   default NAME-<milestone>
  --image-family FAM    default: newest live cos-*-lts
  --boot-size SIZE      default 10GB, raised to the image minimum if higher
  --overlap             keep the old instance until the new one verifies.
                        Needs boot + boot + data inside 30 GB, so a data disk of
                        10 GB or less. The default this wiki creates is 15 GB,
                        which rules it out
  --keep-old            never delete the old instance
  --no-reserve-ip       let the external IP change instead of reserving it
  --disk-name NAME      the vault data disk. Read from the instance when not
                        given; pass it if you named the disk yourself during
                        the migration
  --mount PATH          where the data disk mounts, default /mnt/disks/bwgc
  --boot-disk-device-name NAME
                        device name for the new boot disk
  --reboot-time HH:MM   OS update reboot window, default 06:00
  --delete-first        destroy the old instance first. THIS IS THE DEFAULT.
  --yes                 skip prompts

./upgrade-cos.sh --help prints the same list.

What the update timer does not do

The timer from utilities/ keeps you patched within your milestone. It cannot move you between milestones, and once a milestone stops shipping it finds nothing, forever. Keep the timer; use these scripts to change milestone.

gcloud compute ssh $INSTANCE --zone $ZONE --command 'systemctl list-timers cos-update-reboot.timer --no-pager'

Limits

  • Assumes one instance and no external load balancer. The external IP is preserved by reservation, so DNS pointed at it by hand keeps working. Confirm the address after the upgrade if you passed --no-reserve-ip.
  • SSH host keys change with the instance; your client will warn on first connection.
  • diskSizeGb states what an image requires, not what its partitions consume. On a 30 GB disk COS uses 4.15 GB for OS partitions. Step 9 prints the split on the new instance.

See also


Notes

1. A running instance never moves to a newer milestone. Patches arrive within the milestone it was built on, and when that milestone stops shipping builds, the instance stops receiving them.

2. Cloud Shell has gcloud installed and signed in. Container-Optimized OS does not ship gcloud.

3. The upgrade detaches the vault's disk from the old instance and attaches it to the new one. Instances built before this layout existed, or from the older Installation instructions, keep everything on the boot disk and need the migration once.

4. Milestone-pinned families are always named -lts. The rolling channels (cos-stable, cos-beta, cos-dev) carry no milestone number, so an instance built from one still sits on whichever milestone that image came from.

5. gcloud compute images list leaves out deprecated images unless asked, so a family appears only while at least one of its images is current. Do not add --show-deprecated here: every build is marked deprecated once a newer one replaces it inside a healthy family, so the full listing reports live milestones as dead.

6. Without those tags the replacement would serve only on localhost. On the tunnel path no tags is correct.

7. Deleting the old instance before building the new one keeps only one boot disk and one instance in existence, so neither the disk allowance nor the instance-hours allowance is drawn down twice. Your data is on the detached disk and in the backup from Step 6.

8. The script cannot ssh into a stopped instance.

9. Deleting an instance releases its external IP unless the address is reserved, and a released address can go to someone else. The script reserves it first and reports the address, so your DNS record stays correct. With --no-reserve-ip the replacement gets a new address and your DNS record is stale until you change it; the ddns container fixes it only if you configured it with a provider and credentials.

10. A vault returning 200 is not the same as a vault with your data in it.

11. The script names the reservation after the instance it upgraded, not the replacement, so after Step 8 it no longer matches $INSTANCE: upgrading bitwarden leaves bitwarden-ip, not bitwarden-129-ip. ${ZONE%-*} is the zone without its trailing letter, so us-central1-a becomes us-central1. The reservation is usually still attached to the new instance, which is why the listing is normally empty.

12. This is not a way back to the instance you deleted; it deletes the current instance and its boot disk just as before. By now $INSTANCE is the suffixed name, so without --new-instance you would get names like bitwarden-129-125.

13. bwgc-supervise.timer recreates the stack from /mnt/disks/bwgc every five minutes using the same container names. Stopping the systemd units also does not remove the containers, so they keep their names and a new project cannot reuse them.

14. Not ~/bitwarden_gcloud: on any instance this wiki builds, that is a symlink to the data disk.

15. The archive has no .env if BACKUP_ENV was not true when it was taken.

16. Without --no-deps, compose also starts bitwarden, and the restore would overwrite the database while vaultwarden holds it open.

17. The restore normally refuses unless it can confirm vaultwarden is stopped. It cannot reach the Docker daemon from inside the container, so it needs RESTORE_FORCE=true, which is safe only because the previous command left vaultwarden stopped.

18. With BACKUP_ENV=true the restore writes the archive's .env to bitwarden/.env.restored, but compose reads .env beside docker-compose.yml, so it has to be copied into place.

19. The migration copies ~/bitwarden_gcloud onto the disk with rsync --delete, and that path now points at the restored copy. Pointing it at a disk that still holds a vault would replace that vault with the restore.

20. The instance's metadata still names the disk you lost, so the script shows a diff and asks. Declining aborts the run and leaves the instance set to mount a disk that is gone, so the next reboot comes up with no vault. This prompt does not appear on a first migration.

21. When the disk name is unchanged, the regenerated configuration is identical to what is already set, so there is nothing to replace.

22. ~/bwgc-restore is then a stale copy, and leaving it blocks a future recovery, whose first command clones into that same path.

23. The script prices the overage on a 30 GB boot disk and asks. Google has declared a 10 GB minimum for every COS milestone since 2019, across 22 milestones; the scripts read it rather than assume it.

Clone this wiki locally