System: CentOS 7
cat /etc/udev/rules.d/10-zram.rules
ACTION=="add", SUBSYSTEM=="block", KERNEL=="zram0", DRIVER=="", ATTR{disksize}=="0", ATTR{disksize}="512M", RUN+="/usr/bin/systemd-run /sbin/mkswap $env{DEVNAME}"
(/sbin/mkswap
didn't work for me so I had to add /usr/bin/systemd-run /sbin/mkswap
)
# cat /etc/modules-load.d/zram.conf
zram
# cat /etc/modprobe.d/zram.conf
options zram num_devices=1
Now, the problem is that system tries to mount it (swapon
) before SWAP is set (mkswap
):
# journalctl -o short | grep zram
Aug 04 21:28:54 system kernel: zram: module is from the staging directory, the quality is unknown, you have been warned.
Aug 04 21:28:54 system kernel: zram: Created 1 device(s) ...
Aug 04 21:28:54 system systemd-modules-load[459]: Inserted module 'zram'
Aug 04 21:28:55 system kernel: zram: Initialization done!
Aug 04 21:28:55 system systemd[1]: Found device /dev/zram0.
Aug 04 21:28:55 system systemd[1]: Activating swap /dev/zram0...
Aug 04 21:28:55 system swapon[494]: swapon: /dev/zram0: read swap header failed: Invalid argument
Aug 04 21:28:55 system systemd[1]: dev-zram0.swap swap process exited, code=exited status=255
Aug 04 21:28:55 system systemd[1]: Failed to activate swap /dev/zram0.
Aug 04 21:28:55 system systemd[1]: Unit dev-zram0.swap entered failed state.
Aug 04 21:28:55 system systemd[1]: Started /sbin/mkswap /dev/zram0.
Aug 04 21:28:55 system systemd[1]: Starting /sbin/mkswap /dev/zram0...
Aug 04 21:29:15 system dracut[3292]: -rw-r--r-- 1 root root 27 Aug 4 21:28 etc/modprobe.d/zram.conf
Aug 04 21:29:15 system dracut[3292]: -rw-r--r-- 1 root root 5 Aug 4 15:09 etc/modules-load.d/zram.conf
Aug 04 21:29:16 system dracut[3292]: drwxr-xr-x 2 root root 0 Aug 4 21:29 usr/lib/modules/3.10.0-327.10.1.el7.x86_64/kernel/drivers/staging/zram
Aug 04 21:29:16 system dracut[3292]: -rw-r--r-- 1 root root 28701 Feb 16 17:45 usr/lib/modules/3.10.0-327.10.1.el7.x86_64/kernel/drivers/staging/zram/zram.ko
Is there a way to change the order or perhaps a better way to do this?
Best Answer
Arch has this covered quite good
Example: To set up one lz4 compressed zram device with 32GiB capacity and a higher-than-normal priority (only for the current session):
Swap on zRAM using a udev rule
The example below describes how to set up swap on zRAM automatically at boot with a single udev rule. No extra package should be needed to make this work.
First, enable the module:
Configure the number of /dev/zram nodes you need.
Create the udev rule as shown in the example.
Add /dev/zram to your fstab.