Sunday, December 2, 2007

Create your own udev rules to control removable devices

http://ubuntuforums.org/showthread.php?t=168221


To start with you need to know the dynamic device node that is given to a device when attached for the first time. The way that I would do this is to use the tail command



So next thing is to find out some unique information from the device, information that will be used in defining the udev rule, remembering a match is required to assign the persistant node. The next command I have used is from the Writing udev rules link at the bottom of this HOWTO
Code:
udevinfo -a -p $(udevinfo -q path -n /dev/sdd)

looking at parent device '/devices/pci0000:00/0000:00:1e.0/0000:01:00.3/usb6/6-3/6-3.1':
KERNELS=="6-3.1"
SUBSYSTEMS=="usb"
DRIVERS=="usb"
ATTRS{serial}=="0010100000000000"
ATTRS{product}=="WD2000JB-00GVA0 "
ATTRS{manufacturer}=="Initio"
ATTRS{maxchild}=="0"
ATTRS{version}==" 2.00"
ATTRS{devnum}=="9"
ATTRS{speed}=="480"
ATTRS{bMaxPacketSize0}=="64"
ATTRS{bNumConfigurations}=="1"
ATTRS{bDeviceProtocol}=="00"
ATTRS{bDeviceSubClass}=="00"
ATTRS{bDeviceClass}=="00"
ATTRS{bcdDevice}=="0304"
ATTRS{idProduct}=="1150"
ATTRS{idVendor}=="13fd"
ATTRS{bMaxPower}==" 2mA"
ATTRS{bmAttributes}=="c0"
ATTRS{bConfigurationValue}=="1"
ATTRS{bNumInterfaces}==" 1"
ATTRS{configuration}==""


Create the rule
root@Orangutang:/etc# cd udev
root@Orangutang:/etc/udev# ls
rules.d udev.conf
root@Orangutang:/etc/udev# cd rules.d
root@Orangutang:/etc/udev/rules.d# ls
00-init.rules 30-cdrom_id.rules 80-programs.rules 85-lvm.rules README
05-options.rules 40-permissions.rules 85-alsa.rules 85-mdadm.rules
20-names.rules 60-symlinks.rules 85-hdparm.rules 85-pcmcia.rules
25-dmsetup.rules 65-persistent-input.rules 85-hwclock.rules 90-modprobe.rules
25-iftab.rules 65-persistent-storage.rules 85-ifupdown.rules 99-udevmonitor.rules


root@Orangutang:/etc/udev/rules.d# sudo vi 10-local.rules

Example
BUS=="usb", SYSFS{product}=="TS128MJFLASHA", KERNEL=="sd?1", NAME="transcend128mb", SYMLINK="usbdevices/transcend128mb"
Mine

BUS=="usb", SYSFS{manufacturer}=="Initio", KERNEL=="sd?1", NAME="InitioUSBHDD", SYMLINK="usbdevices/InitioUSBHDD"


No check it created the nodes correctly.

root@Orangutang:/etc/udev/rules.d# ls /dev/InitioUSBHDD
/dev/InitioUSBHDD
root@Orangutang:/etc/udev/rules.d# ls -l /dev/InitioUSBHDD
brw-rw---- 1 root plugdev 8, 113 2007-12-03 10:42 /dev/InitioUSBHDD
root@Orangutang:/etc/udev/rules.d# cd /dev
root@Orangutang:/dev# cd /dev/InitioUSBHDD
bash: cd: /dev/InitioUSBHDD: Not a directory
root@Orangutang:/dev# cd
root@Orangutang:~# ls
autoftp build mbox rename rrdtemps scripts tesa
bin EmailSmart nohup.out RescueCD rsync_goggin_backup spectral.log
root@Orangutang:~# ls -l /dev/usbdev
ls: /dev/usbdev: No such file or directory
root@Orangutang:~# ls -l /dev/usbdevices/
total 0
lrwxrwxrwx 1 root root 15 2007-12-03 10:42 InitioUSBHDD -> ../InitioUSBHDD
root@Orangutang:~#




Running external programs upon certain events

Yet another reason for writing udev rules is to run a particular program when a device is connected or disconnected. For example, you might want to execute a script to automatically download all of your photos from your digital camera when it is connected.

Do not confuse this with the PROGRAM functionality described above. PROGRAM is used for running programs which produce device names (and they shouldn't do anything other than that). When those programs are being executed, the device node has not yet been created, so acting upon the device in any way is not possible.

The functionality introduced here allows you to run a program after the device node is put in place. This program can act on the device, however it must not run for any extended period of time, because udev is effectively paused while these programs are running. One workaround for this limitation is to make sure your program immediately detaches itself.

Here is an example rule which demonstrates the use of the RUN list assignment:

KERNEL=="sdb", RUN+="/usr/bin/my_program"

When /usr/bin/my_program is executed, various parts of the udev environment are available as environment variables, including key values such as SUBSYSTEM. You can also use the ACTION environment variable to detect whether the device is being connected or disconnected - ACTION will be either "add" or "remove" respectively.

No comments: