Set custom path for swapfc_path but permission denied

Trying to set a custom path for the swapfc_path to a different drive but when restarting systemd-swap shows a permission denied error to a folder I named swap.

running $systemctl status systemd-swap shows

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 Fri 2022-04-15 15:22:51 PST; 6s ago
    Process: 7456 ExecStart=/usr/bin/systemd-swap start (code=exited, status=1/FAILURE)
   Main PID: 7456 (code=exited, status=1/FAILURE)
     Status: "Monitoring memory status..."
        CPU: 395ms

Apr 15 15:22:51 PZ0-ASUSTufGaming-FX505DT-HN657T systemd-swap[7456]:   File "/usr/bin/systemd-swap", line 179, in __init__
Apr 15 15:22:51 PZ0-ASUSTufGaming-FX505DT-HN657T systemd-swap[7456]:     makedirs(self.swapfc_path)
Apr 15 15:22:51 PZ0-ASUSTufGaming-FX505DT-HN657T systemd-swap[7456]:   File "/usr/bin/systemd-swap", line 539, in makedirs
Apr 15 15:22:51 PZ0-ASUSTufGaming-FX505DT-HN657T systemd-swap[7456]:     os.makedirs(path, exist_ok=True)
Apr 15 15:22:51 PZ0-ASUSTufGaming-FX505DT-HN657T systemd-swap[7456]:   File "/usr/lib/python3.10/os.py", line 225, in makedirs
Apr 15 15:22:51 PZ0-ASUSTufGaming-FX505DT-HN657T systemd-swap[7456]:     mkdir(name, mode)
Apr 15 15:22:51 PZ0-ASUSTufGaming-FX505DT-HN657T systemd-swap[7456]: PermissionError: [Errno 13] Permission denied: '/home/user/swap'
Apr 15 15:22:51 PZ0-ASUSTufGaming-FX505DT-HN657T systemd[1]: systemd-swap.service: Main process exited, code=exited, status=1/FAILURE
Apr 15 15:22:51 PZ0-ASUSTufGaming-FX505DT-HN657T systemd[1]: systemd-swap.service: Failed with result 'exit-code'.
Apr 15 15:22:51 PZ0-ASUSTufGaming-FX505DT-HN657T systemd[1]: Failed to start Manage swap spaces on zram, files and partitions..

So far, I’ve tried chown the swap folder to root or 777 but I still get the error.

Here’s my swap.conf for reference in case it’s needed:

zswap_enabled=1
zswap_compressor=zstd
zswap_max_pool_percent=99
zswap_zpool=z3fold
zram_enabled=0
#zram_size=$(( RAM_SIZE / 4 ))
#zram_count=${NCPU}
#zram_streams=${NCPU}
#zram_alg=zstd
#zram_prio=32767
swapfc_enabled=1
#swapfc_force_use_loop=0
swapfc_frequency=1
swapfc_chunk_size=1024M
swapfc_max_count=32
swapfc_min_count=1
swapfc_free_ram_perc=35
swapfc_free_swap_perc=100
swapfc_remove_free_swap_perc=100
#swapfc_priority=50
#swapfc_path=/var/lib/systemd-swap/swapfc/
swapfc_path=/home/user/swap/
#swapfc_nocow=1
#swapfc_directio=1
swapfc_force_preallocated=1
swapd_auto_swapon=1
#swapd_prio=1024

Also, is there a limit to the swapfc_chunk_size? I was thinking of increasing it to 4096M since the game XCOM2 WotC modded triggers oom and causes the game to close down at certain parts due to memory leaks or unoptimized code.

Hi @chickengod,

Is there a drive mounted there? Please provide the output of:

sudo stat /home/user/swap/

Hello @Mirdarthos.

There is. It’s a separate drive for my /home directory which is a 2TB drive.

Here’s the output of sudo stat /home/user/swap/

File: /home/user/swap/
  Size: 4096      	Blocks: 8          IO Block: 4096   directory
Device: 8,1	Inode: 94508642    Links: 2
Access: (0777/drwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2022-04-15 15:09:01.267392758 +0800
Modify: 2022-04-15 15:22:20.476210127 +0800
Change: 2022-04-15 15:22:20.476210127 +0800
 Birth: 2022-04-15 15:09:01.267392758 +0800

Hi @chickengod,

Just thought of this, I don’t know if I’m right or not. But IIRC it’s not just simply changing a path. There is a whole procedure to do it, and I don’t know it. Best would be to look here:

and

https://wiki.archlinux.org/title/Swap#Manually

Hope this helps!

When using this do I still need systemd-swap?

Trying this out right now to see if it helps.

Honestly?

:man_shrugging:

Will try out the manually created swapfile and see if it works. Thanks for the help.

Just a simple thing. Systemd-Swap tries to create a folder and the service is restricted to the use which runs it.

$ systemctl cat systemd-swap  
# /usr/lib/systemd/system/systemd-swap.service
[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 <--- HERE
SystemCallArchitectures=native
SystemCallErrorNumber=EPERM
SystemCallFilter=@system-service @swap @module

[Install]
WantedBy=multi-user.target

So the folder must by owned by root.

So create a folder, let it be owned by root and systemd will create a folder within it.

mkdir -pv $HOME/swap/
sudo chown -Rv root:root $HOME/swap/

Then at /etc/systemd/swap.conf add this:

swapfc_path=/home/<replace_username>/swap/swapfc/

Now systemd-swap is able to create the folder swapfc at the folder $HOME/swap/

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.