Zaurus - hotplug

I tried for a few minutes to put the commands here into a file in /etc/hotplug/usb/usbnet, but things weren't working right. CDCEther kept coming up, and even reading the CDCEther source file didn't tell me what CDCEther was. I had installed hotplug from scratch, and I couldn't tell what it was doing or where it made its decisions.

As I want something quite simple-- run some shell cmds when I plug a device into usb, run some other ones when I disconnect it-- I hacked the top of /etc/hotplug/usb.agent to run my own hotplug system. Here's how to change the top of /etc/hotplug/usb.agent so it runs your own program, and then abort if your program is successful (otherwise, we proceed with the normal usb.agent):

...
cd /etc/hotplug
. hotplug.functions
# DEBUG=yes export DEBUG


###############################################################################
###############################################################################

/etc/hotplug/drewp-plug
if [ $? = 0 ]; then
  exit 0
fi
mesg "proceed with old usb.agent"

###############################################################################
###############################################################################

# generated by modutils, for current 2.4.x (and later) kernels
MAP_CURRENT=$MODULE_DIR/modules.usbmap
...

Then, I quickly wrote my own hotplug program:

#!/usr/local/bin/python
import os,sys,logging

# SysLogHandler isn't working, so i log to stdout and
# send that to syslog.
sys.stdout=os.popen("/usr/bin/logger -t drewp-plug","w")
log=logging.getLogger()
hdlr = logging.StreamHandler(sys.stdout)#SysLogHandler()
hdlr.setFormatter(logging.Formatter("%(levelname)s (%(lineno)d): %(message)s"))
log.addHandler(hdlr) 
log.setLevel(logging.INFO)

def run(cmd):
    log.debug("running %r" % cmd)
    os.system(cmd)

def zaurus_add():
  for l in """modprobe usbnet
      ifconfig usb0 192.168.129.1 netmask 255.255.255.255 up
      dhcpd -q usb0
      route add -host 192.168.129.201 usb0
      iptables -t nat -F
      iptables -t nat -A POSTROUTING -j SNAT -o eth0 --to 10.1.0.229
      echo 1 > /proc/sys/net/ipv4/ip_forward""".splitlines():
    run(l.strip())

def zaurus_remove():
    run("kill `cat /var/run/dhcpd.pid`")



##############################################################################
try:
    product={'4dd/8004/0':'zaurus',
             '4a9/3065/1':'digicam',}[os.getenv("PRODUCT")]
except KeyError:
    log.warn("product %s unknown to /etc/hotplug/drewp-plug" % os.getenv('PRODUCT'))
    sys.exit(1)

func=locals()["%s_%s"%(product,os.getenv("ACTION"))]
logging.info("calling %r" % func)
func()

sys.exit(0)

Among the unfortunate hacks in this program are:

  • spawning /usr/bin/logger because of some issue with SysLogHandler
  • hardcoding the product ID codes in here, instead of using whatever table hotplug was using (I don't even know where that is)
  • writing the program in the first place instead of u sing the standard hotplug system completely

But the benefits are:

  • it's not in written in shell
  • my Zaurus is hotplugging sooner than it would have, and I won't have to adjust this for a long time