Custom hotplugging scripts

Instructions how to configure hotplugging to execute a specific script on adding/removing a device

The first thing you have to do is to get hotplugging working in general. Visit Linux Hotplugging to learn how to do that.
You should have a file named usb.usermap in your /etc/hotplug/ directory. This file handles to scripts for particular devices. You have to set several colums in this file to get a script working. The first colum is the filename of the script. The scripts are always expected in /etc/hotplug/usb/. The next column is, i don’t know what it is for. The next two are the Vendor and Product id of your device. /proc/bud/usb/devices helps you out with these as soon as you plug in your device. You don’t need the rest of the columns. Here is an example for my HP IPAQ:
ipaq 0x0003 0x049f 0x505a 0x0000 0x0000 0x00 0x00 0x00 0x00 0x00 0x00 0x00000005
This has to be ONE single line.

Now that hotplug knows which script to invoke, you can write the script itself. Visit /etc/hotplug/usb.agent for a description of preset variables. Here is an example script, setting up IP-Masquerading for my IPAQ:

#!/bin/bash

#This is the IPAQ Organizer. We want IP-Masquerading
#Bring up the interface (We assume the standard Opie usb IP)
ifconfig usb0 192.168.0.200 netmask 255.255.255.0 pointopoint 192.168.0.202 up
#Write the route to the IPAQ into the routing table
route add -host 192.168.0.202 usb0 > /dev/null
#Now set up IP-Masquerading
iptables -t nat -I POSTROUTING -j MASQUERADE -s 192.168.0.202/16
iptables -I FORWARD -s 192.168.0.202/16 -j ACCEPT
iptables -I FORWARD -d 192.168.0.202/16 -j ACCEPT
#And active it
echo 1 > /proc/sys/net/ipv4/ip_forward
#set the remover script
:> $REMOVER
#Remove all the iptables entries we’ve created
echo “iptables -t nat -D POSTROUTING -j MASQUERADE -s 192.168.0.202/16” >> $REMOVER
echo “iptables -D FORWARD -s 192.168.0.202/16 -j ACCEPT” >> $REMOVER
echo “iptables -D FORWARD -d 192.168.0.202/16 -j ACCEPT” >> $REMOVER
chmod +x $REMOVER

Very important are the line down from “:> $REMOVER”. The REMOVER variable is your script which is called on removing the device.

Ok, thats it, you have your mapping from the device to the script, you have your script itself and have set up a REMOVER script as well.

Have fun!

Leave a Reply

Your email address will not be published. Required fields are marked *