Raspberry Pi: getting started, part four

Run at boot time

And here we are again. Maybe this will be the last instalment, maybe it won’t. We’ll see. As I mentioned in my introduction to the Pi, the intention is to use it as a remote USB hub. This requires additional software, and a way to have it running when the Pi boots. But no worries, this too is as easy as pi(e).

What I’m about to tell uses VirtualHere as an example, or in other words: you may use it as a guideline to install other binaries, and let them start or stop automatically.

VirtualHere USB Server

Start by downloading the VirtualHere USB Server binary at http://www.virtualhere.com/usb_server_software. Go for the one called “VirtualHere USB Server for Raspberry Pi “Wheezy” (Hard Float, console only)”. Copy the file, vhusbdpin, to an appropriate bin folder, I choose /usr/bin for that. First we’ll make it owned by root and executable:

sudo chown root:root vhusbdpin
sudo chmod 755 vhusbdpin

Next step is to write a little script that allows us to start and stop the binary as it were a service in Windows, but more appropriately called a “daemon” here. This script will also allow us to start the daemon at boot time. Scripts such as the one we’re going to add normally reside in /etc/init.d, so go there. Below is a simple script I adapted for general use, nothing fancy but you can adapt it for your own use and other daemons as well:

#! /bin/sh
# Provides:          virtualhere
# Required-Start:    $local_fs $network $syslog
# Required-Stop:     $local_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: virtualhere initscript
# Description:       VirtualHere remote USB hub daemon

# initialize variables
DESC="VirtualHere remote USB hub"
ARGS=" -b -c /etc/$NAME.config"

# main
case "$1" in
        echo "Starting $DESC..."
        $BIN $ARGS
        echo "Stopping $DESC..."
        $KILLALL $NAME
        $0 stop
        $0 start
        echo "Usage: $0 {start|stop|restart}"
# end

Some explanation might be in order. The first line is important as it tells the script interpreter that this is a shell script. Equally important is the INIT INFO section, although omitted from many example scripts you’ll find on the internet, without it later on the command that adds our script to the startup script will moan and complain. Set “Provides” to the same value as the name you’ll give your script, so in this case “virtualhere”. The next two lines define which processes should already be running when our script starts, and which processes should still be running when the daemon is stopped at system shutdown. The two lines after that are at which runlevels we’ll allow the daemon to run. The rest is self-explanatory, I think.

And then the script part itself. Because I want to reuse this script as much as possible, the first part just sets some variables. Then these variables are used to either start, stop or restart the daemon. So, in this script, the daemon will be started by: /usr/bin/vhusbdpin -b -c /etc/vhusbdpin.config, and for stopping we’ll use killall vhusbdpin.

Create and edit the script with sudo and nano, you can omit the path if you’re already in the /etc/init.d folder, of course:

sudo nano /etc/init.d/virtualhere

When finished, chmod 755 the file to make it executable.

What remains is to let the script run at system boot. For that, still in the /etc/init.d folder, use the following command:

update-rc.d virtualhere defaults

update-rc.d will check the INIT INFO section in your script and add the script in the system startup script accordingly. That’s one reason why it is important to set “Provides” the same as the name your script.

The VirtualHere USB Server will now start at system boot. You can also manually start, stop or restart it with either:

sudo /etc/init.d/virtualhere start
sudo /etc/init.d/virtualhere stop
sudo /etc/init.d/virtualhere restart

Note that you won’t see the [OK] or [FAILED] messages normally associated with daemon starts and stops. For that to work our script would need to be rather more complex, but that’s outside the scope of this tutorial. Investigate yourself if you think you need this.

Specific to VirtualHere, as you will probably have noted there is a configuration file associated with it. It has been created at the location we specified in our script, the first time we ran our script with the “start” parameter. I edited my vhusbdpin.config file somewhat, changing the hub name (“ServerName”) and disabling the auto discovery service (“UseAVAHI=0”), this means I won’t (can’t) be using the Bonjour service on the client but must connect to the remote USB hub manually.

If you’re not going to use Bonjour anyway, you can safely delete the package “avahi-daemon” again from your Pi:

sudo apt-get remove avahi-daemon


VirtualHere USB Client

Download the appropriate Windows client from http://www.virtualhere.com/usb_client_software and install it. Unfortunately it won’t work on Windows XP, but newer Windows versions are fine, even when running as a virtual machine! If you, like me, don’t want the Bonjour service installed and only want to connect to the USB Server manually (it’s a one-time setting, so why not?), skip the Bonjour installation part during the installation and/or first run. There is no autostart of the client foreseen, but adding it  to the HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run registry key will do the trick.

When running an icon will appear on the taskbar, click it to open the client. Right-click on “USB Hubs” opens a small menu, select “Start minimized” and manually add your USB server. When connected you can start or stop using USB devices connected to your Pi, or auto-connect a device. On first connect Windows will start to install the VirtualHere drivers, unfortunately these are not compliant with Windows XP, that’s why I already said the client doesn’t work on XP. Yes: it will install the client, but not the drivers…

Note that without a VirtualHere license you may only use one device at a time, but that single one will work without any further restrictions.

File transfer with SFTP

When I earlier wrote to copy the USB Server binary file to the /usr/bin folder, where you scratching your head at that time how on earth you were going to accomplish this? You were  probably thinking of downloading it on your Windows PC, right? No need to despair, there are several options:

  • Download it directly to your Pi with the Midori web browser on your LXDE desktop.
  • Download it to a USB stick and plug the stick into your Pi. Note that it is not advisable to hot-plug or -unplug any USB device on your Pi. My Pi has the habit of rebooting when I connect a USB stick. This is not a real problem or fault, but just the way the Pi was designed (unfortunately). In fact the designers have already stated that if they could start all over again with the Pi, the one thing they would do different is the way USB is implemented on the Pi. But that is not going to happen. Anyway, after your Pi has booted with the USB stick attached you’ll find the USB stick is not visible. That’s because you’ll first need to mount the device before you can use it:
sudo mount /dev/sda1 /mnt/usb

will probably work. It will mount the device identified as /dev/sda1 to the folder /mnt/usb. Note that you will have to create /mnt/usb first, or any other name to your liking. In case /dev/sda1 doesn’t work, or you need more mount (and umount) options, use the web.

  • But the best option I kept as the last. Do you need to install a FTP server to transfer files between your PC and your Pi? Not at all! Of course you are already using FileZilla as a FTP client on your PC (if not: why aren’t you?!). Add a site entry in FileZilla, but instead of using the FTP protocol, use SFTP. This is the SSH File Transfer Protocol. Works like a charm! But beware that (and that’s why it is important to secure your login and password) despite starting in your home folder it will give total access to the whole file system on your Pi! I have (not yet) done any investigation on how to restrict this.


One final word…

If there is one additional tip I can offer, and not only by me but many others will agree, its: backup your SD card regularly! Use Win32DiskImager for that, instead of “write” use “read”. Playing around with my Pi, I’ve already been rescued once or twice by a backup. Nothing is as frustrating as having to start all over again from scratch just after you have finished with configuring your Pi. Save yourself the pain, backup.

Oh yes, and this: enjoy your Pi!