Build Your Own NAS/Subversion/Web/Anything Micro-Server

I've long desired a miniature file server that I could put on my home network, always on and ready for my machines to synchronize with at any time. But not just that, oh no, my little box must do everything:
- Handle Mac and Windows clients.
- Mac support must include resource forks and long, oddball filenames.
- Be a Subversion server for efficient revision control of all types of files.
- Maybe be a web server (never know when you might need one of those).
- Streaming iTunes server would be cool, too.
- Have ssh/sftp access so I can build and install anything else I desire.
- Have enough disk to be useful, but also be small and quiet.
- Oh, right, it's got to be super-cheap, too.
Daydream perhaps? No way. It turns out that a bunch of the home NAS (Network Attached Storage) boxes currently for sale run Linux, and most of them are easily hackable to open them up for general-purpose use. You could also use a junker PC for this purpose, but see my "small and quiet" qualification above.
This article describes my choice of NAS box, hacking it so I could install custom software on it, and setting it up for most of the uses listed above. It assumes a general familiarity with Linux and building applications from source code.
So which NAS box? The hackability of the NSLU2 is well known, but it doesn't have any disk built-in. I wanted a single box that contained everything. I discovered that the Buffalo LinkStation has a thriving developer community, so I went that route. You can also get a Kuro Box which is basically an already-open LinkStation with no hard disk. I went for the LinkStation since it came with everything needed, and is trivial to hack without even opening the box.
Then, which LinkStation? They have the "standard" models (HD-HxxxLAN) and also gigabit Ethernet (HD-HGxxxLAN). I chose the latter since it's not much more expensive, and comes with faster CPU and more memory. It turns out that some people have had trouble with re-flashing firmware on the 300GB model I bought, but fortunately I didn't run into this. Also fortunately, Buffalo replaced the LinkStations for people whose firmware update failed.
After buying the LinkStation, basic setup is trivial. Plug it in, turn it on, and it just works. Beautiful. The stock firmware provides SMB (Windows) and AppleTalk file serving, but the AppleTalk is down-rev and may not be useful (more on that later). I'd suggest creating a user account through their web interface at this point.
Hacking the LinkStation
You'll need a Windows machine for this part. Grab the OpenLink firmware, selecting the "HG" firmware if you gave a gigabit model. Follow the included directions, and about 5 minutes later you're done. It's really that easy. Now you have telnet and SSH access to your LinkStation.
The next step is installing the developer tools. Grab those, again selecting the HG tools for the gigabit model if necessary. Read the short document which tells you how to run the installer script, and you're done.
At this point you can install other pre-built packages from the Wiki or build your own from source.
Hard Drive Layout
The LinkStation has three partitions on its disk: one for swap and two for files. From "df":
Filesystem 1k-blocks Used Available Use% Mounted on
/dev/hda1 373359 272459 81624 77% /
/dev/ram0 14521 214 13557 2% /mnt2/ram
/dev/hda3 306690228 15124600 291565628 5% /mnt
The vast majority of the disk is mounted at /mnt, and this is where all user files are stored. When you install new applications on the LinkStation they'll usually go in /usr or /usr/local by default, and this can fill up the hda1 partition if you're not careful. The easiest solution is to create a /mnt/local directory and then make /usr/local a symbolic link over to that, like so:
/usr# cp -r local /mnt/.
/usr# mv local local-bak
/usr# ln -s /mnt/local local
/usr# ls -l
(...)
lrwxrwxrwx 1 root root 10 Mar 29 01:35 local -> /mnt/local
(...)
/usr# rm -rf local-bak <- only when you're sure
Serving Mac Files the Right Way
Most people use SMB to connect their Mac and LinkStation (example here), but this has two problems:
- Resource forks are lost.
- Valid file names on Mac filesystems may not be valid over SMB.
Resource forks are a hold-over from pre-OS-X days and are mostly gone, but if you're got a bunch of expensive fonts around from those days, you need to preserve their resource forks or they'll be toast.
The file name problem bit me halfway through a huge file copy; there was a file with a percent sign in it, and SMB choked. Arrgh.
The AppleTalk file server (AFP) included with the LinkStation is an older version which has its own limitations (e.g. 31 character file name max), so that won't work. Go get the latest 2.x Netatalk source and copy it over to your LinkStation. Switch over to the directory containing the Netatalk source code. (Note that the LS's user directories, as configured through their web interface, are mounted under /mnt.)
There's one configuration change you need to make manually because the LinkStation doesn't use a shadow password file. Here are the build commands:
./configure --without-shadow
make
sudo make install
This will build and install under /usr/local. Now to configure Netatalk. If you're just using OS X machines, you can stick to TCP with strong authentication. Edit /usr/local/etc/netatalk/afpd.conf and add:
- -tcp -uamlist uams_dhx.so
(Yes, that's a leading dash by itself.) One last step to make LS users' home directories accessible: you need to edit /etc/passwd, and change the part right before ":/bin/bash" to match the correct directory. In my case, it looks like this:
Josh:mJdgd6zRFxXHo:101:1000::/mnt/josh:/bin/bash
Now you can start the AFP server:
/usr/local/bin/afpd
To get this new afpd to start at boot, you can edit the /etc/init.d/atalk script, or make a simplified version just for afpd. I'll discuss the latter technique in the Subversion section -- just replace "svnserve" with "afpd."
Revision Control
The term "revision control" (aka "version control" or "source code management") isn't well-known outside programmer geek circles, but it's an extremely useful technique for tracking changes to files. With a good program like Subversion you can archive both text and binary files with equal efficiency. It's also a great way to synchronize sets of files between machines while maintaining complete history -- you can literally "rewind" a copy to any previous version if needed.
I can't do proper justice to the benefits of revision control here; if you're interested, go buy the Subversion book or read it online.
The process of building a Subversion server on the LinkStation is a piece of cake: just copy the source into your user directory, and build. It builds and installs flawlessly. Run "svnserve -d" and you're ready to go.
There are a bunch of ways to access Subversion, including HTTP, SSH tunneling, and just raw Subversion protocol. Since I'm running this on my own private network, I just went with the raw protocol, which is what svnserve provides.
Starting Subversion (And Other Stuff) at Boot
The LinkStation uses normal Linux startup scripts, with only a few minor twists. Here's a simple startup script I used for Subversion:
#! /bin/sh
# ( saved on disk as /etc/init.d/svnserve )
start()
{
echo 'Start services: svnserve'
/usr/local/bin/svnserve -d
/usr/bin/logger -t linkstation -p user.info -i 'Started svnserve'
}
stop()
{
echo 'Stop services: svnserve'
/sbin/killall "/usr/local/bin/svnserve" > /dev/null 2>&1
/usr/bin/logger -t linkstation -p user.info -i 'Stopped svnserve'
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo "usage: $0 {start|stop}"
exit 1
;;
esac
Save this on disk as /etc/init.d/svnserve. The next step is creating a few symbolic links so it's run at the right time during startup/shutdown. Here are the commands:
/etc/rc.d/rc2.d# cd /etc/rc.d/rc2.d/
/etc/rc.d/rc2.d# ln -s ../init.d/svnserve S90svnserve
/etc/rc.d/rc2.d# cd /etc/rc.d/rc0.d/
/etc/rc.d/rc0.d# ln -s ../init.d/svnserve K90svnserve
/etc/rc.d/rc0.d# cd /etc/rc.d/rc6.d/
/etc/rc.d/rc6.d# ln -s ../init.d/svnserve K90svnserve
Note that the LinkStation appears to only enter run level 2, not 3 as I'd expect.
And So On And So On...
You can make the LinkStation with OpenLink firmware into anything you want. So far everything I've experimented with has worked, sometimes right off the bat, other times requiring just a few tweaks. Even better, all the stock features still work, too. It's a great little box, and assuming you're cool with the command line and some Linux sysadmin foo, it's easy to customize to your heart's content.


Comments
Josh, looks like the linkstation wiki you linked has a wrong URL.
Oh and by the way, please have a look at your RSS/Atom feeds, they have a typo that prevents them from validating (and hence, be read from my newsreader)
Posted by: victor | July 18, 2006 4:48 AM
Victor, thanks a bunch for the heads-up. Looks like the original LinkStation Wiki is gone, but there's a new version up at a different address. I fixed the links. Also, I fed the RSS/Atom feeds through a validator and got them fixed up. Whoops -- should have done that sooner!
Posted by: Josh Carter | July 18, 2006 8:25 AM
Josh, I am interested in trying to replace netatalk on my linkstation. I have previously tried following the instructions that you provided, but failed somewhere along the way. Any chance of getting you to review your previous instructions, and providing scripts to get new afp version running? Seems most of the how to guides out there assume you have some linux experience and are leaving parts out.
Posted by: Jason | August 14, 2006 2:16 PM
Whoever took the picture of the linkstation on this page forgot to peel off the blue protection plastic film.
Posted by: Jules | February 28, 2007 11:24 AM
Josh, this is great information. Did you ever get it to stream iTunes files, and if so, how did you do it?
Posted by: Tom Mollerus | April 17, 2007 5:55 PM
Hiya - great HowTo - I've come up against a snag though - after the 'make install' without any errors, there's no such file as "/usr/local/bin/afpd" - loads of other stuff put there by the install script, just not that - any ideas ?
Posted by: Youra | June 7, 2007 2:33 PM