What is udev and how do you write custom udev rules in systemv environments (RHEL 6 and earlier)?

Solution Verified - Updated

Environment

  • Red Hat Enterprise Linux 6
  • Red Hat Enterprise Linux 5
  • Red Hat Enterprise Linux 4
  • udev

Issue

  • What is udev used for?
  • How do you write custom udev rules?

Resolution

Udev is the mechanism used to create and name /dev device nodes corresponding to the devices that are present in the system. Udev uses matching information provided by sysfs with rules provided by the user to dynamically add the required device nodes.

Writing udev rules

  • Udev rule files are kept in the /etc/udev/rules.d/ directory.
  • 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, custom rules should be parsed before the defaults, so it's common to create a file at /etc/udev/rules.d/10-local.rules and write all custom rules into that file.
  • The basic format of a rule is:
    • key1="value", key2="value", ... keyN="value", name="value", symlink="value"
    • At least one key and a name must be provided. Extra keys are optional, but all must match for the rule to be applied.

Examples

  • Notification on device add/remove.

    $ cat /etc/udev/rules.d/10-custom.rules
    
    ACTION=="add", KERNEL=="sdb[1-9]", RUN="/usr/bin/wall  SCSI DEVICE ADDED"
    ACTION=="remove", KERNEL=="sdb[1-9]", RUN="/usr/bin/wall SCSI DEVICE REMOVED"
    
  • To setup symlinks to devices. The %n is the partition number.

    $ cat /etc/udev/rules.d/75-custom.rules
    
    ACTION=="add", KERNEL=="sdb[1-9]", SYMLINK="scsi%n"
    ACTION=="remove", KERNEL=="sdb[1-9]", SYMLINK="scsi%n"
    
  • Apply a default setting for max_sectors_kb to newly discovered devices.

    $ cat /etc/udev/rules.d/54-custom.rules
    
    ACTION=="add", KERNEL=="sd*[!0-9]", SYSFS{vendor}=="WDC WD32", RUN+="/bin/sh -c 'echo 128 > /sys/block/%k/queue/max_sectors_kb'"
    ACTION=="add", KERNEL=="sd*[!0-9]", SYSFS{vendor}=="WDC WD32", RUN+="/usr/bin/wall /sys/block/%k/queue/max_sectors_kb set to 128"
    

The 2nd action line is for debug so that a broadcast message that this rule was being applied is sent to the system. Only the first line is needed to apply the desired setting to max_sectors_kb upon device discovery.

  • The rules can have owner/group/modes set as well.

    KERNEL=="sdc5", OWNER="student", GROUP="student", MODE="0600"
    
  • Here, a specific USB will get /dev/fedora%n symlinks for each partition.

    ACTION=="add", KERNEL=="sd*", SYSFS=="4317210A2880EF89", SYMLINK+="fedora%n"
    
    • The serial number above was grabbed from the output of udevinfo -a -p $(udevinfo -q path -n /dev/sdc).
  • There are several keys available but the three most useful ones are BUS, KERNEL, and SYSFS:

    • BUS: Covers how the device is connected (usb, scsi, etc..)
    • KERNEL: Refers to the standard kernel identification of the device (as used by devfs or a static /dev)
    • SYSFS: Keys use the information on each device that appears in the /sys directory

Useful Commands To Use With Udev:

  • RHEL6

    # udevadm info -a -p $(udevadm info -q path -n /dev/sda)
    # udevadm info -a -p /sys/class/net/eth0
    # udevadm info -q path -n /dev/sda1  
    # udevadm control --reload-rules
    
  • RHEL5

    # udevinfo -a -p $(udevinfo -q path -n /dev/sda)
    # udevinfo -a -p /sys/class/net/eth0
    # udevinfo -q path -n /dev/sda1  
    # udevtrigger
    

Note: udev accesses device info from sysfs using libsysfs library calls.

Further reading

More documentation can be found in /usr/share/doc/udev-*/writing_udev_rules/index.html and man 7 udev

What is udev and how do you write custom udev rules in systemd environments (RHEL 7 and later) ?

Components
Category

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.