In my continued efforts to configure my RPi4, I noticed:
/usr/lib/systemd/scripts/zswap-arm-ctrl
I was looking to add a zram for /var/log. The script loads the zram module, creates and mounts a swap on zram and will “undo” this, allowing for interactive control of the swapfile. All well and good. But it hard codes the zram device to be used and will unload the zram module, when it really does not need to. So, I modified the script.
Will this script be over-written on update? If so, I’ll set it immutable but I don’t like doing that unless it is required.
Below is my modified script if anyone else is interested. Also note I modified the ZRAM_SIZE_PERCENT to be a smaller amount and changed the compression algorithm to be zstd. Modify those as desired.
#!/bin/bash
ZRAM_LABEL="ZSWAP"
start() {
ZRAM_SIZE_PERCENT=20
(( MEM_TOTAL=`cat /proc/meminfo|grep MemTotal|awk '{print $2}'` * 1024 ))
(( ZRAM_SIZE=MEM_TOTAL * $ZRAM_SIZE_PERCENT / 100 ))
ZRAM=`zramctl -f -a "zstd" -s $ZRAM_SIZE`
if [ -n "$ZRAM" ]; then
mkswap $ZRAM -L $ZRAM_LABEL
swapon -p 100 -L $ZRAM_LABEL
fi
}
stop() {
ZRAM=`blkid --label $ZRAM_LABEL`
if [ -n "$ZRAM" ]; then
swapoff -L $ZRAM_LABEL
zramctl -r $ZRAM
fi
}
case $1 in
start|stop) "$1" ;;
esac
You will also want to create /etc/modprobe.d/zram.conf to direct the number of zram devices to create. I use the following:
options zram num_devices=4
Then modify /etc/mkinitcpio.conf to load the zram module on boot.
MODULES=(zram)
BINARIES=("/usr/bin/zramctl")
I then use udev rules to make my zram drive for /var/log by creating /etc/udev/rules.d/05-zram.rules:
KERNEL=="zram0", ATTR{comp_algorithm}="zstd" ATTR{disksize}="512M" RUN="/usr/bin/mkfs.ext2 -L VAR_LOG_MNJRO /dev/zram0", TAG+="systemd"
And I add it to /etc/fstab:
/dev/zram0 /var/log ext2 x-systemd.automount,nodev,nosuid 0 0
And if you want journald to log to /var/log/journal rather than /run/log/journal/, you need to modify /etc/systemd/journald.conf:
#Storage=auto
Storage=persisent
And then finally, rebuild the initramfs:
cd /boot
mkinitcpio -P