OpenWrt Forum Archive

Topic: Music! mpd on Backfire on WGT634U

The content of this topic has been archived on 4 May 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

I finally managed to set up a working music system!  Actually, I have a system which still works, running Kamikaze r8480, but that setup's more limited than my new one, so...

The target system is:
A small box which connects via wires to my stereo system, and via wifi to my network, which includes a Linux system with my MP3 collection. Thus, this is a *client* of my existing wifi access point.

FIRST, a few assumptions: I'm using Netgear WGT634U because when I bought a few of them, they were relatively inexpensive, and almost alone in offering both WiFi and USB.  Newer routers have both - feel free to send me some hardware to test! :-)  At any rate, they're a known unit, so I'll assume you already have Backfire running on one, with wifi running.  I'm grateful for the builds on http://downloads.openwrt.org/backfire/1 … /brcm47xx/

SECOND assumption: I resisted as long as I could, but I finally gave in and plugged a USB hub into the WGT634U, so that I could have a USB audio device and a thumb drive.  This actually resulted in...

THIRD assumption: Much of this could have been done with an extroot system, but I wanted something simpler. With this setup, I can boot with nothing plugged into USB and get a "normal" router, or I can plug in my thumb drive and get a customized setup.  See how this becomes a flexible system?

Here's where we start getting specific.

On the basic system, install some necessary packages:
kmod-usb-core
kmod-usb2
kmod-nls-base
kmod-fs-ext2
kmod-scsi-core
kmod-usb-storage
block-mount
kmod-fuse
ldconfig

Next, create a USB thumb drive with one partition, and create an ext2 file system on it.  Instructions for doing so are abundant, so I'll skip them here.  (As a beginner, I used to hate it when people said that!) By the way, I would have preferred to have a vfat file system on the USB drive, so I could also use it on my Windows systems.  However, we need symlinks, so...
- On my setup, this drive will be mounted as /dev/sda1; you should create several directories: ./bin ./etc ./lib ./music ./tmp ./usr ./var ./var/mpd
- Add this drive to /etc/config/fstab (I like to edit the config files, but you may prefer to use uci)

# /etc/config/fstab
config mount
        option target   /opt
        option device   /dev/sda1
        option fstype   ext2
        option options  rw
        option enabled  1

Oh, and you'll want to "mkdir /opt" on the router! We're now prepared to reboot, and if all is well, /opt should be mounted and ready.

Add this to /etc/rc.local :

# /etc/rc.local
# If we mounted external storage, let it initialize itself
if [ -f /opt/etc/rc.local ] ; then
        sh /opt/etc/rc.local
fi

We'll get to that /opt/etc/rc.local in a minute - first, let's install some packages on the USB drive.

echo "dest opt /opt" >> /etc/opkg.conf
opkg --dest opt install mpd kmod-usb-audio
opkg --dest opt install curlftpfs
ldconfig

I had to add "audio" to /etc/group (555, arbitrary) to keep some portion of the audio programs happy.  It was a small price to pay.
- At this point, I've used only 13 MB of space on the USB drive - finally, I've found a place where my 256 MB drive looks big!

Our music system will use /opt/etc/mpd.conf for a configuration file, with some specific file locations:

# /opt/etc/mpd.conf
music_directory         "/opt/music"
playlist_directory      "/opt/music"
db_file                 "/opt/var/mpd/mpd.db"
log_file                "/opt/var/mpd/mpd.log"
error_file              "/opt/var/mpd/mpd.error"
pid_file                "/opt/var/mpd/mpd.pid"

There's other info in that file that we'll get to later, but for now, keep in mind that "/opt/var/mpd" is on the USB drive, but "/opt/music" is on an external server.

Now let's create /opt/etc/rc.local - this does a few things in order to turn an ordinary router into a music box, so we'll break it up into parts.

# /opt/etc/rc.local

### PART ONE
# Just in case...
PATH=/opt/bin:/opt/sbin:/opt/usr/bin:/opt/usr/sbin:/bin:/sbin:/usr/bin:/usr/sbin

### PART TWO
# Make this a client of home_net
uci set wireless.wifi0.channel=11
uci set wireless.@wifi-iface[0].network=wan
uci set wireless.@wifi-iface[0].mode=sta
uci set wireless.@wifi-iface[0].ssid=home_net
uci set wireless.@wifi-iface[0].encryption=none
uci delete wireless.@wifi-iface[0].key
uci commit wireless
# I *think* all we need is "wifi up", but...
wifi down ; sleep 3 ; wifi up

### PART THREE
# Open up a few firewall ports (ssh, web, mpd)
iptables -t nat -A prerouting_wan -p tcp --dport 22 -j ACCEPT
iptables        -A input_wan      -p tcp --dport 22 -j ACCEPT
iptables -t nat -A prerouting_wan -p tcp --dport 80 -j ACCEPT
iptables        -A input_wan      -p tcp --dport 80 -j ACCEPT
iptables -t nat -A prerouting_wan -p tcp --dport 6600 -j ACCEPT
iptables        -A input_wan      -p tcp --dport 6600 -j ACCEPT

### PART FOUR
# It looks like it takes about 20sec to set up the home_net route
HNET_OK=""
x=0; while [ $x -lt 10 ]; do
  rdate -s mp3serv && HNET_OK="True" && break
  sleep 5
  : $((++x))
done

### PART FIVE
# curlftpfs looks in $HOME/.netrc for user:pass
#- so make sure it's set to the right value!
[ $HNET_OK ] && HOME=/root curlftpfs -o nonempty ftp://mp3serv/ /opt/music
# - Thus we want two possible dB files (w/wo mp3serv),
#   but both stored locally!
( cd /opt/var/mpd
  [ $HNET_OK ] \
    && cat mpd.db.mp3serv > mpd.db \
    || cat mpd.db.local > mpd.db
)
mpd /opt/etc/mpd.conf

Part One:
Some of the programs we're running here are in /opt (particularly "mpd"), so set the PATH accordingly.

Part Two:
Use "uci" to set up this system as a client (mode="sta") of the home network. If you're more cautious than me, your network is encrypted, so you'll need to adjust these settings. (When I set up this system as a standalone
router, I added encryption and a key - for cleanliness, I'm removing that key, but that might not be necessary.)

Part Three:
Because the "wan" connection goes to our home network and not The Internet, we can safely open a few holes in the firewall: SSH, HTTP, and MPD.

Part Four:
The "wifi up" command causes us to use DHCP to get an address from the home router.  This is a double-purpose section - we'll poll the Linux box with the MP3 files (mp3serv) until that setup has finished, then we'll use it to set the date.

Part Five:
This system uses "curlftpfs" to get files from the MP3 server. I've used both NFS and Samba in other situations, but those were both more than I needed here - more CPU cycles, and more memory - so we'll make this simple. Again, because I have a simple and trusted network, I'm not encrypting traffic on the curlftpfs connection - you might want to be more secure.
- I think read-write access is only needed in order to store playlists - if you don't need to create playlists, it might be better to mount this as a read-only system.
- Setting up /root/.netrc : http://curlftpfs.sourceforge.net/

Part Five-point-One:
When I ran mpd on this new Backfire system and attempted to create the db_file, it just about killed the system!  So, I installed mpd on the MP3 server, created a db_file, and copied that to the Backfire system (/opt/var/mpd/mpd.db).  If the preceding sentence results in hours of effort and agony, you have my apologies.

mpd configuration:
It took some tweaking to get the ALSA libraries to stop complaining when mpd started - it would run, but only after several error messages.  One error was cured by adding "audio" to /etc/group (mentioned above); another required specifying some of the "default" values!

# /opt/etc/mod.conf (continued from above)
# Isn't this silly? We need to specify the mixer defaults! (mpd 0.15.0)
audio_output {
        type            "alsa"
        name            "local alsa"
        mixer_device    "default"
        mixer_control   "PCM"
}

Going Onward:
The basic system, without the USBdrive, can remain basic, and "fancy" packages can be installed on /opt - but there are occasional stumbling blocks. For example, any programs using "ncurses" will need this:

ln -s /opt/usr/share/terminfo /usr/share/terminfo

Another Issue:
My USB-audio device (the standard, and cheap, C-Media dongle) only works if it's plugged in at boot time.  If I boot without it and plug it in later, it's not detected - is hotplug broken?

For MPD Debugging Help:
http://openlinksys.info/forum/viewthrea … d_id=10642
- in Polish?! Anyway, many mpd debug hints

Cool and nice. But, you should have posted this in the HOWTO Documentation forum.

Shall I move it?

Jow, perhaps it should be moved to HOWTO/Documentation forum since it is a HOWTO. Leave a link here in case OP will be looking for it.

Yes, go ahead and move it - sorry, I probably should have known better.

The discussion might have continued from here.