Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
1  
I can see this making your system extremely, extremely unstable. The slightest blip in the network could cause system-critical processes to crash if parts of them get paged out. Not to mention anyone that can hack into your Wifi can potentially read any memory that happens to get paged out. –  Darth Android Oct 31 '12 at 21:28
    
I don't use WiFi, but a short network cable that connects two Gbps Ethernet cards. The OSI pile has several checksum fields layered on top of each other that should be enough to prevent most faults and corruptions. For the moment I'd like just to know if and how this is possible. I will think about stability and security implications later on. –  Avio Oct 31 '12 at 21:43
1  
add comment

2 Answers 2

up vote 2 down vote accepted

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.

share|improve this answer
    
Thank you for the explanation. Put in this way it makes sense, even if I continue to see it as a limitation. I don't know if its worth or not. But if I can't even try it out, I will never know. –  Avio Nov 1 '12 at 20:05
add comment

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!

share|improve this answer
    
Probably I forgot to mention it, but this experiment is aimed at using the existing hardware in a more rational way. As far as I can understand about iSCSI, it is simply overly expensive for what I'm trying to do. And I wouldn't be using another machine's hdd because the swapfile is created inside a tmpfs mountpoint, so it's basically inside a ramdrive. –  Avio Oct 31 '12 at 21:57
1  
@Avio There is nothing "rational" about this :) –  Darth Android Oct 31 '12 at 22:00
    
Linux gives you the freedom to use every kind of media for every kind of purpose: it's up to you to decide if the game’s not worth the candle. Now tell me why I can use a floppy as swapspace but I can't use a network ramdrive. :P –  Avio Oct 31 '12 at 22:12
    
iscsi is not expensive, there is target software that's as open as linux itself. –  Matt H Oct 31 '12 at 22:17
1  
Well it's an interesting idea. Worth trying out. I look forward to the day when RDMA is standard and we are able to use other machines unused ram in a reliable way. In theory... why not? –  Matt H Oct 31 '12 at 23:19
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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