I figured out how make the Raspberry Pi swap to a network storage device. Basically, I create and mount a disk image on the network storage and then create and use a swapfile on that disk image:

mount -t cifs //storageserver/myShare /media/remote
truncate --size=2G /media/remote/raspberry.img
mkfs.ext4 /media/remote/raspberry.img
mount -t ext4 -o loop /media/remote/raspberry.img /media/remotedisk
dd if=/dev/zero of=/media/remotedisk/remoteswap.swap
mkswap /media/remotedisk/remoteswap.swap
swapon /media/remotedisk/remoteswap.swap

I know, this is not the most performant way of swapping, esp. compared with local media. However, it is an easy and cheap way to prevent out-of-memory situations on the Raspberry Pi.

The real questions are:

  • Is this a good idea in terms of stability?
  • Is there even a risk that required filesystem drivers (e.g., cifs) are swapped out and create a deadlock?
  • What are the stability requirements (e.g., latency) to the network and storage system?
  • Just to make the question more clear: Is the stability equivalent to running a diskless system over the network?
    – Black
    Jul 12 '14 at 7:06

Short answer: This is a bad idea overall. And a terrible idea in terms of stability.

Long Answer: Memory swapping is something like a last resort measure when your system will run out of RAM available to load all the programs and their data.

How well the Operating System manages the swapping pages will be a determinant to how fast your system will perform.

So your OS will get one page of the memory of a program and will allocate that page on the disk, and free RAM space for another page, and the algorithm responsible for that will choose which pages should be swapped to disk and which will remain in RAM. If a page that was transferred to disk needs to be used by its owner, the OS will then choose another page and put that other page on the swap file/disk/partition and will put that particular page your program needs back into RAM.

Note that the time needed for this operation in tremendous, compared to RAM-scale times.

Now you will have a REAL PROBLEM if, once the OS tries to locate the page on disk, that page, for any reason, is not available. This will most likely result in a kernel error, and a kernel dump on the screen, crashing the OS.

So, if you have a network issue, and thus your swap file gets disconnected, this will crash the system.

As for your other question: I don't think Linux will ever swap the kernel or its loaded modules to disk, but I am not positive on this one.

  • Has anyone an idea what network latencies may cause kernel panics?
    – Black
    Jul 11 '14 at 17:48
  • It won't just send SIGBUS to the process whose page failed at I/O? It crashes the whole system?
    – doug65536
    Feb 27 '19 at 7:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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