8

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

5 Answers 5

11

Please try replacing

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

with

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

0
10

If you're using btrfs you should create your swapfile without copy-on-write. This is done with chattr +C. It's important to set this attribute 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.

3
  • 3
    Thanks for this! dmesg says: BTRFS warning (device dm-1): swapfile must not be copy-on-write Nov 22, 2022 at 20:41
  • chattr: Operation not supported while setting flags on /swapfile
    – Francois
    May 14 at 20:37
  • 1
    @Francois You're obviously doing something wrong. Try a search engine. But you're likely not on a btrfs formatted device (check sudo blkid) or manipulating a symlink (check ls -l /swapfile). May 15 at 21:18
4

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
4

Quite old question but I just ran into the same problem an nothing that was discussed here worked for me, but i found this in the btrfs filesystem manpage :

mkswapfile [-s size] file
       Create a new file that's suitable and formatted as a swapfile. Default size is  2GiB,  fixed  page
       size 4KiB, minimum size is 40KiB.

       A  swapfile  must  be  created  in a specific way: NOCOW and preallocated.  Subvolume containing a
       swapfile cannot be snapshotted and blocks of an activated swapfile cannot be balanced.

       Swapfile creation can be achieved by standalone commands too. Activation needs to be done by  com‐
       mand  swapon(8). See also command btrfs inspect-internal map-swapfile and the Swapfile feature de‐
       scription.

       NOTE:
          The command is a simplified version of 'mkswap', if you want to set label, page size, or  other
          parameters please use 'mkswap' proper.

       Options

       -s|--size SIZE
              Create swapfile of a given size SIZE (accepting k/m/g/e/p suffix).

       -U|--uuid UUID
              specify  UUID  to use, or a special value: clear (all zeros), random, time (time-based ran‐
              dom)

So i did this :

sudo btrfs filesystem mkswapfile --size 12g --uuid clear /swapfile/path

And it worked perfectly

Obviously your filesystem must be btrfs, which is the default in Fedora if I'm not mistaken

2
  • 3
    You know what? I specifically registered for this site to upvote you!
    – Hentioe
    Nov 3, 2023 at 5:26
  • Good comment, thank you! ISource: btrfs.readthedocs.io/en/latest/Swapfile.html 7 hours ago
0

Please, check the updated BTRFS filesystem documentation for new kernels (> v.6.1): https://btrfs.readthedocs.io/en/latest/Swapfile.html

It should help you to solve all the problems with making a swapfile on BTRFS.

My setup: Debian 12, kernel v.6.1, btrfs, dm-crypt, SSD

# btrfs filesystem mkswapfile --size 2G swapfile
# swapon swapfile
New contributor
Dmitry Somov is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .