Create btrfs subvolume

i’m trying to make a swap subvolume for a swapfile, it’s my first time trying to create a tumbleweed btrfs subvolume so i could well be missing something pretty basic, can anyone clue me, what am i missing:

  #  cd /
  #  btrfs sub create /@swap
Create subvolume '//@swap'
  #  mkdir /swap
  #  mount -o subvol=@swap /dev/sda2 /swap
mount: /swap: mount(2) system call failed: No such file or directory.
    dmesg(1) may have more information after failed mount system call.
  #  journalctl -e
[nothing relevant]
  #  dmesg|less +G
[nothing relevant]
  #  btrfs subvolume list /                                                                                    
ID 256 gen 31 top level 5 path @                                           
ID 257 gen 544678 top level 256 path @/var                                                                                                             
ID 258 gen 544002 top level 256 path @/usr/local
ID 259 gen 543349 top level 256 path @/srv    
ID 260 gen 543349 top level 256 path @/root                    
ID 261 gen 544640 top level 256 path @/opt
ID 262 gen 544678 top level 256 path @/home                      
ID 263 gen 415578 top level 256 path @/boot/grub2/x86_64-efi                                                                                           
ID 264 gen 27 top level 256 path @/boot/grub2/i386-pc
ID 265 gen 543349 top level 256 path @/.snapshots                                                                                                      
ID 417 gen 417336 top level 265 path @/.snapshots/143/snapshot
ID 418 gen 367721 top level 265 path @/.snapshots/144/snapshot  
ID 419 gen 367772 top level 265 path @/.snapshots/145/snapshot
ID 420 gen 367774 top level 265 path @/.snapshots/146/snapshot
ID 421 gen 370059 top level 265 path @/.snapshots/147/snapshot
ID 422 gen 370062 top level 265 path @/.snapshots/148/snapshot
ID 423 gen 397273 top level 265 path @/.snapshots/149/snapshot
ID 424 gen 397276 top level 265 path @/.snapshots/150/snapshot
ID 425 gen 416981 top level 265 path @/.snapshots/151/snapshot
ID 426 gen 415577 top level 265 path @/.snapshots/152/snapshot
ID 427 gen 415991 top level 265 path @/.snapshots/153/snapshot
ID 428 gen 417784 top level 265 path @/.snapshots/154/snapshot
ID 429 gen 416980 top level 265 path @/.snapshots/155/snapshot
ID 430 gen 543349 top level 265 path @/.snapshots/156/snapshot
ID 431 gen 417335 top level 265 path @/.snapshots/157/snapshot
ID 432 gen 543349 top level 265 path @/.snapshots/158/snapshot
ID 433 gen 417783 top level 265 path @/.snapshots/159/snapshot
ID 434 gen 544600 top level 265 path @/.snapshots/160/snapshot
ID 435 gen 544415 top level 434 path @swap
  #  btrfs sub delete /@swap
Delete subvolume (no-commit): '//@swap'
  #  btrfs sub create /@/swap
ERROR: cannot access '/@': No such file or directory
  #  btrfs sub create @/swap
ERROR: cannot access '@': No such file or directory
  #  df -mT
Filesystem     Type     1M-blocks   Used Available Use% Mounted on
devtmpfs       devtmpfs         4      0         4   0% /dev
tmpfs          tmpfs         7934     36      7899   1% /dev/shm
tmpfs          tmpfs         3174      2      3172   1% /run
/dev/sda2      btrfs       235914 156006     79636  67% /
/dev/sda2      btrfs       235914 156006     79636  67% /.snapshots
/dev/sda2      btrfs       235914 156006     79636  67% /boot/grub2/i386-pc
/dev/sda2      btrfs       235914 156006     79636  67% /boot/grub2/x86_64-efi
/dev/sda2      btrfs       235914 156006     79636  67% /root
/dev/sda2      btrfs       235914 156006     79636  67% /home
/dev/sda2      btrfs       235914 156006     79636  67% /usr/local
/dev/sda2      btrfs       235914 156006     79636  67% /srv
/dev/sda2      btrfs       235914 156006     79636  67% /opt
/dev/sda2      btrfs       235914 156006     79636  67% /var
tmpfs          tmpfs         7934      5      7930   1% /tmp
/dev/sda1      vfat           511      9       503   2% /boot/efi
tmpfs          tmpfs         1587      1      1587   1% /run/user/1000
  #  zgrep -Eia 'ease\|20|l\|kernel-d' /var/log/zypp/history|tail -2
2023-04-13 15:35:26|install|openSUSE-release|20230412-2224.1|x86_64||repo-oss|707a74a248502dedf80991e1548f04743a23cb96b6629f410c1fe670a8a2b83c6877bbb545d2370ffeea7502b9c5ac44886b5808be7f57959df2371e23892f23|
2023-04-13 15:40:28|install|kernel-default|6.2.9-1.1|x86_64||repo-oss|ed6b8c5e624977f56bc7d38a0f703e88602a743cd7e0b6a367be937f8f539a5706651f3a59bfc656fc958a535c2655e57c696ccebecf0d99261a480ce13417f9|

It creates subvolume inside your currently active root snapshot, most likely @/.snapshots/160/snapshot/@swap.

The / mount point is below top-level btrfs directory so only the content of the subvolume currently mounted on / is visible. To access full content of btrfs you need to explicitly mount top level. Something like

mount -o subvol=/ /dev/sda2 /mnt
1 Like

Ach so. Thank you.
So, to summarize:

  #  mount -o subvol=/ /dev/sda2 /mnt
  #  btrfs sub create /mnt/@/swap
Create subvolume '/mnt/@/swap'
  #  umount /mnt
  #  mkdir /swap
  #  mount -o subvol=/@/swap /dev/sda2 /swap
  #  btrfs filesystem mkswapfile -s6g /swap/swapfile
create swapfile /swap/swapfile size 6.00GiB (6442450944)

Add to fstab:

UUID=<uuid>  /swap btrfs subvol=/@/swap 0 0
/swap/swapfile swap swap nofail         0 0

Activate:

  #  swapon -a

Why in a subvolume? Because the subvolume it’s in cannot be snapshotted if it contains any active swapfiles. [That page ought to be updated to recommend btrfs filesystem mkswapfile.]

1 Like

Thx for this thread, it helped me a lot :smiley: