About

Work - Learn - Share

Lê Quang Thành : quangthanh010290@gmail.com | thanhlev@amazon.com.vn

Udev Quick Reference

Table of contents

  1. Revision history
  2. Thông tin
  3. How rules are organized
  4. Ví dụ
    1. Tự động tắt touch pad khi có chuột gắn vào
  5. The rules syntax
    1. Rule match
    2. Rule Action

Revision history

Revision Date Remark
0.1 Feb-14-2023 Init document

Thông tin

How rules are organized

Rule cho udevd được cấu hình thông qua các file *.rules. Có 2 nơi chính lưu trữ các rule này:

Các rule được đặt tên với prefix là số, giúp user cài đặt luôn thứ tự áp dụng.

Ngoài ra, nếu có 2 file có tên giống nhau được đặt ở /usr/lib/udev/rules.d và ở /etc/udev/rules.d/ thì file ở /etc/udev/rules.d/ sẽ overide file cài đặt bởi system

Ví dụ

Tự động tắt touch pad khi có chuột gắn vào

ACTION=="add" \
, ATTRS{idProduct}=="c52f" \
, ATTRS{idVendor}=="046d" \
, ENV{DISPLAY}=":0" \
, ENV{XAUTHORITY}="/run/user/1000/gdm/Xauthority" \
, RUN+="/usr/bin/xinput --disable 16"

The rules syntax

Các rule được thiết kế có 2 phần:

Rule match

udevadm info -ap /devices/pci0000:00/0000:00:14.0/usb1/1-11/1-11:1.0/input/input15
udevadm info /sys/class/net/wlan0 --attribute-walk
KEY Description
ACTION (add/remove/change): Match the name of the event action.
DEVPATH Match the devpath of the event device
KERNEL Match the name of the event device, KERNEL==”fb0”, KERNEL==”card0”
KERNELS Search the devpath upwards for a matching device name.
NAME Match the name of a network interface. It can be used once the NAME key has been set in one of the preceding rules..
SYMLINK Match the name of a symlink targeting the node. It can be used once a SYMLINK key has been set in one of the preceding ules. There may be multiple symlinks; only one needs to match
SUBSYSTEM Match the subsystem of the event device SUBSYSTEM==”graphics”, SUBSYSTEM==”drm”
SUBSYSTEMS Search the devpath upwards for a matching device subsystem name
DRIVER Match the driver name of the event device. Only set this key for devices which are bound to a driver at the time the event is generated
ATTR{filename} Match sysfs attribute value of the event device. Trailing whitespace in the attribute values is ignored unless the specified match value itself contains trailing whitespace
ATTRS{filename} Search the devpath upwards for a device with matching sysfs attribute values. If multiple ATTRS matches are specified, all of them must match on the same device.
SYSCTL{kernel parameter} Match a kernel parameter value.
ENV{key}: biến môi trường (ENV{SYSTEMD_WANTS}+=”display_mywebapp@root.service)
CONST{key}: biến môi trường (ENV{SYSTEMD_WANTS}+=”display_mywebapp@root.service)
TAG: Match dựa trên TAG , vd device tag TAG+="systemd"
TAGS: Search the devpath upwards for a device with matching tag.

Rule Action

Info! Các RUN sẽ không được thực thi ngay mà chờ quá trình kiểm tra tất cả rule hoàn tất

 udevadm test --action="add" \
  /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.1/0003:046D:C52F.0010/input/input39