Managing swap space Flatcar Container Linux
Swap is the process of moving pages of memory to a designated part of the hard disk, freeing up space when needed. Swap can be used to alleviate problems with low-memory environments. A modern alternative to slow swap partitions is to use RAM compression with zram.
By default Flatcar Container Linux does not include a partition for swap nor a zram configuration, however one can configure their system to have swap, either by including a dedicated partition for it, creating a swapfile, or setting up zram.
Using zram
With zram a virtual /dev/zram0 device acts as swap space which lives compressed in memory.
One can activate zram by creating a [zram0] section in /etc/systemd/zram-generator.conf.
Here are Butane config (YAML) that creates the file:
|
|
You can tweak the size by using the minimum of either 8 GB if one has a lot of RAM or the size of the RAM, e.g., 1 GB, by setting zram-size:
|
|
Allocating a compressed RAM section that can hold an amount up to the size of the RAM itself works well because most RAM contents compress very well.
After a reboot you can check the results:
|
|
Managing swap with systemd
systemd provides a specialized .swap unit file type which may be used to activate swap. The below example shows how to add a swapfile and activate it using systemd.
Creating a swapfile
The following commands, run as root, will make a 1GiB file suitable for use as swap.
From Flatcar 4186, one can directly use mkswap to create a swap file as follow. For older Flatcar versions, you still need to create a swap file with
dd if=/dev/zero of=/var/vm/swapfile1,chmod 600 /var/vm/swapfile1andmkswap /var/vm/swapfile1
|
|
Creating the systemd unit file
The following systemd unit activates the swapfile we created. It should be written to /etc/systemd/system/var-vm-swapfile1.swap.
|
|
Enable the unit and start using swap
Use systemctl to enable the unit once created. The swappiness value may be modified if desired.
|
|
Swap has been enabled and will be started automatically on subsequent reboots. We can verify that the swap is activated by running swapon:
|
|
Problems and Considerations
Btrfs and xfs
Please check the
btrfs instructions
on how to create swapfiles on btrfs.
In summary, you must use a single device filesystem, make sure you create the file on a non-snapshotted subvolume
(e.g., to make sure this is the case you can create a new subvolume for the file), create the file with truncate -s 0 ./swapfile1
and then disable CoW and compression (chattr +C ./swapfile1, btrfs property set ./swapfile1 compression none).
Swapfiles should not be created on xfs volumes. For systems using xfs, it is recommended to create a dedicated swap partition.
Partition size
The swapfile cannot be larger than the partition on which it is stored.
Checking if a system can use a swapfile
Use the df(1) command to verify that a partition has the right format and enough available space:
|
|
The block device mounted at /var/, /dev/sdXN, is the correct filesystem type and has enough space for a 1GiB swapfile.
Adding swap with a Butane Config
The following config sets up a 1GiB swapfile located at /var/vm/swapfile1.
|
|
Using a dedicated swap disk
The following Butane config sets up /dev/sdb to be used as swap:
|
|
NB the systemd unit name is created by
systemd-escape -p /dev/disk/by-partlabel/swap as systemd uses - as the
path separator meaning that paths containing - have to be escaped. This
leads to a file 'dev-disk-by\x2dpartlabel-swap.swap' being created in
/etc/systemd/system.