3

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?

5
  • 4
    Check kernel logs. The kernel can only report a numeric error code to the swapon program, but it can provide more information in its log. Jul 23, 2020 at 10:08
  • The first three commands don't really require sudo. Jul 23, 2020 at 10:11
  • @Archemar the second argument is irrelevant for swap. You can write pretty much anything there. Jul 23, 2020 at 10:43
  • @Archemar I tried with none but as mr. Tashkinov indicated it didn't make any difference
    – simernes
    Jul 23, 2020 at 11:03
  • for me the error message was swapon: swapfile has holes and the answer below worked.
    – lw1.at
    Aug 19, 2020 at 8:44

3 Answers 3

8

Please try replacing

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

with

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

1
  • that worked\\\\
    – simernes
    Jul 23, 2020 at 11:25
1

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.

2
0

If you're using btrfs you should create your swapfile like this. It's important to chattr +C before writing anything to the file:

truncate -s 0 swapfile
chattr +C swapfile
fallocate -l 2G swapfile
chmod 0600 swapfile
mkswap swapfile
swapon swapfile

Add it to /etc/fstab with this entry:

/path/swapfile        none        swap        defaults      0 0

I found it documented in the fine material.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.