Copyright 2009-2012 by djg. All Rights Reserved.

Wonky Gibbon Ramblings


D-Link DNS-320 NAS, DebianSqueeze and Twonky Media Server

Posted on November 28, 2012 by danny

I gave myself a bit of a shock today by working out something in Linux that actually worked, all by mself! Obviously to any proper linux person it’s little more than a child’s first steps – but it was quite satisfying!

So …

Having got the NAS loaded with all my media files, I enabled the UPnP MediaServer that came with it – to discover it was total crap. And promptly disabled it again.

But the word on the street, is that the Twonky Media Server is good, so I thought, put that on it.

And indeed people have put it on the DNS-320. This post covers much of what needs to be done, providing a link to a build of Twonky for the ARM processor and instructions on how to configure.

The only problem, is that all the instructions are for the lightweight ffp Linux install – rather than debiansqueeze which I’m using (because of the subversion project posted about previously).

Nevertheless – I followed the instructions, and with a few variations … got it to work … which surprised me!

The main thing I did was to strip off every occurrence of /ffp wherever I saw it in a path eg: /ffp/opt/twonky/ became /opt/twonky/
I did this both with the instructions in the blog post and with the content of the two scripts downloaded by those instructions ‘twonky.sh’ and ‘twonkyserver-default.ini’
Note: Two things to be aware of here: 1) Don’t edit these files with Windows Notepad, it messes up the line feeds at the end of each line such that linux doesn’t like the result! The free editor Notepad++ can probably handle it. 2) The debiansqueeze equivalent of the /ffp/start directory is /etc/inet.d so drop the twonky.sh file in there.

twonky.sh
#!/bin/sh

# PROVIDE: twonky
# REQUIRE: LOGIN

. /etc/ffp.subr
name="twonky"
start_cmd="twonky_start"
stop_cmd="twonky_stop"

twonky_start()
{
# Add a route for twonky to be published via UPNP
route add -net 224.0.0.0 netmask 240.0.0.0 dev egiga0 >/dev/null 2>/dev/null </dev/null &
cd /opt/twonky
mkdir -p /opt/twonky/data/ &
# Twonky requires one handle per monitored filesystem if inotify is
# used. On many devices, this is set to 8192, which might be too low
# if music or photos are scanned. Therefor this value is raised to
# the doubled amount. If more is required, change the value below.
# Thanks Michael for reporting this!
/sbin/sysctl fs.inotify.max_user_watches=16384 >/dev/null 2>/dev/null </dev/null &
# The line below is without logging (logs are written to /dev/null).
# If you want logging, exchange it with the line below the current one.
/opt/twonky/twonkystarter -logfile /dev/null -appdata /opt/twonky/data/ >/dev/null 2>/dev/null </dev/null &
# /opt/twonky/twonkystarter -logfile /opt/twonky/data/TwonkyMediaServer-log.txt -appdata /opt/twonky/data/ >/dev/null 2>/dev/null </dev/null &
}

twonky_stop()
{
# Kill Twonky
if [ -n "`pidof twonkyserver`" -o -n "`pidof twonkystarter`" ]; then
echo "Stopping twonkyserver"
kill -9 `pidof twonkyserver` `pidof twonkystarter`
fi

if [ -n "`pidof twonkyproxy`" ]; then
echo "Stopping twonkyproxy"
kill -9 `pidof twonkyproxy`
fi

if [ -n "`pidof twonkywebdav`" ]; then
echo "Stopping twonkywebdav"
kill -9 `pidof twonkywebdav`
fi
# Remove the route
route del -net 224.0.0.0 netmask 240.0.0.0 dev egiga0 >/dev/null 2>/dev/null </dev/null &
}

run_rc_command "$1"
twonkyserver-default.ini
#!/bin/sh

# PROVIDE: twonky
# REQUIRE: LOGIN

. /etc/ffp.subr
name="twonky"
start_cmd="twonky_start"
stop_cmd="twonky_stop"

twonky_start()
{
# Add a route for twonky to be published via UPNP
route add -net 224.0.0.0 netmask 240.0.0.0 dev egiga0 >/dev/null 2>/dev/null </dev/null &
cd /opt/twonky
mkdir -p /opt/twonky/data/ &
# Twonky requires one handle per monitored filesystem if inotify is
# used. On many devices, this is set to 8192, which might be too low
# if music or photos are scanned. Therefor this value is raised to
# the doubled amount. If more is required, change the value below.
# Thanks Michael for reporting this!
/sbin/sysctl fs.inotify.max_user_watches=16384 >/dev/null 2>/dev/null </dev/null &
# The line below is without logging (logs are written to /dev/null).
# If you want logging, exchange it with the line below the current one.
/opt/twonky/twonkystarter -logfile /dev/null -appdata /opt/twonky/data/ >/dev/null 2>/dev/null </dev/null &
# /opt/twonky/twonkystarter -logfile /opt/twonky/data/TwonkyMediaServer-log.txt -appdata /opt/twonky/data/ >/dev/null 2>/dev/null </dev/null &
}

twonky_stop()
{
# Kill Twonky
if [ -n "`pidof twonkyserver`" -o -n "`pidof twonkystarter`" ]; then
echo "Stopping twonkyserver"
kill -9 `pidof twonkyserver` `pidof twonkystarter`
fi

if [ -n "`pidof twonkyproxy`" ]; then
echo "Stopping twonkyproxy"
kill -9 `pidof twonkyproxy`
fi

if [ -n "`pidof twonkywebdav`" ]; then
echo "Stopping twonkywebdav"
kill -9 `pidof twonkywebdav`
fi
# Remove the route
route del -net 224.0.0.0 netmask 240.0.0.0 dev egiga0 >/dev/null 2>/dev/null </dev/null &
}

run_rc_command "$1"

NB: These scripts are provided for reference only – more current versions may vary.

Also – I didn’t bother with this line [[ $(ls -1 /mnt|grep -c HD_a2) -eq 0 ]] && sed -i -e ‘s!/HD_a2!/HD/HD_a2!g’ -e ‘s!/HD_b2!/HD/HD_b2!g’ /ffp/opt/twonky/twonkyserver-default.ini
in the instructions – it does a search and replace – but it doesn’t seem necessary.

Finally the ‘twonky.sh’ script makes use of a script called ‘ffp.subr’ which I think is used to start and stop the twonky service. This doesn’t exist in Debiansqueeze. There is probably some native way of achieving the same thing – but I don’t know what it is so instead, I went and found the download for ffp – unpacked it, and retrieved the script. There’s nothing specific to ffp in it so I dropped it into the /etc directory. You do need to go through the file and remove /ffp off the start of any paths that it appears in.

Then as instructed, I ran the twonky,sh script to start the service and connected via a web browser – and OMG!!!! IT WORKED!!!! First time too, which usually only happens to people who are either (a) lucky or (b) liars.

Once I had configured the server through the web page, I left it to get on with the job of indexing my media collection. This was a task that UPnP service that came with the NAS had taken all night to do. Twonky seemed to do it in minutes. Not only that, connecting using Windows Media Player, it downloads the metadata from Twonky so much more rapidly. Streaming of both audio and video is very rapid and smooth as is seeking to a certain spot in the video. All in all – very impressed with Twonky. It’s good.

There are three reasons, changing all this stuff worked:
1) All the scripts use the sh shell which is of course available in both ffp and debiansqueeze (ie the syntax was the same)
2) The folder structures of ffp and debiansqueeze are very similar.
3) Can’t remember – it’ll come back to me.

The only reference for this post, is to thank (again) Uli author of the http://nas-tweaks.net site
for this page http://nas-tweaks.net/384/installation-of-twonky-server-7-on-nas-devices/

7 to “D-Link DNS-320 NAS, DebianSqueeze and Twonky Media Server”

  1. danny says:

    One nasty gotcha to watch out forL Don’t set Twonky to continuously scan the media directories for updates. These NAS devices don’t have a lot of grunt, setting this on the DNS-320 will ensure that the twonky service can no longer be started – which is not good if you want to listen to your music! If you do accidentally do this, find the file twonkyserver.ini and find the setting inside it called scantime. Change this from -1 to something like 60. You should then be able to start twonky again.

  2. Shanks says:

    I have the same NAS with debiansqueeze installed.
    Are there any advantages to installing twonky using this method or the Kurobox version over other versions?

    I was also wondering if your scripts are bootable, and if so, would you mind publishing them?

    I have installed twonky 6 and 7 kirkwood versions (the same versions that run on a raspberry pi) but I can not get them to run automatically when the nas restarts/powers on.

    I installed them using these commands (swap 6.0.39 for 7.0.12-RC1 ofr the latest version)

    mkdir /usr/local/twonky/
    cd /usr/local/twonky/
    wget http://www.twonkyforum.com/downloads/7.0.12-RC1-special/twonky-kirkwood-special-7.0.12-RC1.zip
    unzip twonky-kirkwood-special-7.0.12.zip
    chmod 755 twonky* cgi-bin/* plugins/*
    mkdir /var/twonky
    sh /usr/local/twonky/twonky.sh start

  3. danny says:

    Hi there!

    I’m running 7.0.9 which would have been the current Kurobox version at the time I installed. I don’t have a view on the different ways of installing – other than that I didn’t have to do any compilation and that was a good thing.

    If you want it to start with the appliance, you need to do the debian squeeze equivalent of
    chmod a+x /ffp/start/twonky.sh
    (as per Uli’s article) which would be
    chmod a+x /etc/init.d/twonky.sh

    I’ll add the two scripts to the end of the post, but bear in mind that if the original versions have changed in more recent versions, then you need to merge the changes.

    Hope this helps.

  4. Shanks says:

    Thanks for posting. I’ll try it later and let you know if I get it working. Have you tried the chmod a+x /etc/init.d/twonky.sh command to autoboot twonky?

    Please could you post your /etc/ffp.subr file too? 😀

  5. danny says:

    Hi there – yes I applied the chmod a+x /etc/init.d/twonky.sh command to autoboot twonky.

    The ffp.subr looks like this:

    #!/bin/sh

    unset name command
    unset required_files required_dirs
    unset start_cmd stop_cmd status_cmd
    unset warned

    die()
    {
    echo “$@”
    exit 1
    }

    warn()
    {
    echo “WARNING: $@”
    warned=1
    }

    check_required_files()
    {
    for _f in “$@”; do
    if [ ! -r “$_f” ]; then
    die “$_f: Required file not found or not readable”
    fi
    done
    }

    check_required_dirs()
    {
    for _d in “$@”; do
    if [ ! -d “$_d” ]; then
    die “$_d: Required directory not found”
    fi
    done
    }

    find_pids()
    {
    _prog=$(basename $1)
    ps -o pid,comm,args | while read _pid _comm _arg0 _args; do
    if [ “$_arg0” = “$1” -o “$_comm” = “$_prog” ]; then
    echo -n “$_pid ”
    fi
    done
    echo
    }

    proc_start()
    {
    while [ $# -gt 0 ]; do
    _cmd=$(basename $1)
    eval _flags=\$$(echo $_cmd | tr ‘.-‘ ‘_’)_flags
    _pids=$(find_pids $_cmd)

    if test -z “$_pids”; then
    test -x “$1” || die “$1: Not executable”
    echo “Starting $1 $_flags”
    $1 $_flags
    else
    warn “$_cmd: Already running”
    fi

    shift 1
    done
    }

    # variant of proc_start that detaches a process and redirects output
    # to /var/log/name.log
    proc_start_bg()
    {
    while [ $# -gt 0 ]; do
    _cmd=$(basename $1)
    eval _flags=\$$(echo $_cmd | tr ‘.-‘ ‘_’)_flags
    mkdir -p /var/log
    _log=/var/log/$(echo $_cmd).log
    _pids=$(find_pids $_cmd)
    if test -z “$_pids”; then
    test -x “$1” || die “$1: Not executable”
    echo “Starting $1 $_flags”
    $1 $_flags 1>$_log 2>&1 </dev/null &
    else
    warn "$_cmd: Already running"
    fi
    shift 1
    done
    }

    proc_stop()
    {
    while [ $# -gt 0 ]; do
    _cmd=$(basename $1)
    _pids=$(find_pids $_cmd)

    if test -n "$_pids"; then
    echo "Stopping $_cmd"
    kill $_pids
    else
    warn "$_cmd: Not running"
    fi

    shift 1
    done
    }

    proc_status()
    {
    while [ $# -gt 0 ]; do
    _cmd=$(basename $1)
    _pids=$(find_pids $_cmd)

    if test -n "$_pids"; then
    echo "$_cmd running as pid $_pids"
    else
    echo "$_cmd not running"
    fi

    shift 1
    done
    }

    run_rc_command()
    {
    case "$1" in
    start)
    test -n "$required_files" && check_required_files $required_files
    test -n "$required_dirs" && check_required_dirs $required_dirs

    if test -n "$start_cmd"; then
    $start_cmd
    elif test -n "$command"; then
    proc_start $command
    fi
    ;;

    stop)
    if test -n "$stop_cmd"; then
    $stop_cmd
    elif test -n "$command"; then
    proc_stop $command
    fi
    ;;

    status)
    if test -n "$status_cmd"; then
    $status_cmd
    elif test -n "$command"; then
    proc_status $command
    fi
    ;;

    restart)
    if test -n "$restart_cmd"; then
    $restart_cmd
    else
    ( /bin/sh $0 stop )
    sleep 1
    /bin/sh $0 start
    fi
    ;;

    *)
    # check $extra_commands
    for _rc_cmd in $extra_commands; do
    if [ "$1" = "$_rc_cmd" ]; then
    eval _cmd=\$${_rc_cmd}_cmd
    $_cmd
    return 0
    fi
    done

    # reject unknown commands
    echo "Usage: $(basename $0) start|stop|restart|status"
    if [ -n "$extra_commands" ]; then
    echo "Extra commands: $extra_commands"
    fi
    exit 1
    esac
    }

  6. Shanks says:

    Oh just one more thing, did you install it in /mnt/HD/HD_a2/squeeze/opt/twonky

    or /mnt/HD/HD_a2/opt/twonky

    ?

  7. danny says:

    Hi, I installed to /mnt/HD/HD_a2/squeeze/opt/twonky



Leave a Reply




↑ Top