Zram as SWAP file in Proxmox VE

Zram Technology 

Zram is a technology which allows you to create a block device in RAM on which data is compressed by one of the selected algorithms when saving. 

There are several algorithms available, depending on the version of the operating system. For example, in the latest Proxmox version, these are lzo, lz4, lz4hc, deflate. The default setting is lzo. This is probably because it was added to the kernel module earlier and saved as a default setting for compatibility purposes. The most optimal choice at the moment is lzo4, which has an optimal speed to compression ratio and is very fast to decompress. Furthermore, lz4 is available in almost all current linux distributions.

Benefits of Zram Technology

Implementing Zram technology offers several benefits. Here are some key advantages:

1. Increased Storage Capacity: By compressing data on the Zram block device, the overall storage capacity can be increased. This is particularly useful for devices with limited storage capacity, such as mobile devices.

2. Enhanced Performance: Compression reduces the physical size of data, which, in turn, leads to faster read and write operations. This is particularly beneficial for systems that have high performance demands, such as gaming laptops or servers.

3. Energy Efficiency: Compressing data on a Zram block device consumes less power compared to uncompressed storage. This energy efficiency can result in longer battery life for portable devices or lower power consumption in data centers.

4. Flexibility: Zram technology offers flexibility by allowing the selection of different compression algorithms. Users can choose the one that best meets their specific requirements and preferences.

Requirements

To configure ZRam as a swap file in Proxmox VE, the following requirements must be met:

• Proxmox VE version 5.4 or higher.

• Linux kernel version 5.10 or higher.

How to enable the zRAM module for faster swapping on Debian/Ubuntu

This example should work with recent versions of Debian (from version 9) and Ubuntu (from version 18). In this example we add 32G:

modprobe zram
zramctl -s 32G -a lz4 /dev/zram0
mkswap /dev/zram0
swapon /dev/zram0 -p 10


Next, set up automatic zram swap creation at system startup:

echo "zram" > /etc/modules-load.d/zram.conf
echo "zram" > /etc/modules-load.d/zram.confecho 'KERNEL=="zram0", ATTR { disksize } ="32G" RUN="/sbin/mkswap /dev/zram0", TAG+="systemd"' > /etc/udev/rules.d/99-zram.rules
echo "/dev/zram0 none swap defaults,pri=10 0 0" >> /etc/fstab

You can view the partition usage statistics by using the zramctl command without parameters:

zramctl

Example output:

NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 lz4 32G 48.7M 20.1M 21.5M 16 [SWAP]

Conclusion

By configuring ZRam as a swap file in Proxmox VE, you can enjoy the benefits of compressed RAM, such as improved performance and reduced storage requirements.