Weblog entry #1 for mar

Handling devices with udev
Posted by mar on Wed 31 Jan 2007 at 09:10
Tags:

Recently I was forced to edit some udev init scripts to properly assign device names under the /dev. I will show how to assign the same names to your devices under the /dev, so whenever you plug-in your camera you will find the symlink /dev/my_camera pointing to the real device, whatever it may be. Hope you will find this mini-how-to helpfull.

Maybe you ask, why? The reason is, you may need to write some backup script for USB HDD (once you change hw, you just change the udev rule, the script may remain intact), or just to make the device nodes more readable for your relatives (/dev/ums for USB mass storage is better than /dev/sdb1 IMHO).

Udev is for dynamic device management. When you plug in other device (USB Bluetooth dongle, mice, ...) udev creates nodes under the /dev/* and also it _may_ do some additional actions, like creating a symlink.

In other words, you may say something that reads: "When I insert this usb mass storage, I want it to be reachable via /dev/ums whatever the major and minor numbers are."

In debian you may find configuration for udev in /etc/udev. Entries for devices are under /etc/udev/rules.d directory. For my purposes i created a new file which is automatically proccessed on system startup -- /etc/udev/rules.d/z99.rules -- it looks like this:

# HL-DT-ST DVDRAM GSA-H10N
SUBSYSTEMS=="ide", KERNELS=="0.0", SYMLINK+="cdrom", ENV{GENERATED}="1"
SUBSYSTEMS=="ide", KERNELS=="0.0", SYMLINK+="dvd", ENV{GENERATED}="1"
SUBSYSTEMS=="ide", KERNELS=="0.0", SYMLINK+="cdrw", ENV{GENERATED}="1"
SUBSYSTEMS=="ide", KERNELS=="0.0", SYMLINK+="dvdrw", ENV{GENERATED}="1"

# universal mass storage
SUBSYSTEMS=="block", KERNEL=="sdb1", SYMLINK+="ums", ENV{GENERATED}="1"

This config ensures, that udev will create several symlinks to my cd/dvd-rw combo: (1) /dev/cdrom, (2) /dev/dvd, (3) /dev/cdrw and (4) /dev/dvdrw. And when I insert any usb mass storage, I will be able to find it under /dev/ums.

Ok, you say, the SYMLINK+="cdrom" part is quite well understood, but what the hell is the other mess?

Each line presents a rule for udev. It interprets what is in the line for every device.

SUBSYSTEMS=="ide", KERNELS=="0.0", SYMLINK+="cdrom", ENV{GENERATED}="1"

This line reads: device should have SUBSYSTEM=ide, KERNELS=0.0, SYMLINK /dev/cdrom (if not, create it) and then export a variable GENERATED to the environment. So if a new device is found, those rules are searched for and if matches, some actions may be taken (like creating a symbolic link or exporting something to the env).

To find out the exact values for keys "SUBSYSTEMS" and "KERNELS" for your device a bit of theory and one command might be helpfull.

The theory is, that you search those values in the sysfs filesystem and you need to know for the start, the real device name, ie. /dev/hda (for my cd/dvd-rw combo).

The command is 'udevinfo'.

1. Find out the sysfs device path:

udevinfo -q path -n /dev/hda

2. Find out all the attributes of the device based on sysfs device path:

udevinfo -a -p sysfs_device_path

3. Or use just one command:

udevinfo -a -p `udevinfo -q path -n /dev/hda`

The output of the command ad 3. may be quite long, it contains several sections with key==value pairs that are what you search for. As you will find, running the example ad 3. on my computer will also contain a section:

...snip...

  looking at parent device '/devices/pci0000:00/0000:00:04.0/ide0/0.0':
    KERNELS=="0.0"
    SUBSYSTEMS=="ide"
    DRIVERS=="ide-cdrom"
    ATTRS{modalias}=="ide:m-cdrom"
    ATTRS{drivename}=="hda"
    ATTRS{media}=="cdrom"

...snip...

Where the line KERNELS=="0.0" and SUBSYSTEMS=="ide" can be found, which is exactly what I have in my rule several paragraphs above:

SUBSYSTEMS=="ide", KERNELS=="0.0", SYMLINK+="cdrom", ENV{GENERATED}="1"

You can choose any section (but only one) and you may choose any combination of key==value pairs from that section that make the unique set of key==value pairs identifying your device. Just put them in one line, add the SYMLINK rule and you have the job done.

You may test your setup by running:

udevtest `udevinfo -q path -n /dev/hda`

This command will simulate the actions (matching and symlink creation).

 

Comments on this Entry

Posted by Steve (80.68.xx.xx) on Thu 1 Feb 2007 at 13:08
[ Send Message | View Steve's Scratchpad | View Weblogs ]

Thats a good introduction, and would make a nice article too ;)

Steve

[ Parent | Reply to this comment ]

Posted by impact24 (58.71.xx.xx) on Fri 2 Feb 2007 at 02:52
[ Send Message ]
Wow, great blog entry. I really like how you explain it all step by step and I think you do a very good job explaining what it all means. Perhaps you can make this into an debian-administration article as steve suggested? Cheers!

[ Parent | Reply to this comment ]

Posted by Anonymous (65.113.xx.xx) on Thu 12 Jun 2008 at 23:23
Great Article. Helped me get going. I would add one thing. If you add the symlink as: SYMLINK+="usbdrive%n" it will create partition links as well. Thanks for the info.

[ Parent | Reply to this comment ]

User Login

Username:

Password:

[ Advanced Login ]

Register Account

Quick Site Search