M BUZZ CRAZE NEWS
// general

Can't increase swap size: the swapfile appears to have holes

By Gabriel Cooper

Edit: I gave up to struggle with 19.10. Due to my research it has serious bugs especially with 5.3 kernel. Too much people have same issues and there is not only one solution that won't work for me. I will try 18 lts version.

I'm using Ubuntu 19.10 with 5.3.26 kernel and have 4 GB RAM but my swap size is 2 GB RAM. My system freezes nearly whole time. So, I decided to increase swap size but I have an error like that.

I used these commands:

sudo dd if=/dev/zero of=/swapfile bs=1G count=4
sudo chmod 600 /swapfile
sudo mkswap /swapfile

It's okay until the last part:

sudo swapon /swapfile

When I used the last command, it says:

swapon: /swapfile: skipping - it appears to have holes.

This is lsblk output

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
├─sda1 8:1 0 512M 0 part /boot/efi
├─sda2 8:2 0 50M 0 part /boot/grub
├─sda3 8:3 0 2G 0 part [SWAP]
├─sda4 8:4 0 2G 0 part
└─sda5 8:5 0 114,7G 0 part 

This is df output

Filesystem Type 1M-blocks Used Available Use% Mounted on
/dev/sda1 vfat 511 8 504 2% /boot/efi
/dev/sda2 ext4 45 8 33 20% /boot/grub
udev devtmpfs 1883 0 1883 0% /dev
udev devtmpfs 1883 0 1883 0% /dev
udev devtmpfs 1883 0 1883 0% /dev
8

3 Answers

Ubuntu Community SwapFAQ shows use of fallocate as initial and quicker approach, only falling back on dd if fallocate fails.

Example Given (1GiB):

$ sudo swapoff -a
$ sudo fallocate -l 1g /mnt/1GiB.swap
$ sudo chmod 600 /mnt/1GiB.swap
$ sudo mkswap /mnt/1GiB.swap
Setting up swapspace version 1, size = 1048576 kB
$ sudo swapon /mnt/1GiB.swap
$ cat /proc/swaps
Filename Type Size Used Priority
/home/swapfile file 1048576 1048576 -1
$ echo '/mnt/4GiB.swap swap swap defaults 0 0' | sudo tee -a /etc/fstab
$ reboot
$ free -h total used free shared buff/cache available
Mem: 15G 9.3G 454M 4.0G 5.8G 1.9G
Swap: 1.0G 0B 1.0G

Your Case (4GiB) - Using fallocate:

$ sudo swapoff -a
$ sudo fallocate -l 4G /yourswapfile
$ sudo chmod 600 /yourswapfile
$ sudo mkswap /yourswapfile
$ sudo swapon /yourswapfile
$ echo '/yourswapfile swap swap defaults 0 0' | sudo tee -a /etc/fstab
$ free -h

Your Case (4GiB) - Using dd:
Note: Same process you used except smaller bs value (read/write bytes at a time) to reduce possibility of holes in swapfile. I also added status=progress which shows periodic transfer statistics.

$ sudo swapoff -a
$ sudo dd if=/dev/zero of=/yourswapfile bs=1M count=4096 status=progress

Note: If bs=1M count=4096 is too slow, try bs=4M count=1024

6

The use of files for swap space is relatively new for Ubuntu. Previously, when a swap partition was used, the blocks were of course contiguous, but a file has no such guarantee for its blocks. Apparently, there was some effort to allow for gaps, but there may be limits on what can be handled. See the note at the end of the "man mkswap" manual page:

Note that a swap file must not contain any holes. Using cp(1) to create the file is not acceptable. Neither is use of fallocate(1) on file systems that support preallocated files, such as XFS or ext4, or on copy-on-write filesystems like btrfs. It is recommended to use dd(1) and /dev/zero in these cases. Please read notes from swapon(8) before adding a swap file to copy-on-write filesystems. 

The immediate fix is to simply reallocate some partition to dedicate 4G to a swap partition, and mkswap on it.

Had the same problem after default install with ZDF. 2G swap partition - and no more. The solution I've found in this thread: ZFS swap size, and used it even w/o changing paths, just selected another size, 32G:

zfs create -V 8G -b $(getconf PAGESIZE) -o logbias=throughput -o sync=always -o primarycache=metadata -o com.sun:auto-snapshot=false rpool/swap
mkswap -f /dev/zvol/rpool/swap
swapon /dev/zvol/rpool/swap
# IN FSTAB (before the line with current swap file or partition)
/dev/zvol/rpool/swap none swap discard 0 0

Check if it worked for you: free -m

But my new SSD was almost free, so no problem to allocate large continuous files. Doing it on almost full drive after many create/append/shrink/remove file operations could be a problem for sure!!!

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy