How to NFS root on the raspberry pi


Top of Form

I’d like to start writing a few questionably useful guides for people. We’re going to start with this one cause I find it’s actually quite a handy thing to do.. This is probably for the slightly more experienced user.

I’ll probably be editing this a few times, just to make sure I get it reasonably right, and that the formatting isn’t terrible. Or to add/change the methods of doing things. There’s almost always more than one way to do something in Linux.

So you’ve got your raspberry pi with a 4GB SD card, or like me find yourself saddled with a bunch of otherwise nearly useless 2GB or smaller cards that are really slow. Good news! The raspberry pi really only needs a 64MB boot partition (and a bit of swap if you want). For this guide, I’m going to assume you’re using at least a 4GB card as it makes things somewhat easier. I’m also going to assume that you’re using reserved DHCP addresses for your raspberry pi and other things. I don’t recommend this for those using wireless, wired network only.

It can also make backing up and restoring them a snap. Should you ever find yourself wanting to fully run it off the SD card again, you can just as well copy it back later.

You’ll need a running NFS server to do this. It can be another raspberry pi if you want it to be. I’m not going to go over setting it up at this point. You’ll want to have gparted installed somewhere as well since we’ll be changing the partitioning of the SD card at the end.

 
 

Step 1: Write your raspbian image to the SD card, and do your initial boot and setup as you normally would. Make sure it all works.

 
 

Step 2: Install nfs-common. If it needs to upgrade anything while doing this then let it. We can save a dist-upgrade for later. Run:

sudo apt-get update

sudo apt-get install nfs-common

 
 

Step 3: We need to remove dphys-swapfile. This is a must. Swap generally expects direct access to the file (or partition even), and NFS prevents this. If you don’t do this the system will crash at some point when it starts swapping memory. Run:

sudo apt-get purge dphys-swapfile dphys-config

sudo update-rc.d -f dphys-swapfile remove

Then reboot. Next we need to check that no other swap devices are in use. Run “swapon -s”. You should get a single line like this:

Filename Type Size Used Priority

If that’s the case, you can go ahead and run “sudo rm /var/swapfile” to make sure the dphys-swapfile is gone.

 
 

Step 4: Check/edit /etc/sysctl.conf. This step involves upping vm.min_free_kbytes. When the first raspberry pis came out they had this set to something like 4MB or so. With 256MB RAM every little bit counted. However this seemed to be causing more issues with the network if you used it heavily. So, run “sudo nano /etc/sysctl.conf”. Scroll to the bottom. There will probably be a comment line “# rpi tweaks”. There will probably be a vm.swappiness=1 and a vm.min_free_kbytes line. If not add it:

vm.min_free_kbytes = 16128

This will try and make sure that when your system is busy it still has enough memory to do the things it needs to. Some people have set it to 24576, but I found my issues went away when set to 16128. You can up the swappiness to 40 if you want now as well. We’ll be re-adding swap in later.

 
 

Step 5: Remove the / partition from /etc/fstab. Open up “sudo nano /etc/fstab” find the line that looks like this and remove it:

/dev/mmcblk0p2 / ext4 defaults,noatime 0 1

 
 

Step 6: Copy the contents of the root partition on the SD card to your NFS server. Power off your raspberry pi, and plug it into a reader on your NFS server. This needs to be on a proper linux supported partition, don’t use something exFAT or NTFS.

Make a folder somewhere, you’ll have to decide this on your own. In my case, I’m running it off of some external 2TB drives I setup into a raid1 when I got them back from warranty repair. You can also set the permissions how you want, but you’ll notice the uids probably won’t match up (aside from root). Either way, your sequence of commands is going to look somewhat like this, with extra care on the cp arguments:

mkdir rpi-nfs1

chmod 777 rpi-nfs1

cd rpi-nfs1

sudo cp -rapv /mnt/<SD CARD ROOT LOCATION> .

 
 

Step 7: Add your export to NFS. We need to export the above folder we copied into, with the full path. Run “sudo nano /etc/exports” and add the following line with your corresponding reserved IP address in:

/mnt/external2tb/rpi-nfs1 <IP ADDRESS HERE>(rw,no_root_squash,async,no_subtree_check)

Save and exit. Then export it by running “sudo exportfs -rav”. Make sure it shows up. It’ll say something like this:

exporting <IP ADDRESS HERE>:/mnt/external2tb/rpi-nfs1

 
 

Step 8: Still with me? Still got that SD card inserted into your NFS server? Good. Now we’re going to edit some files on the boot partition of the SD card. MAKE SURE YOU’RE EDITING THE ONES ON THE SD CARD AND NOT THE COMPUTER YOU’RE USING. First file that needs some fixing up is /boot/cmdline.txt. This file is important, and it needs to all be on one line, no returns to make it look pretty.

Yours probably looks exactly like this:

dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait

We now need to tell it we’re using NFS, and where it is, with a few other things. You’ll want to make yours look more like this:

dwc_otg.lpm_enable=0 console=tty1 root=/dev/nfs nfsroot=<YOUR NFS SERVER IP>:/mnt/external2tb/rpi-nfs1,tcp ip=dhcp rootfstype=nfs smsc95xx.turbo_mode=N elevator=deadline rootwait

We’ve changed the root and the rootfstype. We also add in ip=dhcp so that the raspberry pi knows to get it’s IP by dhcp. We’ve also put in smsc95xx.turbo_mode=N, which is a fix for some issues the network chip has/had while under heavy loads. We want this thing rock solid when we’re done.

 
 

Step 9: Remove the / ext4 partition from the SD card, convert to swap if desired. You can skip this part if you want, but I recommend doing it. Open up gparted. Select your SD card. Select the 2nd, much larger partition. Click Partition->Format To>Linux-Swap. Once you think you’ve got it right, click the “Apply All Operations” checkmark, or select Edit>”Apply All Operations”

 
 

Step 10: Almost done. Panic and worry that you forgot something in the above steps.

 
 

Step 11: Unmount/eject the SD card from the computer you’ve been copying/editing on. Stick it into your raspberry pi and turn it on. If you’re lucky it should all start up and run just like before, except over the network now.

 
 

Step 12: What about that swap partition? Lets re-enable swap. Run “ls -l /dev/disk/by-id”. You’ll get an output like this:

lrwxrwxrwx 1 root root 15 May 28 15:14 49273d6c-2a19-4dbf-a7b5-58075bc6a1c2 -> ../../mmcblk0p2

lrwxrwxrwx 1 root root 15 May 8 15:40 C522-EA52 -> ../../mmcblk0p1

mmcblk0p2 should be the swap partition. Lets check. Run “sudo fdisk -l”. You should get something like this:

Device Boot Start End Blocks Id System

/dev/mmcblk0p1 8192 122879 57344 c W95 FAT32 (LBA)

/dev/mmcblk0p2 122880 3788799 1832960 82 Linux swap / Solaris

Okay! Now we take that long hex number we got from the ls, and use that in /etc/fstab. That’s our UUID for the swap partition. Add the line to /etc/fstab, yours will have a different ID number, but otherwise it’s the same:

UUID=49273d6c-2a19-4dbf-a7b5-58075bc6a1c2 none swap sw 0 0

Once that’s in your fstab file, run “sudo swapon -a”. Then check it. Run “swapon -s”. You should see it’s now using the leftover space on the SD card as swap. Something like this, except this is from a 2GB card:

Filename Type Size Used Priority

/dev/mmcblk0p2 partition 1832956 0 -1

 
 

Step 13: All done. Run your sudo apt-get update, sudo apt-get dist-upgrade, sudo apt-get clean and enjoy your fancy new NFS root using raspberry pi. Check your disk usage by running “df -h”. You’ll now get something like this, the X’s replaced by your NFS server’s IP address.

Filesystem Size Used Avail Use% Mounted on

rootfs 1.8T 70G 1.7T 5% /

192.168.X.XXX:/mnt/external2tb/rpi-nfs1 1.8T 70G 1.7T 5% /

devtmpfs 87M 0 87M 0% /dev

tmpfs 19M 184K 18M 1% /run

tmpfs 5.0M 0 5.0M 0% /run/lock

tmpfs 395M 0 395M 0% /run/shm

/dev/mmcblk0p1 56M 19M 38M 34% /boot

Look at that! Suddenly I’ve got 1.7TB to work with now!

 
 

Feel free to complain that I should’ve written a proper guide on how to software raid & samba/nfs on the raspberry pi first. NFS/samba setups are boring, this is way more fun. So you get this one first.

edit: With the Jessie update you need to add ‘rw’ to your cmdline.txt after the ‘nfsroot=***’ bit. Otherwise it will mount it read only.

 

Reference:
https://www.reddit.com/r/raspberry_pi/comments/37n9tb/how_to_nfs_root_on_the_raspberry_pi/Bottom of Form

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s