Tuesday, March 15, 2011

How To Have udev Run A Script


Ubuntu uses the udev system to handle devices such as USB connected peripherals. By creating a udev rule, you can have a script run when you plug something into a USB port.  This can be helpful if you want to have a script automatically copy data off the USB stick as one example.


To create a new udev rule, you need to get the product and manufacturer information after the USB device is connected. To get that information, you first need to identify the block device node that was assigned after connecting the USB device. This can be done with the following steps:
1. Connect USB device.
2. Run the command: 
$ dmesg


Now look for the device name. The following is an example:
[201777.817900] usb 1-1.2: new high speed USB device using ehci_hcd and address 23
[201778.012085] usb 1-1.2: selecting invalid altsetting 1
[201778.012535] scsi27 : usb-storage 1-1.2:1.0
[201779.005735] scsi 27:0:0:0: Direct-Access USB Flash Memory 1.00 PQ: 0 ANSI: 2
[201779.006925] sd 27:0:0:0: Attached scsi generic sg3 type 0
[201779.010271] sd 27:0:0:0: [sdd] 7823296 512-byte logical blocks: (4.00 GB/3.72 GiB)
[201779.010909] sd 27:0:0:0: [sdd] Write Protect is off
[201779.010917] sd 27:0:0:0: [sdd] Mode Sense: 0b 00 00 08
[201779.010923] sd 27:0:0:0: [sdd] Assuming drive cache: write through
[201779.013841] sd 27:0:0:0: [sdd] Assuming drive cache: write through
[201779.014673] sdd: sdd1 <---- THIS IS THE DEVICE NAME.
[201779.016085] sd 27:0:0:0: [sdd] Assuming drive cache: write through
[201779.016100] sd 27:0:0:0: [sdd] Attached SCSI removable disk


In the previous example you can see the block device node name is sdd. Once you have this name, you can get the product and manufacturer information. This can be done with the udevadm command:
udevadm info --attribute-walk --name /dev/sdd1 > /tmp/udevadm.out


The udevadm command reports allot of information, so I have it redirect the output to a file. After running the udevadm command, I edit the file with vi (Use your preferred editor), and search for the strings product and manufacturer. In the previous example, it is reported as the following:
ATTRS{product}=="USB Flash Memory"
ATTRS{manufacturer}==" "


The specific USB thumb drive in this example doesn't have a vendor string. However, this is not the case for all devices. For example, my mobile phone connected as a USB device reports the following:
ATTRS{manufacturer}=="SAMSUNG "
ATTRS{product}=="SGH-I897 "


Once you have the required information, you can create a rule in the /etc/udev/rules.d/ directory:
sudo vi /etc/udev/rules.d/95-usbtrigger.rules


Refer to the README file in that directory on how to name rule files.
Here is an example of a simple rule. This is just an example. The model, vendor and name of the script to run will be different depending on hardware and script to be run.  You could even exclude some of the attributes if you want to have the script triggered for any USB device:
ACTION=="add", SUBSYSTEM=="usb", ATTRS{product}=="USB Flash Memory",ATTRS{manufacturer}==" ", RUN+="/usr/local/bin/usbtrigger.sh"


The script usbtrigger.sh, is just a simple script that creates a file:
#!/bin/bash

date=`date +%m%d%Y-%k%M%S`
echo 'udev script triggered' > /tmp/usb_trigger_testfile.$date


In this simple example, the script /usr/local/bin/usbtrigger.sh will be run when a USB device with the matching "product" and "manufacturer" is inserted. The usbtrigger.sh script can be replaced with a script to perform the desired functionality.

2 comments: