swapon: swapfile has holes

Written on

After the last kernel update I noticed that there was a warning during the boot process, but it was displayed too short to see where the problem was. Also, the system booted correctly so it couldn’t be critical.

My first thought was that it had something to do with the network as I had some problems with it recently, but to be sure I checked the logs:

journalctl -b

After scrolling trough the log there it was.

kernel: swapon: swapfile has holes
systemd[1]: swapfile.swap: Swap process exited, code=exited, status=255/EXCEPTION
systemd[1]: swapfile.swap: Failed with result 'exit-code'.
systemd[1]: Failed to activate swap /swapfile.
systemd[1]: Dependency failed for Swap.
systemd[1]: swap.target: Job swap.target/start failed with result 'dependency'.

So it looks like my swap1 file has holes, but what does that mean? I did a quick search and found the following explanation:

The swap file implementation in the kernel expects to be able to write to the file directly, without the assistance of the filesystem. This is a problem on preallocated files (e.g. fallocate(1)) on filesystems like XFS or ext4, and on copy-on-write filesystems like btrfs.

Well, that wasn’t really helpful, so I kept searching and found the issue FS#66921 - [linux] “Swapfile has holes” on Linux 5.7 in the archlinux bug report list. That sounded more like what I was looking for.

Gladly, there was also a solution. So I followed the instructions and deleted the old swap file so I could create a new one.

As described in the issue I first created a new swapfile with dd2

dd if=/dev/zero of=/swapfile bs=1M count=16384 status=progress

Afterwards I set the correct permissions and set the swap area to the swapfile.

chmod 600 /swapfile
mkswap /swapfile

Finally I used swapon to specify the /swapfile on which swapping will take place.

swapon /swapfile

After a restart the problem was gone, success!

  1. Linux divides its physical RAM (random access memory) into chunks of memory called pages. Swapping is the process whereby a page of memory is copied to the preconfigured space on the hard disk, called swap space, to free up that page of memory. The combined sizes of the physical memory and the swap space is the amount of virtual memory available. — Archlinux Wiki
  2. dd is a command-line utility for Unix and Unix-like operating systems, the primary purpose of which is to convert and copy files. On Unix, device drivers for hardware (such as hard disk drives) and special device files (such as /dev/zero and /dev/random) appear in the file system just like normal files; dd can also read and/or write from/to these files, provided that function is implemented in their respective driver. As a result, dd can be used for tasks such as backing up the boot sector of a hard drive, and obtaining a fixed amount of random data. — Wikipedia