Gemini AI 101 on udev
udev rules and rules files
udev Rules and Rule Files: Explained
What is udev?
udev is a Linux device manager that dynamically creates and manages device nodes (files) in the /dev
directory. It responds to events from the kernel when devices are added, removed, or their state changes, allowing for dynamic device management and configuration.
What are udev Rules?
udev uses rule files to determine how it identifies devices and creates device names. These rules, typically stored in files with the .rules
extension, provide a way to customize device handling. They are executed in userspace and can perform various actions, such as:
- Renaming device nodes: Assigning a specific name to a device, regardless of the port it’s plugged into.
- Creating symbolic links: Providing alternative, persistent names for devices.
- Setting permissions and ownership: Granting specific users or groups access to devices.
- Executing external programs: Running scripts or commands when a device event occurs.
Where are udev Rule Files Located?
udev rule files are primarily located in the following directories:
- /lib/udev/rules.d/: Contains default rules provided by the system. Avoid editing these files directly.
- /etc/udev/rules.d/: This is where you should place your custom rules. Files in this directory override those in
/lib/udev/rules.d/
if they have the same name.
- /run/udev/rules.d/: Contains temporary rules files.
udev Rule File Naming Convention:
Rule files are processed in lexical (alphabetical) order, regardless of their directory. It’s recommended to name your custom rule files with a numerical prefix followed by a descriptive name and the .rules
extension, like xx-descriptive-name.rules
. This helps control the order in which rules are processed. A priority number of 99
is often used for custom rules to ensure they are processed last.
Syntax of udev Rules:
Each rule is typically written on a single line (though line breaks can be escaped with a backslash \
). Rules consist of comma-separated key-value pairs.
- Match keys: These define the conditions that must be met for a rule to apply. They use operators like
==
(equality) and !=
(inequality).
- Assignment keys: These specify the actions to be taken if the rule matches. They use operators like
=
(assignment) and +=
(add to a list).
Example udev rule:
KERNEL=="video[0-9]*", SUBSYSTEM=="video4linux", SUBSYSTEMS=="usb", ATTRS{idVendor}=="05a9", ATTRS{idProduct}=="4519", SYMLINK+="video-cam"
This rule:
- Matches: Devices with kernel names starting with
video
followed by a number (video[0-9]*
), belonging to the video4linux
and usb
subsystems, and having the specific vendor and product IDs 05a9
and 4519
respectively.
- Action: Creates a symbolic link named
video-cam
pointing to the device node.
Applying and Testing udev Rules:
- Automatic Reload: On systems with inotify support, udev automatically detects changes to rule files and reloads them.
- Manual Reload: If rules don’t reload automatically, use
udevadm control --reload
.
- Testing: Use
udevadm test <device_path>
to test a specific rule and see the generated device attributes and symlinks.
- Triggering Events: Use
udevadm trigger
to force udev to reprocess devices and apply the rules.
Important Considerations:
- Persistence: udev rules enable persistent device naming, ensuring devices are consistently recognized across reboots and reconnections.
- User Permissions: udev rules can be used to set appropriate permissions and ownership for device nodes, allowing non-root users to access devices.
- Avoid Long-Running Processes: Do not start long-running processes directly from
RUN
directives in udev rules, as they may block event handling. Instead, use systemd services triggered by udev rules.