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

**URL:** <https://forum.endeavouros.com/t/cant-create-swap-file-swapon-failed-invalid-argument/27485>\
**Category:** Newbie\
**Created:** [May 26, 2022, 10:42pm UTC](https://forum.endeavouros.com/t/cant-create-swap-file-swapon-failed-invalid-argument/27485 "2022-05-26T22:42:12Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![StJohnSmythe](https://forum.endeavouros.com/letter_avatar_proxy/v4/letter/s/d78d45/32.png) [@StJohnSmythe](https://forum.endeavouros.com/u/StJohnSmythe)\
**Post date:** [May 26, 2022, 10:42pm UTC](https://forum.endeavouros.com/t/cant-create-swap-file-swapon-failed-invalid-argument/27485/1 "2022-05-26T22:42:12Z")

</div>

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:

```nohighlight

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:

```nohighlight
~> 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?

---

<div class="post-metadata">

**Author:** ![dalto](https://forum.endeavouros.com/user_avatar/forum.endeavouros.com/dalto/32/5577_2.png) [@dalto](https://forum.endeavouros.com/u/dalto)\
**Post date:** [May 26, 2022, 10:47pm UTC](https://forum.endeavouros.com/t/cant-create-swap-file-swapon-failed-invalid-argument/27485/2 "2022-05-26T22:47:50Z")

</div>

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](https://wiki.archlinux.org/title/btrfs#Swap_file)

Also, welcome to the forum!

---

<div class="post-metadata">

**Author:** ![StJohnSmythe](https://forum.endeavouros.com/letter_avatar_proxy/v4/letter/s/d78d45/32.png) [@StJohnSmythe](https://forum.endeavouros.com/u/StJohnSmythe)\
**Post date:** [May 26, 2022, 10:50pm UTC](https://forum.endeavouros.com/t/cant-create-swap-file-swapon-failed-invalid-argument/27485/3 "2022-05-26T22:50:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pebcak](https://forum.endeavouros.com/user_avatar/forum.endeavouros.com/pebcak/32/100472_2.png) [@pebcak](https://forum.endeavouros.com/u/pebcak)\
**Post date:** [May 26, 2022, 10:52pm UTC](https://forum.endeavouros.com/t/cant-create-swap-file-swapon-failed-invalid-argument/27485/4 "2022-05-26T22:52:02Z")

</div>

What does

`sudo btrfs subvolume list -p /`

show?

---

<div class="post-metadata">

**Author:** ![StJohnSmythe](https://forum.endeavouros.com/letter_avatar_proxy/v4/letter/s/d78d45/32.png) [@StJohnSmythe](https://forum.endeavouros.com/u/StJohnSmythe)\
**Post date:** [May 26, 2022, 10:53pm UTC](https://forum.endeavouros.com/t/cant-create-swap-file-swapon-failed-invalid-argument/27485/5 "2022-05-26T22:53:12Z")

</div>

```auto
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

```

---

<div class="post-metadata">

**Author:** ![StJohnSmythe](https://forum.endeavouros.com/letter_avatar_proxy/v4/letter/s/d78d45/32.png) [@StJohnSmythe](https://forum.endeavouros.com/u/StJohnSmythe)\
**Post date:** [May 26, 2022, 10:58pm UTC](https://forum.endeavouros.com/t/cant-create-swap-file-swapon-failed-invalid-argument/27485/6 "2022-05-26T22:58:39Z")

</div>

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

```auto
> 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

```

---

<div class="post-metadata">

**Author:** ![dalto](https://forum.endeavouros.com/user_avatar/forum.endeavouros.com/dalto/32/5577_2.png) [@dalto](https://forum.endeavouros.com/u/dalto)\
**Post date:** [May 26, 2022, 10:59pm UTC](https://forum.endeavouros.com/t/cant-create-swap-file-swapon-failed-invalid-argument/27485/7 "2022-05-26T22:59:46Z")

</div>

> [@StJohnSmythe](#):
>
> ```auto
> > 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.

---

<div class="post-metadata">

**Author:** ![StJohnSmythe](https://forum.endeavouros.com/letter_avatar_proxy/v4/letter/s/d78d45/32.png) [@StJohnSmythe](https://forum.endeavouros.com/u/StJohnSmythe)\
**Post date:** [May 26, 2022, 11:06pm UTC](https://forum.endeavouros.com/t/cant-create-swap-file-swapon-failed-invalid-argument/27485/8 "2022-05-26T23:06:56Z")

</div>

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:

```auto
> 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?

---

<div class="post-metadata">

**Author:** ![pebcak](https://forum.endeavouros.com/user_avatar/forum.endeavouros.com/pebcak/32/100472_2.png) [@pebcak](https://forum.endeavouros.com/u/pebcak)\
**Post date:** [May 26, 2022, 11:14pm UTC](https://forum.endeavouros.com/t/cant-create-swap-file-swapon-failed-invalid-argument/27485/9 "2022-05-26T23:14:44Z")

</div>

> [@StJohnSmythe](#):
>
> For a 16GB machine, is 4GB going to be a large enough swap file to enable suspend?

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](https://wiki.archlinux.org/title/Power_management/Suspend_and_hibernate)

---

<div class="post-metadata">

**Author:** ![StJohnSmythe](https://forum.endeavouros.com/letter_avatar_proxy/v4/letter/s/d78d45/32.png) [@StJohnSmythe](https://forum.endeavouros.com/u/StJohnSmythe)\
**Post date:** [May 26, 2022, 11:22pm UTC](https://forum.endeavouros.com/t/cant-create-swap-file-swapon-failed-invalid-argument/27485/10 "2022-05-26T23:22:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://forum.endeavouros.com/uploads/default/original/2X/9/9ea52f9fd5ef8e609e593c362d61a36295f48be8.png) [@system](https://forum.endeavouros.com/u/system)\
**Post date:** [May 28, 2022, 11:23pm UTC](https://forum.endeavouros.com/t/cant-create-swap-file-swapon-failed-invalid-argument/27485/11 "2022-05-28T23:23:24Z")

</div>

Closed on May 28, 2022, 11:23pm UTC

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