Word count: 1104, reading time approximately 6 minutes
Udev Hotplug Rules in Linux
Background
Udev is a tool used in Linux systems to manage device nodes. It can dynamically create device files based on the attributes of the devices and execute specific commands, typically used for handling hotplugging. Udev rules are used to specify these behaviors, generally stored in <span>/etc/udev/rules.d/</span>
directory in <span>.rules</span>
files.
Introduction to Udev Rule Syntax
The syntax of udev rules is as follows:
ACTION=="action", KERNEL=="kernel", ATTR{attribute}=="value", ... , RUN{command}
Here is a detailed explanation of each part of the udev rule:
-
• <span>ACTION</span>
: The type of event that triggers the rule, such as<span>add</span>
,<span>remove</span>
,<span>change</span>
, etc. -
• <span>KERNEL</span>
: The kernel name of the device, usually the name of the device file under<span>/dev</span>
, excluding the<span>/dev/</span>
prefix. -
• <span>ATTR{attribute}</span>
: The attribute of the device, typically corresponding to attributes in the sysfs filesystem. -
• <span>VALUE</span>
: The value of the attribute. -
• <span>RUN{command}</span>
: The command to execute when the rule matches.
Each line in the rule file typically represents a separate rule, or a rule can be continued to the next line using the <span>\</span>
symbol, with comments starting with <span>#</span>
. Below are some keywords in the rules and their meanings:
-
• <span>==</span>
: Means equal to. -
• <span>!=</span>
: Means not equal to. -
• <span>></span>
,<span><</span>
,<span>>=</span>
,<span><=</span>
: Used for size comparisons. -
• <span>RUN{}</span>
: The program to run when the rule matches,<span>RUN+</span>
means to run after all rules are processed. -
• <span>IMPORT{}</span>
: Import the result of an external program into the environment. -
• <span>NAME</span>
: Set the name of the device node. -
• <span>SYMLINK</span>
: Create a symbolic link for the device node. -
• <span>OWNER</span>
,<span>GROUP</span>
,<span>MODE</span>
: Set ownership, group, and permissions for the device node. Udev rules can be very complex and can be customized as needed to meet various device management requirements.
Examples of Udev Rules
Here are some example rules:
-
1. Set a label for a specific USB storage device: ACTION=="add", KERNEL=="sd?[", SUBSYSTEMS=="usb", ATTRS{idVendor}=="0951", ATTRS{idProduct}=="1603", SYMLINK+=\"my_usb_drive\"
This rule creates a symbolic link named
<span>my_usb_drive</span>
when a USB storage device with a specific vendor and product ID is detected. -
2. Run a script when a USB mouse is plugged in: ACTION=="add", KERNEL=="mouse[0-9]*", SUBSYSTEM=="input", RUN+="/usr/local/bin/mouse-connected.sh"
-
3. Run a script and pass relevant parameters when a specific device is inserted: SUBSYSTEM=="usb", ATTRS{idVendor}=="3574", ATTRS{idProduct}=="6241", ACTION=="add", RUN+="/usr/local/bin/my_shell.sh %s{idVendor} %s{idProduct} %k"
Here,
<span>%k </span>
and<span>%s</span>
are placeholders for dynamic replacement, where<span>%k </span>
represents the device kernel name, and<span>%s</span>
represents the device attribute value, typically used to reference a specific attribute of the device, corresponding to the field descriptions in the first section. -
4. Set permissions for all serial devices: KERNEL=="ttyUSB[0-9]*", MODE="0666"
This rule sets the permissions for all serial devices (e.g., USB-to-serial devices) to be readable and writable by all users.
Activation and Triggering of Udev Rules
Udev rules are typically triggered when devices are added, removed, or changed. If you have created new udev rules or modified existing ones and want them to take effect immediately, rather than waiting for the next device plug/unplug event, you can use the <span>udevadm</span>
tool to make the rules take effect immediately:
-
1. Reload udev rules: You can reload udev rules using the <span>udevadm</span>
tool without rebooting the system. The following command can do this:sudo udevadm control --reload-rules
This command tells udev to re-read the rules files.
-
2. Trigger udev events for devices: If you want to apply the new rules to already connected devices, you can use the following command to trigger udev events: sudo udevadm trigger
This command triggers the uevent for all devices, allowing udev to reprocess them according to the new rules.
-
3. For specific devices: If you only want to apply new rules to a specific device, you can use the following command: sudo udevadm trigger --action=add --subsystem-match=subsystem --attr-match=ATTR{attribute}=value
Replace
<span>subsystem</span>
with your device subsystem (e.g.,<span>usb</span>
,<span>sound</span>
, etc.), and replace<span>attribute</span>
and<span>value</span>
with your device’s specific attribute and value. -
4. Check if rules are correctly applied: You can use the following command to check if the rules have been correctly applied: sudo udevadm test /sys/class/your_deviceSubsystem/your_device
Replace
<span>your_deviceSubsystem</span>
and<span>your_device</span>
with the path of your device under<span>/sys/class/</span>
.
Please note that these commands require root privileges to execute. If you encounter issues after modifying the rules files, you can check the udev logs for debugging using the <span>journalctl -u udev</span>
command or by directly viewing the <span>syslog</span>
.
Note:<span>udevadm</span>
is a command-line tool used in Linux systems to manage and debug the udev device manager. It provides various functions, including viewing device information, triggering device events, and monitoring device changes.
Welcome to Follow:
For more, please click the bottom left corner Read the original article!