Do you have a couple of external hard drives lying around and a Raspberry Pi? Make a cheap, low powered networked attached storage device out of them. While the end result certainly won’t be as impressive as a $500 NAS device like the Synology DiskStation, it will give you a low-powered bit of network storage – particularly useful if you’re getting weary of having all your data rifled through by the prying eyes of the NSA – you can hide this in the attic.
You’ll need a Raspberry Pi, of course, and one or two spare drives. Smaller 2.5″ drives can be powered directly over USB, but we’re going to need a powered hub as the power provided over the RPi’s USB ports is just not enough for them. Alternatively, you can use a USB thumbdrive, or even an SD card. In fact, I’ve used a mix of a USB hard drive and a thumbdrive today, but the procedure is identical.
With just one drive, you can still make a shared network storage area, but with two you’ll be able to setup data redundancy in case one fails.
Prepare Your Drives
Start by formatting your drives as NTFS from a desktop. This is for convenience, so that if anything goes wrong we’ll be able to disconnect them from the NAS and still read the data from any PC.
We could format them from the Raspberry Pi, but it will take a few hours and is far quicker to perform from a desktop. Do that now.
To configure SSH and enable the root user, first create a password for root user:
(type your password)
Then run the raspi-config script from the command line, either using sudo or having logged out and in again as root. From advanced options menu, enable SSH.
After restarting, you should be able to login from another networked machine using (use Putty if you’re on Windows)
SSH root@[IP address]
Once logged in, figure out which devices are your additional drives. I’m assuming you’ll be using two for data redundancy. Type
to list the attached storage devices. You should see something like this.
the /dev/mmc partitions are you Pi operating system, mmc referring to the SD card. Confusingly, the /dev/sda1 and /dev/sdb1 are actually nothing to do with the SD card, and those are in fact your attached USB drives. (Originally, “SCSI device”, but now means any attached SATA or storage device)
Install ntfs-3g for linux so we can access the NTFS formatted Windows drives.
apt-get install ntfs-3g
Next, create directories to use as mount points, then mount the drives. I’m keeping it simple here and referring to them as 1 and 2.
mkdir /media/1 mkdir /media/2 mount -t auto /dev/sda1 /media/1 mount -t auto /dev/sdb1 /media/2 mkdir /media/1/shares mkdir /media/2/shares
Next, we’ll set up Samba. Samba is the network sharing protocol used by Windows (and the newest OSX Mavericks, in fact).
apt-get install samba apt-get install samba-common-bin cp /etc/samba/smb.conf /etc/samba/smb.conf.bak nano /etc/samba/smb.conf
If you’re not familiar with these kind of config files, a # at the start of the line means it is commented out, and therefore not currently set or configured. To enable something, you can either add a new line, or un-comment an existing line to make it active.
We’ll start by enabling user security; press CTRL-W and type “security” to find the relevant section. Remove the # symbol from the line that says
security = user
Lastly, scroll down to the bottom (or hold CTRL V until you reach there) and add as many network shares as you like. Use the following format:
[test] comment = Test share path = /media/1/shares valid users = @users force group = users create mask = 0660 directory mask = 0771 read only = no
Only refer to the first mounted drive though – we’ll be syncing this later with the 2nd share to provide redundancy.
Once you’re done, hit CTRL X and then y to save.
Then restart Samba with the following command.
service samba restart
Now, add a new user to your Pi, assuming you don’t want the same login (substitute “jamie” for your own user)
useradd jamie -m -G users
After typing in the following command, you’ll be prompted to enter a password for your user, and confirm it.
Then we can go ahead and add this system user to Samba. You’ll need to confirm your password again, twice.
smbpasswd -a jamie
Go ahead and test the network share now – it should be visible from your other machines (Windows or Mac), and you should be able to write files to it.
The only problem at this point is that the drives will be unmounted when you restart the Pi. To solve this, install autofs.
apt-get install autofs nano /etc/auto.master
Add the following line underneath +auto.master
/media/ /etc/auto.ext-usb --timeout=10,defaults,user,exec,uid=1000
Now you should be able to restart safely without breaking everything
Assuming you installed two drives, we can now setup an automatic script for syncing data from the 1st drive to the 2nd, thereby offering us a backup in case one fails. We’ll use the rsync utility for this.
apt-get install rsync crontab -e
The crontab in linux is a way of automating tasks; I talked briefly about before when showing you how to automate site backups. Add following line:
30 5 * * * rsync -av --delete /media/1/shares /media/2/shares/
The numbering scheme is used like this:
minute | hour | day-of-the-month | month | day-of-the-week
So in our newly added line, the rsync command wil be run at 5:30 am , every day (the * wildcard meaning “every”, so “every day of every month”)
If you want to go ahead and run the backup immediately, just paste in the rsync command like so
rsync -av --delete /media/1/shares /media/2/shares/
Depending on what you put in the shared folder, it may take a few seconds or longer to give you a report. The great thing about rsync is that it knows which files are updated, added or should be deleted. Go ahead and try the same command again. It should finish instantly, because it knows nothing has changed.
That’s it, finished – you now have your own quick and dirty NAS. Yes, it might not have all the flashy features of a proper NAS, but it gets the job done nicely and for much less power consumption.
Are you having problems? Let us know in the comments and I’ll see what I can do, but please ensure you’re running the latest Raspian image.