Unable to enable/start systemd-swap for Zram: bunch of errrors

Hi, since I would try Zram instead of classic swap partition, I followed these guides:
Move from swap partition to zRAM - Tutorials - Manjaro Linux Forum and
systemd-swap/README.md at master · Nefelim4ag/systemd-swap · GitHub

I made /etc/systemd/swap.conf.d/zram.conf:
zswap_enabled=0
zram_enabled=1
swapfc_enabled=0
zswap_compressor=lz4

Then:

  • swapoff /dev/zram0
  • zramctl --reset /dev/zram0
  • zramctl --find --size 2G
  • mkswap /dev/zram0
  • swapon /dev/zram0

Then:
swapon --show :
NAME TYPE SIZE USED PRIO
/dev/zram0 partition 2G 0B -2

But as I attempt to start systemd-swap.service, I get:

● systemd-swap.service - Manage swap spaces on zram, files and partitions.
     Loaded: loaded (/usr/lib/systemd/system/systemd-swap.service; enabled; vendor preset: disabled)
     Active: failed (Result: exit-code) since Thu 2020-10-29 15:13:31 CET; 4s ago
    Process: 1024265 ExecStart=/usr/bin/systemd-swap start (code=exited, status=1/FAILURE)
   Main PID: 1024265 (code=exited, status=1/FAILURE)
     Status: "Setting up Zram..."

ott 29 15:13:31 Probook systemd-swap[1024265]:   File "/usr/bin/systemd-swap", line 904, in <module>
ott 29 15:13:31 Probook systemd-swap[1024265]:     main()
ott 29 15:13:31 Probook systemd-swap[1024265]:   File "/usr/bin/systemd-swap", line 892, in main
ott 29 15:13:31 Probook systemd-swap[1024265]:     start()
ott 29 15:13:31 Probook systemd-swap[1024265]:   File "/usr/bin/systemd-swap", line 660, in start
ott 29 15:13:31 Probook systemd-swap[1024265]:     mode = os.stat(output).st_mode
ott 29 15:13:31 Probook systemd-swap[1024265]: FileNotFoundError: [Errno 2] No such file or directory: 'zramctl: /dev/zram1: failed to set algorit>
ott 29 15:13:31 Probook systemd[1]: systemd-swap.service: Main process exited, code=exited, status=1/FAILURE
ott 29 15:13:31 Probook systemd[1]: systemd-swap.service: Failed with result 'exit-code'.
ott 29 15:13:31 Probook systemd[1]: Failed to start Manage swap spaces on zram, files and partitions..

I have /dev/zram0 (which is active) and /dev/zram1…

If I run zramctl -r /dev/zram1, /dev/zram1 goes away, but the systemd-swap, still claims about /dev/zram1.

What is happen here?

EDIT:

I rebooted and now:

● systemd-swap.service - Manage swap spaces on zram, files and partitions.
     Loaded: loaded (/usr/lib/systemd/system/systemd-swap.service; enabled; vendor preset: disabled)
     Active: failed (Result: exit-code) since Thu 2020-10-29 15:45:49 CET; 2s ago
    Process: 5137 ExecStart=/usr/bin/systemd-swap start (code=exited, status=1/FAILURE)
   Main PID: 5137 (code=exited, status=1/FAILURE)
     Status: "Setting up Zram..."

ott 29 15:45:49 Probook systemd-swap[5137]:   File "/usr/bin/systemd-swap", line 904, in <module>
ott 29 15:45:49 Probook systemd-swap[5137]:     main()
ott 29 15:45:49 Probook systemd-swap[5137]:   File "/usr/bin/systemd-swap", line 892, in main
ott 29 15:45:49 Probook systemd-swap[5137]:     start()
ott 29 15:45:49 Probook systemd-swap[5137]:   File "/usr/bin/systemd-swap", line 660, in start
ott 29 15:45:49 Probook systemd-swap[5137]:     mode = os.stat(output).st_mode
ott 29 15:45:49 Probook systemd-swap[5137]: FileNotFoundError: [Errno 2] No such file or directory: 'zramctl: /dev/zram0: failed to set algorithm: Invalid argument'
ott 29 15:45:49 Probook systemd[1]: systemd-swap.service: Main process exited, code=exited, status=1/FAILURE
ott 29 15:45:49 Probook systemd[1]: systemd-swap.service: Failed with result 'exit-code'.
ott 29 15:45:49 Probook systemd[1]: Failed to start Manage swap spaces on zram, files and partitions..

But: I set lz4 and is also present in the output of lsmod:
lz4 and lz4_compress.

The only way to have Zram available, is to use these commands instead of rely on systemd-swap.

zramctl --reset /dev/zram0
zramctl --find --size 2G
mkswap /dev/zram0
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=06e6616f-c6d5-4125-a429-1b45ea2f5365
swapon /dev/zram0

And in facts I got:
swapon --show
NAME TYPE SIZE USED PRIO
/dev/zram0 partition 2G 0B -2

Maybe provide the output of systemctl cat systemd-swap

[Unit]
Description=Manage swap spaces on zram, files and partitions.

[Service]
Type=notify
NotifyAccess=main
ExecStart=/usr/bin/systemd-swap start
ExecStop=/usr/bin/systemd-swap stop
RemainAfterExit=true
TimeoutStopSec=600
OOMScoreAdjust=-1000
CPUSchedulingPolicy=fifo
CPUSchedulingPriority=20
CapabilityBoundingSet=CAP_SYS_ADMIN CAP_SYS_MODULE
DeviceAllow=block-blkext r
DeviceAllow=block-device-mapper r
DeviceAllow=block-loop rw
DeviceAllow=block-sd r
DeviceAllow=block-virtblk rw
DeviceAllow=block-zram rw
NoNewPrivileges=true
PrivateNetwork=true
ProtectClock=true
ProtectControlGroups=true
ProtectHome=read-only
ProtectHostname=true
ProtectKernelLogs=true
ProtectSystem=full
RestrictNamespaces=true
RestrictSUIDSGID=true
SystemCallArchitectures=native
SystemCallErrorNumber=EPERM
SystemCallFilter=@system-service @swap @module

[Install]
WantedBy=multi-user.target

Ill check later, need to install systemd-swap myself also :smiley:

I didn’t use those manual commands you posted but just created the config file:

> xclip -in /etc/systemd/swap.conf.d/zram.conf
zswap_enabled=0
zram_enabled=1
swapfc_enabled=0
zswap_compressor=lz4

Then started it manually without enabling it.

> systemctl start systemd-swap
> systemctl status systemd-swap | xclip 
● systemd-swap.service - Manage swap spaces on zram, files and partitions.
     Loaded: loaded (/usr/lib/systemd/system/systemd-swap.service; disabled; vendor preset: disabled)
     Active: active (exited) since Thu 2020-10-29 18:28:49 +03; 2min 51s ago
    Process: 165268 ExecStart=/usr/bin/systemd-swap start (code=exited, status=0/SUCCESS)
   Main PID: 165268 (code=exited, status=0/SUCCESS)
     Status: "Swap unit activation finished"

Eki 29 18:28:48 manjaro systemd-swap[165268]: INFO: Zram: initialized: /dev/zram9
Eki 29 18:28:48 manjaro systemd-swap[165268]: INFO: Zram: trying to initialize free device
Eki 29 18:28:48 manjaro systemd-swap[165268]: INFO: Zram: initialized: /dev/zram10
Eki 29 18:28:49 manjaro systemd-swap[165268]: INFO: Zram: trying to initialize free device
Eki 29 18:28:49 manjaro systemd-swap[165268]: INFO: Zram: initialized: /dev/zram11
Eki 29 18:28:49 manjaro systemd-swap[165268]: INFO: Writing destroy info...
Eki 29 18:28:49 manjaro systemd-swap[165268]: INFO: swapD: pick up devices from systemd-gpt-auto-generator
Eki 29 18:28:49 manjaro systemd-swap[165268]: INFO: swapD: searching swap devices
Eki 29 18:28:49 manjaro systemd-swap[165268]: INFO: swapD: enabled device: /dev/mapper/luksSWAP
Eki 29 18:28:49 manjaro systemd[1]: Started Manage swap spaces on zram, files and partitions..

Seems all fine so far…
Then i stopped it.

> systemctl stop systemd-swap
> systemctl status systemd-swap | xclip 
● systemd-swap.service - Manage swap spaces on zram, files and partitions.
     Loaded: loaded (/usr/lib/systemd/system/systemd-swap.service; disabled; vendor preset: disabled)
     Active: failed (Result: exit-code) since Thu 2020-10-29 18:32:30 +03; 15s ago
    Process: 165268 ExecStart=/usr/bin/systemd-swap start (code=exited, status=0/SUCCESS)
    Process: 166233 ExecStop=/usr/bin/systemd-swap stop (code=exited, status=1/FAILURE)
   Main PID: 165268 (code=exited, status=0/SUCCESS)
     Status: "Swap unit activation finished"

Eki 29 18:32:30 manjaro systemd-swap[166233]:   File "/usr/bin/systemd-swap", line 763, in stop
Eki 29 18:32:30 manjaro systemd-swap[166233]:     for unit_path in filter(lambda u: "swapfc" in read(u), swap_units):
Eki 29 18:32:30 manjaro systemd-swap[166233]:   File "/usr/bin/systemd-swap", line 763, in <lambda>
Eki 29 18:32:30 manjaro systemd-swap[166233]:     for unit_path in filter(lambda u: "swapfc" in read(u), swap_units):
Eki 29 18:32:30 manjaro systemd-swap[166233]:   File "/usr/bin/systemd-swap", line 454, in read
Eki 29 18:32:30 manjaro systemd-swap[166233]:     with open(file) as f:
Eki 29 18:32:30 manjaro systemd-swap[166233]: FileNotFoundError: [Errno 2] No such file or directory: '/run/systemd/system/dev-dm\\x2d0.swap'
Eki 29 18:32:30 manjaro systemd[1]: systemd-swap.service: Control process exited, code=exited, status=1/FAILURE
Eki 29 18:32:30 manjaro systemd[1]: systemd-swap.service: Failed with result 'exit-code'.
Eki 29 18:32:30 manjaro systemd[1]: Stopped Manage swap spaces on zram, files and partitions..

PS:

> swapon -s | xclip
Filename				Type		Size	Used	Priority
/dev/dm-0                              	partition	67092476	0	-2
/dev/zram0                             	partition	683208	0	32767
/dev/zram1                             	partition	683208	0	32767
/dev/zram2                             	partition	683208	0	32767
/dev/zram3                             	partition	683208	0	32767
/dev/zram4                             	partition	683208	0	32767
/dev/zram5                             	partition	683208	0	32767
/dev/zram6                             	partition	683208	0	32767
/dev/zram7                             	partition	683208	0	32767
/dev/zram8                             	partition	683208	0	32767
/dev/zram9                             	partition	683208	0	32767
/dev/zram10                            	partition	683208	0	32767
/dev/zram11                            	partition	683208	0	32767

Where /dev/dm-0 is a 64GiB swap on a Luks2 partition from my normal system without this util…

Ok, I managed to make it works.
However: the purpose, for me, of zram, is to get rid of the swap partition.
What I also did:
edited /etc/fstab anc commeted the swap partition (#)
swapoff /dev/sda2.

But when systemd-swap.service starts, it still enable the swap partition:

● systemd-swap.service - Manage swap spaces on zram, files and partitions.
     Loaded: loaded (/usr/lib/systemd/system/systemd-swap.service; enabled; vendor preset: disabled)
     Active: active (exited) since Thu 2020-10-29 19:10:24 CET; 52min left
    Process: 379 ExecStart=/usr/bin/systemd-swap start (code=exited, status=0/SUCCESS)
   Main PID: 379 (code=exited, status=0/SUCCESS)
     Status: "Swap unit activation finished"

ott 29 19:10:23 Probook systemd-swap[379]: INFO: Zram: initialized: /dev/zram1
ott 29 19:10:23 Probook systemd-swap[379]: INFO: Zram: trying to initialize free device
ott 29 19:10:23 Probook systemd-swap[379]: INFO: Zram: initialized: /dev/zram2
ott 29 19:10:24 Probook systemd-swap[379]: INFO: Zram: trying to initialize free device
ott 29 19:10:24 Probook systemd-swap[379]: INFO: Zram: initialized: /dev/zram3
ott 29 19:10:24 Probook systemd-swap[379]: INFO: Writing destroy info...
ott 29 19:10:24 Probook systemd-swap[379]: INFO: swapD: pick up devices from systemd-gpt-auto-generator
ott 29 19:10:24 Probook systemd-swap[379]: INFO: swapD: searching swap devices
ott 29 19:10:24 Probook systemd-swap[379]: INFO: swapD: enabled device: /dev/sda2
ott 29 19:10:24 Probook systemd[1]: Started Manage swap spaces on zram, files and partitions..

cat /proc/swaps
Filename				Type		Size	Used	Priority
/dev/zram0                              partition	1019248	0	32767
/dev/zram1                              partition	1019248	0	32767
/dev/zram2                              partition	1019248	0	32767
/dev/zram3                              partition	1019248	0	32767
/dev/sda2                               partition	8895484	0	32767

If i mask dev-sda2.swap, then systemd-swap, will claim about the fact that sda2 is not found.

Why?

Yep, I solved by editing the entry swapd_auto_swapon=0
Tha last “issue” is that, now, my boot time is increased: systemd-swap take about 1.200 seconds to start.

EDIT:

Ok: I have been able also to solve this: I disabled systemd-swap.service and I made systemd-swap.timer which, at boot, start the service after 15 seconds.

Anyway: would be great if other users could give me some hints and opinions about all that I’ve done. Thanks.

You might checkout zramswap, i just improved it’s script a bit…

1 Like

First of all: thank you for your detailed post. :+1:

Which would be the advantages which I can gain from zswapram?

Anyway, about the settings of zram.conf, I understand how to choose/set the size of the RAM used, the compression algorithm, and the core numbers used.

My system has Samsung SSD 860 EVO 500GB, CPU Intel Core i5-3210M and 16GB of RAM DDR3L

This is how I set /etc/systemd/swap.conf.d/zram.conf

Summary

zswap_enabled=0
zram_enabled=1
zram_size=$(( RAM_SIZE / 2 ))
zram_count=$(( 4 / 2 ))
zram_streams=$(( 4 / 2 ))
zram_alg=lz4
zram_prio=32767
swapfc_enabled=0
swapd_auto_swapon=0
swapd_prio=0

swapon --show
NAME       TYPE      SIZE USED  PRIO
/dev/zram0 partition 3,9G   0B 32767
/dev/zram1 partition 3,9G   0B 32767

(total 7,8G)

This is my /etc/sysctl.d/99-sysctl_tweaks.conf

Summary

vm.swappiness=1
vm.vfs_cache_pressure=80
vm.dirty_background_ratio=5
vm.dirty_ratio=10
vm.page-cluster=0

Tweaked mq-deadline-scheduler for the SSD:

Summary

ACTION==“add|change”, KERNEL==“sd[a-b]”, ATTR{queue/rotational}==“0”, ATTR{queue/scheduler}=“mq-deadline”
ACTION==“add|change”, KERNEL==“sda”, ATTR{queue/rotational}==“0”, ATTR{queue/iosched/fifo_batch}=“1”
ACTION==“add|change”, KERNEL==“sda”, ATTR{queue/rotational}==“0”, ATTR{queue/iosched/writes_starved}=“8”
ACTION==“add|change”, KERNEL==“sda”, ATTR{queue/rotational}==“0”, ATTR{queue/iosched/front_merges}=“0”
ACTION==“add|change”, KERNEL==“sda”, ATTR{queue/rotational}==“0”, ATTR{queue/iosched/write_expire}=“500”
ACTION==“add|change”, KERNEL==“sda”, ATTR{queue/rotational}==“0”, ATTR{queue/iosched/read_expire}=“50”
ACTION==“add|change”, KERNEL==“sd[a-b]”, ATTR{queue/rotational}==“0”, ATTR{queue/read_ahead_kb}=“256”

I’ve already done some tests.
Usually, when I had the swap partition, especially when I used to do intensive I/O tasks, the output command of free --human gave buff/cache also goes to 17 GB and the system used to start swapping to disk despite the fact that my system has 16 GB of RAM, and become a little bit laggy. And the kernel never reclaimed/freed the used ram/cache. But I never seen excessive stuttering/lags/hiccups.

Then I tried with zram and no swap partition: I’ve done a lot of intensive I/O tasks (copied huge files from and to external USB HDD) when in the meantime I opened five gimp projects (each of one of 400 MB), Firefox with 20 tabs and many other programs which eat a lot of ram.
Well… The swapping didn’t triggered yet and the system was responsive and snappy as never seen before… And the buff/cache, despite the facts that reached 18GB has been freed as I closed everything.

It’s as far as i could tell a lot simpler and easier to setup. (only 1 zram device)
It is also independent of your “normal” swap configuration(s).
And especially my modified version prevents misconfiguration (to some extend) :wink:

1 Like