Connecting your Raspberry Pi to Network Storage at Boot

Connecting your Raspberry Pi to Network Storage at Boot

In my last tutorial I told you how you can access a network drive from your Raspberry Pi, but when you turn off your Raspberry Pi, that network drive would disappear. What about if you want to be able to access that network drive every time your Raspberry Pi is turned on? Of course, that is possible. I am assuming that your Network Attached Storage (or NAS) is Linux-based, or is sharing NFS storage. Most are - all three of my NAS drives, all from different manufacturers, are Linux-based.

In the Linux world, the task of connecting to a network drive is known as 'mounting' the drive. This is achieved using the 'mount' command. Your Raspberry Pi already has a number of 'drives' mounted, as you can see using the mount command on it's own:

mount

Which will list (at least) the following:

/dev/root on / type ext4 (rw,noatime,data=ordered)
devtmpfs on /dev type devtmpfs (rw,relatime,size=437904k,nr_inodes=109476,mode=755)
tmpfs on /run type tmpfs (rw,nosuid,noexec,relatime,size=88440k,mode=755)
tmpfs on /run/lock type tmpfs (rw,nosuid,nodev,noexec,relatime,size=5120k)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /run/shm type tmpfs (rw,nosuid,nodev,noexec,relatime,size=176860k)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
/dev/mmcblk0p1 on /boot type vfat (rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,errors=remount-ro)

These are, of course, all on the SD card of your Raspberry Pi. I'm not going to explain them all as it's not necessary to know what each of them do at this stage. What we want to do is to add another entry to this list.

Making a Mount Point

To 'mount' a network storage folder on your Raspberry Pi, you must first create a folder onto which you will 'mount' the external network drive. This is the location on your Raspberry Pi where you will find all the files from your network storage. For example, go to your home folder and create a new folder with:

cd ~
mkdir MyNAS

This directory can be anywhere on your Raspberry Pi, and is often in the /media or /mnt directories.

Turning on File Locking

Being a secure operating system, Linux has the concept of 'locking' files when they are being accessed by someone. You are able to mount shared folders without this facility, but I recommend that you use it to safeguard your files. You need to turn on the 'rpcbind' service, which is not on by default. Do this with:

sudo update-rc.d rpcbind enable

Preparing your NAS

All Network Attached Storage drives will have some sort of security built in. This means that it will be able to limit access to files and folders within your home network. If you want your Raspberry Pi to access any files or folders on the NAS then you will have to 'open' access to those files and folders. How you do this will depend on the NAS you have. I have three here at home, and each one is different. Depending on what you are sharing, the way you share those files and folders will be different. If, for example, you want to share your music of videos, then you may feel that it's okay for anyone on your home network to see them. If you have files that you don't want others to see, then you need to share those only with certain user accounts.

What I'm going to assume here is that you are going to either share music/video with everyone, or any files that your Raspberry Pi is going to create are okay for all to see. This means that you need to make a folder and all folders and files under it either 'public' or accessible by a 'guest' account. How you do this, as I say, is dependent on your NAS.

Defining the Mount

There is a file in the /etc folder called fstab which defines the 'file system table'. In other words, it tells the Raspberry Pi what storage should be mounted every time your Raspberry Pi boots. You can include your network drive in this file. But before you edit it, back it up as it is quite easy to mess up your Raspberry Pi by inadvertently changing something you shouldn't in this file.

sudo cp /etc/fstab /etc/fstab.backup

This is an important step. While creating this tutorial I edited the fstab on my IQaudIO Volumio Raspberry Pi2 which is permanently connected to my NAS that shares my music files. Unfortunately I rebooted it with an error in the fstab file, which stopped it from booting correctly. For many, this would be a disaster, but fortunately I was able to take the microSD card from this Raspberry Pi and add it as an external drive on my beta-Raspberry Pi2 and was able to restore my backup fstab file! The day was saved.

Now we can edit fstab:

sudo nano /etc/fstab

And add a line at the end of the file:

//192.168.13.2/Qmultimedia/Music /home/pi/MyNAS nfs default 0 0

As usual with nano, press Ctrl+X to exit, responding 'Y' to whether you want to save, and press 'Return'.

The folder is not automatically mounted. You have to tell your Raspberry Pi to read the fstab file and mount the drives. Do this with:

sudo mount -a

which will read the fstab file and mount any drives that are not already mounted.

If all has gone well, you should now be able to go to the directory and see what files are there.

cd ~/MyNAS
ls

Which will list all the files and folders that you have shared. If, after a bit of experimenting, you find that something just doesn't work, remember to restore your last fstab file before you reboot and try again by doing:

sudo cp /etc/fstab.backup /etc/fstab

Conclusion

Hopefully you should now have access to a shared folder on your NAS. Using these instructions I have my 'Volumio OS' based Raspberry Pi (with IQaudIO DAC and Amp) connected to my QNAP NAS that serves my music. I have another Raspberry Pi that is my XBMC client that connects to another NAS for my video files.

While I cannot answer all questions, I would like to hear about your experiences with shared network drives. Why not tweet me at @Geeky_Tim - I'd like to hear about your solutions.

Featured Products

Raspberry Pi 3 Model B+ - The Pi HutRaspberry Pi 3 Model B+ - The Pi Hut
Raspberry PiRaspberry Pi 3 Model B+
Sale price £33.60 incl. VAT excl. VAT
Raspberry Pi Zero W - The Pi HutRaspberry Pi Zero W - The Pi Hut
Raspberry PiRaspberry Pi Zero W
Sale price £13.50 incl. VAT excl. VAT

1 comment

Rico

Rico

I think the fstab option should be “defaults” with an s rather than default.

I think the fstab option should be “defaults” with an s rather than default.

Leave a comment

All comments are moderated before being published.

This site is protected by hCaptcha and the hCaptcha Privacy Policy and Terms of Service apply.