Skip to content

Commit

Permalink
docs: Document new zram generator
Browse files Browse the repository at this point in the history
With the generator added in flatcar/scripts#1772
we don't need the manual setup anymore.
  • Loading branch information
pothos committed Apr 16, 2024
1 parent bf241bf commit f7b537d
Showing 1 changed file with 43 additions and 17 deletions.
60 changes: 43 additions & 17 deletions content/docs/latest/setup/storage/adding-swap.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,50 @@ aliases:
---

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.
An alternative is to use RAM compression with zram.
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, however one can configure their system to have swap, either by including a dedicated partition for it or creating a swapfile.
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:

```yaml
variant: flatcar
version: 1.0.0
storage:
files:
- path: /etc/systemd/zram-generator.conf
contents:
inline: |
[zram0]
```
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`:

```yaml
variant: flatcar
version: 1.0.0
storage:
files:
- path: /etc/systemd/zram-generator.conf
contents:
inline: |
[zram0]
zram-size = min(ram, 8192)
```

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:

```shell
$ zramctl
NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 lzo-rle 1G 4K 74B 12K 8 [SWAP]
```

## Managing swap with systemd

Expand Down Expand Up @@ -161,18 +202,3 @@ NB the systemd unit name is created by
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`.

## Using zram

With zram a virtual `/dev/zram0` device acts as swap space which lives compressed in memory.
At the moment there is no zram generator and instead, a manual setup needs to be done, similar to the creation of a swap file.

```shell
$ sudo modprobe zram
$ sudo zramctl -f -s 1G
$ sudo mkswap /dev/zram0
$ sudo swapon /dev/zram0
$ zramctl
NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 lzo-rle 1G 4K 74B 12K 8 [SWAP]
```

0 comments on commit f7b537d

Please sign in to comment.