Certainly! udev is the device manager for the Linux kernel. It manages device nodes in the /dev directory and can run custom actions when devices are added or removed. udev rules are configured in .rules files, typically located in /etc/udev/rules.d/ or /lib/udev/rules.d/. Understanding how udev rules can get overwritten is essential for managing and troubleshooting device configurations.
How udev Rules are Processed
When a device event occurs (e.g., a device is plugged in or removed), udev processes rules in a specific order:
- Built-in Rules: These are the default rules that come with
udev, located in /lib/udev/rules.d/.
- Administrative Rules: These are custom rules created by the system administrator, located in
/etc/udev/rules.d/.
Within each directory, rules are processed in lexicographical order. Files in /lib/udev/rules.d/ are processed before those in /etc/udev/rules.d/. Within each directory the files are processed in alphabetical order.
Overwriting udev Rules
Udev rules can "overwrite" or take precedence over previous rules through the following mechanisms:
-
Rule Order:
- Rules are processed in the order mentioned above. If a rule in
/etc/udev/rules.d/ matches a device after a rule in /lib/udev/rules.d/, the former will take precedence.
- Within each directory, files are processed in alphabetical order. For example,
/etc/udev/rules.d/10-local.rules will be processed before /etc/udev/rules.d/99-local.rules.
- If multiple rules match a device, the last matching rule wins. This means that a rule defined later in the processing order can overwrite the actions defined by an earlier rule.
-
Rule Specificity:
- More specific rules take precedence over more general ones. For example, a rule that matches a device by its unique serial number will take precedence over a rule that matches by a more general attribute like the device type.
- Specificity is determined by the match keys and their values. More specific matches (e.g.,
ATTR{idVendor}=="1234", ATTR{idProduct}=="5678") will overwrite less specific matches (e.g., KERNEL=="sd*").
-
GOTO Statements:
- Within a rules file you can use the
GOTO statement to jump to a labeled section within the same file. This can be used to create more complex rule processing order within a single file.
- If a
GOTO statement is encountered udev will jump to the label and continue processing from there. This can be used to skip over rules or to create loops.
- It is important to note that a
GOTO will only jump to labels within the same file. It cannot be used to jump to labels in other files.
-
Implicit and Explicit Rule Priorities:
- You can explicitly set the priority of a rule using the
PRIORITY key. Rules with a higher numeric priority are processed before those with a lower priority.
- If the
PRIORITY key is not set, the default priority is 0.
- For example
PRIORITY="1000" will be processed before PRIORITY="100".
- Note that priority only affects the order of rules within the same file. It does not affect the order of files.
Examples
-
Order and Specificity:
/lib/udev/rules.d/60-persistent-storage.rules has a general rule for all USB storage devices.
/etc/udev/rules.d/70-custom-storage.rules has a more specific rule for a particular USB stick with a unique serial number.
- When the USB stick is plugged in, the rule in
70-custom-storage.rules will take precedence because it is more specific and is processed after 60-persistent-storage.rules.
-
Priority
/etc/udev/rules.d/70-custom-storage.rules contains two rules.
- The first rule has
PRIORITY="1000" and matches all USB storage devices.
- The second rule has
PRIORITY="100" and matches a particular USB stick with a unique serial number.
- When the USB stick is plugged in, the second rule will take precedence because it has a lower priority and is processed after the first rule within the same file.
Best Practices to Avoid Unintended Overwrites
-
Name Your Rules Files Appropriately:
- Use numeric prefixes to control the order (e.g.,
10-custom.rules, 99-custom.rules).
-
Be Specific:
- Use specific match keys to ensure your rules target the correct devices without conflicting with more general rules.
-
Use PRIORITY Key:
- Use the
PRIORITY key to control the order of rules within a file.
-
Test and Validate:
- Use
udevadm test /sys/path/to/device to simulate device events and see how rules are applied.
- Use
udevadm monitor to monitor udev events in real time.
- Check the udev log (
journalctl -u systemd-udevd) for any issues or to verify that your rules are being applied as expected.
By understanding the order and specificity of udev rule processing, you can effectively manage and avoid unintended overwrites in your udev configurations.