Writing udev rules

udev is since long time ago included in Linux kernel (everything started back in 2003), and here we will not go back and write about history of udev,if you want to know a more about udev it,then do a simple search for it in Google.

Here I am going to write udev rule which will recognize your disk always under same name. The reasons for that,you want to have your USB disk,and all its partitions to be recognized for example as /dev/my_usb and to remain with that name after reboots.

The process described here is tested on RHEL 5, but will work on CentOS, Fedora, … and Debian as well. Writing udev rules is very easy once you get basic understanding what to put inside them. The best starting point for getting information about udev rules are ones already written for your distribution. On RHEL 5 in /etc/udev/rules.d/ you will find a list of udev rules currently present on your system.

For example in /etc/udev/rules.d/50-udev.rules you will find [ here is just part of it ]

KERNEL==”*”, OWNER=”root” GROUP=”root”, MODE=”0600″

  1. all block devices

SUBSYSTEM==”block”, GROUP=”disk”, MODE=”0640″
KERNEL==”root”, GROUP=”disk”, MODE=”0640″

……
……

Every time you plug your external usb/drive ( for printers / cameras / NICs applies the same ) OS will execute actions planned for particular device.If you take a closer look at udev rules in /etc/udev/rules.d/*.rules you will see how how are particular rules written.They are something like

[ as an example ]

ACTION==”add”, SUBSYSTEM==”firmware”, ENV{FIRMWARE}==”*”, RUN=”/sbin/firmware_helper”, OPTIONS=”last_rule”

You now probably wonder from where comes all these options?They come from udev system,as it generate set of actions for devices already connected / newly connected.

For ACTION, SUBSYSTEM, RUN … etc, check

# man udev

So how can one write udev rule? Udev designers made our lives easier,and provided us tools for easier work. As part of udev package is also among other goodies udevinfo

man page for udevinfy says

udevinfo – query device information from the udev database

Fine.

We will not try and see how we can use udevinfo to get more information about particular device we would like to create custom udev rule.In case I know that my disk is marked as /dev/sda, I can run udevinfo to get more information about it

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

It will produce a very long output,with all necessary information for new udev rule. In my case we have (only part)

looking at device ‘/block/sda’:
KERNEL==”sda”
SUBSYSTEM==”block”
….
….
BUS==”scsi”
DRIVER==”sd”
….
….

We can now do simple copy/paste to our new udev rule.Let’s do that

KERNEL==”sda[1-9]”,SUBSYSTEM==”block”,ACTION==”add”,SYMLINK+=”elvir%n”

Here we instructing udev,to check device sda ( and all partitions on it ),which is on subsystem recognized as block device, add them to system,and create an symbolic link for these original devices (sda1, sda2, sda3…) under /dev as elvir1,elvir2,
Above are some options, you can use also disk serial number in udev rule. For disk I have udevinfo return for serial number

SYSFS{serial}==”FC6E441F8289001C”

probably needles to say that adding it to new udev rule is simple as copying it there

KERNEL==”sda[1-9]”,SUBSYSTEM==”block”, SYSFS{serial}==”FC6E441F8289001C”,ACTION==”add”,SYMLINK+=”elvir%n”

New rule must be under /dev/udev/rules.d/45-custom.rules,and must have extension .rules .Important is to mention that udev rules are read sequentially. rule 05-, will be read before 40-, please check your /etc/udev/rules.d/ and check that all rules have names in format

number-rule.name.rules

Udev rules must be in one line,and do not use “line delimiters” in them

We have now have our udev rule, it is simple,but its purpose is to understand writing more complex ones.More about udev,is possible to find

#man udev

man pages give excellent overview of all above mentioned options and their explanations

Advertisements

#udev-writting-udev-rules-disk-storage