Chris Banes


I work as a DPE in the Android Developer Relations team. This is my personal blog and any views expressed here are mine, and not my employers.


Raspberry Pi: Media Server Pt. 1

Now that I’ve got my Pi working a bit more efficiently it’s time to actually start putting it to use: a media server. This post will detail the initial steps in getting your Raspberry Pi serving media files from an external drive via DLNA.

Mount External Hard Drive

The first thing we need to do is get the external hard drive mounted and read-able. When you plug in your hard drive Debian will not do anything with it, so you have two options: mount it manually each time or setup Debian to automatically mount it for you. As I didn’t really want to ssh into the Pi every time I restart it, I opted for the latter.

I will only ever be using 1 external hard drive, which means that I can set up Debian to auto mount this hard drive on startup by adding a fstab entry. If you’re going to be using lots of different drives, I’d recommend you having a look at install ‘autofs’ which will automatically mount any drives you plug in. See here on how to set it up. I could have done this for my setup, but autofs runs a daemon in the background and I’d rather not waste the RAM/CPU on it.

The first thing you need to do is plug in your external hard drive (obviously), now we need to find out what it’s UDID is. This fairly easy to do:

sudo blkid

Which will return a list with all of the drives (partitions really) connected to the Pi. You should be able to easily recognise your hard drive as it will not be any of the items starting with /dev/mmcblk. Here’s an example of my drive:

/dev/sda1: LABEL="CHRIS HD" UUID="4956-13EB" TYPE="vfat"

There are two important bits of information here, the UDID and partition type. With this information we can change the fstab file and add the hard drive to it, but first we need to create a directory for the hard drive to mount to. The following commands will create a directory, and then set the permissions so that we can read it without problems:

sudo mkdir -p /media/ExternalHd  
sudo chmod 755 /media/ExternalHd

Now we can edit the fstab file:

sudo nano /etc/fstab

Then add the following to the bottom of the file (replacing the UDID with TYPE with whatever blkid returned, and the mount location with whatever you created above):

UUID=4956-13EB  /media/ExternalHd   vfat    defaults      0     0

Now if you call ‘sudo mount -a’ the hard drive should be mounted! Your drive will also be mounted every time the Pi boots.

MiniDLNA

The next step is to now install a media server. I have chosen MiniDLNA as it’s lightweight and works well with large libraries. Luckily MiniDLNA is available for Debian Wheezy!

Install and Setup MiniDLNA

First thing we need to do is install it:

sudo apt-get install minidlna

The next step is to set it up to your needs by editing it’s configuration file:

sudo nano /etc/minidlna.conf

I’m not going to write about every option here, but I’ll list some of the modifications I have made:

# Change the Media Dir's to point to the external hard drive, obviously change these to match your directory layout  
media_dir=A,/media/ExternalHd/Media/Music  
media_dir=V,/media/ExternalHd/Media/Video  
media_dir=P,/media/ExternalHd/Media/Photos

# Change db_dir so that the database is saved across reboots
db_dir=/home/pi/.minidlna

# Uncomment log_dir for now in case we hit problems
log_dir=/var/log

# Inotify doesn't seem to work with this kernel
inotify=no

Finally you can start MiniDLNA now (and also on boot) by running:

# Start MiniDLNA at boot  
sudo update-rc.d minidlna defaults

# Start MiniDLNA now
sudo service minidlna start

MiniDLNA will now begin indexing all of your media files. The more files you have the longer this takes, on my machine with 12000 media files it takes about 15 minutes. Luckily, as we changed the config to make MiniDLNA save it’s database to /home this only happens once.

If you’ve given MiniDLNA enough time to complete it’s indexing you’ll be ready to start serving media files. If you find that something isn’t working, you can check the log by running:

less /var/log/minidlna.log

If everything is working well I’d recommend re-commenting out the log_dir variable in the MiniDLNA config, as it does write out a lot of log information which may affect performance.

Next Steps

There are a number of improvements we can make to this setup for performance and power-saving which I’ll detail in a later post. For now though, enjoy your new DLNA media server!

comments powered by Disqus