Linux Swap File – Why Using Swap File Over SMB/NFS Mounted Filesystem is Not Possible

linuxnetwork-sharesramdiskswapswap-file

I'd like to use another machine's unused RAM as swapspace for my primary Linux installation. I was just curious about performance of network ramdisks compared to local (slow) mechanical hard disks.

The swapfile is on a tmpfs mountpoint and is shared through samba. However, every time I try to issue:

swapon /mnt/ramswap/swapfile

I get:

swapon: /mnt/ramswap/swapfile: swapon failed: Invalid argument

and in dmesg I read:

[ 9569.806483] swapon: swapfile has holes

I've tried to allocate the swapfile with dd if=/dev/zero of=swapfile bs=1024 (but also =4096 and =1048576) and with truncate -s 2G (both followed by mkswap swapfile) but the result is always the same.

In this post (dated back to 2002) someone says that using a swapfile over NFS/SMB is not possible in Linux. Is this statement still valid? And if yes, what is the reason of this choice and is there any workaround to have this working?

Best Answer

To quote the man page...

This is due to the swap file implementation in the kernel expecting to be able to write to the file directly, without the assistance of the file system.

It's not possible to directly write to an NFS mount without going through the filesystem, so you get this slightly confusing error message.

As @MattH noted, you might be able to use iSCSI to do this, but the real question is whether it's worth it at all. RAM is quite cheap, after all.

Related Question