# Creating Swapfile Btrfs

**URL:** <https://forum.endeavouros.com/t/creating-swapfile-btrfs/27364>\
**Category:** Newbie\
**Created:** [May 23, 2022, 2:40pm UTC](https://forum.endeavouros.com/t/creating-swapfile-btrfs/27364 "2022-05-23T14:40:46Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![VolantesMagus](https://forum.endeavouros.com/user_avatar/forum.endeavouros.com/volantesmagus/32/49259_2.png) [@VolantesMagus](https://forum.endeavouros.com/u/VolantesMagus)\
**Post date:** [May 23, 2022, 2:40pm UTC](https://forum.endeavouros.com/t/creating-swapfile-btrfs/27364/1 "2022-05-23T14:40:46Z")

</div>

1. sudo btrfs subvolume create @swap

2. sudo mkdir /swap

3. sudo mount -o subvol=@swap /dev/XXX /swap ## replace XXX with your actual partition for example sda2, nvme0n1p2

4. cd /swap

5. sudo truncate -s 0 swapfile

6. sudo chattr +C swapfile

7. sudo btrfs property set swapfile compression none

8. sudo dd if=/dev/zero of=swapfile bs=1M count=512 status=progress

9. sudo chmod 600 swapfile

10. sudo mkswap swapfile

11. sudo swapon swapfile

12. Now Edit the /etc/fstab and add the following two lines:

UUID=NNNN /swap btrfs subvol=@swap ## replace NNNN with the UUID of the partition where @swap resides

/swap/swapfile none swap defaults 0 0

* * *

I have been trying to follow the guide above but it seems I am doing something wrong, as far as I see I created the @swap subvolume and its inside / Once I get to the 3rd command its not working the way it is probably supposed to or I am doing something wrong.

nvme0n1 is the drive/partition? I have eos installed on, when I run ls in my root directory after I create the btrfs @swap subvolume I get:

```auto
bin dev home lib64	opt root sbin @swap sys usr
boot etc lib mnt	proc run srv swap tmp var

```

Is @swap supposed to be there?

lsblk:

```auto
seb@PrimeCore/ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 465.8G 0 disk
├─sda1 8:1 0 1K 0 part
└─sda5 8:5 0 465.8G 0 part
sdb 8:16 0 223.6G 0 disk
├─sdb1 8:17 0 500M 0 part
└─sdb2 8:18 0 223.1G 0 part
sdc 8:32 0 1.8T 0 disk
└─sdc1 8:33 0 1.8T 0 part /mnt/diamond-drive
sdd 8:48 0 3.6T 0 disk
└─sdd1 8:49 0 3.6T 0 part /mnt/titan-drive
nvme0n1 259:0 0 931.5G 0 disk
├─nvme0n1p1 259:1 0 300M 0 part /boot/efi
├─nvme0n1p2 259:2 0 922.4G 0 part /var/log
│ /home
│ /var/cache
│ /
└─nvme0n1p3 259:3 0 8.8G 0 part

```

I tried running the 3rd command from the guide and tried different partitions I dont know what I am doing wrong here…

```auto
sudo mount -o subvol=@swap /dev/nvme0n1p2 /swap
mount: /swap: mount(2) system call failed: No such file or directory.
       dmesg(1) may have more information after failed mount system call.              

sudo mount -o subvol=@swap /dev/nvme0n1 /swap
mount: /swap: /dev/nvme0n1 already mounted or mount point busy.
       dmesg(1) may have more information after failed mount system call.

```

Is this maybe not the best guide or am I more likely doing something wrong?

---

<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 23, 2022, 2:52pm UTC](https://forum.endeavouros.com/t/creating-swapfile-btrfs/27364/2 "2022-05-23T14:52:45Z")

</div>

> [@VolantesMagus](#):
>
> when I run ls in my root directory after I create the btrfs @swap subvolume I get:

This is the wrong “root”.

That command needs to be run in the root of your btrfs partition which you may not have mounted.

You can mount it like this:

```plaintext
sudo mkdir /mnt/btrfs
sudo mount /dev/nvme0n1p2 /mnt/btrfs -o subvolid=5

```

---

<div class="post-metadata">

**Author:** ![VolantesMagus](https://forum.endeavouros.com/user_avatar/forum.endeavouros.com/volantesmagus/32/49259_2.png) [@VolantesMagus](https://forum.endeavouros.com/u/VolantesMagus)\
**Post date:** [May 23, 2022, 3:29pm UTC](https://forum.endeavouros.com/t/creating-swapfile-btrfs/27364/3 "2022-05-23T15:29:50Z")

</div>

I dont quite understand this, this is really confusing, once I mount it do I just do cd /mnt/btrfs then create the swap subvolume there?

---

<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 23, 2022, 3:30pm UTC](https://forum.endeavouros.com/t/creating-swapfile-btrfs/27364/4 "2022-05-23T15:30:41Z")

</div>

> [@VolantesMagus](#):
>
> I dont quite understand this, this is really confusing

One word is being used for two different things.

The “root” of your filesystem(aka `/`) is mounted on a subvolume not the “root” of the btrfs partition.

Those instructions want you to create a subvolume at the root of the partition.

> [@VolantesMagus](#):
>
> do I just do cd /mnt/btrfs then create the swap subvolume there?

Yes. That is correct.

---

<div class="post-metadata">

**Author:** ![VolantesMagus](https://forum.endeavouros.com/user_avatar/forum.endeavouros.com/volantesmagus/32/49259_2.png) [@VolantesMagus](https://forum.endeavouros.com/u/VolantesMagus)\
**Post date:** [May 23, 2022, 3:33pm UTC](https://forum.endeavouros.com/t/creating-swapfile-btrfs/27364/5 "2022-05-23T15:33:36Z")

</div>

sudo mount -o subvol=@swap /dev/nvme0n1 /swap

then I run this while still in the /mnt/btrfs directory? then just continue with the rest of the steps?

---

<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 23, 2022, 3:34pm UTC](https://forum.endeavouros.com/t/creating-swapfile-btrfs/27364/6 "2022-05-23T15:34:09Z")

</div>

> [@VolantesMagus](#):
>
> sudo mount -o subvol=@swap /dev/nvme0n1 /swap
> 
> then I run this while still in the /mnt/btrfs directory? then just continue with the rest of the steps?

You can run that command from anywhere.

The rest of the steps should work now.

---

<div class="post-metadata">

**Author:** ![VolantesMagus](https://forum.endeavouros.com/user_avatar/forum.endeavouros.com/volantesmagus/32/49259_2.png) [@VolantesMagus](https://forum.endeavouros.com/u/VolantesMagus)\
**Post date:** [May 23, 2022, 3:40pm UTC](https://forum.endeavouros.com/t/creating-swapfile-btrfs/27364/8 "2022-05-23T15:40:57Z")

</div>

```auto
seb@PrimeCore/swap sudo btrfs property set swapfile compression none 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 23, 2022, 3:42pm UTC](https://forum.endeavouros.com/t/creating-swapfile-btrfs/27364/9 "2022-05-23T15:42:16Z")

</div>

> [@VolantesMagus](#):
>
> ```plaintext
> sudo btrfs property set swapfile compression none swapfile compression none
> 
> ```

did you double copy/paste there? There is too much command.

---

<div class="post-metadata">

**Author:** ![VolantesMagus](https://forum.endeavouros.com/user_avatar/forum.endeavouros.com/volantesmagus/32/49259_2.png) [@VolantesMagus](https://forum.endeavouros.com/u/VolantesMagus)\
**Post date:** [May 23, 2022, 3:44pm UTC](https://forum.endeavouros.com/t/creating-swapfile-btrfs/27364/10 "2022-05-23T15:44:56Z")

</div>

```auto
1. sudo btrfs property set swapfile compression none
ERROR: failed to set compression for swapfile: Invalid argument

```

Sorry it just does that in my terminal, something wrong with the terminal due to zsh, I dont know why it does that but the command was entered properly

---

<div class="post-metadata">

**Author:** ![VolantesMagus](https://forum.endeavouros.com/user_avatar/forum.endeavouros.com/volantesmagus/32/49259_2.png) [@VolantesMagus](https://forum.endeavouros.com/u/VolantesMagus)\
**Post date:** [May 23, 2022, 3:54pm UTC](https://forum.endeavouros.com/t/creating-swapfile-btrfs/27364/11 "2022-05-23T15:54:32Z")

</div>

Its actually something to do with alacritty, I switched to konsole and tried the command in that, here is what I get:

```auto
seb@PrimeCore~ cd /swap
 seb@PrimeCore/swap sudo btrfs property set ./swapfile compression none
[sudo] password for seb: 
ERROR: failed to set compression for ./swapfile: Invalid argument
 seb@PrimeCore/swap

```

---

<div class="post-metadata">

**Author:** ![Kresimir](https://forum.endeavouros.com/user_avatar/forum.endeavouros.com/kresimir/32/73739_2.png) [@Kresimir](https://forum.endeavouros.com/u/Kresimir)\
**Post date:** [May 23, 2022, 6:01pm UTC](https://forum.endeavouros.com/t/creating-swapfile-btrfs/27364/12 "2022-05-23T18:01:39Z")

</div>

Either type out the command, or paste it in a text editor and edit it before pasting it in the terminal emulator.

---

<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 24, 2022, 12:59pm UTC](https://forum.endeavouros.com/t/creating-swapfile-btrfs/27364/13 "2022-05-24T12:59:29Z")

</div>

I think this is actually a btrfs issue/change.

After disabling CoW, the compression property is no longer available.

---

<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 24, 2022, 4:14pm UTC](https://forum.endeavouros.com/t/creating-swapfile-btrfs/27364/14 "2022-05-24T16:14:50Z")

</div>

It looks like this step is no longer needed.

btrfs now disables compression when CoW is disabled.

[https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=0e852ab8974cd2b5946766b2d9baf82c78ace03d](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=0e852ab8974cd2b5946766b2d9baf82c78ace03d)
