How to make zram swap persistent with higher priority?

Hi all. I’m trying to make zram swap persistent and everything is ok except that swap has low priority after booting.

Steps I did:

  1. Added zram to /etc/modules-load.d
  2. Created environment file for zram-setup service in /usr/local/lib/zram.conf.d/zram0-env with content:
ZRAM_DEV_SIZE=4294967296
SWAP=y

But zram-setup service mounts swap with -3 priority that lower than my swap partition on HDD.
As I can see in /usr/lib/systemd/system/zram-setup@.service, service does not has option to set priority. So is there’s way to set it with condition that swupd repair will not replace service’s file (if I add -p option to swapon command)?

Ok. Because of stateless, I had to edit service via systemctl edit zram-setup@zram0.service to be sure that swupd repair won’t “repair” modified service file.

I also made some modifications to service so I can specify compression algorithm and priority in /usr/local/lib/zram.conf.d
In override.conf (opened by executing systemctl edit zram-setup@zram0.service) I wrote:

[Service]
ExecStart=
ExecStart=-/bin/sh -c 'echo $ZRAM_COMP_ALG > /sys/class/block/%i/comp_algorithm'
ExecStart=-/bin/sh -c 'echo $ZRAM_NUM_STR > /sys/class/block/%i/max_comp_streams'
ExecStart=-/bin/sh -c 'echo $ZRAM_DEV_SIZE > /sys/class/block/%i/disksize'
ExecStart=-/bin/sh -c '[ "$SWAP" = "y" ] && mkswap /dev/%i && swapon -p $SWAP_PRI /dev/%i'
ExecStop=
ExecStop=-/bin/sh -c '[ "$SWAP" = "y" ] && swapoff /dev/%i'
ExecStop=-/bin/sh -c 'echo 1 > /sys/class/block/%i/reset'

/usr/local/lib/zram.conf.d/zram0-env file:

ZRAM_COMP_ALG=zstd
ZRAM_NUM_STR=8
ZRAM_DEV_SIZE=4294967296
SWAP=y
SWAP_PRI=10
1 Like

You should create modified unit files with systemctl edit. This puts the changes in the correct location automatically. It’ll go under /etc/systemd/system.

In this case systemd executes both files for some reason
https://pastebin.com/C4weNjmY

P.S. Edited with --full option, but
ExecStart=/bin/sh -c [ ! -z ${ZRAM_COMP_ALG+x} ] && echo $ZRAM_COMP_ALG > /sys/class/block/zram0/comp_algorithm
fails for some reason considering that command works fine if executed from terminal

So I changed line to set algorithm just to
echo $ZRAM_COMP_ALG > /sys/class/block/zram0/comp_algorithm