How to write to a NAS (Network Attached Storage) drive from a Raspberry Pi
This post applies to a WD MyCloud 2TB NAS (Network Attached Storage) but the process should be similar for other brands. NAS drives connect to your wi-fi router via ethernet cable and are available to other laptops and computers connected to your wi-fi network. So for example, you could set up your Mac laptops and desktops anywhere in your house to back up to these drives using Apple’s Time Machine without having to physically connect a drive to the Macs.
My main goal was to write to the drive from a few Raspberry Pi‘s. I’ve built motion sensing security cameras with the pi’s (more on this in a future post). This gives much more storage than the pi’s SD cards and allows me to view the images without having to copy them to a computer first. The Mac mounts the NAS drive and it appears in the finder. You can also make the WD MyCloud visible outside your home network but I don’t turn that on unless I’m traveling.
All of my Raspberry Pi’s are running Raspbian Wheezy.
Here are the steps I took to get this to work:
1- Follow the directions in the WD MyCloud product package to install the drive and the WD Setup software to initially configure it. I’m assuming your Pi’s are all set up with wi-fi access to your network.
2- Next, download and install these two: WD Quick View and WD My Cloud. The first one makes it easy to launch the dashboard from your Mac menu bar and the dashboard lets you configure the drive, for example we’ll need to enable SSH access to the drive (these things are actually little Debian servers).
3- Make sure you can see the drive in the Finder on any Mac connected to your wi-fi network.
4- Click on the WD Quick View icon on your Mac menu and launch the MyCloud dashboard and click on the Settings icon:
5- Next, click on Network on the left and then under Network Services, enable SSH. IMPORTANT: write down the username and password in the pop-up window. This will allow you to connect to the server and enable NFS (network file sharing). This is key to writing to the drive from the Raspberry Pi.
6- On any computer connected to your home network open a Terminal window and connect to the NAS:
Where “xxx.xx.x.x” is the IP address of the NAS drive (this will be on the same screen as step 5 above).
7- Next we’ll edit the exports file with this command:
sudo nano /etc/exports
Add this line to the bottom of the file:
Where “xxx.xx.xx.x” is the IP address of your router. If you have an Apple router the easiest way to determine it’s IP address is to launch AirPort Utility and click on the router. In my case I have the NAS drive connected to an Apple AirPort Extreme:
When finished hit CTRL X and press ‘Y’ and enter to save and exit.
8- Next step is to SSH into your Raspberry Pi. I’m going to assume you know how to do that. The next step is to edit /etc/fstab on the pi and tell it to mount the NAS drive as a path you’ll use on the pi. For example, if you want to write files on the pi to a directory called /tmp/myfiles, then we’ll mount the NAS as /tmp/myfiles. Any file you write on the pi to /tmp/myfiles will actually be written to the NAS. So type this to edit /etc/fstab on the pi:
sudo nano /etc/fstab
Add this line to the bottom of the file:
xx.x.x.xx:/nfs/Public/myfiles /tmp/myfiles nouser,atime,auto,rw,dev,exec,suid 0 0
Where “xx.x.x.xx” is the IP address of the NAS drive you used in step 6 above. Hit CTRL X and ‘Y’ and Enter to save and exit.
8- Next, this command will mount all partitions in your fstab file:
sudo mount -a
You might get an error, if you do, type this:
sudo rpcbind start
Then repeat the “sudo mount -a” line again.
9- Test this by writing a file to /tmp/myfiles (in this example) and you should see that file on the NAS from any Mac on your network.
That’s it. You should now be able to access the NAS drive from the pi by simply accessing the directory on the pi you assigned as the mount point. This will make more sense if you do a “df -h” on the pi you’ve connected to via SSH. You’ll see the NAS file system and the mount.