Your Linux Data Center Experts

I finally got some time to look around at how hotplug, udev, and hal work. These are all processes that do something when you connect new hardware or your computer. Read on for my quick summary of how they all fit together.

Hotplug is run directly from the kernel when a new device is attached to your computer (usb, firewire, scsi). The kernel looks in /proc/sys/kernel/hotplug for a command to run when a new device attaches. On most modern distributions, that command is '/sbin/hotplug'.

The kernel passes this command some information about the device type. Hotplug then calls the scripts in /etc/hotplug.d. In fedora core 3 based systems, this includes binaries that notify udev and hal about the new device, and then setup some default env for hotplug.

Once those scripts are run, hotplug runs a /etc/hotplug/{bustype}.agent script (so, /etc/hotplug/usb.agent for usb devices). This script loads any kernel modules needed for the device by looking in modules.usbmap, which is in /lib/modules/uname -r/ and generated by depmod.

Next the script looks for a /etc/hotplug/usb.handmap, which ships with hotplug and is any additional modules that might not be known by the kernel that should be loaded for that device.

Finally it looks for a /etc/hotplug/usb.usermap file that has user/local mappings for the device. In addition to kernel modules, you can tell it to run scripts with these map files.

As I mentioned above, udev (and hal ) get notifed about hotplug events from hotplugs inital startup. All udev does is to look at it's rules files (/etc/udev/rules.d/) and if there is a matching rule it creates the device node for the device in /dev/. It doesn't do anything at all fancy. It can create links to device nodes if you want it to, but thats about it. This is very nice in that /dev used to contain precreated device nodes for every possible device anyone could think of. This made it very difficult to find anything in there, difficult to install (lots of files), and just took up space.

hal (Hardware Abstraction Layer) is a subproject of the freedesktop project. It's a way to collect information about devices that you have and what they can do. It's queried by KDE or Gnome about what devices you have and if one can do something you want to do. As far as I can tell it's just an information gathering process. It doesn't do anything with the devices, just lets KDE or Gnome know they are there.

So, in order to get it so when I connect my USB digital camera it runs a script that copies off the pictures, removes them from the camera and mounts/unmounts the camera, I did the following:

  • First I needed a way to tell hotplug to run my script when this particular device showed up. I connected the camera and ran a 'lsusb' to get the vendor id and product ID. For my camera those showed as “07cf/1001”.
  • I edited the /etc/hotplug/usb.usermap and added my camera in as:

    usbcam 0x0003 0x07cf 0x1001 0x0000 0x0000 0x00 0x00 0x00 0x00 0x00 0x00 0x00000000

That tells it to run the 'usbcam' script when that device connects.
* In the /etc/hotplug/usb/usbcam script, I set it up to do all of my copies: (The way hotplug handles removing a device is to run a “REMOVER” script, which you have to setup when you add the device).

        if [ "$ACTION" = "add" ]; then
        /bin/mount /media/camera
        /usr/local/bin/casio -get
        /usr/local/bin/casio -delete

        if [ ! -z "$REMOVER" ]; then
            mkdir -p `dirname $REMOVER`
            ln -s /etc/hotplug/usb/usbcam $REMOVER

    elif [ "$ACTION" == "remove" ]; then
        umount /media/camera
        /sbin/rmmod ohci-hcd
comments powered by Disqus

Join our other satisfied clients. Contact us today.