Btrfs swapfile patch help

I thought this was the best place to discuss, instead of doing it on the bug report, I hope I wasn’t wrong.

Hello.
As said on bugreport [1], I’m not a developer, it’s the first time I try to write a patch, unfortunately I don’t know how to do it.
Surely the code is not perfect, is there anyone who can help complete, revise and improve the code?

Mainly the changes are:

  1. If you choose btrfs, the code to detect the partition does not work, with the help of friends on Telegram I managed to improve the codide and now it perfectly detects Btrfs and ext4 [2].
  2. I create a nested subvolume (avoid mounting on /etc/fstab) [3] before activating the swap, to avoid carrying the swapfile if using snapshots, then I set the nocow [4] flag on the subvolume.
  3. I created ifs, if it reveals btrfs it points to the subvolume folder.

I tried the change in VM on Ubuntu 20.04, first with btrfs, then with ext4, the swpafile is created correctly on both installations

[1] https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1818811
[2]

rootfstype=$(df -T /target | grep /target | awk ‘{print $2}’)

[3] https://www.spinics.net/lists/linux-btrfs/msg103190.html
https://www.spinics.net/lists/linux-btrfs/msg103213.html

btrfs subvolume create /target/swap

[4] chattr + C /target/swap

For a complete view of the code:
patch: 25create_swapfile
patch: 45fstab_swapfile

Hello,
I don’t understand it all. Do I need to boot in a live to created the btrfs swap subvolume?
What does the “rootfstype=$(df -T /target | grep /target | awk ‘{print $2}’)” command line do? What are the precise steps to be made?

No. This was my attempt to help fix the bug in the Ubuntu installer. But I don’t have the right skills and I finally let it be.
The swapfile on Ubuntu can be created after installation.
Here are the steps:

  • sudo mount -t btrfs /dev/sdYN /mnt
  • sudo btrfs subvolume create /mnt/@swap
  • sudo chattr +C /mnt/@swap
  • sudo truncate -s 0 /mnt/@swap/swapfile
  • sudo fallocate -l 2000M /mnt/@swap/swapfile
  • sudo chmod 600 /mnt/@swap/swapfile
  • sudo mkswap /mnt/@swap/swapfile
  • sudo umount /mnt
    Now you need to edit fstab to mount the subvolume and activate the swapfile inside the subvolume:
    UUID=yourUUID /swap btrfs noatime,subvol=@swap
    /swap/swapfile none swap defaults 0 0

Hope it is useful for you.

This site isn’t designed for technical support. The Finding Help topic details where you can get support for Ubuntu.