Working around connection problems with Emacs Tramp

From time to time I have to edit files on a SunOS 5.10 server. I use Emacs with tramp for this. However, after some time I get this error message from tramp:

File error: Couldn’t find exit status of `test -e …

It seems that the ssh connection goes bad for some reason. After this you won’t be able to save the file anymore. You can work around this by running M-x tramp-cleanup-all-connections and then saving again.

Avoiding page allocation failures on the Pi

Since I’ve been using my 256MB Model B Pi as a server, I had been getting regular page allocation failures of the following kind:


sshd: page allocation failure: order:0, mode:0x20

This is rather annoying and seems to affect stability as well, like disrupted ssh or smb connections. It seems that the kernel has a setting to affect the minimum free memory that it keeps for allocations, which may be too low. So in the /etc/sysctl.conf file you can edit the last lines to bump this value from 8 Mbegabytes to 16 Megabytes:


# rpi tweaks
vm.swappiness=1
vm.min_free_kbytes = 16184

Settings are effective after a reboot. For me, this seems to fix the problem. But to be extra sure, I also changed the memory split from 192/64 to 224/32. Since my Pi runs headless, 64 MBytes seemed too much simply for a framebuffer console that is not even used. So after these tweaks my Pi shows 216 MByte of available memory and is running for some days now without page allocation errors.

Delay warnings when using USB audio on the Raspberry Pi

My kernel.log and debug log files had grown to over 2 GBytes each. This was due to log messages of this kind:


delay: estimated 0, actual 133

As another blog stated, this can be fixed by adding a parameter to the sound-usb-audio module of Alsa. So I did the same thing, creating a file /etc/modprobe.d/snd_usb_audio.conf with this content:


options snd-usb-audio nrpacks=1

After restarting Alsa or a reboot, the problems were gone, the logfiles kept small.

The Raspberry Pi and Ralink rt2800 based WiFi dongles

I strongly discourage the usage of rt2800 based WiFi dongles with the Raspberry Pi. For some weeks I have been debugging why my Raspberry Pi freezes and crashes all the time. And it turns out that it is the WiFi dongle. I was using an Edimax EW-7711UTn, but the chip is also being used in other devices. There seems to be a bug report for the Raspbian kernel, and I have spent quite some time on the Raspberry forum at Stackexchange. Currently, I hooked up the Raspberry Pi via Ethernet directly to my router. This works fine, but the router is upstairs, while the Raspberry is supposed to be downstairs, connected to my stereo. So this is only a stopgap measure until I have found a WiFi dongle that does not crash the Pi. Hints are welcome.

Update: I now switched to a TP-Link TL-WN725N with a RTL8188CUS chip. This adapter is much smaller (no external antenna) and surprisingly also quite a bit faster. I achieve ~1.9 MByte/sec sustained point to point datarates via SMB. So far no crashes or other problems. I will continue testing, but it looks much better than with the Ralink chip.
Update 2: Almost four months into testing the RTL8188CUS based WiFi dongle and it runs absolutely fine. I have uptimes of almost a month without any problems. After that the Pi degrades due to some of the daemons leaking or memory fragmentation. But I think this is bearable!

How to make the mpdas run as a daemon

The other day I installed the mpdas, which is the audio scrobbler for the music player daemon. Since there’s no debian package for the Raspberry Pi, I compiled mpdas from scratch and installed it. Now I don’t want to run it manually each time the Raspberry Pi boots up. So I found a nice template for writing your own debian-style init-script. I changed it a little and also installed the daemon tool, to turn the interactive mpdas program into a daemon. Just run apt-get install daemon to install it. Then put the following file under /etc/init.d/mpdas and run update-rc.d mpdas defaults. Then mpdas will be run automatically upon boot. Oh, one more thing: put your mpdas configuration under /usr/local/etc/mpdasrc or adjust the DAEMONOPTS in the init script accordingly.


#!/bin/bash
### BEGIN INIT INFO
# Provides: mpdas
# Required-Start: $remote_fs $syslog $mpd
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# X-Interactive: true
# Short-Description: Audio Scrobbler for mpd
# Description: Starts the Audio Scrobbler for the mpd music player daemon.
### END INIT INFO

DAEMON_PATH="/usr/bin/"

DAEMON=daemon
DAEMONOPTS="-u pi -r -X /usr/local/bin/mpdas"

NAME=mpdas
DESC="The mpdas audio scrobbler for mpd"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

case "$1" in
start)
printf "%-50s" "Starting $NAME..."
cd $DAEMON_PATH
PID=`$DAEMON $DAEMONOPTS > /dev/null 2>&1 & echo $!`
#echo "Saving PID" $PID " to " $PIDFILE
if [ -z $PID ]; then
printf "%sn" "Fail"
else
echo $PID > $PIDFILE
printf "%sn" "Ok"
fi
;;
status)
printf "%-50s" "Checking $NAME..."
if [ -f $PIDFILE ]; then
PID=`cat $PIDFILE`
if [ -z "`ps axf | grep ${PID} | grep -v grep`" ]; then
printf "%sn" "Process dead but pidfile exists"
else
echo "Running"
fi
else
printf "%sn" "Service not running"
fi
;;
stop)
printf "%-50s" "Stopping $NAME"
PID=`cat $PIDFILE`
cd $DAEMON_PATH
if [ -f $PIDFILE ]; then
kill -HUP $PID
printf "%sn" "Ok"
rm -f $PIDFILE
else
printf "%sn" "pidfile not found"
fi
;;

restart)
$0 stop
$0 start
;;

*)
echo "Usage: $0 {status|start|stop|restart}"
exit 1
esac

How to make the Raspberry Pi automatically restart the WiFi interface

My WiFi router sometimes goes haywire and the Pi won’t notice when the WiFi connection is up again. So I wrote this little script:

#!/bin/bash                                   

TESTIP=192.168.1.1

ping -c4 ${TESTIP} > /dev/null

if [ $? != 0 ]
then
logger -t $0 "WiFi seems down, restarting"
ifdown --force wlan0
ifup wlan0
else
logger -t $0 "WiFi seems up."
fi

You can put this script under /usr/local/bin and add the following line to the system wide /etc/crontab:

*/5 * * * * root /usr/local/bin/testwifi.sh

This will check every five minutes if the connection is still up, and restart it, if the router cannot be pinged. If you dislike all the syslog messages, you can comment them out in the script.
My corresponding /etc/network/interfaces looks like this (I uninstalled all the network managers):


auto lo

iface lo inet loopback
iface eth0 inet static
address 192.168.3.42
netmask 255.255.255.0

auto wlan0
iface wlan0 inet dhcp
pre-up wpa_supplicant -Dwext -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf -B
post-down killall wpa_supplicant ; rmmod 8192cu ; modprobe 8192cu

iface default inet dhcp

The wpa_supplicant.conf should be easy to generate, there are lots of guides on the web for this.

Attaching a USB sound card to the Raspberry Pi

Since my Raspberry Pi runs headless, and the analog audio output is not that great, I decided to add a USB sound card to my little machine. I took a Roland UA-1G, which I was using before on an OpenWRT machine. The device was immediately recognized:


Bus 001 Device 007: ID 0582:00ea Roland Corp.

However, ALSA will prohibit the card from becoming sound device #0, thus being the default. For that you have to comment out the following line in /etc/modprobe.d/alsa-base.conf:


# Keep snd-usb-audio from beeing loaded as first soundcard
#options snd-usb-audio index=-2

After rebooting or restarting ALSA, the Roland will become the default sound device:


$ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: UA1G [UA-1G], device 0: USB Audio [USB Audio]
Subdevices: 0/1
Subdevice #0: subdevice #0
card 1: ALSA [bcm2835 ALSA], device 0: bcm2835 ALSA [bcm2835 ALSA]
Subdevices: 7/8
Subdevice #0: subdevice #0
Subdevice #1: subdevice #1
Subdevice #2: subdevice #2
Subdevice #3: subdevice #3
Subdevice #4: subdevice #4
Subdevice #5: subdevice #5
Subdevice #6: subdevice #6
Subdevice #7: subdevice #7

How to turn the Raspberry Pi into a music server

I have set up my Raspberry Pi as a music and file server. My requirements were:

  • Big local storage (HD), shared over WiFi
  • Local music playing capability, remotely controllable
  • AirPlay speaker capability
The means by which I fulfilled the requirements were:
  • Platinum 1TB MyDrive and Edimax EW-7711UTn USB wireless adapter
  • mpd Music Player Daemon
  • shairport AirPort emulator
For the first part, I bought a WiFi adapter, the Edimax EW-7711UTn. This one works out of the box with Raspbian, using WPA encryption (Note: I switched to a RTL8188 based dongle by now). It identifies itself with lsusb as:
Bus 001 Device 004: ID 7392:7711 Edimax Technology Co., Ltd EW-7711UTn nLite Wireless Adapter [Ralink RT2870]
This can be easily configured using the wpa_gui tool that comes with the default Raspbian installation. Settings will be saved and restored upon reboot.
Second, I installed samba, samba-common and samba-common-bin for sharing my USB drive. The latter one is a Platinum MyDrive, which is attached to a powered Belkin 7-port USB hub, so that I only need two power supplies. One for the Raspberry Py, and one for the Hub and its attached devices. The MyDrive has been formatted with NTFS, so as to be easily mountable under Linux, OS X and Windows. I mount it using the standard /etc/fstab mechanism. Just added one line to the file:
/dev/sda1       /media/MyDrive  ntfs-3g defaults          0       0
The /etc/samba/smb.conf gets in its first iteration only one additional share, a write-for-all public share, as a big file dump: 
[BigDump]
    comment = Free for all
    read only = no
    path = /media/MyDrive/FreeForAll
    guest ok = yes
Note that literally everybody in your LAN can access this and write to it! You may want to fine tune this…
Now to the mpd. It is easily installed by doing apt-get install mpd. When configuring it via /etc/mpd.conf make sure to change the following lines:
music_directory         “/media/MyDrive/Music/”
password                “MyVeryOwnPassword@read,add,control,admin”
bind_to_address         “any”

Change the directory and password to your liking. Then restart the service or reboot you device. You can control the mpd using a magnitude of clients. For example Theremin for OS X or Mpod for iOS.
Finally, I would like to be able to use the RasPi as an AirPlay target for my Mac and my iOS devices. This can be done via shairport. There are already a lot of good howtos for shairport on the Raspberry Pi. So I refer you to one of those. Two things come to my mind, though:
  1. The Net::SDP library, required by shairport, is not available on Raspbian by default. It is best to clone the github mirror, and go by its installation instructions. Installation via CPAN fails, plus CPAN needs huge amounts of RAM.
  2. The configuration needs to be tweaked a bit. The /etc/init.d/shairport script should be tuned to a sensible name for your RasPi. 
This way, you will be able to see the RasPi in your AirPlay speakers list and it will be happily churning along.

Clementine Player

A couple of years back, I was a big fan of Amarok, the music player. Then came along a rewrite and version 2.0, and I also switched to OS X. The version 2.0 was not very nice, stable or useful. The OS X version was very hard to install, due to the KDE dependencies. So I ditched Amarok. I replaced it with CogX for a while, and now I am using the horrid iTunes and the wonderful mpd (together with MPoD and Theremin). But today I read about the Clementine Player. It’s a cross platform Amarok 1.4 fork. And after 10 minutes of testing, I think it’s wonderful!