Wait for USB

Having installed Debian GNU/Linux 5.0 (“lenny”) on a PC Engines Alix 2D3 system I ran into problems with the external USB disk drive not being mounted at boot-up.

Mounting local filesystems…mount: special device UUID=… does not exist

These error messages were coming when /etc/rcS.d/S35mountall.sh was attempting to mount all the local filesystems from /etc/fstab. In my case the USB subsystem had not yet initialised and so the entries in /dev had not been created yet.

I could simply wait until booting has finished and mount the partitions manually, or add them to /etc/rc.local, but I was after a more elegant solution.

The answer is to introduce a deliberate pause into the boot sequence to allow the USB subsystem to initialise before the mounts are started.

#!/bin/sh
# Script to force boot sequence to wait for USB disk detection
# Copy to /etc/init.d/waitforusb
# ln -s /etc/init.d/waitforusb /etc/rcS.d/S25waitforusb
# Disk drive mount we're waiting on
usbdrive="/dev/sda"
# Max time to wait (in seconds)
maxwait=30
case "$1" in
start)
  echo "Waiting for USB disk drive $usbdrive"
  for (( i = 0; i <= $maxwait; i++ ))
  do
    [ -b $usbdrive ] && exit 0
    echo -n "."
    sleep 1
  done
  exit 1
  ;;
stop)
  ;;
esac

Copy this startup file to /etc/init.d and then create a symbolic link in /etc/rcS.d just before the filesystem checks:

ln -s /etc/init.d/waitforusb /etc/rcS.d/S25waitforusb

The script will check for the disk drive entries in /dev every second and allow boot execution to continue when it’s there. There is a safety timeout which allows execution to continue regardless after 30 seconds.