Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Document new zram generator #323

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]
```
tormath1 marked this conversation as resolved.
Show resolved Hide resolved

## 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]
```
Loading