Raspberry PI --- Network Attached Storage (NAS)
Mr. Google has a lot of information on how to build a NAS using a Raspberry Pi. A lot of this information is very confusing and solutions seen to be very complicated solutions.
My approach didn’t seem that
complicated. So I decide to write down my Samba approach.
This was written when I was using
Raspbian Wheezy. The new version of Raspbian (Jessie) uses the dreaded systemd.
This changes all the IP network configuration. I beleive the samba and fstab
still applies.
Generally the installation is to:
1. configure the Pi for a static IP address
2. install a USB drive
3. install & configure SAMBA
……simple, easy, beautiful.
IP Network Configuration
First, we need to modify /etc/networking/interfaces file to setup a static IP address for the Pi.
You don’t have to but I always make a backup before any edits:
>> cd /etc/network
>> cp interfaces interfaces.1
>> sudo vi interfaces
Find the line that looks like:
iface eth0 inet dhcp
Edit the eth0 section to include the static network configuration:
iface eth0 inet static
address 192.168.2.10
network 192.168.2.0
netmask 255.255.255.0
broadcast 192.168.2.255
gateway 192.168.2.1
Notices I picked an easy to remember IP “.10” but you can pick any address in your networks sub-net. My home gateway network is in the 192.168.2.x address space. Most home gateways use 192.168.1.x as the subnet range.
Setting at static IP will mean that you will no longer be getting your DNS setting from the DHCP server. The next step is to statically configure the host name (DNS) resolution:
>> cd /etc/
>> cp resolve.conf resolve.conf.1
>> sudo vi resolve.conf
Use the nameserver directive to specify your name server(s). In my case the Home Gateway performs DNS proxy function, so I point it to the gateway and failover to my service providers public “dns-nb.aliant.net”
nameserver 192.168.2.1
nameserver 142.166.145.137
Last thing to do is set the Pi’s hostname:
>> sudo vi /etc/hostname
You can pick anything you want. In my case it is set mine to “tardis”.
Restart networking with:
>> sudo /etc/init.d/networking restart
or
>> sudo service networking restart
Install USB Drive
First you need to plug it into the USB connector. Pick a USB drive that is externally powered so you won’t having the Pi’s USB plug power the drive.
You need a place to mount the drive. In my case I created a directory in user pi’s home directory:
>> mkdir /home/pi/share
Find out the device id for the drive with the blkid command.
>> sudo blkid
/dev/mmcblk0p1: SEC_TYPE="msdos" UUID="3312-932F" TYPE="vfat"
/dev/mmcblk0p2: UUID="b7b5ddff-ddb4-48dd-84d2-dd47bf00564a" TYPE="ext4"
/dev/sda1: UUID="3d5effff-1456-489e-9dc6-70f4cff0a4b9" TYPE="ext4"
The first two lines are the filesystems on the Pi’s SD card. The third entry tells you that the USB drive is connected on /dev/sda1 It will also give you the UUID for the drive. You will need this long ugly number to configuring the /etc/fstab file.
Try mounting the drive to ensure is working
>> sudo mount –t ext4 /dev/sda1 /home/pi/share
Now hold up…..that command mounts a linux formatted drive. I had previously formatted my drive as ext4, your drive is probably pre-formatted as a Windows drive so try:
>> sudo mount –t ntfs-3g /dev/sda1 /home/pi/share
If that doesn’t work, it will give you an error message, then instead of ntfs-3g try vfat
Assuming that all works, you will want to update the /etc/fstab file so that the drive gets mounted every time you reboot.
>> cd /etc/
>> cp fstab fstab.1
>> sudo vi fstab
Add the line to remount the drive during boot-up:
UUID=3d5effff-1456-489e-9dc6-70f4cff0a4b9/home/pi/share ext4 defaults 0 1
Unless you half a knack for remembering 16 digit hexadecimal numbers, you should probably copy/past the UUID from the “blkid” command.
Install Samba
Generally there are three steps to installing samba; install the samba server, edit the configuration file and set a password for the samba user.
Install the software.
>> sudo apt-get install samba
>> sudo apt-get install samba-common-bin
Next you need to edit the samba configuration file.
>> cd /etc/samba
>> sudo cp smb.conf smb.conf.1
>> sudo vi smb.conf
Add the lines to create a share definition section:
#======================= Share Definitions =======================
;X john 2013/01/22
[share]
path=/home/pi/share
comment=Drive K
browsable=yes
public=yes
writable=yes
follow symlinks=yes
wide links=yes
In this case I have created a share called “share” which is pointed at the directory where the USB is mounted.
Samba has its own password system so we need to create a samba password for user “pi”.
>> smbpasswd –a pi
You can pick any password you want but remember “john the ripper” would suggest at least 9 characters from a mixed character set. Of course for home use I ignore this and use a simple one.
Restart samba server to pickup these changes:
>> sudo /etc/init.d/samba restart
That’s all folks….. fire up a Windoze box and try mapping \\192.168.2.10\share as a network drive. Or have a look on your Mac’s Finder application and you should see it listed under the Pi’s hostname. When you connect to the share make sure you use the user/password (pi/xxxx) or you will only be able to read not write to the share.
2013/12/17
Appendix – File Details
>> sudo blkid
/dev/mmcblk0p1: SEC_TYPE="msdos" UUID="3312-932F" TYPE="vfat"
/dev/mmcblk0p2: UUID="b7b5ddff-ddb4-48dd-84d2-dd47bf00564a" TYPE="ext4"
/dev/sda1: UUID="3d5effa6-1456-489e-9dc6-70f4cff0a4b9" TYPE="ext4"
/etc/fstab
------------------------------------------------
proc /proc proc defaults 0 0
/dev/mmcblk0p1 /boot vfat defaults 0 2
/dev/mmcblk0p2 / ext4 defaults,noatime 0 1
# a swapfile is not a swap partition, so no using swapon|off from here on, use dphys-swapfile swap[on|off] for that
# 2013/02/10
UUID=3d5effa6-1456-489e-9dc6-70f4cff0a4b9 /home/pi/share ext4 defaults 0 1
/etc/network/interfaces
------------------------------------------------
auto lo
iface lo inet loopback
iface eth0 inet static
address 192.168.2.6
network 192.168.2.0
netmask 255.255.255.0
broadcast 192.168.2.255
gateway 192.168.2.1
allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp
/etc/resolv.conf
------------------------------------------------
nameserver 192.168.2.1
nameserver 142.166.145.137