Can't create swap file: swapon failed: Invalid argument

Hi All,

Brought a new laptop and decided to jump to Arch and EndevourOS over from Regolith/Ubuntu.

During the install decided on i3 and BTRFS. I also enabled the swap file on the installer. However on lid close and then open the screen would just display a frozen desktop screen from the moment I closed it and required a hard restart. Having no idea if it was trying to suspend or hibernate I decided to check out the swap file first:


Filename				Type		Size		Used		Priority
/swap/swapfile                          file		524284		0		-2

Right that’s too small for a start as it’s got 16GB RAM, I decide to try increasing the swap file to 4GB:

~> sudo dd if=/dev/zero of=/swap/swapfile bs=1M count=4000 status=progress
3995074560 bytes (4.0 GB, 3.7 GiB) copied, 1 s, 4.0 GB/s
4000+0 records in
4000+0 records out
4194304000 bytes (4.2 GB, 3.9 GiB) copied, 1.05945 s, 4.0 GB/s
~> sudo chmod 600 /swap/swapfile
~> sudo mkswap /swap/swapfile 
Setting up swapspace version 1, size = 3.9 GiB (4194299904 bytes)
no label, UUID=blah
~> sudo swapon /swap/swapfile
swapon: /swap/swapfile: swapon failed: Invalid argument

So as you can see I’ve already hit a brick wall, can’t even get past the first part of setting up the swap file for a suspend then hibernate setup. Any ideas?

On btrfs, swap files need to be a on a subvolume with no snapshots and CoW should be disabled.

Did you create a separate subvolume to hold the swapfile?

Full instructions are here:
https://wiki.archlinux.org/title/btrfs#Swap_file

Also, welcome to the forum!

1 Like

This is a fresh EOS install using the Calamares installer, I just selected swap file and btrfs during it. I haven’t made any modifications.

What does

sudo btrfs subvolume list -p /

show?

ID 256 gen 1896 parent 5 top level 5 path @
ID 257 gen 1897 parent 5 top level 5 path @home
ID 258 gen 1525 parent 5 top level 5 path @cache
ID 259 gen 1895 parent 5 top level 5 path @log
ID 260 gen 1801 parent 5 top level 5 path @swap
ID 261 gen 25 parent 256 top level 256 path var/lib/portables
ID 262 gen 26 parent 256 top level 256 path var/lib/machines
1 Like

Thanks, I followed those instructions (had to sudo touch swapfile first as I deleted the previous one). Still no joy

> cd /swap/
> sudo touch swapfile
> sudo truncate -s 0 ./swapfile
> sudo chattr +C ./swapfile
> sudo btrfs property set ./swapfile compression none
ERROR: failed to set compression for ./swapfile: Invalid argument

You can skip that step.

A recent change to btrfs makes it no longer needed/possible.

2 Likes

Ah cheers that’s done the trick! Finished it with running the regular swap file creation commands. In future if someone else hits this problem here’s the full solution:

> cd /swap
> sudo truncate -s 0 ./swapfile
> sudo chattr +C ./swapfile
> sudo dd if=/dev/zero of=/swap/swapfile bs=1M count=4000 status=progress
4011851776 bytes (4.0 GB, 3.7 GiB) copied, 2 s, 2.0 GB/s
4000+0 records in
4000+0 records out
4194304000 bytes (4.2 GB, 3.9 GiB) copied, 2.35927 s, 1.8 GB/s
> sudo chmod 0600 /swap/swapfile
> sudo mkswap -U clear /swap/swapfile
Setting up swapspace version 1, size = 3.9 GiB (4194299904 bytes)
no label, UUID=00000000-0000-0000-0000-000000000000
> sudo swapon /swap/swapfile
> swapon -s

Filename				Type		Size		Used		Priority
/swap/swapfile                          file		4095996		0		-2

For a 16GB machine, is 4GB going to be a large enough swap file to enable suspend?

2 Likes

Suspension, that is Suspend-to-RAM won’t need any swap device.

If you need hibernation (Suspend-to-Disk) then you would need a swap device as large as you RAM. You may get away with less as the content of RAM is compressed before being written to disk but that all depends how “full” you RAM is before hibernation.

So I think swap-device size=ram size is good.

https://wiki.archlinux.org/title/Power_management/Suspend_and_hibernate

2 Likes

Ah thanks for that, yeah I meant hibernation, sorry it’s getting late and it’s been a long day fixing all the setup issues with my laptop and i3/EOS. I think I’ll leave it till tomorrow now and resize the swap file to 16GB then. Thanks for yours and @dalto help! Glad I finally made the leap to Arch/EOS.

1 Like

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