Jump to contentJump to page navigation: previous page [access key p]/next page [access key n]
documentation.suse.com / Installation, Configuration and Management of zram on SUSE Linux Micro

Installation, Configuration and Management of zram on SUSE Linux Micro

Publication Date: 05 Dec 2024
WHAT?

zram is a kernel module that creates a compressed block device in RAM.

WHY?

The article describes how to install and configure zram on SUSE Linux Micro.

EFFORT

It takes approximately 15 minutes to read the article.

GOAL

You will have block devices configured in RAM.

REQUIREMENTS

Running and registred instance of SUSE Linux Micro.

1 What is zram?

zram is a kernel module that creates a compressed block device in RAM. The block device can be used as a RAM disk (usually to store temporary files) or for swap.

1.1 Benefits of zram

Using zram can have the following benefits:

  • Fast I/O operations — due to the RAM-based nature of zram, block devices provide very fast access to data.

  • Memory savings — as the data is compressed using zram, a decent amount of memory can be saved. Thus, zram uses RAM efficiently and is particularly suitable in environments with low RAM size, for example, embedded systems.

  • File system on the block device — you can create file systems on the block devices.

1.2 Limitations of zram

Hibernation to zram swap is not supported even if you configure a backing device on permanent storage.

2 Setting up zram

To properly set up zram on your system, perform the following steps. Details are provided in the subsequent sections.

  1. Install the zram-generator utility that creates the RAM block device according to the provided configuration. For details on the installation, refer to Section 2.1, “Installing zram packages”.

  2. Prepare the configuration as described in Section 2.2, “Configuration needed for zram”.

  3. Run commands to create new device units as described in Section 2.3, “Creating device units”.

  4. Reboot your machine.

  5. Verify that everything works correctly. Run the zramctl command, which should produce an output similar to the following:

    > sudo zramctl
    
    NAME       ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
    /dev/zram1 lzo-rle      96.6M   4K   79B   12K       1 [SWAP]
    /dev/zram0 lzo-rle       483M   4K   79B   12K       1 [SWAP]

2.1 Installing zram packages

The package zram-generator is not available by default on SUSE Linux Micro. Install the package as follows:

> sudo transactional-update pkg install zram-generator

After successful installation, reboot the system.

2.2 Configuration needed for zram

The main configuration file of zram is /etc/systemd/zram-generator.conf. However, you can create a directory /etc/systemd/zram-generator.conf.d/ with a set of drop-in files that can override entries in the main configuration file.

2.2.1 The /etc/systemd/zram-generator.conf file

The file is not installed along with the zram-generator package, so you need to write your own. Options to add are described below. An example is provided in Section 2.2.1.1, “An example of /etc/systemd/zram-generator.conf.

Define at least one zram device. Numbering starts at 0, so to define one zram device, add a [zram0] section to the configuration file.

You can define the following entries under the zram device section:

host-memory-limit

Specify the maximum RAM size of the host in MiB when a block device can be created. If the value is higher, the zram device will not be created. If not set or set to none, there is no limit to the host RAM.

zram-size

Specify the size of the zram device as compared to the host RAM. The default value is zram-size = min(ram / 10, RAM_HARD_LIMIT). It is a minimum of either, where ram is the host RAM size obtained using MemTotal.

  • ram is the total amount of the host memory in MB.

  • ram/10 – is the ratio of total host memory that can be used for zram. According to the example, zram can use 10% of the total host memory. We recommend fractions of the host RAM in the range 10 – 50%.

  • RAM_HARD_LIMIT is the maximum amount of RAM in MB that can be used by zram.

  • min() is a function that compares the value of RAM_HARD_LIMIT and the calculated ratio and selects the lower one.

compression-algorithm

Specify the algorithm used to compress data in zram. You can use any of the following values:

  • deflate — a lossless data compression file format

  • lz4 or lz4hc — a lossless data compression algorithm that is focused on compression and decompression speed

  • lzo-rle — a lossless data compression algorithm that is focused on decompression speed

  • zstd aka Zstandard — a lossless data compression algorithm comparable to DEFLATE with the compression ratio but faster during decompression

  • 842 — a data compression algorithm

If omitted, the kernel default is used (usually LZO-RLE).

options

Specify options for swap or mount options for a RAM disk. If set to discard or omitted, the unused compressed pages are trimmed during the system run. Such a solution provides efficient zram management. To override the default (discard), leave the value empty:

options =
writeback-device

Specify a device where to store incompressible pages. These pages are then removed from zram.

fs-type

If the device is used as a disk, specify the file system to be used there. For example, ext4.

mount-point

Specify a directory where to mount the file system. This option is not used when the zram device is used for swap.

2.2.1.1 An example of /etc/systemd/zram-generator.conf

The following example creates two block devices in RAM — zram0 for swap and zram1 with the ext4 file system. If the machine has over 9 GB of RAM, zram0 will not be created. Each block device will allocate up to 2 GB of RAM.

[zram0]
host-memory-limit = 9048      
zram-size = min(ram / 10, 2048)    
compression-algorithm = lzo-rle    
options = discard      
writeback-device = /dev/zvol/tarta-zoot/swap-writeback
    
[zram1]      
zram-size = ram / 10
fs-type = ext4
mount-point = /run/compressed-mount-point

2.3 Creating device units

After you prepare the configuration, create and start the devices. To do this, see the procedure below:

  1. Create the zram device units by running the command:

    > sudo systemctl daemon-reload
  2. Start the device:

    > sudo systemctl start DEVICE_NAME
    DEVICE_NAME is the name you configured in
    /etc/systemd/zram-generator.conf and it is in the format /dev/zramN
  3. To verify that modules were started, run the following command for each device:

    > sudo systemctl status DEVICE_NAME