How and why a modern Linux device should use a virtual swap partition
Typically a Linux operating system will create and use a swap partition that’s resident on the hard drive, and this would act as an extension to system memory. The swap partition and system memory are abstracted into a single virtual memory space, but the kernel, by default, will usually read and write data between the two in 4KB ‘pages’.
I’ve only recently had a personal laptop with an SSD (which can’t easily be replaced), and it occurred to me that having data paged continually to a partition might reduce its lifespan. Fortunately there is a known method of disabling the disk-based swap partition, and still making the kernel think there’s more system memory than there actually is. Rather, it’s a technique that uses in-memory file compression for swap paging. This would also enhance security, because it would reduce the chances of encryption keys and suchlike being written to persistent storage (which itself is encrypted anyway on my laptop).
The following guide is going to deviate somewhat from the Red Hat and Arch Linux docs, because I’ve done the following by experimentation.
First thing we need is to install zram-tools. There are no obvious command line tools for managing the zram kernel module(s), but a configuration for it will be created in /etc/default/zramswap. The default (abbreviated) content of the file would look something like this:
# Compression algorithm selection
ALGO=lz4
# The amount of RAM to use for zram as a percentage of the
# available memory. Takes precedence and overrides SIZE below
PERCENT=50
# Specifies a static amount of RAM that should be used for
# the ZRAM devices, this is in MiB
SIZE=512
# Specifies the priority for the swap devices, see swapon(2)
PRIORITY=100
The ‘PERCENT’ value determines the size of the zram partition. Unless we’re running some resource-intensive software, 2GB or 25% should be fine, for zram size, on a device with 8GB memory. There are several possible compression algorithms zram could use. As a general rule, there’s a trade-off between compression rate and processing time. LZ4 (the default here) is the best for most cases. I have the PRIORITY value set to 100, so the kernel should default to using zram rather than the conventional zswap.
The next question is how to get the kernel to actually use zram. There’s obviously going to be something that sets up the virtual disk, and a kernel module to control the paging of data to it. And the service needs to run every time the device is started.
Creating a file at /etc/modules-load.d/zram.conf, and simply writing ‘zram’ to it seemed to work.
I used the ‘zramctl’ command, to check whether zram was indeed active, and the operating system created /dev/zram0 as a virtual block device of just over 3GB (it should have been 2GB). I then restarted my device to make sure, and it was being loaded automatically.
Next thing to check is whether the standard swap partition is still being used. The main setting for this is in /etc/default/grub. If there’s a zswap.enabled=1 somewhere in the CMDLINE_LINUX_DEFAULT lines of the file, remove it, save the file and run update=grub.
Running swapon -s, the output should look something like this:
root@beatrix2# swapon -s
Filename Type Priority
/dev/dm-1 partition -2
/dev/zram0 partition 100
The entry with the higher priority number will take precedence. In this case, it’s zram0.