2013-05-16

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.

2013-04-14

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.

2013-04-01

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.

2013-03-24

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 "%s\n" "Fail"
 else
     echo $PID > $PIDFILE
     printf "%s\n" "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 "%s\n" "Process dead but pidfile exists"
     else
  echo "Running"
     fi
 else
     printf "%s\n" "Service not running"
 fi
;;
stop)
 printf "%-50s" "Stopping $NAME"
     PID=`cat $PIDFILE`
     cd $DAEMON_PATH
 if [ -f $PIDFILE ]; then
     kill -HUP $PID
     printf "%s\n" "Ok"
     rm -f $PIDFILE
 else
     printf "%s\n" "pidfile not found"
 fi
;;

restart)
$0 stop
$0 start
;;

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

2013-03-22

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.

2013-03-15

Good console fonts for OS X terminal applications

Today I was looking for a better console font for the Apple terminal application. Up until now I was using a variant of the Terminus font, but it did not have the line drawing characters. So several programs looked quite borked. But there is a nice page with a collection of the X11 terminal fonts in Apple's dfont format, which you know and like from all kinds of Linux distributions.
You have to tweak the spacings, for example for the 7x14 font you need to pick 0.98 and 0.93 for the horizontal and vertical spacing. Also make sure to use the fonts at their specified size. I.e. the 7x14 font at 14 points only.


2013-03-10

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