1

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.

user149408
  • 1,142

1 Answers1

1

Some concepts around which btrfs was built do not work well with swap files (imagine creating a read-only snapshot of a swap file). Resolving this needs some extra logic, which was only added in kernel 5.0. With an OS as old as the one in question, there are two basic options:

Option 1: Upgrade to a newer OS with kernel 5.0 or later, then proceed as in https://superuser.com/a/1442752/432690. While running an up-to-date OS is generally recommended, an update needs to be planned and may not be a quick win.

Option 2: Repartition the HD to make room for a swap partition, then use that. This is easier than a OS upgrade, while also being cleaner and less hassle in the long run.

With the btrfs file system mounted, do:

sudo btrfs filesystem resize -512g /mnt/btrfs

(this will reduce the filesystem size by 512 GB, adjust as needed. You want to shrink the FS by a little more than the space you need for the new partition, then resize the partition and re-grow btrfs to fill the resized partition again.)

sudo umount /mnt/btrfs

Resize the btrfs partition (e.g. using parted) and create a swap partition after it. Then,

sudo mount /mnt/btrfs
sudo btrfs filesystem resize max /mnt/btrfs

Add the following line to /etc/fstab (assuming /dev/sda2 is your swap partition):

/dev/sda2    swap    swap    defaults    0    0

Then do:

sudo mkswap /dev/sda2
sudo dphys-swapfile swapoff
sudo dphys-swapfile uninstall
sudo update-rc.d dphys-swapfile remove
sudo apt purge dphys-swapfile -y
sudo swapon -a

Verify that swap space is available and the swap partition is used by running free -h and sudo swapon --show. free should show more than zero total swap space, and swapon should show only the swap partition, no more swap files. Reboot and re-run free -h and sudo swapon --show to ensure swap space is enabled on boot.

This is the option which worked for me.

user149408
  • 1,142