Memset’s next generation Cloud VPS have introduced several new features which are not possible on the previous Miniserver platform. One such possibility is the loading and configuring of custom kernel modules. This blog piece will examine the zRAM kernel module and how to use it as a swap partition on a Cloud VPS.
What is zRam and why is it useful as a swap partition?
Swapping occurs when the Operating System transfers some data from RAM to an alternative storage device, typically the hard disk. Swapping will occur for two reasons. The first is when programs no longer requires some data which was only required during start up. The second occasion is when there is insufficient RAM to support all the active programs. When the latter situation occurs the OS will resort to using the swap as active memory where it would normally use RAM. When this occurs there is a serious performance penalty because accessing the disk is far slower than accessing RAM.
The zRAM module enables the swap partition to be located in RAM instead of the hard disk. This appears counter-intuitive because swapping usually occurs the available RAM has been exhausted. Therefore, putting the data for which there is insufficient RAM back into RAM does not look like it is going to solve any problems. The answer is that the data stored in a zRAM partition is compressed thereby allowing more data to be stored in RAM than there is RAM available. A small amount of CPU time is traded for much more performant swap. These performance gains are why many systems, such as Android 4.4+, which are RAM-constrained employ zRAM.
Enabling a zRam swap partition
This guide will cover the three OS types available as stock images for next-gen Cloud VPS:
- Debian 8 Jessie
- Centos 7
- Ubuntu 16.04 LTS
All of these Linux distributions employ systemd as their init system so this guide will configure systemd to enable and manage the zRAM swap partitions. The steps below use the zile text editor but any command line text editor that you are comfortable will work just as well.
Please note, the zram swap partition is being managed by systemd so an entry in the fstab is not necessary and so has been deliberately omitted.
The following steps are the same for all three distributions mentioned above: The zram module needs to be loaded on boot. This is done by creating the following file:
and adding only the single word:
Then another new file needs to be created with an instruction to set the number of zram devices which should be created:
with the following contents:
options zram num_devices=1
Next, the size of the swap space is configured in this file:
with the following contents for a 512 megabyte partition:
The size of the partition can be changed to suit the needs of your system. The actual size of this partition i.e. how much RAM is used, will grow as more swap is needed from a starting size of 0MB.
Now that the zram kernel module will be loaded and configured at boot we need to create a systemd unit file that will prepare the zram space as swap (mkswap) as well as enable it (swapon). This must be repeated on every boot because the swap partition resides in RAM and is therefore destroyed every time the server is powered off.
Due to the different paths for the mkswap and swapon binaries in CentOS versus the Debian based distributions, there are two versions of the systemd unit file.
All of the distributions have the same location and name for the unit file:
Debian and Ubuntu unit file
[Unit] Description=Swap with zram After=multi-user.target [Service] Type=oneshot RemainAfterExit=true ExecStartPre=/sbin/mkswap /dev/zram0 ExecStart=/sbin/swapon /dev/zram0 ExecStop=/sbin/swapoff /dev/zram0 [Install] WantedBy=multi-user.target
CentOS unit file
[Unit] Description=swap with zram After=multi-user.target [Service] Type=oneshot RemainAfterExit=true ExecStartPre=/usr/sbin/mkswap /dev/zram0 ExecStart=/usr/sbin/swapon /dev/zram0 ExecStop=/usr/sbin/swapoff /dev/zram0 [Install] WantedBy=multi-user.target
Once the zram unit file has been created and populated the new zram service needs enabling so that systemd will start it at boot. This is done on all distributions with the following command:
systemctl enable zram.service
Finally, reboot the server and see if the new swap partition has been enabled. The status of the zram swap space can be checked with the following command:
Which will, if everything has been configured correctly, give the following output:
Filename Type Size Used Priority /dev/zram0 partition 524284 0 -1
The zram systemd service can be controlled like any other systemd service so the following commands will start and stop it:
systemctl start zram.service systemctl stop zram.service
And these will enable and disable it:
systemctl enable zram.service systemctl disable zram.service