Messing around with Zram

I’m playing around with zram. But somehow I’m not sure how to check if it really works as it should.

As written in the Arch Wiki, I first disabled zswap with zswap.enabled=0 in /etc/default/grub → GRUB_CMDLINE_LINUX_DEFAULT

Then I installed zram-generator and created a zram-generator.conf in /etc/systemd.
Which looks like this:

[zram0]
host-memory-limit = none
zram-size = 12000
compression-algorithm = zstd
fs-type = btrfs
mount-point = /var/compressed
options = X-mount.mode=1777

After that I typed sudo systemctl daemon-reload and sudo systemctl start /dev/zram0.

Depending on the instructions on the internet I now find different commands how to check if zram is running. Unfortunately the answers I get from the system are not clear for me.

lsblk shows me the zram0 device I also see /var/compressed.
swapon command gives me no output and free -h shows me the output enclosed

Also, I can’t write to /var/compressed which, if I understood correctly, should be possible by using options = X-mount.mode=1777.

I am pretty sure that this is not correct. Where did you get this from?

Try this:

systemctl start systemd-zram-setup@zram0.service

and then check the status:

systemctl status systemd-zram-setup@zram0.service

EDIT:

I find your zram-generator.conf also pretty odd. If you want to use zram0 as swap you would not want to create it with btrfs filesystem. swap has its own filesystem.

For testing pruposes I suggest you start with a minimalistic zram-generator.conf like:

[zram0]
compression-algorithm=lz4
zram-size=12000

I would start off with the example in ArchWiki:

zram-generator provides a systemd-zram-setup@.service unit to automatically initialize zram devices without users needing to enable/start the template or its instances. See zram-generator(8) and zram-generator.conf(5).

For example, to create a zram swap device using zstd and half of the entire available ram, install zram-generator, then create /etc/systemd/zram-generator.conf with the following:

/etc/systemd/zram-generator.conf

[zram0]
zram-size = ram / 2
compression-algorithm = zstd
swap-priority = 100
fs-type = swap

Reboot, then check the swap status of your configured /dev/zramN devices by reading the unit status of the systemd-zram-setup@zramN.service instance(s), or by using zramctl(8). 

Note:

zram-generator provides a systemd-zram-setup@.service unit to automatically initialize zram devices without users needing to enable/start the template or its instances

Just reboot and check: sudo swapon -s

Try this:
systemctl start systemd-zram-setup@zram0.service
and then check the status:
systemctl status systemd-zram-setup@zram0.service

With this and the minimal config it has already worked that zram0 is shown up.

That with …sudo systemctl start /dev/zram0… I had from this forum post.

have then now tried line by line to test the config from my first post. But as soon as I enter one following lines* zram0 is gone again. So here seems to be a problem.
I had the lines from the example config “zram-generator.conf.example” and from https://github.com/systemd/zram-generator/blob/main/README.md

*fs-type = btrfs
mount-point = /var/compressed
options = X-mount.mode=1777

You added lines dedicated to mountpoints. But this is not what you want for swap. They do not apply for swap.

I “played around” with the following in zram-generator.conf:

[zram1]
# This section describes the settings for /dev/zram1.
#
# host-memory-limit is not specified, so this device will always be created.

# Size the device to a tenth of RAM.
zram-size = ram / 2

# The file system to put on the device. If not specified, ext2 will be used.
fs-type = ext2

# Where to mount the file system. If a mount point is not specified,
# the device will be initialized, but will not be used for anything.
mount-point = /run/compressed-mount-point

and it seems to be working:

$ zramctl --output-all 
NAME       DISKSIZE  DATA COMPR ALGORITHM STREAMS ZERO-PAGES TOTAL MEM-LIMIT MEM-USED MIGRATED MOUNTPOINT
/dev/zram1     3.7G  1.3M 15.8K zstd            8          0  124K        0B     124K       0B /run/compressed-mount-point
/dev/zram0       4G    4K   64B zstd            8          0   20K        0B      20K       0B [SWAP]
$ systemctl status systemd-zram-setup@zram1.service 
● systemd-zram-setup@zram1.service - Create swap on /dev/zram1
     Loaded: loaded (/usr/lib/systemd/system/systemd-zram-setup@.service; static)
    Drop-In: /run/systemd/generator/systemd-zram-setup@zram1.service.d
             └─bindings.conf
     Active: active (exited) since Mon 2023-05-22 17:33:43 CEST; 19min ago
       Docs: man:zram-generator(8)
             man:zram-generator.conf(5)
    Process: 344 ExecStart=/usr/lib/systemd/system-generators/zram-generator --setup-device zram1 (code=exited, status=0/SUCCESS)
   Main PID: 344 (code=exited, status=0/SUCCESS)
        CPU: 41ms

May 22 17:33:43 linux systemd[1]: Starting Create swap on /dev/zram1...
May 22 17:33:43 linux systemd-makefs[347]: /dev/zram1 successfully formatted as ext2 (label "zram1", uuid 184ae5f9-235c-4143-be05-9f3064b342fb)
May 22 17:33:43 linux systemd[1]: Finished Create swap on /dev/zram1.
$ lsblk -f
NAME        FSTYPE FSVER LABEL     UUID                                 FSAVAIL FSUSE% MOUNTPOINTS
zram0                                                                                  [SWAP]
zram1                                                                      3.6G     0% /run/compressed-mount-point

I’m not that clear about the nature of this second zram device. Perhaps is it to be regarded more like a swap partition? :thinking: