Monday 21 April 2014

Using a Raspberry Pi as a headless media store on a Sonos network

This is to build a headless media centre and is also being built that way - that is, I'm not going to plug a screen or keyboard/mouse directly into the Pi. This means that I can install the version of Debian without the window manager which makes it far lighter on the processor.

It's not difficult, but I've compiled my notes and written some English to link them together as someone may find them useful. I'm going to assume some basic Linux and home networking knowledge.

You will need

Sourced from Amazon, total cost (at time of writing, without hunting around) is: £47.60

You'll also need some USB hard drives. The Pi (model B here) only has USB 2.0 ports so you can use older drives if you're not planning on plugging them in anywhere else. Also, the Pi runs on very low power so you really want powered drives or a powered USB hub to sit between them. If you want to back up your data, you're going to need two drives. If you don't, you can ignore all the stuff about rsync that comes up later.

Let's go

Up and running

  1. download Raspbian
    Sadly, the NOOBS install method requires the use of a screen so wont work for us.
  2. install Raspbian onto the SD card
    These are the instructions for prepping the card on a Windows machine.
  3. put the Pi in the case, plug in the ethernet cable and put in the SD card
  4. finally plug in the power cable and we're off

Connect and update

Find the IP address of your Pi, shell to it and log in (default username / password is pi / raspberry). You need to run:
sudo raspi-config
and select the option to expand the root filesystem to fill the SD card. It's important you do this before any of the other steps or you will be starting again.

Next update the operating system via:
sudo apt-get update
sudo apt-get upgrade
This will likely take a while.

Connect the drives

Plugging the drives in will auto-mount them, however if we are using one drive as a backup to the other we need to ensure they are always mounting the same way round on restart. To do this we will identify the drives via their UUID in /etc/fstab:
UUID=0AC4D607C4D5F543 /mount/location ntfs rw,defaults 0 0
To find the UUID of the drives:
blkid /dev/sda1
blkid /dev/sdb1
Remember that if you format a drive, the UUID will change.

Install the software

Time to install some useful software.

Samba shares specified parts of your filesystem on the network. It will allow you to mount the drive on another computer which will let you put files on your media server when it's ready. It is also the shared area that Sonos will be able to search for music files.

Minidlna shares an area of your filesystem via DLNA. This means a DLNA-ready device can locate the share and request any files in it. I use this for watching video on my iPad (my viewing program of choice being 8player).
# install samba
sudo apt-get install samba samba-common-bin

# install minidlna
sudo apt-get install minidlna
I also installed some extra tools to make other tasks easier. You don't need them for this build.
sudo apt-get install tree locate chkconfig

Configure samba

To configure samba you need to edit /etc/samba/smb.conf. You'll want something like this:
[global]
    netbios name = NETBIOS_NAME
    workgroup = WORKGROUP
    security = user
    encrypt passwords = yes
    smb passwd file = /etc/samba/smbpasswd

[media]
    comment = My media
    path = /PATH/TO/SHARE
    writeable = yes
    create mask = 0770
    force create mode = 0770
    locking = yes
Mixed to taste, of course.

Then add the user to the samba password file:
sudo smbpasswd -L -a USERNAME
Restart samba and you should then be able to mount the drive on other machines on the network.

Configure minidlna

The minidlna config file is found at /etc/minidlna.conf

Docs on configuring minidlna are available on the minidlna site. The important bits are:
media_dir=A,/home/user/Music      # Music directory
media_dir=P,/home/user/Pictures   # Pictures directory
media_dir=V,/home/user/Videos     # Video directory
friendly_name=Laptop              # Name of the share
db_dir=/var/cache/minidlna        # Index files (make sure location is writeable)
log_dir=/var/log                  # Log files (make sure location is writeable)
inotify=yes                       # Index new files as they are added
It's important to make sure the minidlna user can read and write to the index and log directories and read the media files. Alternately, if you're lazy like me you can make minidlna run as the main user by editing /etc/init.d/minidlna (around line 68). Obviously this is not recommended for any significant install of anything, but I'm assuming we're at home on a very limited access network.

When starting minidlna you may get an error message claiming to be exceeding the watch limit. To increase this limit, add to /etc/sysctl.d/90-inotify.conf
fs.inotify.max_user_watches = 100000
and restart.

Note that there is a new version of minidlna on the way called ReadyMedia possibly rendering this completely obsolete.

Back up the drives

I wanted to keep a backup of my media on the second drive. I could have used RAID 1 but reading around suggested that the amount of read/write information going across the USB bus would cause a bottleneck and therefore problems serving the files. To get around this, I use rsync to copy everything across on a nightly basis.

Something like this in the crontab will sync the drives each night at 3am:
0 3 * * * rsync /mountpoint/maindrive /mountpoint/backupdrive >> ~/rsync_log.txt 2>&1
And with this, the setup is complete.

Getting the media files in order

Sonos will read the ID3 tags on the music files so they need to correct. Due to the fun of an iTunes crash some time in the past mine are a mess so it's time for some scripting. More on this in a future post.

No comments: