Mount NFS server and move SWAP to nfs share on the Raspberry Pi

2 min read

RaspberryPiBefore we can start with this on the Raspberry we need to make sure we have the NFS Server up and running. In my case this is a FreeNas box that has multiple disk running in zfs raid. After the share is set up on the server (/mnt/pool1/pi) we can switch to the Pi.

First we need to make sure the rpcbind service is running and started on bootup.

sudo update-rc.d rpcbind defaults && sudo service rpcbind start
Now we can test if we can mount our share before we add it to our fstab:
sudo mount YourNfsServerIp://mnt/pool1/pi /mnt
Check with df -h if the share is mounted.(normally when it doesn't show an error all should be ok.) My results:
pi@pi ~ $ df -h Filesystem                  Size  Used Avail Use% Mounted on rootfs                      7,4G  2,5G  4,7G  35% / /dev/root                   7,4G  2,5G  4,7G  35% / devtmpfs                    239M     0  239M   0% /dev tmpfs                        49M  260K   49M   1% /run tmpfs                       5,0M     0  5,0M   0% /run/lock tmpfs                        97M     0   97M   0% /run/shm /dev/mmcblk0p1               56M  9,6M   47M  18% /boot  113G  7,5G  106G   7% /mnt
Now that all is working we can add a new line to /etc/fstab  (change the ip with your own.) /mnt    nfs    defaults    0    0
Now that NFS is setup correct we can move the swap to a real drive. Let's start with cleaning and removing the old on the sd-card.
sudo apt-get remove --purge dphys-swapfile -y sudo rm /var/swap sudo rm /etc/init.d/dphys-swapfile sudo update-rc.d dphys-swapfile remove
Now we can create and mount a new file.(1Gb change the value count=1024 if you need a smaller/larger swap)
sudo dd if=/dev/zero of=/mnt/swap bs=1M count=1024 sudo losetup /dev/loop0 /mnt/swap sudo mkswap /dev/loop0 sudo swapon /dev/loop0
Check with free -m if you can see the swap file. Now we can edit our /etc/rc.local and add before exit 0
echo "Setting up loopy/mnt/swap.." sleep 2 losetup /dev/loop0 /mnt/swap mkswap /dev/loop0 swapon /dev/loop0
Have fun.