Tuesday, April 7, 2009

Removable Devices & udev

The Linux udev capability provides naming support for dynamic devices such as USB drives, cameras, etc. udev rules follow a simple syntax to match a dynamic device when it is first "plugged in" and subsequently name it based on parameters in the matching rule. For the following example, I used CentOS 5.2 and a SanDisk compact flash reader connected via USB.

The first step is to plug the device into your computer and query its characteristics. In my case, when I plugged in the CF reader, it was auto-mounted as /dev/sdb1. Use the udevinfo command to identify the characteristics of the device: udevinfo -a -p $(udevinfo -q path -n /dev/sdb1). This prints a long list of characteristics for the device itself, as well as the "parent" devices. The key is to use these characteristics to generate a rule that matches only the CF reader.

I identified a section from the output of the udevinfo command that seemed to have characteristics that would be unique to the reader. These were the idVendor value of "0781" and the idProduct value of "b4b5". I then went to the udev rules folder (/etc/udev/rules.d) and created a 10-local.rules file. Inside the rules file, I added the following rule: BUS=="usb", SYSFS{idVendor}=="0781", SYSFS{idProduct}="b4b5", NAME+="cf%n". This rule matches on a dynamic device on the usb bus with a Vendor ID of 0781 and a Product ID of b4b5. The output of this rule is defined by the NAME parameter, which simply states to create a device named /dev/cf1, where 1 could vary if other /dev/cf? devices are already in use.

It sometimes takes a few tries to properly define the rule so that it matches the desired devices and performs the desired naming. Two commands are very useful to help you debug and test. The first is the 'udevtest dev-path' command. udevtest takes the device path of the device in question and outputs the actions that would be performed. I used the command 'udevtest /block/sdb/sdb1' for my particular device. If you need to add, delete, or modify a rule, make the change in the file (10-local.rules) and then run 'udevcontrol reload_rules'.

To also define the mount point, I created a mount point in the /media folder named "cfboot" and entered the following line in the /etc/fstab file: /dev/cf1 /media/cfboot ext3 defaults,user 0 0. Note that the 'user' option is required if you wish any user other than root to be able to mount this device. The combination of the udev rule and the fstab line, when the CF reader is plugged in, creates a device node named /dev/cf1 and mounts the device on /media/cfboot.

Resources which I found useful are:
  • http://linux.die.net/man/8/udev
  • http://reactivated.net/writing_udev_rules.html
  • http://ubuntuforums.org/showthread.php?t=168221
  • http://www.linuxjournal.com/article/7316

Labels: , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home