Turning Synology NAS into a geeky media server

This post was written in pre-docker era. Back in the time it was necessary to perform many things that are no longer required on modern devices (like installing Debian Chroot package which, expectedly, is no longer offered).

Modern devices with Docker support or equivalent features (FWIW, my choice is Linux Station from QNAP) are much more friendly towards linux enthusiasts

Required files can be downloaded from https://github.com/edio/synology-mediaserver

I’m not a huge fan of Synology. I bought my DS214 by friend’s recommendation and became frustrated with it right after the purchase. Bundled software was limited and buggy. Many people find Synology products great. But for me it just didn’t work well because it forced its use cases on me instead of being a helpful tool for my own very specific use cases.

Anyway, I wasn’t willing to lose money on reselling my NAS, so I decided to get as much as possible from it and currently with my DS214Play I can:

  1. Play virtually any sound via connected USB sound card.

  2. Particularly, I can play my huge collection of high-resolution music in various formats (with replaygain tags respected). All that thanks to mpd, which I can control from any decent phone, web-browser or even smart-watch and scrobble all statistics to Last.fm.

  3. Stream sound from my laptop via PulseAudio, while watching videos on YouTube or movies.

In this and few next posts I’m going to tell, what I have done to get all this.

Although everything here have been tried on DS214Play it should also work on any other x86-based (or theoretically, even on ARM-based) Synology product.

I do not expect anything bad, but, just in case, standard disclaimer:

everything described here may contain mistakes and inaccuracies, cause data loss or even damage to your hardware.

Notations

The code you need to execute in a terminal will be presented in code blocks, $ prompt means, that code is to be executed from an unprivileged user, # means that code must be executed from root. Environment will be given in braces before the prompt. For example:

  • Execute on NAS from an unprivileged user

    (nas)$ date
  • Execute as root in chroot

    (chroot)# date

Setup Debian chroot

We start with Debian chroot setup because it will help us a lot during testing and it’ll be required for PulseAudio to work later.

Debian repositories contain thousands of applications, and unlike optware solutions all packages are relatively fresh and usually work without issues.

  1. Add SynoCommunity repo following official guide. Do not forget to set Trust level to Synology Inc. and trusted publishers

  2. Install Debian Chroot package

  3. Start this package from Package Center

Configure Debian chroot

  1. SSH to NAS and su to root

    (nas)$ sudo -s
  2. Chroot to Debian

    (nas)# /var/packages/debian-chroot/scripts/start-stop-status chroot
  3. Update package index and install locales to remove pesky warning about missing locales

    (chroot)# apt-get update
    (chroot)# apt-get install locales-all
  4. Install alsa utils as we’ll require them later

    (chroot)# apt-get install alsa-utils
  5. Exit form chroot (or create another SSH session)

    (chroot)# exit

Share media files to Debian chroot

By default Debian chroot won’t get access to files stored on NAS (that’s the idea of chroot). We have to mount explicitly those directories, we want to expose:

(nas)# mount -o bind /path/to/dir/on/nas /var/packages/debian-chroot/target/var/chroottarget/path/to/dir/in/chroot

I created a script under /usr/local/etc/rc.d/ to do that mounting automatically on startup. The script must be named S[00-99]whatever.sh to be picked up by DSM init framework, where [00-99] determines order of execution. Mine is named S90chrootmount.sh:

#!/bin/sh

. /etc.defaults/rc.subr

CHROOTDIR=/var/packages/debian-chroot/target/var/chroottarget/media
MEDIADIR=/volume1/media

case $1 in
        start)
                mount -o bind $MEDIADIR $CHROOTDIR
        ;;
        stop)
                umount $CHROOTDIR
        ;;
        restart)
                $0 stop
                sleep 1
                $0 start
        ;;
        *)
                echo "Usage: $0 start|stop|restart"
        ;;
esac

To expose directory to chroot then, do

(nas)# /usr/local/etc/rc.d/S90chrootmount.sh start

After doing that, we can chroot to Debian again and check that mount worked as expected (/media in chroot should corresponds to /volume1/media on NAS for my case).

Setup sound card

Obviously, sound card should be supported in Linux. I own Audinst USB HUD mini and it’s a real plug-n-play. I just plug it in, and if alsa is set up, it just works. For what it’s worth, I’m satisfied with the device, it sounds not as good as my beloved ESI Juli@, but still it is good for its price.

Loading kernel modules

For sound card to work we need to install required kernel modules:

#!/bin/sh

. /etc.defaults/rc.subr

KERNELMODULE="soundcore snd-page-alloc snd snd-seq-device snd-rawmidi snd-usbmidi-lib snd-usb-lib snd-hwdep snd-timer snd-pcm snd-usb-audio snd-mixer-oss snd-pcm-oss"
COMPATMODULE="snd-usb-hiface"

case $1 in
        start)
                SYNOLoadModules $KERNELMODULE
                SYNOLoadModules $COMPATMODULE
        ;;
        stop)
                SYNOUnloadModules $COMPATMODULE
                SYNOUnloadModules $KERNELMODULE
        ;;
        restart)
                $0 stop
                sleep 1
                $0 start
        ;;
        *)
                echo "Usage: $0 start|stop|restart"
        ;;
esac

This will load exactly the modules, that are loaded by AudioStation.

To do that automatically on NAS startup I saved the script as /usr/local/etc/rc.d/S01audiod.sh.

Load modules without reboot:

(nas)# /usr/local/etc/rc.d/S01audiod.sh start

Verify it works

Chroot to Debian chroot and test it with aplay. You’ll need some *.wav file in your media files

(nas)# /var/packages/debian-chroot/scripts/start-stop-status chroot
(chroot)# aplay /media/path/to/some.wav

If it doesn’t work right ahead, examine output of aplay -L, maybe you need to choose a different device

(chroot)# aplay -L
(chroot)# aplay -D front /media/path/to/some.wav

Fixing permissions issue

By default sound device is initialized to be accessible only by root user. An easy temporary fix would be

(nas)# chmod a+rw /dev/dsp*
(nas)# chmod -R a+rw /dev/snd/

But then, if device is replugged, you have to do that again. To fix that permanently we need an udev rule to set mode each time device is connected.

Create /usr/lib/udev/rules.d/50-sound.rules with the following content

SUBSYSTEM=="sound", MODE="0666"

Reload udev rules

(nas)# udevadm control --reload

Reconnect sound card or use reload kernel modules

(nas)# /usr/local/etc/rc.d/S01audiod.sh restart

You should see that newly created devices have rw mode for all users:

$ ls -l /dev/dsp*
crw-rw-rw- 1 root root 14, 67 Aug 15 16:44 /dev/dsp4

$ ls -l /dev/snd/*
crw-rw-rw- 1 root root 116,  0 Aug 15 16:44 /dev/snd/controlC0
crw-rw-rw- 1 root root 116, 16 Aug 15 17:38 /dev/snd/pcmC0D0p
crw-rw-rw- 1 root root 116, 17 Aug 15 16:44 /dev/snd/pcmC0D1p
crw-rw-rw- 1 root root 116, 33 Aug 15 16:44 /dev/snd/timer

Now every user in audio group in Debian chroot will have access to audio device.

Basically that’s it. For more experienced users it shouldn’t be hard to setup mpd on Debian to play via ALSA.

If you’re interested in sound streaming over network with PulseAudio, please check out the next part.