4

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?

Avio
  • 957

4 Answers4

12

I know this is a bit of an old thread now but I've just run across this problem and found this it is possible to use an NFS swap partition by making use of the kernel loopback device:

Make an empty file (fill with zeros from /dev/zero):

root@machine:/# dd if=/dev/zero of=/swap bs=1024 count=1048576

Make a loop device:

root@machine:/# losetup /dev/loop0 /swap

Make it suitable for swapping:

root@machine:/# mkswap /dev/loop0
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=931d6e04-78ec-41fd-ab2c-22522ac2711d

Now use it:

root@machine:/# swapon /dev/loop0

Make the change permanent

Write a script to make this swap permanent. /etc/init.d/swap may be like this:

#!/bin/sh

set -e

case "$1" in
start)
losetup /dev/loop0 /swap
swapon /dev/loop0
;;
stop)
swapoff /dev/loop0
losetup -d /dev/loop0
;;
restart)
swapoff /dev/loop0
swapon /dev/loop0
;;
*)
echo "Usage: swap { start | stop | restart }" >&2
exit 1
;;
esac

exit 0

Make the file executable:

root@machine:/# chmod +x /etc/init.d/swap

Enable it:

root@machine:/# update-rc.d swap defaults

Source: http://www.emanuelis.eu/2010/06/21/how-to-swap-to-nfs-mount/

3

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.

1

If you could do it as you describe I should point out that you're not using another machines unused RAM. Swap files are a file based backing for local faster RAM. You'd be using up the other machines hard disk space.

You can't do it because Linux has a special partition type for swap, it must be on a local drive, not a network share. Windows doesn't do it this way, it uses just a file on an existing filesystem. If I recall correctly, that file also must be on a local drive. So it can't be on NFS or SMB.

However, I think you may be able to do it with iSCSI. If you mount an iSCSI target locally and provide a swap within it may work. Is it a good idea? no. I think it's a terrible idea for reasons of performance and reliability. Most likely Linux will freeze if the network goes down and it needs to access the swap partition. You should get better performance with AoE. But again, one issue with the network or the other machine and you're in for a bumpy ride.

Just buy more RAM, or if you can't fit anymore, get an SSD and create a swap partition on it.

EDIT: Ok you could do it. Still not recommended but this is one way to achieve it in theory.

  1. Create a tmpfs or ramfs on the remote host.
  2. Format the tmpfs or ramfs as ext4.
  3. Create a raw disk image file on it. (dd or truncate will do it)
  4. Export the disk image with iscsi or AoE.
  5. On your local linux PC, connect the target. Then create the swap on the target with mkswap.
  6. Then swapon.

Good luck!

hookenz
  • 4,095
0

I'm sharing the RAM of another machine via a swap over NBD

host/server

Setup ramfs (if needed, if not just skip to nbd)

sudo mkdir /mnt/ram
sudo mount -t tmpfs -o size=16G tmpfs /mnt/ram

Config file /etc/nbd-server/config:

[generic]
  user = nbd
  group = nbd
[swap]
  exportname = /mnt/ram/swap
  copyonwrite = false
  prerun = truncate -s 16G /mnt/ram/swap && mkswap /mnt/ram/swap

Start NBD

sudo systemctl start nbd

client

sudo nbd-client 192.168.1.3 -name swap /dev/nbd0
sudo swapon /dev/nbd0
brauliobo
  • 131