Swap file creation on btrfs

wiki link : Swap - Manjaro

creating a new subvolume

btrfs subvolume create /@swapfile

but after this, trying to create file inside throws an error

[HPRYZEN username]# sudo truncate -s 0 /@swapfile/swapfile
truncate: cannot open '/@swapfile/swapfile' for writing: Permission denied

if this post require some additional information do let me know, below is my hardrive and ssd info,

/root uses nvme
/home uses hard drive…

NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sda           8:0    0 931.5G  0 disk 
├─sda1        8:1    0 301.8G  0 part /home
├─sda2        8:2    0 518.7M  0 part /boot/efi
└─sda3        8:3    0   629G  0 part 
nvme0n1     259:0    0 465.8G  0 disk 
├─nvme0n1p1 259:1    0   260M  0 part 
├─nvme0n1p2 259:2    0    16M  0 part 
├─nvme0n1p3 259:3    0 367.3G  0 part 
├─nvme0n1p4 259:4    0   522M  0 part 
└─nvme0n1p5 259:5    0  97.7G  0 part /run/timeshift/backup
                                      /var/lib/docker/btrfs
                                      /var/log
                                      /var/cache
                                      /



Maybe this is a better way:

sudo mount /dev/nvme0n1p5 /mnt
sudo btrfs subvolume create /mnt/@swap
sudo truncate -s 0 /mnt/@swap/swapfile
sudo umount /mnt
sudo mkdir -pv /swap
sudo mount -o subvol=@swap /dev/nvme0n1p5 /swap
/dev/nvme0n1p5 

this is my root partition mounting it on /mnt should’nt create an issue?

Nope, the root partition on btrfs is commonly a subvolume called @ So by mounting btrfs without the subvol option, it will mount the real partition.
Check it:

sudo btrfs subvolume list /
1 Like

Yes, There are ton of subvolume that docker created

ID 257 gen 107633 top level 5 path @cache
ID 258 gen 107650 top level 5 path @log
ID 1085 gen 107651 top level 5 path @
.
.
.
ID 1818 gen 95206 top level 1085 path var/lib/docker/btrfs/subvolumes/230630e584523285e1e337ec36fb5f320e8f6036c2a8ded27d983bcaa7a3b008
ID 1819 gen 107601 top level 1085 path var/lib/docker/btrfs/subvolumes/ff567fc46b0c624d9e88c353a82444b57694ecb5595a613a9462dbe6595c123c
ID 1820 gen 95196 top level 1085 path var/lib/docker/btrfs/subvolumes/de6082ba570922ba17b22aabf2afd816b87a54746b6ed12181ebb92ed8ff6c37-init
ID 1821 gen 95197 top level 1085 path var/lib/docker/btrfs/subvolumes/de6082ba570922ba17b22aabf2afd816b87a54746b6ed12181ebb92ed8ff6c37
ID 1823 gen 107094 top level 5 path timeshift-btrfs/snapshots/2022-05-12_01-00-02/@
ID 1827 gen 107094 top level 5 path timeshift-btrfs/snapshots/2022-05-18_14-05-24/@
ID 1828 gen 107094 top level 5 path timeshift-btrfs/snapshots/2022-05-19_01-00-01/@
ID 1831 gen 107094 top level 5 path timeshift-btrfs/snapshots/2022-05-22_01-25-45/@
ID 1832 gen 107094 top level 5 path timeshift-btrfs/snapshots/2022-05-22_23-36-54/@
ID 1833 gen 107649 top level 1085 path var/lib/systemd-swap/swapfc



Your old reply helped me, moved from zram to systemd… for now…

Thats a bad thing…btw… let me show how it looks on my machine:

subvolume

ID 310 gen 216323 top level 5 path @swap <-- created by me
ID 1383 gen 218505 top level 310 path @swap/swapfc <-- created by systemd-swap

lsblk

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sda      8:0    0 223,6G  0 disk 
├─sda1   8:1    0   100M  0 part /boot/efi
└─sda2   8:2    0 223,5G  0 part /run/timeshift/backup
                                 /home
                                 /var/lib/systemd-swap
                                 /

fstab

UUID=%UUID% /var/lib/systemd-swap/   btrfs   subvol=@swap,defaults,noatime,nospace_cache,nodatacow,commit=120               0 2
1 Like

so its because of fstab that systemd uses @swap subvolume…

how can i have similar setup as yours? by following this?

i think it make sense now…

sudo mount /dev/nvme0n1p5 /mnt
sudo btrfs subvolume create /mnt/@swap
sudo umount /mnt

Add this to fstab:

UUID=%UUID% /var/lib/systemd-swap/   btrfs   subvol=@swap,defaults,noatime,nospace_cache,nodatacow,commit=120               0 2

(of course replace %UUID%)

sudo systemctl stop systemd-swap.service
sudo rm -Rv /var/lib/systemd-swap/*
sudo mount /var/lib/systemd-swap/
sudo systemctl start systemd-swap.service
ID 1834 gen 107681 top level 5 uuid 550fac1e-bc55-f449-ac83-7dbfdfa76040 path @swap
sudo mount /var/lib/systemd-swap/
mount: /var/lib/systemd-swap/: can't find UUID=550fac1e-bc55-f449-ac83-7dbfdfa76040.
blkid -s UUID -o value /dev/nvme0n1p5

:wink:

1 Like

I thought i have to add that particular subvolumes uuid… works like charm now…