From Proxmox VE
Jump to navigation Jump to search

Warning: There are some reports that zRam may cause soft lockups on certain hardware, if you run into such issues try to disable zRam


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
/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 at boot

If you wish to create more than one zram-disk at boot, you can to pass the number of disks you want to have 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. Further you have to setup udev rules to format and fstab entries to automout also these disks.