hotplug, udev and hal, oh my! (tummy.com, ltd. Journal Entry)
tummy.com: we do linux

Sunday December 19, 2004 at 14:09
Subject: hotplug, udev and hal, oh my!
Keywords: HAL, Hotplug, Tech, UDEV
Posted by: Kevin Fenzi

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
    		fi
    	
    	elif [ "$ACTION" == "remove" ]; then
    		umount /media/camera
    		/sbin/rmmod ohci-hcd
    	fi
    	

(Post Reply)
Comment
1Paul
Subject: what if i want my palm zire71 work in that way?
Do I have to write a /etc/hotplug/usb/visor myself?
Comment
Author: kevin
Subject: visor hotplugging
Well, you could of course do your own visor hotplug script. There are likely a number of scripts available from a google search, like: http://www.raoult.com/palm/hotplug-visor There are also some pilot apps that run a daemon process that just waits for you to plug the pilot in and syncs it. For example, gnome-pilot has a 'gpilotd' that runs and waits for connections from pilots.
Comment
Author: JZA
Subject: Extending the script to gallery
I guess this is a enhacemnt of your script and it might even be pretty cool to do. Probably using python or something similar. But what if the pictures not just get downloaded to my HD but also uploaded to my website.

I currently have a Gallery web-app. Is this possible?

Comment
dominic
Subject: ipod touch
I presume that udev was already set up to recognise the camera in this case and you'd set up something in mtab to map a path to the camera device?

If I want to do something similar with an ipod touch, i think what I need to do is first set up udev to recognise the ipod and create a /dev/ipod link. Then, set up a mapping that will mount /media/ipod to /dev/ipod and have an ipod hotplug script perform the mounting/unmounting.

Does that sound about right?

Comment
jody
Subject: Fedora problem
Hi

Following your description i wanted to trigger a particular reaction when i insert a memory stick.

However, i have no directory /etc/hotplug on my fedora 8 machine, even though 'locate hotplug' shows lots of kernel modules like for instance /lib/modules/2.6.25.11-60.fc8/kernel/drivers/pci/hotplug/acpiphp.ko

Do i have to do some sort of installation or configuration to get it running?

Thank You
Jody

Comment
Author: Kevin Fenzi
Subject: Re: hotplug, udev and hal, oh my!
> I guess this is a enhacemnt of your script and it might even be pretty cool to do. 
> Probably using python or something similar. 
> But what if the pictures not just get downloaded to my HD 
> but also uploaded to my website.
>
>I currently have a Gallery web-app. Is this possible?

Possibly. I use the 'digikam' application to do that. It has a gallery export.

>Subject: ipod touch
>I presume that udev was already set up to recognise the camera in 
>this case and you'd set up something in mtab to map a path to the camera device?
>
>If I want to do something similar with an ipod touch, 
> i think what I need to do is first set up udev to recognise 
> the ipod and create a /dev/ipod link. Then, set up a mapping 
> that will mount /media/ipod to /dev/ipod and have an ipod 
> hotplug script perform the mounting/unmounting.
>
>Does that sound about right?

It would be on a system using hotplug... see below tho.

>Following your description i wanted to trigger a particular reaction 
> when i insert a memory stick.
>
>However, i have no directory /etc/hotplug on my fedora 8 machine, 
> even though 'locate hotplug' shows lots of kernel modules like for 
> instance /lib/modules/2.6.25.11-60.fc8/kernel/drivers/pci/hotplug/acpiphp.ko
>
>Do i have to do some sort of installation or configuration to get it running?

This article is from 2004, when Fedora still used hotplug. ;) So, it's out of date and no longer usefull for modern installs.

Pretty much every now is done with udev itself. See articles like: http://ubuntuforums.org/showthread.php?t=502864 for more info.

kevin