udev is targeted at Linux kernels 2.6 and beyond to provide a userspace solution for a dynamic /dev directory, with persistent device naming. The previous /dev implementation, devfs, is now deprecated, and udev is seen as the successor.
Background: udev and sysfs
The original /dev directories were just populated with every device that might possibly appear in the system. /dev directories were typically very large because of this. devfs came along to provide a more manageable approach (noticeably, it only populated /dev with hardware that is plugged into the system), as well as some other functionality, but the system proved to have problems which could not be easily fixed.
udev is the “new” way of managing /dev directories, designed to clear up some issues with previous /dev implementations, and provide a robust path forward. In order to create and name /dev device nodes corresponding to devices that are present in the system, udev relies on matching information provided by sysfs with rules provided by the user.
sysfs is a new filesystem to the 2.6 kernels. It is managed by the kernel, and exports basic information about the devices currently plugged into your system. udev can use this information to create device nodes corresponding to your hardware. sysfs is mounted at /sys and is browseable.
what udev rule can do?
udev rules are flexible and very powerful. Here are some of the things you can use rules to achieve:
Rename a device node from the default name to something else
Provide an alternative/persistent name for a device node by creating a symbolic link to the default device node
Name a device node based on the output of a program
Change permissions and ownership of a device node
Launch a script when a device node is created or deleted (typically when a device is attached or unplugged)
Rename network interfaces
Built-in persistent naming schemes
udev provides persistent naming for some device types out of the box. This is a very useful feature, and in many circumstances means that your journey ends here: you do not have to write any rules.
udev provides out-of-the-box persistent naming for storage devices in the /dev/disk directory. To view the persistent names which have been created for your storage hardware, you can use the following command:
This works for all storage types.
Rule writing
These udev rule files are kept in the /etc/udev/rules.d directory, and they all must have the .rules suffix, and parsed in lexical order, and in some circumstances, the order in which rules are parsed is important. In general, you want your own rules to be parsed before the defaults, so it is good idea to create a file at /etc/udev/rules.d/10-local.rules and write all your rules into this file.
Rule syntax
Each rule is constructed from a series of key-value pairs, which are separated by commas. match keys are conditions used to identify the device which the rule is acting upon. When all match keys in a rule correspond to the device being handled, then the rule is applied and the actions of the assignment keys are invoked. Every rule should consist of at least one match key and at least one assignment key. e.g. KERNEL==”hdb”, NAME=”my_special_disk”
Basic Rules
udev provides several different match keys which can be used to write rules which match devices very precisely. Some of the most common keys are introduced below, others will be introduced later in this document. For a complete list, see the udev man page.
KERNEL - match against the kernel name for the device
SUBSYSTEM - match against the subsystem of the device
DRIVER - match against the name of the driver backing the device
After you have used a series of match keys to precisely match a device, udev gives you fine control over what happens next, through a range of assignment keys. For a complete list of possible assignment keys, see the udev man page.
The most basic assignment keys are introduced below. Others will be introduced later in this document.
NAME - the name that shall be used for the device node
SYMLINK - a list of symbolic links which act as alternative names for the device node
As hinted above, udev only creates one true device node for one device. If you wish to provide alternate names for this device node, you use the symbolic link functionality. With the SYMLINK assignment, you are actually maintaining a list of symbolic links, all of which will be pointed at the real device node. To manipulate these links, we introduce a new operator for appending to lists: +=. You can append multiple symlinks to the list from any one rule by separating each one with a space.
example #1:
KERNEL=="hdb", NAME="my_spare_disk"
The above rule says: match a device which was named by the kernel as hdb, and instead of calling it hdb, name the device node as my_spare_disk. The device node appears at /dev/my_spare_disk.
example #2:
KERNEL=="hdb", DRIVER=="ide-disk", SYMLINK+="sparedisk"
The above rule says: match a device which was named by the kernel as hdb AND where the driver is ide-disk. Name the device node with the default name and create a symbolic link to it named sparedisk.
### Matching sysfs attributes and find them from sysfs
To make the match criteria more granular, usually it is needed to get attributes from sysfs, which is exposed through /sys/.
There is linux tool "udevadm" which can retrieve sysfs information and show it in a proper way:
e.g. to get a disk /dev/sda info, this can be done below:
Another example, there are many USB serail adapters are connected to the same linux box, they are named as based on the time it is plugged. So sometimes it is very important to create a persistent name based on the USB port it is connected to.
e.g. I want "/dev/ttyUSB0" named as /dev/UUT03 no matter when it is pluggeed into the box. to achieve that, the following rule is needed:
Please note if you are adding a new rule, you need possible restart udev by "udevadm control --reload-rules"
For network devices, there is no entry under /dev/, but still you can find sysfs info using udevadm command, e.g.
From the output, we can see that the device has MAC address "00:1b:21:9b:11:d1", and we could possiblely to make the device name to be something else like "myLan"
KERNEL=="eth*", ATTR{address}=="00:1b:21:9b:11:d1", NAME="myLan"
### monitor udev event
udevadm can show udev event so that you could learn better about the device.
# Reference:
http://reactivated.net/writing_udev_rules.html