Linux – How does udev get device numbers for the devices it creates

linux-kerneludev

Spun off from a small exchange in chat.

One of the primary advantages of udev is that you don't have to worry about major and minor device number exhaustion anymore. But the devices created by udev still have these numbers associated with them. How does udev cooperate with the kernel to create device files with the proper major and minor device numbers?

Best Answer

The kernel allocates a major:minor number, either statically (in drivers that do have a static allocation and haven't run out) or dynamically (in drivers that support dynamic allocation and have used up their static allotment). This number is listed in the message that the kernel sends to udev.

You can see the variables that udev sees about a device in /sys/devices/…/uevent. For example:

$ cat /sys/devices/virtual/mem/null/uevent 
MAJOR=1
MINOR=3
DEVNAME=null
DEVMODE=0666
$ cat /sys/block/sda/sda1/uevent 
MAJOR=8
MINOR=1
DEVNAME=sda1
DEVTYPE=partition

The udev rules determine the name(s) and permissions of the device files. The device number comes from the kernel (it has to, since the kernel has already taken a decision and udev's job is to ensure that applications accessing the device by name will reach the number chosen by the kernel).