21

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?

6 Answers 6

23

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.

Let's 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.

  2. Create a new @swap subvolume.

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

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

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

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

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

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

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

    sudo dd if=/dev/zero of=/swap/swapfile bs=1M count=4096
    
  10. Format the swapfile.

    sudo mkswap /swap/swapfile
    
  11. 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.

Swap file can't be located on a btrfs raid of any sort.

Comments and suggestions are welcome.

11
  • 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, 2020 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, 2020 at 18:47
  • Would it work to have a separate swap partition (alongside the btrfs partition) on the same drive?
    – sudodus
    Jan 28, 2020 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, 2020 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, 2020 at 18:52
2

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
2
  • noatime isn't necessary as relatime is the default and that also won't write unless the file has been written to. This hasn't been a thing for ~15 years since the kernel changed the default to it. I'm not certain you need defaults, or if a swapfile benefits from lazytime or compress. It probably would though. Jul 28, 2021 at 1:33
  • defaults doesn't make any sense if there are other options. It is used if nothing else is there.
    – Pilot6
    Feb 27 at 11:06
2
+50

You can have a swap on btrfs, if you take care what the btrfs doku tell about this:

filesystem - must be only single device
filesystem - must have only single data profile
swapfile - the containing subvolume cannot be snapshotted
swapfile - must be preallocated (i.e. no holes)
swapfile - must be NODATACOW (i.e. also NODATASUM, no compression)

For more information see the source:

1

The popular answer is correct except that you should not use fallocate(1) to allocate space to the swapfile. It can create file-system 'holes' which are incompatible with swapfile usage (swap needs 100% contiguous space since a filesystem is not used within the swap space). You should use 'dd if=/dev/zero ...' instead. See the answer on this thread: fallocate vs dd for swapfile? The mkswap(8) and swapon(8) manpages both explicitly discourage the use of fallocate(1) to instantiate a swapfile on the filesystem.

2
  • 2
    Yes but the answer in that link only creates a 1Mb swap file. To create say a 8Gb one, you need to do "sudo dd if=/dev/zero of=/swap/swapfile bs=1M count=8192"
    – bshor
    Jul 2, 2021 at 5:12
  • I changed from allocate to dd.
    – Pilot6
    Oct 22, 2021 at 18:28
0

If your partition is encrypted (LUKS), the mount point is on /dev/mapper for example:

/dev/mapper/nvme0n1p5_crypt /swap btrfs defaults,noatime,subvol=@swap 0 0
/swap/swapfile none swap sw 0 0 here

To get the uuids run the blkid command:

/dev/mapper/nvme0n1p5_crypt: UUID="06c8c73c-1cc4-477b-a687-6c21697d645d"   UUID_SUB="7f884b26-d76e-49db-9959-311fa2a5dd20" TYPE="btrfs"
/dev/nvme0n1p1: UUID="8090a824-63fa-4087-a948-89cca1a369cd" TYPE="ext2" PARTUUID="887ff27b-01"
/dev/nvme0n1p5: UUID="46405308-2ed1-40f3-a86b-906f1118970b" TYPE="crypto_LUKS" PARTUUID="887ff27b-05"

And then replace the mapper location with the specific uuid

UUID=06c8c73c-1cc4-477b-a687-6c21697d645d /swap btrfs   defaults,noatime,subvol=@swap 0 0 
/swap/swapfile none swap sw 0 0
0

The best answer for me didn't get it. I was getting error:

$ sudo swapon -v --show /swap/swapfile

swapon: /swap/swapfile: swapon failed: Invalid argument

for me work solution was from https://btrfs.readthedocs.io/en/latest/btrfs-man5.html#swapfile-support

# truncate -s 0 swapfile
# chattr +C swapfile
# fallocate -l 2G swapfile
# chmod 0600 swapfile
# mkswap swapfile
# swapon swapfile

/etc/fstab:

/swap/swapfile              none            swap    sw              0       0
1
  • This answer doesn't look correct. it is unclear where the swap file is located. Is it /swapfile or /swap/swapfile?
    – Pilot6
    Feb 27 at 11:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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