As the Raspberry PI uses an SD Card for it’s boot device there are times when you need either more space than is available on that device or a device that’s faster – writing to flash is slow and flash cards do have a limited number of writes that can be made to them.
Now there’s several ways to accomplish this:
- Use an external USB drive (the common route)
- Use a network shared drive
Using a USB drive is simple and is the faster option but it means it’s dedicated to the PI whilst it’s in use, hence this article on using a network drive – in this instance a directory on another Linux box in the network.
Also having it shared on the network means that multiple machines could use it at the same time. Imagine if you are a teacher with a collection of PI’s being used by your students. You could setup a central read-only directory with your class work which they can all access as if it’s installed locally.
For this article I’m going to show how to setup a directory on a server and the raspberry pi as the client.
- tabitha – Ubuntu server that will host the directory
- lindesfarne – Raspberry PI client running the stock Debian SD Image
- kell – second Raspberry PI client also running Debian.
Although the examples will show tabitha and lindesfarne at the end I’ll also show how to copy files between two Raspberry PI’s via the share, kell being the second pi.
Also although I’m using a linux box in this setup if you wanted to use a PI as the server (sharing a usb drive for example) then it’s exactly the same.
Setting up the server
First I presume you are running a current Linux distribution. In this example I’m setting this up on a machine running Ubuntu but these instructions will work on Linux Mint or Debian as well.
First you need to pick a directory which you want to use as shared space. For the purpose of this article I’m going to use /srv/public
tabitha:~$ sudo mkdir -p /srv/public tabitha:~$ sudo chmod 777 /srv/public
Now first we have to use sudo as /srv is under the root directory so only root can create it. Most distributions have a /srv directory already but the -p attribute means that mkdir will create it as well if it doesn’t exist.
Also the chmod is a cheat for this article – it means that any user can not just read but write to that directory. Normally you would limit it to a specific user or group but that’s outside the scope of this article.
Next we need to install the NFS server.
tabitha:~$ sudo apt-get install nfs-kernel-server nfs-common portmap
Now you may already have a couple of these installed but usually nfs-kernel-server isn’t installed by default.
Next you need to configure nfs by adding the directory to /etc/exports
tabitha:~$ sudo vi /etc/exports
and add the following line to the end of the file:
Now what this does is declare that the /srv/public directory is Read Write (rw) and it’s accessible by any machine within the local network – 192.168.2.x in my instance.
Now you could replace the nework definition with a * which means any machine but if it’s accessible from the public internet that’s not a good idea.
Alternatively you could specify a user name instead but this is a bit tricky to set unless your user’s are configured the same on both the server and the client.
Now entries within ( ) are options specific to this share. common entries are:
- rw : Allow clients to read as well as write access
- ro : Read only access
- insecure : Tells the NFS server to use unpriveledged ports (ports > 1024).
- no_subtree_check : If the entire volume (/users) is exported, disabling this check will speed up transfers.
- async : async will speed up transfers.
Now you just need to restart nfs-server:
tabitha:~$ sudo service nfs-kernel-server restart
Now your NFS sever is sharing /srv/public
Mounting the directory on the PI
Now we are going to mount this share on the PI.
First you need to install NFS. If you are using the standard Debian image then you have enough to mount nfs. If in doubt then simply install it as we did for the server – if it’s already there it will just tell you it’s already installed.
Next decide where to mount it. Now you can mount it anywhere you want but I’m going to use the same /srv/public location – an alternative location for example is /mnt/public.
First make the directory as you did on the server.
pi@lindesfarne:~$ sudo mkdir -p /srv/public pi@lindesfarne:~$ sudo chmod 777 /srv/public
Next we’ll mount the share:
pi@lindesfarne:~$ sudo mount tabitha:/srv/public /srv/public
Here tabitha:/srv/public defines the server and the directory on the server whilst the /srv/public on the right is your local mount point. Now it should return almost immediately so next lets look at what we now have mounted:
pi@lindesfarne:~$ df -h Filesystem Size Used Avail Use% Mounted on tmpfs 94M 0 94M 0% /lib/init/rw udev 10M 180K 9.9M 2% /dev mpfs 94M 0 94M 0% /dev/shm rootfs 1.6G 1.3G 257M 83% / /dev/mmcblk0p1 75M 28M 47M 38% /boot /dev/mmcblk0p4 2.0G 42M 1.8G 3% /home tabitha:/srv/public 141G 5.8G 128G 5% /srv/public
Now the machine has 128Gb of space available to it under /srv/public
Right lets test it. First we’ll create a file on the server and see if we can read it on the PI.
peter@tabitha:~$ uname -nmo tabitha i686 GNU/Linux peter@tabitha:~$ uname -nmo >/srv/public/tabitha
Now can we read it:
pi@lindesfarne:~$ cat /srv/public/tabitha tabitha i686 GNU/Linux
Now that one would always work as were reading so next we want to write from the client:
pi@lindesfarne:~$ uname -nmo lindesfarne armv6l GNU/Linux pi@lindesfarne:~$ uname -nmo >/srv/public/lindesfarne
And on the server?
peter@tabitha:~$ cat /srv/public/lindesfarne lindesfarne armv6l GNU/Linux
Using the drive on more than one PI
Ok now to bring in the second pi (kell) and we will copy a file from lindesfarne to it via the share.
First follow the instructions for mounting the drive above on the second pi – you should now have both attached to the server.
Next select a file to copy. For this article we’ll create a dummy file but if you have another locally you could use that instead:
pi@lindesfarne:~$ ( uname -a;df -h) > dummy pi@lindesfarne:~$ cat dummy Linux lindesfarne 3.1.9+ #90 Wed Apr 18 18:23:05 BST 2012 armv6l GNU/Linux Filesystem Size Used Avail Use% Mounted on tmpfs 94M 0 94M 0% /lib/init/rw udev 10M 180K 9.9M 2% /dev tmpfs 94M 0 94M 0% /dev/shm rootfs 1.6G 1.3G 256M 83% / /dev/mmcblk0p1 75M 28M 47M 38% /boot /dev/mmcblk0p4 2.0G 42M 1.8G 3% /home tabitha:/srv/public 141G 5.8G 128G 5% /srv/public
Next copy it to the drive & on the second pi copy it into the home directory
pi@lindesfarne:~$ cp dummy /srv/public/dummy pi@kell:~$ cp /srv/public/dummy
That’s it. View that file and you should see it’s exactly the same.
Unmounting the drive
If you want to unmount the drive then it’s simply a case of:
pi@lindesfarne:~$ sudo umount /srv/public
Mounting the drive on boot
Now this article shows how to mount the drive on the pi – but what if you want to mount it automatically every time you want to boot?
Well all you need to do is to add an entry to /etc/fstab
pi$lindesfarne:~$ sudo vi /etc/fstab
And add the following entry at the end of the file:
tabitha:/srv/public /srv/public nfs defaults 0 0
Thats it. The next time you reboot the pi it will automatically mount that drive.
Personally for the pi’s I leave that out and manually mount it – simply because I might be doing something else with the pi – possibly not connected to the network, but that’s how to do it.