I’ve got a Raspberry Pi lying around for some time, and finally I got time to set it up as a small NAS for my home. After some poking around I got a nicely working system that can expose shares to my Mac computers, stream to the TVs with everything backed up on a secondary USB disk.
The hardware setup is as follows:
- Raspberry Pi Model B, with 4GB flash card
- Two external USB drives of same size, first drive is used for file sharing, second for backup.
I installed and configured the following software:
Raspbian:
Raspbian “Wheezy” is downloaded from http://www.raspberrypi.org/downloads and then put on a flash card. When booting up the Pi you need a keyboard and screen attached to do the initial configuration.
To install software use apt-get and to enable the software to run at every boot you can use update-rc.d. Example:
apt-get install netatalk
update-rc.d netatalk defaults
udev
udev is essential when working with usb devices on Linux, it makes sure that usb devices always gets assigned the same device name. I tried different mapping strategies and finally found the method described here to be the one working best, i.e. based on serial number of the devices. Note that the command usbadm info does crash on Raspbian. Instead use the command usb-devices to get the serial IDs. After getting the serial IDs of my two USB disks I created a new file /etc/udev/rules.d/50-usb-hd.rules with the following content:
SUBSYSTEMS=="usb", ATTRS{serial}=="DEF10C3BCD6C", KERNEL=="sd?", NAME="%k", SYMLINK+="usb-share", GROUP="storage"
SUBSYSTEMS=="usb", ATTRS{serial}=="DEF10C3BCD6C", KERNEL=="sd?1", NAME="%k", SYMLINK+="usb-share", GROUP="storage"
SUBSYSTEMS=="usb", ATTRS{serial}=="000000560938", KERNEL=="sd?", NAME="%k", SYMLINK+="usb-backup", GROUP="storage"
SUBSYSTEMS=="usb", ATTRS{serial}=="000000560938", KERNEL=="sd?1", NAME="%k", SYMLINK+="usb-backup", GROUP="storage"
To test, restart udev (you may even need a reboot), and then check if the devices are created.
automount
Good. Old. Automount. First introduced in Solaris, autofs is used to automatically mount filesystems. Using automount instead of hard mounting is way safer when using USB drives. Should one of your disks or controllers die for some reason it would not bring down your system. Edit your /etc/auto.master and add the line:
/media /etc/auto.media --timeout=100,defaults,user,exec
Then add a new file /etc/auto.media with the following content:
usb-share-hd -fstype=auto :/dev/usb-share
usb-backup-hd -fstype=auto :/dev/usb-backup
After restarting autofs check that it’s working first cd to /media and do an ls. The directory should be empty. then type cd usb-share-hd which should mount the file system.
Netatalk
There used to be CAP, luckily today we have Netatalk for file sharing with Macs (you would want a Samba server if you run Windows). After you’ve setup udev and autofs you just need to edit your /etc/netatalk/AppleVolumes.default to add your shares, in my case I added the line:
/media/usb-share-hd "USB Drive"
hdparm
hdparm is used to spin down your hard drives when they are not in use. Actually this proved to be the most tricky part of getting my home NAS up and running. There exists many tools to configure the hard drives to spin down. I tried all of them; on my hardware the only one that worked was hdparm. This may depend on your disks and usb adapters. After installing hdparm, test it by running:
hdparm -S1 /dev/usb-share
This should spin down your drive after 10 seconds of inactivity. If it works, go ahead and add the following lines to your /etc/hdparm.conf and add hdparm to the init.d startup sequence:
/dev/usb-share {
write_cache = off
spindown_time = 120
}
/dev/usb-backup {
write_cache = off
spindown_time = 120
}
minidnla
minidnla for streaming videos (I also use kissdx for streaming to my old Kiss DP-1500) to UPnP enabled TVs. If you don’t have an UPnP enabled TV an excellent option is to run openelec on a Raspberry.
minidnla is easily configured by adding the following line to /etc/minidnla.conf:
media_dir=/media/usb-share-hd/Video
rsync
For backup I actually setup two disks, one which I share using netatalk, and one that I use for backup. It would be wonderful to have a full ZFS stack on Debian, but from what I’ve seen it’s still fairly unstable. I opted for a tried and tested solution using rsync to sync files from my shared disk to the backup disk rsync -a /media/usb-share-hd /media/usb-backup-hd does the job. I actually run it manually right now to be on the safe side.
And that’s it. Once you’ve configured everything, remember to take a backup of the flash card!
