Writing a simple udev rule

A unique device node is needed for us in several occasion for a an example When a Tata Docomo  netconnect device is connected it creates three ttyUSB devices  ttyUSB0, ttyUSB1 and  ttyUSB2. one is for mass storage ,one acts a s modem and ones as gprs device so its difficult to identify similarly for usb storage devices it will be like /dev/sdb1,/dev/sda1,/dev/sdb5 etc.So of they have unique names like /dev/modem or /dev/pendrive it will be easy to handle

Getting Info from the Device

Use udevadm to get information about the device. With this information, a udev rule can be created to give the device a unique id (device-node).

udevadm info -a -p `udevadm info -q path -n /dev/sdb1`

Output will be something like this

looking at parent device '/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.0/host4/target4:0:0/4:0:0:0':
KERNELS=="4:0:0:0"
SUBSYSTEMS=="scsi"
DRIVERS=="sd"
ATTRS{rev}=="8.07"
ATTRS{type}=="0"
ATTRS{scsi_level}=="3"
ATTRS{model}=="Transcend 2GB "
ATTRS{state}=="running"
ATTRS{queue_type}=="none"
ATTRS{iodone_cnt}=="0x176"
ATTRS{iorequest_cnt}=="0x176"
ATTRS{timeout}=="30"
ATTRS{evt_media_change}=="0"
ATTRS{max_sectors}=="240"
ATTRS{ioerr_cnt}=="0x2"
ATTRS{queue_depth}=="1"
ATTRS{vendor}=="JetFlash"
ATTRS{device_blocked}=="0"
ATTRS{iocounterbits}=="32"

Rule files are stored in the /etc/udev/rules.d/ directory. We got some advice from the README in that directory on how to name rule files:

We plan to run a script with this rule so we gave it a name that started with a higher number than our other rules but lower than 90. We used the filename:

81-thumbdrive.rules

Rules are in the form of:

key, [key,...] NAME [, SYMLINK]

Create and edit that file

sudo vim /etc/udev/rules.d/81-thumbdrive.rules

For a usb serial device like Tata Docomo Netconnect

SUBSYSTEM=="tty", ATTRS{idVendor}=="12d01", ATTRS{idProduct}=="1001", SYMLINK+="modem"

For a Flash drive

BUS=="usb", ATTRS{idVendor}=="0781", ATTRS{idProduct}=="7400",NAME="sandisk"

Now restart udev

sudo /etc/init.d/udev restart

Now the device can be accessed as /dev/modem or /dev/sandisk etc

Happy Hacking 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s