Swap on tmpfs (Obviously a bad idea, but is it possible?)

It shouldn't be possible. swapon system call requires readpage and bmap (indirectly) calls being implemented by filesystem:

http://lxr.free-electrons.com/source/mm/swapfile.c?v=4.0#L2412

if (!mapping->a_ops->readpage) {
    error = -EINVAL;
    goto bad_swap;
}   

But none of them are implemented by tmpfs, such an entry is missing from corresponding address_space_operations: http://lxr.free-electrons.com/source/mm/shmem.c?v=4.0#L3104

For the same reason, tmpfs cannot hold loop mounts, and ramfs won't work either (it doesn't have bmap call)


From this Q&A https://superuser.com/questions/539287/swapon-failed-invalid-argument-on-a-linux-system-with-btrfs-filesystem (the original referenced site is not responding):

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

The incompatibility reason I suspect is a "circular dependency". From this article: http://www.jamescoyle.net/knowledge/951-the-difference-between-a-tmpfs-and-ramfs-ram-disk:

These two differences between ramfs and tmpfs make tmpfs much more manageable however this is one major drawback; tmpfs may use SWAP space. If your system runs out of physical RAM, files in your tmpfs partitions may be written to disk based SWAP partitions and will have to be read from disk when the file is next accessed.

Which would be pretty much impossible in your scenario.

It might work with ramfs, tho - which doesn't have this problem.

Tags:

Linux

Swap

Tmpfs