9

Modern Ubuntu versions are using a swap file instead of a swap partition by default.

Before the 5.0 Linux kernel it was not possible to place a swap file on a btrfs partition, btrfs file system could be damaged.

Now kernels support swap files on btrfs partitions. Can I use a swap file on Ubuntu installed on btrfs and what are possible problems?

9

It is possible to use a swap file on btrfs, but there are some considerations that need taking care of.

btrfs filesystem doesn't let to create snapshots if there is a working swap file on the subvolume. That means that it is highly recommended to place a swap file on a separate subvolume.

Lets assume that the current swap is already off, the / is on /dev/sda1 and Ubuntu is installed with / on @ subvolume and /home is on @home subvolume.

  1. Mount /dev/sda1 to /mnt.

     sudo mount /dev/sda1 /mnt
    

If you run ls /mnt, you'll see @, @home and other subvolumes that may be there.

  1. Create a new @swap subvolume.

     sudo btrfs sub create /mnt/@swap
    
  2. Unmount /dev/sda1 from /mnt

     sudo umount /mnt
    
  3. Create /swap directory where we plan to mount the @swap subvolume.

     sudo mkdir /swap
    
  4. Mount the @swap subvolume to /swap.

     sudo mount -o subvol=@swap /dev/sda1 /swap
    
  5. Create the swap file.

     sudo touch /swap/swapfile
    
  6. Set 600 permissions to the file.

     sudo chmod 600 /swap/swapfile
    
  7. Disable COW for this file.

     sudo chattr +C /swap/swapfile
    
  8. Set size of the swap file to 4G as an example.

     sudo fallocate /swap/swapfile -l4g
    
  9. Format the swapfile

    sudo mkswap /swap/swapfile
    
  10. Turn the swap file on.

    sudo swapon /swap/swapfile
    

Now the new swap should be working.

You also need to update /etc/fstab to mount all this on boot. Add there two lines:

UUID=XXXXXXXXXXXXXXX /swap btrfs subvol=@swap 0 0
/swap/swapfile none swap sw 0 0

The UUID is the one of your /dev/sda1.

Comments and sugestions are welcome.

8
  • Please explain to us who have never tried btrfs: What is the alternatives to a swap file like this, and what is the advantage with this method compared to the alternatives? – sudodus Jan 28 '20 at 18:45
  • If you simply have a swap file on btrfs, like installer creates by default, snapshots don't work. I mentioned this. This is the main purpose of moving it to another subvolume. One of the main reasons to use btrfs is snapshots capability. – Pilot6 Jan 28 '20 at 18:47
  • Would it work to have a separate swap partition (alongside the btrfs partition) on the same drive? – sudodus Jan 28 '20 at 18:48
  • No problem to have a swap partition, but the Ubuntu installer creates a swap file now. That makes trouble for people using snapshots and software like timeshift. – Pilot6 Jan 28 '20 at 18:49
  • So it is the best alternative, when using btrfs, at least if no swap partition was created during the original partitioning? Or if the the user wants more swap? – sudodus Jan 28 '20 at 18:52
1

Note that for modern systems/laptops with NVME SSDs, you have /dev/nvme0n1p2 instead of /dev/sda1 and you should ignore /dev/nvme0n1p1 because that is your efi boot disk that your OS created when you installed it.

In addition it should be recommended to add two mount options 'defaults' and 'noatime'. Defaults will automatically load the mount options for the drive (SSD, HDD). Noatime will prevent files being written if only opened:

UUID=XXXXXXXXXXXXXXX /swap btrfs defaults,noatime,subvol=@swap 0 0
/swap/swapfile none swap sw 0 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.