Zend certified PHP/Magento developer

dphys-swapfile on btrfs volume fails

I have used dphys-swapfile with the default path of /var/swap and a 2048M swap file.

However, since /var/swap is on a SD card, I wanted to move the swap file to a magnetic disk. So I ran sudo dphys-swapfile swapoff, edited the path, then ran:

sudo dphys-swapfile setup
sudo dphys-swapfile swapon

The latter failed with:

swapon: /srv/file/swap: swapon failed: Invalid argument

sudo journalctl -xe gives me:

kernel: swapon: swapfile has holes

Just to be sure, I tried the following, all to no avail:

  • Delete the new swap file and recreate it with sudo dphys-swapfile setup
  • Copy the old swap file to the new location
  • sudo dd if=/dev/zero of=/srv/file/swap bs=1024 count=2097152 status=progress (zero out the entire swapfile, bs * count being equal to the size of the swap file – this time the error I got was that the swap file had an invalid header
  • dd the old swap file over the zeroed-out one – again resulted in a complaint about holes in the file

The only difference between the old and new path to the swap file is that the old path is on an ext4 filesystem while the new one is on btrfs. (Apart from the old path being on a SD card and the new one being on a magnetic disk, though I doubt this would make any difference.)

For some reason the swap file on ext4 either seems to get created without holes, or dphys-swapfile accepts holes on ext4. How can I obtain the same result on btrfs? (Or am I having a different problem than I think I have?)

OS is Raspbian Linux 9 (I know, it’s old), dphys-swapfile is version 20100506-3.