Manage devices with udev#

Devices are managed by a framework called udev.

Getting information about a found device#

To get more information about a device found under /dev run

$ udevadm info -n /dev/<device name>

e.g.

$  udevadm info -n /dev/spidev1.0
P: /devices/platform/soc@0/30800000.bus/30820000.spi/spi_master/spi1/spi1.0/spidev/spidev1.0
N: spidev1.0
L: 0
S: bus/spi/by-id/SPI0.0
E: DEVPATH=/devices/platform/soc@0/30800000.bus/30820000.spi/spi_master/spi1/spi1.0/spidev/spidev1.0
E: DEVNAME=/dev/spidev1.0
E: MAJOR=153
E: MINOR=0
E: SUBSYSTEM=spidev
E: USEC_INITIALIZED=21403971
E: DEVLINKS=/dev/bus/spi/by-id/SPI0.0

Here you can see which actual device (P) is use under the /sys tree. Most devices will expose additional parameters and information under that tree.

e.g.

$ ls /sys/devices/platform/soc@0/30800000.bus/30820000.spi/spi_master/spi1/spi1.0/spidev/spidev1.0
consumers  dev        device     power      subsystem  suppliers  uevent
$ cat /sys/devices/platform/soc@0/30800000.bus/30820000.spi/spi_master/spi1/spi1.0/spidev/spidev1.0/power/runtime_suspended_time
0

Creating a fixed name for a device#

To create a fixed path for a device (e.g. /sys/devices/platform/soc@0/30800000.bus/30820000.spi/spi_master/spi1/spi1.0/spidev/spidev1.0) you will need to create a file /etc/udev/rules.d/00-my-device.rules

$ vi /etc/udev/rules.d/00-my-device.rules
# or
$ nano /etc/udev/rules.d/00-my-device.rules

now you will need to define the udev rule

ACTION=="add", DEVPATH=="<path to the device, without the /sys/ prefix>", SYMLINK+="<path under /dev>"

e.g.

ACTION=="add", DEVPATH=="/devices/platform/soc@0/30800000.bus/30820000.spi/spi_master/spi1/spi1.0/spidev/spidev1.0", SYMLINK+="my/spi-device"

would create an entry /dev/my/spi-device if the device has been found.

Reload required

You will need to reload the udev rules by running

$ udevadm control --reload-rules

add reattach the device (or reboot).

Wildcards

Within the DEVPATH setting you can use wildcards, such as ? for a single character or * for any amount of characters.

Testing your rules#

To test your custom rules you can make use of udevadm test. E.g. for our above example run

$ udevadm test /sys/devices/platform/soc@0/30800000.bus/30820000.spi/spi_master/spi1/spi1.0/spidev/spidev1.0
Load module index
Found cgroup2 on /sys/fs/cgroup/unified, unified hierarchy for systemd controller
Found container virtualization none.
Loaded timestamp for '/etc/systemd/network'.
Loaded timestamp for '/run/systemd/network'.
Skipping overridden file '/lib/systemd/network/99-default.link'.
Skipping empty file: /etc/systemd/network/99-default.link
Parsed configuration file /lib/systemd/network/70-CAN1.link
Parsed configuration file /lib/systemd/network/70-CAN0.link
Created link configuration context.
Loaded timestamp for '/etc/udev/rules.d'.
Reading rules file: /lib/udev/rules.d/10-eth-interfaces.rules
...
spidev1.0: /lib/udev/rules.d/10-spi.rules:6 LINK 'bus/spi/by-id/SPI0.0'
spidev1.0: Preserve permissions of /dev/spidev1.0, uid=0, gid=0, mode=0600
spidev1.0: Handling device node '/dev/spidev1.0', devnum=c153:0
spidev1.0: sd-device: Created db file '/run/udev/data/c153:0' for '/devices/platform/soc@0/30800000.bus/30820000.spi/spi_master/spi1/spi1.0/spidev/spidev1.0'
DEVPATH=/devices/platform/soc@0/30800000.bus/30820000.spi/spi_master/spi1/spi1.0/spidev/spidev1.0
DEVNAME=/dev/spidev1.0
MAJOR=153
MINOR=0
ACTION=add
SUBSYSTEM=spidev
DEVLINKS=/dev/bus/spi/by-id/SPI0.0 /dev/my/spi-device
USEC_INITIALIZED=21403971
Unload module index
Unloaded link configuration context.

At the bottom you would see the effective settings determined by udev.

Any syntax error would be shown as part of the log.

In case your custom settings won’t show up at the bottom, it is best to check the used DEVPATH first.