systemd / zram-generator

Systemd unit generator for zram devices
MIT License
600 stars 50 forks source link
systemd-generator zram-devices

systemd-zram-setup@.service generator for zram devices

Packaging status

This generator provides a simple and fast mechanism to configure swap on /dev/zram* devices.

The main use case is create swap devices, but devices with a file system can be created too, see below.

Configuration

A default config file may be located in /usr. This generator checks the following locations:

… and the first file found in that list wins.

In addition, "drop-ins" will be loaded from .conf files in /etc/systemd/zram-generator.conf.d/, /usr/lib/systemd/zram-generator.conf.d/, etc.

The main configuration file is read before any of the drop-ins and has the lowest precedence; entries in the drop-in files override entries in the main configuration file.

See systemd.unit(5) for a detailed description of this logic.

See zram-generator.conf.example for a list of available settings.

Swap devices

Create /etc/systemd/zram-generator.conf:

# /etc/systemd/zram-generator.conf
[zram0]
zram-size = ram / 2

A zram device will be created for each section. No actual configuration is necessary (the default of zram-size = min(ram / 2, 4096) will be used unless overriden), but the configuration file with at least one section must exist.

Mount points

# /etc/systemd/zram-generator.conf
[zram1]
mount-point = /var/compressed

This will set up a /dev/zram1 with ext2 and generate a mount unit for /var/compressed.

In case you want this path to be user-writable, since util-linux v2.39 you can use

[zram1]
options = X-mount.mode=1777

(and/or the relevant X.mount.{owner,group}= arguments, cf. mount(8)).

Otherwise, you can use the following "high-quality hack": for the above example, create an override for systemd-zram-setup@zram1.service, for example with systemctl edit, containing the following (note the sticky bit as required for [/var]/tmp):

[Service]
ExecStartPost=/bin/sh -c 'd=$(mktemp -d); mount "$1" "$d"; chmod 1777 "$d"; umount "$d"; rmdir "$d"' _ /dev/%i

Rust

The second purpose of this program is to serve as an example of a systemd generator in rust.

Installation

It is recommended to use an existing package:

To install directly from sources, execute make build && sudo make install NOBUILD=true:

To install and configure with puppet puppet-zram_generator is available.

tl;dr

Once installed and configured, the generator will be invoked by systemd early at boot, there is no need to do anything else.

Testing

The tests require either the zram module to be loaded, or root to run modprobe zram.

Set the ZRAM_GENERATOR_ROOT environment variable to use that instead of / as root.

The "{generator}" template in units/systemd-zram-setup@.service.d/binary-location.conf can be substituted for a non-standard location of the binary for testing.

Authors

Written by Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>, Igor Raits <i.gnatenko.brain@gmail.com>, наб <nabijaczleweli@nabijaczleweli.xyz>, and others. See https://github.com/systemd/zram-generator/graphs/contributors for the full list.