SwapFile Resize On Btrfs

After a fresh install using btrfs how do you resize the swap file. I tried it but it wouldn’t activate the swap file after. There is a @swap and a swap file i assume? So i’m not sure how this is supposed to work compared to swap file on Ext4.

[ricklinux@eos-plasma ~]$ cat /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a device; this may
# be used with UUID= as a more robust way to name devices that works even if
# disks are added and removed. See fstab(5).
#
# <file system>             <mount point>  <type>  <options>  <dump>  <pass>
UUID=01B4-EB21                            /boot/efi      vfat    umask=0077 0 2
UUID=885d2794-fc59-4d49-95a0-f57f19884102 /              btrfs   subvol=/@,defaults,noatime,space_cache,autodefrag,compress=lzo 0 1
UUID=885d2794-fc59-4d49-95a0-f57f19884102 /home          btrfs   subvol=/@home,defaults,noatime,space_cache,autodefrag,compress=lzo 0 2
UUID=885d2794-fc59-4d49-95a0-f57f19884102 /var/cache     btrfs   subvol=/@cache,defaults,noatime,space_cache,autodefrag,compress=lzo 0 2
UUID=885d2794-fc59-4d49-95a0-f57f19884102 /var/log       btrfs   subvol=/@log,defaults,noatime,space_cache,autodefrag,compress=lzo 0 2
UUID=885d2794-fc59-4d49-95a0-f57f19884102 /swap          btrfs   subvol=/@swap,defaults,noatime,space_cache,autodefrag,compress=lzo 0 2
/swap/swapfile                            swap           swap    defaults,noatime 0 0
tmpfs                                     /tmp           tmpfs   defaults,noatime,mode=1777 0 0
[ricklinux@eos-plasma ~]$ 

I haven’t looked at swap on the new installer yet but presumably @swap is just a subvolume to store the swapfile. You put the swapfile in a separate subvolume on btrfs because you can’t take snapshots of a subvolume with a swapfile. That being said, you really shouldn’t mount that subvolume with compression enabled so I would remove that from the mount options.

In general swapfiles on btrfs need some special handling. The Arch wiki has a pretty simple description on how to handle that: https://wiki.archlinux.org/title/Btrfs#Swap_file

I really don’t understand the arch wiki related to the swap file on that page.

There are 4 commands in a box. What don’t you understand

I don’t understand what to do with what i currently have that was set up by the install on btrfs. Can i use the existing @swap subvolume or i have to delete it and also the existing swap file. The wiki says to create a non snapshotted subvolume @swap. Not sure i cant’ just use what’s there if i delete the swap file and start from there. Or do i need to delete the subvolume @swap also and create a new one?

Then if i create a directory for swap i get that. But not 100% sure on every step.

I just reinstalled KDE with the new ISO on bare metal and set it all up again. I just don’t want to do it again because i wreak havoc on it trying to resize a swap file. :sob:

Edit: I might install kde on v-box first and then if i can understand what I’m doing I’ll try it on that first. I already tried to resize the file and it didn’t work the normal way.

You can leave the subvolume alone, you can just use swapoff to disable the existing swapfile.

Once you do that you can just delete it and recreate it however you want.

1 Like

What directory am i supposed to cd into? Don’t i have to create a directory? The wiki doesn’t explain it just tells you to do something. :persevere:

Edit: I don’t understand how to do this at all. I have tried everything i know. The wiki is not helpful. I can seem to delete this current swap file on btrfs and i don’t understand how to increase the size but also how to do it how it’s supposed to be. I give up!

I decided to go with a swap partition instead of a swap file thereby removing it from the btrfs equation. Nothing i tried worked. Well some commands worked some didn’t. Couldn’t get it set up the way i wanted so i took an alternate route. :smile_cat:

[ricklinux@eos-kde ~]$ sudo btrfs subvolume list /
[sudo] password for ricklinux: 
ID 256 gen 143 top level 5 path @
ID 257 gen 146 top level 5 path @home
ID 258 gen 125 top level 5 path @cache
ID 259 gen 146 top level 5 path @log
ID 262 gen 25 top level 256 path var/lib/portables
ID 263 gen 26 top level 256 path var/lib/machines
[ricklinux@eos-kde ~]$ 

If you feel like trying again the following should work:

based on (copy/paste from :blush:)

https://wiki.archlinux.org/title/Btrfs#Swap_file
https://wiki.archlinux.org/title/Swap#Swap_file_creation

  1. create a directory for the subvolume containing the swapfile:

sudo mkdir /swap

  1. create a subvolume in it

sudo btrfs subvolume create /swap/swap

  1. cd into it and create a zero length file

cd /swap/swap
sudo truncate -s 0 ./swapfile

  1. set the No_COW attribute on it

sudo chattr +C ./swapfile

  1. disable the compression

sudo btrfs property set ./swapfile compression none

  1. set the right permissions

sudo chmod 600 ./swapfile

  1. create the swapfile (here with the size 1024 MiB)

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

  1. format it to swap:

sudo mkswap ./swapfile

  1. Activate the swap file:

sudo swapon ./swapfile

  1. check if it is activated

sudo swapon -s

  1. add the following entry for it in the /etc/fstab:

/swap/swap/./swapfile none swap defaults 0 0

  1. reboot and check again

sudo swapon -s

Edited to add the command for the creation of the subvolume containing the swapfile that I had forgotten :man_facepalming:t5:

6 Likes

Hey, yes this is working guide.

I always used guides from this : https://discovery.endeavouros.com/encrypted-installation/btrfsonluks-verbose-version/2021/03/
and never had issues.

But I am curious, with new iso-s with better btrfs support, should we still follow guides from wiki ( posted above ) or no need ?

1 Like

I am afraid I haven’t tried yet the new BTRFS installation and encryption with the new ISO, so I couldn’t answer your question properly. I suppose it will set up everything out-of-the-box if you choose those settings. Perhaps @joekamprad or @2000 could tell you that.

1 Like

i have not really any deeper understanding on BTRFS but it is now possible to use automatic BTRFS install from ISO with the fixed subcol scheme on autopartition, it can use all 3 swap options swap no hibernate, swap with hibernate, and swapfile… and encryption is working also…
subvol scheme:

btrfsSubvolumes:
    - mountPoint: /
      subvolume: /@
    - mountPoint: /home
      subvolume: /@home
    - mountPoint: /var/cache
      subvolume: /@cache
    - mountPoint: /var/log
      subvolume: /@log
3 Likes

You can try zram instead.

sudo pacman -S systemd-swap
sudo pacman -S zram-generator
sudo vi /etc/systemd/zram-generator.conf
sudo systemctl start systemd-swap.service
sudo systemctl enable systemd-swap.service

https://wiki.archlinux.org/title/improving_performance#zram_or_zswap

In the stable linux official kernel, zswap is enabled by default.

https://wiki.archlinux.org/title/zswap

This worked on a btrfs install that i didn’t have any swap until i got up to the fstab part. Are you supposed to put the uuid in there too?

/swap/swap/./swapfile none swap defaults 0 0

Edit: I guess not. So this worked as i said on a btrfs install that had no swap. What i was trying to do was take the btrfs install that has swap which is only 512 by default and make it larger. So i tried the normal way of stopping swap and tried to make a new swap file that was bigger. This install already has the subvolume for swap and the swap file so i couldn’t figure out how on earth to do it no how & it just made me frustrated with the whole episode.

Edit2: So this is different than what is on the btrfs install if you use a swipe file.

[ricklinux@rick-virtualbox ~]$ sudo btrfs subvolume list /
[sudo] password for ricklinux: 
ID 257 gen 70 top level 5 path @
ID 258 gen 70 top level 5 path @home
ID 259 gen 33 top level 5 path @cache
ID 260 gen 70 top level 5 path @log
ID 263 gen 25 top level 257 path var/lib/portables
ID 264 gen 26 top level 257 path var/lib/machines
ID 270 gen 42 top level 257 path swap/swap
[ricklinux@rick-virtualbox ~]$ 

On the btrfs install with swap file it had @swap and a swap file. Like i say i don’t understand it enough. So i couldn’t get it to work. This worked from scratch when i started with a btrfs install with no swap.

Edit3: So there is no @swap Subvolume?

[ricklinux@rick-virtualbox ~]$ swapon --show
NAME                TYPE  SIZE USED PRIO
/swap/swap/swapfile file 15.6G   0B   -2
[ricklinux@rick-virtualbox ~]$ 

No need for UUID .

/swap/swapfile none swap defaults 0 0

this is fine, I never had issues

1 Like

It wouldn’t boot when i put a uuid in front of it. :scream:

Hm, I never tried it so don’t know.

Btrfs world is so complicated , even when it is easy xD

It is a file, where did you get the UUID from?

When i created the swap file. :man_shrugging:

What is easy xD short for Easy BSD? Or something else?