RHEL: udev rules basics

RHEL: udev rules basics

# Tested on RHEL 6 & 7

# 'udev' is a mechanism for maintaining device nodes on a server. It handles the management
# of the devices nodes through the 'udevd' daemon as well as rules defined on the system.
# This way we can manage the creation of device nodes and their properties and names.

# Once a device is recognized by the kernel it triggers a series of events. First it
# populates /sys structures, then the kernel sends a uevent received by 'udev' and,
# finally, 'udev' creates a device node for the new device or parses the 'udev' rules files,
# under /etc/udev/rules.d/, in alphanumeric order, to decide what action should be taken.



# To query the 'udev' database for device information or the properties of a device from its
# sysfs representation we can use 'udevadm info':

# Export the content of the udev database:

udevadm info --export-db

# Print all sysfs properties of the specified device that can be used in 'udev' rules to match
# the specified device. It prints all devices along the chain, up to the root of sysfs that
# can be used in 'udev' rules.

udevadm info --attribute-walk --name=/dev/<sdc>

# For a scsi device, we may use its unique SCSI identifier



# 'udevd' watch for any changes in rules files and will automatically apply any changes to
# those files. If that's not the case, one can ask 'udevd' to reload the rules files
# (reloading rules does not apply any changes to already existing devices) by:

udevadm control --reload-rules

# Request to trigger the execution of all 'udev' rules:

udevadm trigger



# Writing a simple custom rule
# ------------------------------------------------------------------------------------------

# A 'udev' rule has two parts: one part that tests certain conditions and one part that
# assigns variables (name, permissions, symbolic links, etc) when all conditions are
# fulfilled

# 'udev' rule operators
# ---------------------
#
#   ==   Compare for equality
#   !=   Compare for inequality
#    =   Assign a value to a key
#   +=   Add the value to a key that holds a list of entries
#   :=   Assign a value to a key and lock the key to prevent any further modification


# Some examples of udev rules:

vi /etc/udev/rules.d/<99-my-custom.rules>


# 1.- Create a symbolic link under /dev/mydisks/ for each matching sd device

   SUBSYSTEM=="block", KERNEL=="sd*", SYMLINK:="mydisks/%k"

# If the new device is a block device, then we test if the internal kernel name starts
# with 'sd'. If both match, we add 'mydisks/%k' to the list of symbolic links to be created
# for this device



# 2.- Create a symbolic link and run a command for each sd matching device

   KERNEL=="sd[b-d]", SUBSYSTEM=="block", SUBSYSTEMS=="scsi", DRIVERS=="sd", SYMLINK+="shared/%k", RUN+="/usr/bin/wall MESSAGE:
shared/%k  -->  $tempnode"


ll /dev/sd*
   brw-rw---- 1 root disk 8,  0 Jul  2 17:18 /dev/sda
   brw-rw---- 1 root disk 8,  1 Jul  2 17:18 /dev/sda1
   brw-rw---- 1 root disk 8,  2 Jul  2 17:18 /dev/sda2
   brw-rw---- 1 root disk 8, 16 Jul  2 17:18 /dev/sdb
   brw-rw---- 1 root disk 8, 32 Jul  2 17:18 /dev/sdc
   brw-rw---- 1 root disk 8, 48 Jul  2 17:18 /dev/sdd


udevadm trigger

   Broadcast message from root@mynode (Wed Jul  2 17:18:44 2014):

   MESSAGE: /dev/sdd

   Broadcast message from root@mynode (Wed Jul  2 17:18:45 2014):

   MESSAGE: /dev/sdb

   Broadcast message from root@mynode (Wed Jul  2 17:18:46 2014):

   MESSAGE: /dev/sdc


ll /dev/shared/
   total 0
   lrwxrwxrwx 1 root root 6 Jul  2 17:18 sdb -> ../sdb
   lrwxrwxrwx 1 root root 6 Jul  2 17:18 sdc -> ../sdc
   lrwxrwxrwx 1 root root 6 Jul  2 17:18 sdd -> ../sdd



# 3.- Name a network interface bearing a given mac-address (/etc/udev/rules.d/70-persistent-net.rules)

SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="08:00:27:48:a7:ef", ATTR{type}=="1", KERNEL=="eth*", NAME="my_eth0"



# 4.- Create a symbolic link and run a command for a device matching a given SCSI identifier

SUBSYSTEM=="block", KERNEL=="sd?", PROGRAM=="/usr/lib/udev/scsi_id -g -u -d /dev/%k", RESULT=="1ATA_VBOX_HARDDISK_VB0d221c89-8b845902", SYMLINK:="shared/disk01"


ll /dev/shared
   total 0
   lrwxrwxrwx 1 root root 6 Feb  8 16:18 disk01 -> ../sdb



# 5.- Creating Oracle ASM devices under /dev/oracle/ using disks' SCSI identifiers

ACTION=="add", BUS=="scsi", ENV{ID_SERIAL}=="360c22lm4rr4nzf88df13aa9a0c8", NAME="oracle/DGTEST_01", OWNER="oracle", GROUP="dba", MODE="0660"
0 (0)
Article Rating (No Votes)
Rate this article
Attachments
There are no attachments for this article.
Comments
There are no comments for this article. Be the first to post a comment.
Full Name
Email Address
Security Code Security Code
Related Articles RSS Feed
RHCS6: ’fencing’ basics
Viewed 1981 times since Sun, Jun 3, 2018
Easily Find Bugs In Shell Scripts With ShellCheck
Viewed 3024 times since Thu, Apr 18, 2019
Installing and Configuring an OCFS2 Clustered File System
Viewed 5470 times since Sat, Jun 2, 2018
Cron YUM How to use yum-cron to automatically update RHEL/CentOS Linux
Viewed 2174 times since Fri, Oct 26, 2018
Linux – delete the LUN and remove traces from OS
Viewed 2861 times since Tue, May 22, 2018
VMWare tools free
Viewed 8106 times since Mon, Jul 16, 2018
How to encrypt a partition using LUKS?
Viewed 1560 times since Fri, Jul 13, 2018
OpenSSL – sprawdzanie czy klucz pasuje do certyfikatu
Viewed 2398 times since Thu, May 24, 2018
FIO (Flexible I/O) – a benchmark tool for any operating system
Viewed 18994 times since Wed, Jul 25, 2018
How to create a Systemd service in Linux
Viewed 2386 times since Mon, Dec 7, 2020