swapfilegoogle-container-os

How to enable swap/swapfile on Google container optimized OS on GCE?


Using the cos-stable container optimized OS on GCE. Micro instance so ram is pretty sparse. Tried to enable swap to prevent locking due to OOM during docker pulls, but can't get it to work.

I realize most folders are stateless, so I put the swapfile under home:

sudo fallocate -l 1G /home/user/swapfile
sudo chmod 600 /home/user/swapfile
sudo mkswap /home/user/swapfile

results in:

Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=6e965805-2ab9-450f-aed6-577e74089dbf

But sudo swapon /home/user/swapfile gives the error:

swapon: /home/user/swapfile: swapon failed: Invalid argument

Any ideas how to enable swap on COS?


Solution

  • Disk based swap is disabled in the COS image. You can enable disk based swap with

    sysctl vm.disk_based_swap=1

    I have the following in my cloud-init:

    bootcmd:
    - sysctl vm.disk_based_swap=1
    - fallocate -l 1G /var/swapfile
    - chmod 600 /var/swapfile
    - mkswap /var/swapfile
    - swapon /var/swapfile