15

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
  • 6
    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. Commented Jul 23, 2020 at 10:08
  • The first three commands don't really require sudo. Commented Jul 23, 2020 at 10:11
  • @Archemar the second argument is irrelevant for swap. You can write pretty much anything there. Commented Jul 23, 2020 at 10:43
  • @Archemar I tried with none but as mr. Tashkinov indicated it didn't make any difference Commented Jul 23, 2020 at 11:03
  • 1
    for me the error message was swapon: swapfile has holes and the answer below worked. Commented Aug 19, 2020 at 8:44

5 Answers 5

29

If you're using btrfs it's important that you create your swapfile without copy-on-write.

Since btrfs version 6.1, a swapfile can be created correctly with this command:

btrfs filesystem mkswapfile --size 2G /path/swapfile 

For older btrfs versions, you need to create the swapfile like below, where notably the chattr +C disables copy-on-write. It's important to set this attribute before writing anything to the file:

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

Activate it:

swapon /path/swapfile 

Add it to /etc/fstab with this entry:

/path/swapfile none swap defaults 0 0 

I found it documented in the fine material.

4
  • 3
    Thanks for this! dmesg says: BTRFS warning (device dm-1): swapfile must not be copy-on-write Commented Nov 22, 2022 at 20:41
  • chattr: Operation not supported while setting flags on /swapfile Commented May 14, 2024 at 20:37
  • 2
    @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). Commented May 15, 2024 at 21:18
  • 1
    worked for me as well Commented May 16 at 14:54
13

Please try replacing

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

with

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

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.

1
  • #dd if=/dev/zero of=/home/.swap/swapfile bs=1M count=4096 This comment work with me. Or you can change this command like: #dd if=/dev/zero of=/home/.swap/swapfile bs=1G count=4 Commented May 3 at 10:30
12

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

3
  • 5
    You know what? I specifically registered for this site to upvote you! Commented Nov 3, 2023 at 5:26
  • Good comment, thank you! ISource: btrfs.readthedocs.io/en/latest/Swapfile.html Commented May 25, 2024 at 19:26
  • Simplicity is a virtue. Commented Jun 27 at 1:07
4

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 
0

As other have noted there is a btrfs variant of creating a swapfile:

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

However, even with this when you go to swapon /swapfile/path you will get the same Invalid argument error. The reason is because in some btrfs configurations you are using more than one device, such as with a Just a Bunch of Disks (JBoD) setup.

My strategy was to allocate a large swapfile on a btrfs volume that was backed by many separate disks. This doesn't seem to be possible using this simple configuration. Instead I am investigating safely resizing each btrfs partition so that each disk has a spare 16GB that can be used as a dedicated swap partition.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.