/ linux

How To Use The Raspberry Pi As A Sonos NAS Server

I love Sonos. In my opinion, there is no easier to use or more reliable multi-room music system available within the price range of the average consumer. I used to feed music to my Sonos system from an always-on Mac Mini running iTunes. Having recently received my second Raspberry Pi in the post (my first powers my home theatre setup), I thought I would try to use this ultra low-powered computer as the backbone of my Sonos setup. I’m happy to say that I’ve got it working beautifully and I thought I’d write a step-by-step guide on how to do this.

Please note that I’m a Mac user and whilst all of this can be achieved with a Windows machine, these instructions are written from a Mac perspective.

Step 1: Get the required equipment

You’re going to need at least one Sonos ZonePlayer (I use a mixture of Play:3 and Play:5 components) and a Bridge.

Next get yourself a Raspberry Pi, a suitable power supply, an 8GB SD card and a powered external USB hard drive. The USB hard drive is where we will store our music files, the SD card will store the Raspberry Pi’s operating system. Initially I was going to use one of the sleek and small Western Digital My Passport hard drives but I just couldn’t get it to work reliably because it draws too much power from the Raspberry Pi’s USB port. I opted for an old powered Iomega hard drive I had lying around.

Step 2: Install the operating system on the SD card

This is relatively straight forwards, just follow these step-by-step instructions.

Step 3: Complete the installation over SSH

Since we are creating a headless server (i.e. without a monitor) we’ll need to configure the Pi from our Mac over SSH. To do this, we need to get the local IP address of the Pi. Once the Pi is plugged into your router and booted, log into your router’s admin page (it’s usually 192.168.0.1 or something similar). Router admin pages vary a bit but you’re essentially looking for the ’attached devices’ section. This will list the IP addresses of every device currently attached to your router (either via ethernet or WiFi). Find the entry for the Pi and note down its IP address. In my case, it’s 192.168.0.29.

At this stage, I strongly advise that you reserve this IP address from your router’s admin page only for your Pi. This means that whenever the Pi is rebooted, it will be assigned the same IP address by the router. You’ll need to check your individual router’s instructions on how to do this.

Make sure your Mac is connected to your home network and open Terminal. Connect to your Pi:

ssh pi@192.168.0.29

Obviously enter the correct IP address for your setup. This command will log you in remotely as user pi (the default user). You’ll be asked to enter the password. It’s raspberry by default. You might be asked something about a verification key, just type yes and press Return.

Everything you now see in front of you in Terminal is coming from the Pi. It’s as if you’re sat in front of the Pi typing commands into its console. Follow the onscreen prompts to set the timezone, etc and when you’re done restart the Pi, wait for it to reboot and then SSH into it again using the instructions above.

Step 4: Update the system and add a new user

Make sure you have the most up-to-date packages for Raspian (the operating system you just installed) by typing the following:

sudo apt-get update && sudo apt-get upgrade -y

For those of you not familiar with Linux, prefixing a command with sudo means that the command is executed with root or administrative privileges. You’ll need to enter the currently logged in user’s password (in this case it’s pi’s password which is raspberry).

Although strictly speaking we don’t need to add another user to the Raspberry Pi, I would recommend creating one with a unique password rather than using the default user pi. In my case, I created a user called garry with the following command:

sudo adduser garry

Enter a password when prompted and again to confirm it. In order to run processes as root as described above (very important) you’ll need to add this user to the list of approved users:

sudo visudo

You’ll be taken to a list of the currently approved sudo users. I added the user garry as follows:

root ALL=(ALL:ALL) ALL
garry ALL=(ALL:ALL) ALL
#pi ALL=(ALL:ALL) ALL

By adding a # (or commenting-out) in front of the user pi, I’ve removed pi’s ability to behave as root. This is a good security measure.

Samba can grant groups of users access to shares. To do this add this new user to the users group. The group users will be added to Samba’s config file.

sudo usermod -a -G users garry

Close the Terminal window.

Step 5: Setup the external HDD

Whilst you can format the external drive with any file system, for simplicity, format it as a single NTFS partition. This is trivial to do from a Windows machine but if (like me) you’re a Mac user, you’ll need to install the third party driver by Paragon on your Mac to format drives as NTFS. It works like a charm (tested on OS Mountain Lion). Once installed, you can format as NTFS using Apple’s Disk Utility. Choose an appropriate label for the drive (you’ll need it later).

Copy your music files to the external HDD. If you use iTunes to manage your music you’ll need to copy your iTunes Media folder to the HDD. If you don’t use iTunes then just copy your music files to the HDD in any folder you want (just make sure that the music files have the correct embedded metadata so Sonos displays album art, etc).

Step 6: Mount the external HDD

SSH into the Pi but this time do it as your new user. In my case:

ssh garry@192.168.0.29

By default, Raspian only supports reading NTFS volumes, not writing to them. If you want to be able to copy new songs to the drive we’ll need to fix this. To do this, we will install a driver called ntfs-3g:

sudo apt-get install ntfs-3g

Now plug the external drive into the Pi and turn it on. Wait for it to spin up and then type:

sudo blkid

This command will give an output similar to this:

/dev/sda1: LABEL="MY LABEL" UUID="ABCD-EFGH" Type="ntfs"

The UUID is a unique identifier unique to your drive. We will use this to make sure the Pi mounts our drive where we want it to. Make a note of the UUID.

We need to create a mount point for the drive. Basically, a mount point is a folder on the Pi that represents the hard disk. I created a mount point at /mnt/pidrive. To do this type:

sudo mkdir /mnt/pidrive

Now we need to tell the Pi to mount the USB drive at boot to this mount point. To do this, we need to edit a file called fstab:

sudo nano /etc/fstab

Use the arrow keys to navigate the text file and move to the bottom. I have added the following line:

UUID=ABCD-EFGH /mnt/pidrive ntfs-3g defaults 0 0

Obviously, put the correct UUID and desired mount point in your entry. Press CTRL-X and type y to save the changes.

Reboot the Pi by typing:

sudo reboot

Hopefully, when the Pi reboots, the contents of the hard disk will now be visible at your mount point. You can verify this by navigating to the mount point (in my case /mnt/pidrive):

cd /mnt/pidrive
ls

cd changes the directory to /mnt/pidrive and ls lists the contents of the external HDD.

If after doing this you the contents of your mount point is empty, the Pi may have booted before the external drive had time to spin up. In this case, you can manually force a remount by typing:

sudo mount -a

Check to see if this has worked. If so, continue on to the next step.

Step 7: Share the external HDD over your network

We’re going to make the external HDD available to any computer on our network using Samba. First install it by typing:

sudo apt-get install samba samba-common samba-common-bin

When it's finished installing, we need to give our user (garry in my case) a Samba login. This is the login details that you’ll give to Sonos to access the drive (you can also use it to access the drive from the Finder on your Mac). Type:

smbpasswd -a garry

and enter a password.

We now need to modify Samba’s configuration file. Bring it up for editing in Nano by typing:

sudo nano /etc/samba/smb.conf

We want to make sure that only registered uses can access our Samba shares so replace:

#security = user

with:

security = user

Now we need to add a sharepoint for our music. I’m going to share the whole drive and call it PiDrive. This is the label that will show up in the sidebar in Finder on your Mac. Simply add the following somewhere underneath the Definitions section of the file:

[PiDrive]
comment = External HDD attached to the Raspberry Pi
path = /mnt/pidrive
browseable = yes
writable = yes
read only = no
valid users @users

Remember to change path to the mount point you created earlier. Restart Samba for the changes to take effect:

sudo /etc/init.d/samba restart

Step 8: Tell Sonos where your music is

Open the Sonos controller application on your Mac/PC. From the Manage menu select Music Library Settings...

Click the + button and select On a networked device such as a network attached storage (NAS) drive. Type:

\raspberrypiPiDrive

Obviously replace PiDrive with the share name you entered earlier when we setup Samba on the Pi. You’ll be prompted to enter the username (in my case, garry and password you also set earlier). Click OK.

You can mount the volumes shared by Samba (PiDrive in this case) on your Mac from the Finder. There should be an entry in the sidebar called raspberrypi. Click this and you’ll see the shares you defined earlier. Simply click them and they’ll mount as networked drives.

Hope this helps.