In this Linux tutorial, you will learn how to enable zRAM in Rocky Linux 9 or other Red Hat based Linux distros.
What is zRAM?:
zRAM, formerly called compcache, is a Linux kernel module for creating a compressed block device in RAM, i.e. a RAM disk with on-the-fly disk compression. The block device created with zRAM can then be used for swap (named as zswap) or as general-purpose RAM disk.
The zram module creates RAM-based block devices named /dev/zram<id> (<id> = 0, 1, …). Pages written to these disks are compressed and stored in memory itself. These disks allow very fast I/O and compression provides good amounts of memory savings. Some of the use cases include /tmp storage, use as swap disks, various caches under /var and maybe many more.
Statistics for individual zram devices are exported through sysfs nodes at /sys/block/zram<id>/
Environment Specification:
We are using a minimal Rocky Linux 9 virtual machine with following specifications.
- CPU - 3.4 Ghz (2 cores)
- Memory - 2 GB
- Storage - 20 GB
- Operating System - Rocky Linux release 9.1 (Blue Onyx)
- Hostname – rocky9.centlinux.com
- IP Address - 192.168.116.131/24
Enable zRAM Linux Kernel Module:
By using a ssh client, login to your Linux server as root user.
Create a file with following content, to enable zRAM module during loading of your Linux Kernel.
# cat > /etc/modules-load.d/zram.conf << EOF > zram > EOF
Create configuration file for zRAM Kernel Module.
In this configuration file, you can set the number of zRAM block devices (num_devices=x) for your Linux server.
# cat > /etc/modprobe.d/zram.conf << EOF > options zram num_devices=1 > EOF
Create a udev rule and specify the size of your zRAM device therein.
# cat > /etc/udev/rules.d/99-zram.rules << EOF > KERNEL=="zram0", ATTR{disksize}="2G",TAG+="systemd" > EOF
Open /etc/fstab file by using vim text editor.
# vi /etc/fstab
Find and comment the following line to disable automount of default swap partition. If you have more than one swap partitions then disable all of them.
#/dev/mapper/rl-swap none swap defaults 0 0
Create a systemd unit for zRAM to enable zswap partition on your Rocky Linux server.
By using vim text editor create a systemd unit.
# vi /etc/systemd/system/zram.service
Add following directives in this 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
Enable zram.service by executing following command.
# systemctl enable zram.service
Created symlink /etc/systemd/system/multi-user.target.wants/zram.service → /etc/systemd/system/zram.service.
Reboot your Linux server to let it start with the new zswap partition.
# reboot
After reboot, check the status of your zram block devices by executing zramctl command.
# zramctl
NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 lzo-rle 2G 4K 74B 12K 1 [SWAP]
Check the status of zswap memory by using free command at Linux bash prompt.
# free -m
total used free shared buff/cache available
Mem: 1743 392 1323 5 174 1351
Swap: 2047 0 2047
Currently, the zswap memory is not in used, because we are using a test machine with almost zero load on it. The output may vary on a producion grade server.
You need to write a C language program, to create some imaginery load on your Rocky Linux server.
You can use vim text editor to write your C language program.
# vi ~/memeater.c
Add following lines of code in this file.
#include <stdlib.h> #include <stdio.h> #include <string.h> #include <unistd.h> int main(int argc, char** argv) { int max = -1; int mb = 0; int multiplier = 100; // allocate 1 MB every time unit. Increase this to e.g.100 to allocate 100 MB every time unit. char* buffer; if(argc > 1) max = atoi(argv[1]); while((buffer=malloc(multiplier * 1024*1024)) != NULL && mb != max) { memset(buffer, 1, multiplier * 1024*1024); mb++; printf("Allocated %d MB\n", multiplier * mb); sleep(1); // time unit: 1 second } return 0; }
You may need to install C language compiler to compile your program.
Use dnf command to install gcc package.
# dnf install -y gcc
Now, use gcc command to compile your C language program.
# gcc ~/memeater.c -o ~/memeater
Adjust the swappiness of your Linux server to 100% for immediate result.
# sysctl vm.swappiness=100
Now execute memeater command.
# ~/memeater
Open another ssh session and check the usage of zswap partition.
# free -m
total used free shared buff/cache available
Mem: 1743 1713 30 13 473 30
Swap: 2047 834 1213
Our zswap partition is in use now.
Reboot your Linux server now.
# reboot
Conclusion:
In this Linux tutorial, you have learned how to enable zRAM on Rocky Linux 9 or other Red Hat based Linux distros.
No comments:
Post a Comment