This is a simple systemd service that enables and disables zram swap. It, of course, assumes that the zram kernel module is available; the service will fail if it encounters an error at any point while creating the swap or enabling the swap space.
To use this service, place the service file into /etc/systemd/system/
or /usr/lib/systemd/system/
, depending on the specific Linux distribution (openSUSE uses the latter), and the two shell scripts into /usr/local/sbin/
(see below if you prefer a different location), then run systemctl daemon-reload
. From there, you can run systemctl {enable|disable|start|stop|restart} zramswap
as appropriate.
Important security note: The script files should have permission values 744 or 544, while the service file should have permission value 644 or 444; both should be owned by root. If they are writable by any user other than root, an unprivileged user will be able to alter them and cause the system to execute arbitrary code.
- Size of zram swap: By default, the size of the zram swap space is 1 GiB. This can be changed by editing the
echo 1G > /sys/block/zram0/disksize
line in thezramswap-on.sh
file, replacing1G
with the desired size. Valid suffixes include K, M, G, T, P, and E. Extremely large swap spaces likely will not work correctly; use a size that is appropriate to the amount of physical memory on your system. We suggest using no more than 50% of the amount of memory on your computer, but you may want to experiment with different sizes depending on your workload and memory available. - Location of script files: If you'd like to put the script files in a different directory, edit the
ExecStart
andExecStop
lines inzramswap.service
to point to the actual location of the script files.