Hardware Device Management in Linux using udev


===What is udev?===
Udev creates and removes device nodes in /dev, based on events the kernel sends out on device discovery or removal. In other words, Udev is the system that maps hardware devices to files you can interact with in the /dev directory. It’s only been around since 2003/2004. All modern distributions use udev instead of the now depreciated hotplug or devfs.

===Major/Minor numbers and devices===
kernel-corn-windows
The Linux kernel assigns a major and minor number to a hardware device and so files can be created in /dev using this number pair with names such as sda, ttyS0, ttyUSB0, etc., to allow for interaction with the hardware from user space. In the past, device nodes were created for everything supported by the kernel, so you can imagine it would be hard to figure out which devices were actually connected at any given time, especially if you’re using a generic kernel.

===Udev gives you dynamic hardware naming===
Udev runs in user space and creates points in /dev when the kernel detects and recognizes new hardware as it’s attached.

===Udev gives you persistent hardware naming===
When you plug in two USB devices, whichever one is recognized first by the kernel gets the next device name. The same goes for SATA block devices. The first disk will be referred to as “sda” while the next disk will be “sdb”. Interchange the devices the next time you boot, and the names will be swapped. Udev lets you ignore this so you can refer to a device by an identifier. The identifiers made by udev are soft links that point to the right underlying device name.

$ ls -l /dev/disk/by-id/

You can find devices using several different identifiers in the /dev/disk directory. The following four identifiers all point to the same /dev/sda2 device.

by-id : ata-ST590915AS_7RN1NJA1-part1 -> ../../sda2
by-label : SWAP-sda2 -> ../../sda2
by-path : pci-0000:00:1f.2-scsi-0:0:0:0-part1 -> ../../sda2
by-uuid : 1181329b-3198-4753-a408-ca46978ddd59 -> ../../sda2

===Udev API===
You can write your own rules to control how devices are recognized by udev and to create custom events such as running a specific program when a device shows up.

===Building from scratch===
Proc must be mounted at /proc and sysfs must be mounted at /sys. The basic prerequisites you’ll need are olibacl, libglib2, libusb, usbutils, pciutils, and gperf, but if you want to build it from scratch, good luck working with a generic distribution such as Ubuntu or Fedora. Most distributions have their own customized versions of udev that may be relied on for some of their specific features to work properly. You may be able to get away with building your own and using the files from their /etc tree.

$ ls -l /etc/udev/

===Order of Operations===
* /dev is mounted in tmpfs during the boot process.
* Udev copies static devices from /lib/udev/devices to /dev.
* Udev gets uevents from the kernel for all connected devices.
* Udev runs through its rules in rules.d and creates nodes and links in /dev.
* If any rules are changed, udev receives an inotify event and updates accordingly.


Posted on December 28th, by admica in hardware, Linux.

One thought on “Hardware Device Management in Linux using udev