<< Prev. Page: Linux Device Hot plugging

udev tool

It automatically detects devices added or removed from system and generates device files for them in the /etc/dev directory. The name of the device files reflects their task like Printer device files begin with lp for ‘line print.' If more than one printer connected to the system, the number is attached after the prefix ‘lp', like lp0, lp1, lp2. The terminal device files begin with ‘tty' for ‘teletype' like tty0, tty1, tty2, and so on.

Each time the system boots, it invokes /sbin/udevstart which runs udev  and create kernel devices making device files in the ‘dev' directory. udev uses a set of rules located in the /etc/udev/rules.d file to direct the generation of device files, including any corresponding symbolic links like /dev/cdrom. The udev keeps track of currently installed devices. The rules files have ‘.rules' extension. Their name starts with numbers to establish priority. They are categorized into three general categories: names, permissions/ownership, and symbolic links.

On Red Hat, Fedora and similar distributions categories included in '50-udev.rules' primary rules file. And on Ubuntu or Debian included in '20-names.rules, 40-permissions.rules and 60-symlink.rules'. For specialized devices its 60-rules-libsane for scanners, 60-rules-libmtp for music players, 60-pcmcia.rules for PCMCIA devices, and 90-alsa.rules for a Sound driver.

Once the device file created, it runs the program:
/etc/dev.d

The rules consist of a comma-separated list of fields composed of matching or assignable keys. The operators used to assign keys values are:

  • +
  • += (appends the value to already attached),
  • := (making assignment values final, preventing later changes) operators

KEY Fields

  • The SYMLINK fields define symbolic links to devices to enable access with other device names. They bear common device names linked to the actual device files used as a shortcut, referencing that file.
  • NAME fields to create an original device interface. Attributes specified by keys match a device, and then the associated name used and device file generated with that name.
  • The KERNEL supports pattern matching to specify device collection. For example, mouse* matches all devices beginning with pattern mouse. The brackets specify the range of numbers or characters like lp[0-9].
  • The PROGRAM determines the device. The key is valid if the program returns ‘successful', which gets matched with the RESULT key.
  • The RESULT matches the returned string of the last PROGRAM call and used after a PROGRAM call.
  • The MATCH matches the event action
  • DEVPATH matches the device path
  • ENV matches the environment variable values
  • BUS matches the type of device
  • DRIVER matches device driver names
  • ID is the device number on the bus
  • SUBSYSTEM matches the device subsystem
  • SYSFS (filename) maches sysfs device attribute. Each device interface has a corresponding sysfs file, containing attributes like serial number, device major and minor numbers, used to identify the device. These attributes are used as keys in the udev rules to create a device interface in the /dev directory which gets listed in udev database with permissions specified in the udev permission rules. The changes manually done get lost with a system reboots.

udev Configuration

/etc/udev/udev.conf

It contains global options such as:

  • udev_root: location of the device files directory set to official device directory /dev on Linux systems and is never changed.
  • udev_rules files: specify the location of rules files used to generate the device files
  • udev_log: enables them to turn ‘logging' on or off
  • udev_permissions: specifies the permission files location that holds permissions applied to specific devices
  • default_mode, default_owner and default_group: to set defaults
  • the location of udev database, the defaults for device permissions, owner and group, and the location of udev rules files.

>> Next Page: Hardware Abstraction Layer (HAL)