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)

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

  • 4
    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. – David Schwartz Jan 22 '13 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 '13 at 12:20

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

  • 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 '15 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 '15 at 11:05

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.

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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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