Header Image - charles newman's blog

Category Archives

3 Articles

How to write to a NAS (Network Attached Storage) drive from a Raspberry Pi

by Charles Newman

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).

wd-software-scrnshot

 

 

 

 

 

 

 

 

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:

wd-mycloud-db-settings

 

 

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:

ssh root@xxx.xx.x.x

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:

/nfs/Public xxx.xx.xx.x/24(rw,subtree_check,secure)

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:

Screen-Shot-AirPort

 

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.

How to back up and restore your Raspberry Pi SD card to your Mac

I periodically take a “snapshot” of my Raspberry Pi’s SD cards, especially before I make a major change, by copying the SD card image to one of my Macs.  Subsequently, that image file gets backed up again by Time Machine running on the Mac. This allows me to return the pi to some determinate state if the card gets corrupted or if I mess something up experimenting.

Note these instructions are for Mac OS X but you can probably figure out other platforms by looking at this page.

These are Terminal commands. Therefore…
DISCLAIMER: if you don’t know what you’re doing on the command line leave this post now. I take no responsibility for you obliterating either your Mac or your SD card. I use both of these procedures on a regular basis but again, you need to understand what you’re doing here, don’t just type away blindly.

Back up the SD card to OS X:

  1. Type “diskutil list” to find the SD Card (it’s helpful to do this before you insert the SD card into your reader, it will be obvious which one is the pi’s SD card). If your Mac doesn’t have an SD card reader you can find a USB card reader fairly cheap, such as this one.
  2. Note the disk number of SD card. I’ll use disk2 in this example.
  3. Type “sudo dd if=/dev/disk2 of=~/Desktop/raspberrypi-[date].img” to create a disc image of the SD card on the desktop.
    Obviously you can supply a directory path there other than Desktop.
    Note: If you are running as a non-admin user on a Mac, type “su <adminusername>” and it will prompt you for the password for that account. This will switch your session to an admin session. Then include the sudo in the command above and type the admin user password again.

Restore the SD card from an OS X back up:

  1. Type “diskutil list” before you insert the card so it’s obvious which one is the SD card.
  2. Insert a blank SD card into the Mac’s SD card reader. If your Mac doesn’t have an SD card reader you can find a USB card reader fairly cheap, such this one.
  3. Type “diskutil list” to find the SD Card to be restored. I’ll use disk2 in this example. Again, you need to know what you’re doing here. Don’t blame me if you wipe out your Mac’s system drive. If you don’t understand these commands don’t type them into a terminal window.
  4. Unmount the SD card with this command: “diskutil unmountDisk /dev/disk2”.
  5. Format the SD Card: “sudo newfs_msdos -F 32 /dev/disk2”. Make damn sure you’ve got the correct disk number.
  6. To restore from the SD card image in the backup sample above: “sudo dd if=~/Desktop/raspberrypi-[date].img of=/dev/disk2”. This may take a very long time and you’ll get no feedback while it’s working. Don’t touch anything until you get the command prompt again, it will give you a summary of the blocks written when it’s complete.

HDMI to DVI not working with Raspberry Pi

Looking to re-use an old Samsung SyncMaster 930B monitor and save some money by avoiding the purchase of an HDMI monitor, I bought a cheap HDMI to DVI cable.  I thought I could re-use the old monitor for development on the Raspberry Pi and avoid dumping the dinosaur at an electronics recycle place.

So the cable arrived and I plugged it in, powered up the pi and… …nothing.  It flashed from “digital” to “analog” and back a few times and then more of nothing.  Messed with the monitor settings, nothing.

So I wondered if there might be a config setting on the pi that might help me.  I started reading up on config.txt (I’m running RASPBIAN (Debian Wheezy)). And sure enough, the solution was to uncomment this line in /boot/config.txt and restart the pi:

hdmi_force_hotplug=1

This line forces the pi to use the HDMI port even if it doesn’t detect a connected display.  Works every time now.