TransWikia.com

Swapfile Swapon invalid argument

Unix & Linux Asked by simernes on December 10, 2021

I had swap from a swapfile working for quite some time, but for some reason it stopped working.

sudo fallocate -l 4G /home/.swap/swapfile
sudo chmod 600 /home/.swap/swapfile
sudo mkswap /home/.swap/swapfile

# /etc/fstab
/home/.swap/swapfile swap swap defaults 0 0

sudo swapon -a

swapon: /home/.swap/swapfile: swapon failed: Invalid argument

I’m running the newest version of Fedora, so is it maybe possible something has changed with an update or what could be the reason?

2 Answers

Building on the accepted answer with an explanation:  Quoting from swapon(8):

Files with holes

    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 files with holes or on copy-on-write files on filesystems like Btrfs.

    Commands like cp(1) or truncate(1) create files with holes.  These files will be rejected by swapon.

    Preallocated files created by fallocate(1) may be interpreted as files with holes too depending of the filesystem.  Preallocated swap files are supported on XFS since Linux 4.18.

    The most portable solution to create a swap file is to use dd(1) and /dev/zero.

The italicized note seems to explain everything.  Unfortunately, even with verbose output, swapon doesn't mention the cause (files with holes) of failure.

Answered by valecs on December 10, 2021

Please try replacing

fallocate -l 4G /home/.swap/swapfile

with

dd if=/dev/zero of=/home/.swap/swapfile bs=1M count=4096

Answered by Artem S. Tashkinov on December 10, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP