27

What's wrong with this sequence of operation, running on a Debian Squeeze system with a Btrfs filesystem?

$ dd if=/dev/zero of=swapfile2 bs=1024 count=524288
$ sudo mkswap swapfile2
$ sudo chown root:root swapfile2
$ sudo chmod 0600 swapfile2  
$ sudo swapon -v -f swapfile2
swapon on swapfile2
swapon: /home/mathieu/swapfile2: found swap signature: version 1, page-size 4, same byte order
swapon: /home/mathieu/swapfile2: pagesize=4096, swapsize=536870912, devsize=536870912
swapon: swapfile2: swapon failed: Invalid argument

My file system is:

$ mount
/dev/mapper/voxbox-root on / type btrfs (rw)
tmpfs on /lib/init/rw type tmpfs (rw,nosuid,mode=0755)
proc on /proc type proc (rw,noexec,nosuid,nodev)
sysfs on /sys type sysfs (rw,noexec,nosuid,nodev)
udev on /dev type tmpfs (rw,mode=0755)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=620)
/dev/mapper/voxbox-boot on /boot type ext2 (rw)
fusectl on /sys/fs/fuse/connections type fusectl (rw)
3
  • malat, I've reopened your question & added your answer as below. Feel free to post your answer, I'll delete mine.
    – Sathyajith Bhat
    Jan 22, 2013 at 9:59
  • Related: How to create swap file on ext4.
    – kenorb
    Mar 7, 2018 at 18:10
  • After btrfs-convert an ext4 root partition to btrfs, I encountered same error when booting. I have swap file in that ext4 root partition. Oct 22, 2021 at 1:53

7 Answers 7

27

This bug report leads to this discussion

So "Invalid argument" should be read as "Your filesystem do not support swap file"

as posted by malat

3
  • 13
    Nice catch! The btrfs filesystem cannot support swap files because it moves file data around and the Linux swap code only takes the swap file's mapping once. Catastrophe would occur if this is allowed -- random file data that used blocks that the swap file once used would get corrupted. Jan 22, 2013 at 9:52
  • 1
    newer swapon man page document a new trick "One possible workaround is to map the file to a loopback device". I have not tried it yet.
    – malat
    Jan 22, 2013 at 12:20
  • Links in the quote section seems dead. Oct 22, 2021 at 1:54
16

In Linux kernel 5.0.0, btrfs natively supports swapfile now. (You must set it as no-COW)

Here's some information from kernel.org: https://btrfs.wiki.kernel.org/index.php/FAQ#Does_btrfs_support_swap_files.3F

From kernel 5.0+ btrfs have native swap files support, but with some limitations. Swap file - must be fully allocated as NOCOW with no compression on one device.

This link shows how to do it: https://wiki.archlinux.org/index.php/swap#Swap_file .

DO NOT TRY IT IN LINUX < 4.21 !!!

Warning: Btrfs on Linux kernel before version 5.0 does not support swap files. Failure to heed this warning may result in file system corruption. While a swap file may be used on Btrfs when mounted through a loop device, this will result in severely degraded swap performance.

0
10

I got the cryptic error now after an unclean shutdown. The only solution was to delete the old swapfile and recreate it.

As on btrfs, there are certain restrictions (e.g. copy-on-write has to be disabled), I recommend to follow the steps on the Arch Wiki (I copied the steps to create a 512M swap, but please read their documentation before executing):

# truncate -s 0 /swapfile
# chattr +C /swapfile
# btrfs property set /swapfile compression none
# fallocate -l 512M /swapfile
# chmod 600 /swapfile
# mkswap /swapfile

Now swapon /swapfile should succeed.

If not, ensure that the Kernel is at least 5.0, as is earlier Kernels swapfiles are not supported in btrfs (source: btrfs FAQ):

From kernel 5.0+ btrfs have native swap files support, but with some limitations. Swap file - must be fully allocated as NOCOW with no compression on one device.

For kernels before 5.0, swap files are not supported. Just making a file NOCOW does not help, swap file support relies on one function that btrfs intentionally does not implement due to potential corruptions. The swap implementation used to rely on some assumptions which may not hold in btrfs, like block numbers in the swap file while btrfs has a different block number mapping in case of multiple devices. There is a new API that could be used to port swap to btrfs; for more details have a look at project ideas#Swap file support.

2

If you want to swap on a file anyway, just use btrfs-swapon

2
  • I came to the same solution, but I have concerns about correctness of such approach. Ex. with loop dev I cane work around holes check created with truncate -s 4G swapfile_holes.img. I.e. the reason why swapon checks for something is that swapfile should fulfill some requirements for reasonable performance.
    – ony
    Jan 18, 2015 at 10:23
  • 1
    And just a moment ago I decided to avoid using losetup mapping. My system got stuck after starting using swap through losetup on btrfs (with pre-allocated file). Maybe it interfere somehow with buffers and caches. I.e. system wants to free memory and swaps something out, but this results in increasing cache for the same size. Though I'm not sure, I'd avoid that.
    – ony
    Jan 18, 2015 at 11:05
2

Since version 6.1 it has become easier to create a swapfile in btrfs:

# btrfs filesystem mkswapfile --size 2G swapfile
# swapon swapfile

More in the btrfs documentation.

0

Update April 2019: Please see recolic's answer, which claims that Btrfs started supporting swap as of Linux kernel version 5.0.0.

Original answer:

Sathya's answer seems to contains dead links.

I found this in the Btrfs FAQ:

Does btrfs support swap files?

Currently no. Just making a file NOCOW does not help, swap file support relies on one function that btrfs intentionally does not implement due to potential corruptions. The swap implementation used to rely on some assumptions which may not hold in btrfs, like block numbers in the swap file while btrfs has a different block number mapping in case of multiple devices. There is a new API that could be used to port swap to btrfs; for more details have a look at project ideas#Swap file support.

A workaround, albeit with poor performance, is to mount a swap file via a loop device.

Source: https://btrfs.wiki.kernel.org/index.php/FAQ#Does_btrfs_support_swap_files.3F

2
  • the FAQ also says that swap on Btrfs is supported since Linux kernel 5.0
    – phuclv
    Apr 23, 2019 at 8:25
  • @phuclv: Yay, progress!
    – mpb
    Apr 24, 2019 at 16:47
-3

Invalid argument says that something is wrong in arguments for swapon command.

First of all, read man swapon.

Try sudo swapon swapfile2. In my case it works.

1
  • 7
    Do you possibly think, I could find -v and -f flag without reading the man page ?
    – malat
    Jan 21, 2013 at 8:33

You must log in to answer this question.

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