Kernel Korner - udev—Persistent Device Naming in User Space
Starting with the 2.5 kernel, all physical and virtual devices in a system are visible to user space in a hierarchal fashion through sysfs. /sbin/hotplug provides a notification to user space when any device is added or removed from the system. Using these two features, a user-space implementation of a dynamic /dev now is possible that can provide a flexible device naming policy.
This article discusses udev, a program that replaces the functionality of devfs. It provides /dev entries for devices in the system at any moment in time. It also provides features previously unavailable through devfs alone, such as persistent naming for devices when they move around the device tree, a flexible device naming scheme, notification of external systems of device changes and moving all naming policy out of the kernel.
The /dev directory on a Linux machine is where all of the device files for the system should be located. A device file details how a user program can access a specific hardware device or function. For example, the device file /dev/hda traditionally is used to represent the first IDE drive in the system. The name hda corresponds to both a major and a minor number, which are used by the kernel to determine what hardware device to talk to. Currently, a wide range of names that match up to different major and minor numbers has been defined.
All major and minor numbers are assigned a name that matches up with a type of device. This allocation is done by the Linux assigned names and numbers authority (LANANA), and the current device list can be found on its Web site (see the on-line Resources section).
As Linux begins supporting new kinds of devices, these devices need to be assigned a major and minor number range in order for users to access them through the /dev directory. An alternative is to provide access through a filesystem; my 2002 linux.conf.au paper provides more details on how to do this (see the on-line Resources section). In kernel versions 2.4 and earlier, the valid range of major numbers was 1–255, and the valid range of minor numbers was 1–255. Because of this limited range, a freeze was placed on allocating new major and minor numbers during the 2.3 development cycle. This freeze since has been lifted, and the 2.6 kernel had the valid range of major numbers increased to 4,095. More than a million minor numbers are available per major number.
When the kernel finds a new piece of hardware, it typically assigns the next major/minor pair for that kind of hardware to the device. So, on boot, the first USB printer found would be assigned the major number 180 and minor number 0, which is referenced in /dev as /dev/usb/lp0. The second USB printer would be assigned major number 180 and minor number 1, which is referenced in /dev as /dev/usb/lp1. If the user rearranges the USB topology, perhaps adding a USB hub to support more USB devices in the system, the USB probing order of the printers might change the next time the computer is booted, reversing the assignment of the different minor numbers to the two printers.
This same situation holds true for almost any kind of device that can be removed or added while the computer is powered up. With the advent of PCI hot-plug-enabled systems and hot-pluggable buses, such as IEEE 1394, USB and CardBus, almost all devices have this problem.
With the advent of the sysfs filesystem in the 2.5 kernel, the problem of determining which device minor number is assigned to which physical device is much easier to determine. For a system having two different USB printers plugged in to it, the sysfs /sys/class/usb directory tree would look like this:
/sys/class/usb/
|-- lp0
| |-- dev
| |-- device -> ../../../devices/pci0/00:09.0/usb1/1-1/1-1:0
| `-- driver -> ../../../bus/usb/drivers/usblp
`-- lp1
|-- dev
|-- device -> ../../../devices/pci0/00:0d.0/usb3/3-1/3-1:0
`-- driver -> ../../../bus/usb/drivers/usblp
$ cat /sys/class/usb/lp0/device/serial
HXOLL0012202323480
$ cat /sys/class/usb/lp1/device/serial
W09090207101241330
Within the individual USB device directories pointed to by the lp0/device and lp1/device symbolic links, a lot of USB-specific information can be determined, such as the manufacturer of the device and the (hopefully unique) serial number.
As can be seen by the serial files in the above description, the /dev/usb/lp0 device file is associated with the USB printer with serial number HXOLL0012202323480, and the /dev/usb/lp1 device file is associated with the USB printer with serial number W09090207101241330. If these printers are moved around, say by placing them both behind a USB hub, they might be renamed, as they are probed in a different order on startup:
$ tree /sys/class/usb/
/sys/class/usb/
|-- lp0
| |-- dev
| |-- device -> ../../../devices/pci0/00:09.0/usb1/1-1/1-1.1/1-1.1:0
| `-- driver -> ../../../bus/usb/drivers/usblp
`-- lp1
|-- dev
|-- device -> ../../../devices/pci0/00:09.0/usb1/1-1/1-1.4/1-1.4:0
`-- driver -> ../../../bus/usb/drivers/usblp
$ cat /sys/class/usb/lp0/device/serial
W09090207101241330
$ cat /sys/class/usb/lp1/device/serial
HXOLL0012202323480
Trending Topics
| Let's talk advertising | Jan 06, 2018 |
| Where Do I Start? | Jan 06, 2018 |
| Ansible: the Automation Framework That Thinks Like a Sysadmin | Jan 05, 2018 |
| Now What? | Jan 03, 2018 |
| Happy New Year- Welcome to Linux Journal 2.0! | Jan 01, 2018 |
| Linux Journal Ceases Publication | Dec 01, 2017 |
- Where Do I Start?
- Happy New Year- Welcome to Linux Journal 2.0!
- Let's talk advertising
- Ansible: the Automation Framework That Thinks Like a Sysadmin
- Understanding Firewalld in Multi-Zone Configurations
- Simple Server Hardening
- IGEL Universal Desktop Converter
- Linux Journal February 2017
- Server Technology's HDOT Alt-Phase Switched POPS PDU
- A Switch for Your RPi
Geek Guides
Pick up any e-commerce web or mobile app today, and you’ll be holding a mashup of interconnected applications and services from a variety of different providers. For instance, when you connect to Amazon’s e-commerce app, cookies, tags and pixels that are monitored by solutions like Exact Target, BazaarVoice, Bing, Shopzilla, Liveramp and Google Tag Manager track every action you take. You’re presented with special offers and coupons based on your viewing and buying patterns. If you find something you want for your birthday, a third party manages your wish list, which you can share through multiple social- media outlets or email to a friend. When you select something to buy, you find yourself presented with similar items as kind suggestions. And when you finally check out, you’re offered the ability to pay with promo codes, gifts cards, PayPal or a variety of credit cards.
Get the Guide


Comments
What about Garrick?
After reading this fine article, I'm still confused about Garrick. Did he respond and is the font I'm seeing already the small one, or did he utterly ignore all requests directed at him? Would be great if somebody could provide some insight. Thanks.
Changing device order and modprobe.d
I think I have been under the misapprehension that it is possible to change device order or allocation using udev rules. I wanted to make dsp dsp1 and dsp1 dsp, but it seems to me that that is impossible without changing the boot order first. Assigning names to devices only identifies them regardless of the boot order. However it is not possible to reassign those devices following startup, it's all locked up in /sys.
Also, how does /etc/modprobe.d fit into the scheme of aliases/device naming in light of udev. How is the modalias file in /sys applied to /etc/modprobe.d.
Writing udev rules is quite easy with...
I recently needed some custom stuff in /dev on my GNU/Linux Gentoo. Somehow I cam across this wounderfull article about how to actually implement some of all this stuff - and it works, is easy and very customizable.
http://www.reactivated.net/writing_udev_rules.html
Most of the problems people tend to smoke about would be solved by reading that document. These problems are
"They should be named with this syste: ...",
"I want a copy of a device node at ...",
"XXX application can't find /dev/YYY - udev is bad!",
...
...
udev rocks!
udev ruined Mepis
I had to switch from Mepis to SUSE 10.0 because of problems with udev. As far as I can see it's not ready for prime time. THe fact of the matter is it wrecked a lot of previously good Linux distributions. Try managing an iPod SUffle with udev. It's as good as impossible.
udev and why I will not use the 2.6 kernel
I am a gentoo user with to installations, one based on the 2.4 kernel and the other on the 2.6 kernel. I decided to do a system upgrade over the new year. What a mistake. I no longer have a useable modem, DRI still does not function and I suspect the problem may, in fact, be udev, which I have yet to determine how to correctly set rules for even with the docs currently available. Add to this that I once was able to view movies under linux I am no longer able to do so.
I am willing to admit I just do not have the big picture on this, but at the same time I have misgivings as to why such a system is really necessary. IF it is the default for th 2.6 kernel I can be sure of one thing...I will never use that kernel further.
Nice try, but no cigar.
.I will never use that kernel further.
It is not good to smoke Cigars. You are quick to judgement and that makes you closed off to change. The mind is like a parachute, it only functions when it is open. Your attitude is like a child who throws a tantrum. udev is a great improvement and these people work for free. How about a bit more grace. Make peace and love your path..
auto-load drivers a la devfs
FAQ says that driver autoloading is not possible with udev and will not be implemented by ydev maintainer.
But i think there still are projects to achieve this.
For example in AltLinux.com it is done by tmpfs + modules_lookup script
I wonder, if there is somewhere a list of such a projects?
Program field
Well, you could try ysing the program field in udev to fire off an event to another program that could do driver lookups?
BUS="scsi", PROGRAM="/sbin/checkdrv", RESULT="somevalue" might be a start?
Trying sysfs with a USB memory stick
Maybe it's more of a sysfs question, but I immediately tried Greg's suggestion with a USB memory stick. I have several of them, and they always appear at another /dev/sdx or /dev/sdx[1-3] device so this is a proper candidate for udev.
But nothing seems to appear in /sys/class/usb. An entry does appear in /sys/block/sda, but there's no identifying information like a serialno there, and there are suddenly 4 subdirs underneath: sda[1-4].
Is my sysfs set up correctly? How do I figure out the right partition (if any) to mount on such a 'pseudo disk' device?
Re: Kernel Korner: Kernel Korner: udev--Persistent Device Naming
Given that the probe function for every registered USB driver will be called when a device is detected on the USB, what prevents the current lp driver from always choosing the same minor number for the same device regardless of location within the USB. Creating lp1 before or without an lp0 would not cause any problems, would it ?
Re: Kernel Korner: Kernel Korner: udev--Persistent Device Naming
You are proposing that the kernel device driver should maintain a database for the every device it has handled over the time? This sounds like a really bad idea. A lot of devices are very difficult to distinguish from another, e.g. EPSON has the same USB ID's for a lot of printer models and you need to query the printer information with a vendor specific protocol, which surely belongs to userspace.
Thanks,
Kay