Some Linux filesystems do not natively support swapfiles as they do not implemented to required kernel calls. Notable amongst this list of filesystem is btrfs, whose snapshot functionality I use to streamline my backup system.
Unfortunately, some systems I maintain have a chronic lack of RAM and require a swapfile to complete some long running operations. As a workaround it is possible to create a loopback device from a file, which is then possible to pass off to the kernel as a swap device.
This option comes with a performance impact, but is functionally identical for our purposes.
To create a swap device we create a swap image, pass it to losetup for device creation, then enable the swap as follows:
path="/swap" truncate -s 1G "$path" mkswap "$path" dev=$(losetup -f) losetup "$dev" "$path" swapon "$dev"
To clean up we perform the reverse of the above operations:
swapoff "$dev" losetup -d "$dev" rm "$path"