Home > Uncategorized > Making Udev to work for you

Making Udev to work for you

What is Udev?

Whenever you plug a device in your linux box,the node(device file) corresponding to the device is created in /dev directory.Udev is daemon which manages dynamic creation of these files.udev not only provides dynamic device management but also creation of user chosen device names,running a script or program when a device node is created.
The above functionality of udev is controlled by rules.The default rules are kept in /etc/udev/rules.d/ directory.The sweet part of udev is that we can write our own rules and make udev work for us :).

Well how i made udev to work for me?

I have no broadband connection 😦 (fighting with my dad to get one :P).I use my nokia 3110c mobile phone to connect Airtel GPRS/EDGE.To connect the internet i plug my mobile using a usb connector the usbmodem(well mobile acts as usbmodem) will be detected as /dev/ttyACM0.Then i open terminal use wvdial command to connect the internet,its really a boring task to open the terminal and issue wvdial command each time.

How to write a rule so that it automatically connects me to the internet when i plug-in my mobile:

To write a rule we should identify and match the device uniquely, in my case the device is my usb modem.
There are several matching keys available so that you can precisely finger point your device to udev and make udev to take appropriate action defined by you.

Here are some matching keys:
* KERNELS – match against the kernel name for the device, or the kernel name for any of the parent devices
* SUBSYSTEMS – match against the subsystem of the device, or the subsystem of any of the parent devices
* DRIVERS – match against the name of the driver backing the device, or the name of the driver backing any of the parent devices
* ATTRS – match a sysfs attribute of the device, or a sysfs attribute of any of the parent devices

For example to pinpoint my hard drive to udev the rule will be like this:

KERNEL==”sda”,SUBSYSTEM==”block”,ATTR{size}==”312581808″,NAME=”my_hard_drive”

The above rule specifies udev if the kernel name for the disk is ‘sda’, if its a block device and the size of it is 312581808 then name it as my_hard_drive..so you can see /dev/my_hard_drive device file created when the rule is parsed by udev.

Well one might wonder how did i get these information? Here comes udevinfo in handy.

[root@localhost ~]# udevinfo -a -p /sys/block/sda/

The above command will give you the detailed information about the device(hard drive in this case) which is something like this.

looking at device ‘/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda’:
KERNEL==”sda”
SUBSYSTEM==”block”
DRIVER==””
ATTR{range}==”16″
ATTR{removable}==”0″
ATTR{size}==”312581808″
ATTR{capability}==”12″
ATTR{stat}==” 60259 55701 1692944 1457888 342794 1393752 13915984 186839073 0 1982886 188349105″

looking at parent device ‘/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block’:
KERNELS==”block”
SUBSYSTEMS==””
DRIVERS==””

looking at parent device ‘/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0’:
KERNELS==”0:0:0:0″
SUBSYSTEMS==”scsi”
DRIVERS==”sd”
ATTRS{device_blocked}==”0″
ATTRS{type}==”0″
ATTRS{scsi_level}==”6″
ATTRS{vendor}==”ATA “
ATTRS{model}==”ST9160823ASG “
ATTRS{rev}==”3.AD”
ATTRS{state}==”running”
ATTRS{timeout}==”60″
ATTRS{iocounterbits}==”32″
The output is truncated….

when writing rules you cannot combine and mix-up attribute of diffrent parent devices.

The above method might be easy if you can find where your device is in sysfs file system (i.e /sys ).But its really difficult(atleast for the firsttimer ) to find out exactly the desired device in /sys folder.To get around this problem just plug-in your device and use the below command.

udevinfo -a -p $(udevinfo -q path -n /dev/)

To list the attributes details about usb modem(mobile) use the below command:

udevinfo -a -p $(udevinfo -q path -n /dev/ttyACM0)

well ttyACM0 is the usb modem i just inserted.The output will be something like….

looking at device ‘/devices/pci0000:00/0000:00:1d.1/usb6/6-2/6-2:1.1/tty/ttyACM0’:
KERNEL==”ttyACM0″
SUBSYSTEM==”tty”
DRIVER==””

looking at parent device ‘/devices/pci0000:00/0000:00:1d.1/usb6/6-2/6-2:1.1/tty’:
KERNELS==”tty”
SUBSYSTEMS==””
DRIVERS==””

looking at parent device ‘/devices/pci0000:00/0000:00:1d.1/usb6/6-2/6-2:1.1’:
KERNELS==”6-2:1.1″
SUBSYSTEMS==”usb”
DRIVERS==”cdc_acm”
ATTRS{bInterfaceNumber}==”01″
ATTRS{bAlternateSetting}==” 0″
ATTRS{bNumEndpoints}==”01″
ATTRS{bInterfaceClass}==”02″
ATTRS{bInterfaceSubClass}==”02″
ATTRS{bInterfaceProtocol}==”01″
ATTRS{modalias}==”usb:v0421p005Ed0491dc02dsc00dp00ic02isc02ip01″
ATTRS{bmCapabilities}==”6″

looking at parent device ‘/devices/pci0000:00/0000:00:1d.1/usb6/6-2’:
KERNELS==”6-2″
SUBSYSTEMS==”usb”
DRIVERS==”usb”
ATTRS{configuration}==””
ATTRS{bNumInterfaces}==”15″
ATTRS{bConfigurationValue}==”1″
ATTRS{bmAttributes}==”e0″
ATTRS{bMaxPower}==”100mA”
ATTRS{urbnum}==”13625″
ATTRS{idVendor}==”0421″
ATTRS{idProduct}==”005e”
ATTRS{bcdDevice}==”0491″
ATTRS{bDeviceClass}==”02″
ATTRS{bDeviceSubClass}==”00″
ATTRS{bDeviceProtocol}==”00″
ATTRS{bNumConfigurations}==”1″
ATTRS{bMaxPacketSize0}==”64″
ATTRS{speed}==”12″
ATTRS{busnum}==”6″
ATTRS{devnum}==”5″
ATTRS{version}==” 2.00″
ATTRS{maxchild}==”0″
ATTRS{quirks}==”0x0″
ATTRS{authorized}==”1″
ATTRS{manufacturer}==”Nokia”
ATTRS{product}==”Nokia 3110c”

[The output is truncated…]
And here is the rule:

KERNEL==”ttyACM0″,SUBSYSTEM==”tty”,ATTRS{manufacturer}==”Nokia”,ATTRS{product}==”Nokia 3110c”,RUN+=”/usr/bin/wvdial”

RUN+=”/usr/bin/wvdial” calls the wvdial to dial the internet (my case airtel :)) when the modem is detected by udev in /dev/ttyACM0 according to the rule written it will automatically call wvdial to connect 🙂

Create a new file something called 10-local.rules inside /etc/udev/rules.d/ directory, write your own rules and save it.Remember don’t mess with the existing rule files!

Custom rules can be written to handle any device like digital camera,pendrive,external harddrive,etc that you plug-in into your linux box…

So lets start writing rules…… 🙂 i guess does windows have such feature? or do they allow users to do these type of things without a third-party software?


References and Further reading:


http://reactivated.net/writing_udev_rules.html
-Good documentation on writing udev rules

http://kernel.org/pub/linux/utils/kernel/hotplug/udev_vs_devfs –Udev vs devfs

http://www.kernel.org/pub/linux/docs/device-list/devices.txt — list of devices and their corresponding device names in /dev folder helps you to find your device file names 🙂

Advertisements
Categories: Uncategorized
  1. tissit
    August 10, 2012 at 6:38 pm

    Windows users generally can’t automate their computers to do anything. They need to have a trained monkey at the mouse to start required programs, wait forever for the dialogs to come up and then click “connect”.

  1. No trackbacks yet.

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

%d bloggers like this: