Linux - udev (rules file)

> Operating System - Kernel (Windows, Unix, Linux) > Linux / Unix

1 - About

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.

udev manages the /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.

The process of rule-writing is the only udev-related tasks that must (optionally) be performed by the user.

Having persistently named device nodes has several advantages. Assume you own two USB storage devices: a digital camera and a USB flash disk. These devices are typically assigned device nodes /dev/sda and /dev/sdb but the exact assignment depends on the order which they were originally connected. This may cause problems to some users, who would benefit greatly if each device could be named persistently every time, e.g. /dev/camera and /dev/flashdisk.

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.

Advertising

3 - Why?

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

Even if there are no matching rules, udev will create the device node with the default name supplied by the kernel.

4 - How to view the persistent name created by udev ?

To view the persistent names which have been created for your storage hardware, you can use the ls command (listing directory) to see the content of the directory (/dev/disk):

ls -lR /dev/disk

In the below example of output, you can see that udev has created

/dev/disk/by-id/ata-VMware_Virtual_IDE_CDROM_Drive_10000000000000000001  

which is a persistent-named symbolic link to the CD Rom partition.

[root@oel11g ~]# ls -lR /dev/disk
.....................
/dev/disk/by-id:
total 0
lrwxrwxrwx 1 root root 9 Jul  1 15:06 ata-VMware_Virtual_IDE_CDROM_Drive_10000000000000000001 -> ../../hdc
.....................
Advertising

5 - Rules files

When deciding how to name a device and which additional actions to perform, udev reads a series of rules files. These files are kept in the /etc/udev/rules.d directory, and they all must have the .rules suffix.

Default udev rules are stored in /etc/udev/rules.d/50-udev.rules. You may find it interesting to look over this file - it includes a few examples, and then some default rules proving a devfs-style /dev layout. However, you should not write rules into this file directly.

Files in /etc/udev/rules.d/ are 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 if you create a file at /etc/udev/rules.d/10-local.rules, all your rules will be first read.

In a rules file, lines starting with “

" are treated as comments. Every other non-blank line is a rule. Rules cannot span multiple lines. udev does not support any form of line continuation. Do not insert any line breaks in your rules, as this will cause udev to see your one rule as multiple rules and will not work as expected.

One device can be matched by more than one rule. This has it's practical advantages, for example, we can write two rules which match the same device, where each one provides its own alternate name for the device. Both alternate names will be created, even if the rules are in separate files. It is important to understand that udev will not stop processing when it finds a matching rule, it will continue searching and attempt to apply every rule that it knows about.

1 - Documentation / Reference

os/linux/udev.txt · Last modified: 2018/09/16 12:42 by gerardnico