swap file appears to have holes


error message

root@host:~# swapon /swap-file-00
swapon: /swap-file-00: skipping - it appears to have holes.

solution – you need to zero-fill the file you are using as swap

steps to remediate this –

first of all, remove the existing file with the holes.

root@host:~# rm /swap-file-00

create the new swap file using the following command:

root@host:~# dd if=/dev/zero of=/swap-file-00 bs=1M count=5120 status=progress
5322571776 bytes (5.3 GB, 5.0 GiB) copied, 6 s, 887 MB/s
5120+0 records in
5120+0 records out
5368709120 bytes (5.4 GB, 5.0 GiB) copied, 6.08009 s, 883 MB/s

check the file type being displayed now. it’s just a data file.

root@host:~# file /swap-file-00
/swap-file-00: data

if your system says something like the following, it just means that the file command is not installed on your system. you may either ignore the file commands in the further steps since they are not required in getting a swap file operational on your system. they are being used to verify the file contents based on their magic numbers. or you may chose to install the file command using the package manager on your gnu/linux, say apt -y install file on a debian/ubuntu:

file: command not found

change the permissions on it:

root@host:~# chmod 600 /swap-file-00

make the new file as swap file:

root@host:~# mkswap /swap-file-00
Setting up swapspace version 1, size = 5 GiB (5368705024 bytes)
no label, UUID=738f253b-aed0-46ac-bc50-b4d1fe2d1702

check the file type being displayed now. it’s a swap file.

root@host:~# file /swap-file-00
/swap-file-00: Linux/i386 swap file (new style), version 1 (4K pages), size 1310719 pages, no label, UUID=738f253b-aed0-46ac-bc50-b4d1fe2d1702

turn on the swap, as usual

root@host:~# swapon /swap-file-00

check if it is being used as a swap file

root@host:~# swapon --show
NAME TYPE SIZE USED PRIO
/swap-file-00 file 5G 0B -2

2 responses to “swap file appears to have holes”

    • Hey Alexandru, I checked it works on Ubuntu 22.
      But I did notice that the file command isn’t available in Ubuntu 22’s minimal installation. If it failed for you while executing the file command, you can well just ignore this command completely since it isn’t doing anything to help facilitate making the swap file as such. It is used just to verify what the contents of any file are.
      If you want to use the “file” command anyways, run the command apt install file and you should get the command on your system. Have fun.

Leave a Reply

Your email address will not be published. Required fields are marked *