Zram

From Proxmox VE
Revision as of 10:33, 8 February 2019 by Martin (talk | contribs) (page create)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Introduction

zram is a Linux kernel module to create RAM based block devices [1]. It's main feature distinguishing it from the classical ramdisks such as tmpfs and ramfs is the on-the-fly compression via LZ4 or LZO, LZ4 being the default [2]. LZO compression is more efficient, but LZ4 is faster at compression/decompression.

Use cases for zram are as compressed swap disk or as general purpose RAM disk, providing high throughput and low latency. The compression is of special interest for low memory devices. The huge drawbacks are of course the possible data loss on power failure and the limited disk size.

With zswap there exists a RAM-based compressed cache for swap devices as alternative. zswap provides the ability to compress swapped pages and stores less frequent pages to a physical disk, which is therefore required [3].

Setup and usage

The easiest way to manage zram block devices is via the zramctl utility provided by util-linux which is already installed on any Proxmox VE.

For this to work, the zram kernel module, part of the Linux mainline kernel since version 3.14, needs to be loaded as Proxmox VE does not loads this module by default.

Enable a zram-disk

To load the zram module, run the following command in a root shell:

modprobe zram

This will load the kernel module and create a new block device accessible under /dev/zram0. With a initial disk size of 0B the zram-disk is rather useless, so one might change the size to something usable, e.g. 4GiB. This is done with the command:

zramctl --size 4G /dev/zram0

Statistics for the newly created block device can be obtained via zramctl. For example, we can display the properties for the newly created zram-disk with:

# zramctl
NAME       ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 lzo             4G   0B    0B    0B       4 

To make this device a swap device run the two following commands:

mkswap /dev/zram0
swapon /dev/zram0 -p 10

The -p option of swapon gives this swap device a higher priority over others, using the zram-disk prefered over other swap devices.

Disable the zram-disk

To disable the swap and also zram-disk we first run:

swapoff /dev/zram0

to disable it as swap device and finally reset the size of the zram-disk to zero by:

zramctl --reset /dev/zram0

For further detail please refere to the manpage man zramctl or [4].

Permanent setup

This is based on a setup described in [5]. To permanently set up a zram-disk as swap device, perform the following steps. Add a file /etc/modules-load.d/zram.conf with the content zram by running:

echo "zram" > /etc/modules-load.d/zram.conf

This will ensure the kernel module is loaded at boot. Next one needs a udev rule to automatically setup the disk size and make a swap device. For this, run:

echo 'KERNEL=="zram0", ATTR{disksize}="4G" RUN="/sbin/mkswap /dev/zram0", TAG+="systemd"' > /etc/udev/rules.d/99-zram.rules

Replace the '4G' disksize with the value you want for your system. As last step, define the device as swap disk in your /etc/fstab by running:

echo "/dev/zram0 none swap defaults,pri=10 0 0" >> /etc/fstab

Make sure to use >> and not > in this command, otherwise you will overwrite your current fstab entries.

Setting up multiple disks

If you wish to create more than one zram-disk at boot, you need to pass the number of disks to the kernel module, which is achieved by:

echo options zram num_devices=N > /etc/modprobe.d/zram.conf

where N should be replaced by the number of zram-disks you want to create. Also you have to add udev rules for each of the devices, e.g. for adding a zram1 disk:

echo 'KERNEL=="zram1", ATTR{disksize}="4G" RUN="/sbin/mkswap /dev/zram1", TAG+="systemd"' >> /etc/udev/rules.d/99-zram.rules

Finally, each disk needs its own entry in /etc/fstab, e.g. by adding the zram1 disk:

echo "/dev/zram1 none swap defaults,pri=10 0 0" >> /etc/fstab

References