Chapter 8. Devices and UDEV

Chapter 8. Devices and UDEV

Udev is a tool to perform administration of hardware and external devices (peripherals), both during system boot and when the system is operational.  Devices Nodes are created automatically and they are being used from applications and operating system subsystems in order to transfer data to and from the devices or hardware.

OBJECTIVES

  • Explain the role of Device roles and how they use major and minor numbers
  • Understand the need for udev and list its key components
  • Describe how the udev device manager functions
  • Identify udev rule files and learn how to create custom rules

DEVICE NODES

Block and character devices have filesystem entries associated with them, such entries can be found at this folder

​/dev

​Such nodes can be used by programs in order to stablish a communication among them through normal I/O system using methods like

  • open()
  • close()
  • read()
  • write()

On the other hand, network devices work by transmiting and receiving packets. A device driver can handle multiple device nodes. For example a sound driver can handle several nodes associated with sound related hardware, like speakers or earphones

MAJOR AND MINOR NUMBERS

These numbers basically specify the device driver asociated with the device node, for example the next list of files show that

  • sda
  • sda1
  • sda2

Are device nodes associated with the device driver "8". This number (8) is related with the block device driver that is capable of handle sda devices.

brw-rw----. 1 root    disk      8,   0 mar 17 02:29 sda
brw-rw----. 1 root    disk      8,   1 mar 17 02:29 sda1
brw-rw----. 1 root    disk      8,   2 mar 17 02:29 sda2

Major

The major number in the above case "8" is associated with the device driver.

Minor

The minor number in the above case "0", "1", "2" is used to differenciate the different device nodes that the device driver can handle.

UDEV

The udev method creates device nodes on the fly as they are needed. the "u" in udev stands for user.  This is like that since most of the device operations is being done while in user space. 

This mechanism have interesting features like persistent device naming, which allows a standard way of naming device nodes, that do not depend on the order of device connection or plugging in. This behavior is controlled by udev rules

UDEV COMPONENTS

Udev runs as a daemon (either udevd or systemd-udevd) and monitors a netlink socket. Whenever a new device is initialized or removed, the uevent kernel facility sends a message through the socket, then the udev daemon read such message and takes appropiate action to remove or create a device node with the name and properties defined by the rules.

The 3 main components of udev are

  • libudev : library of devices information
  • udevd : daemon that manages the /dev folder
  • udevadm : utility for control and diagnosis

UDEV AND HOTPLUG

In order to manage the device nodes life cycle, udev uses information from these sources

  • /sys : sysfs pseudo file system may contain registered information about devices "naming and properties"
  • /etc/udev/udev.conf : contains information such as where to place devices nodes, default permissions and ownership
  • /etc/udev/rules.d : contains the rules for device naming 

UDEV DEVICE MANAGER

When a device is connected or removed, the udev deamon realizes about this through its daemond udevd that its being notified by a message to the netlink socket by a kernel uevent.

Once the udev daemon realizes about such action it will search the rules file located at

/etc/udev/rules.d/*.rules 

To see if there are any rules associated to the device that has been added or removed. It then takes the appropiated actions like

  • Device node naming
  • Device node and symbolic links creation
  • Setting file permissions and ownership for the device node
  • Taking other actions to initialize and make device available 

UDEV RULE FILES

These rules are located at

/etc/udev/rules.d/*.rules

With names like

  • 30-usb.rules
  • 90-mycustom.rules

CREATING UDEV RULES

The format for a udev rule is simple :

<match><op>value [, ...] <assignment><op>value [, ...]

There are two separated parts defined in one line.

  • First part : is the matching comparison 
  • Second part : is the assignment part which could have one or more assignment in the format of key value pairs to describle file name, group and even file permissions

Examples

Here is an example of a rules file for a Fitbit device:

$ cat /etc/udev/conf.d/rules.d/99-fitbit.rules

SUBSYSTEM=="usb", ATTR{idVendor}=="2687", ATTR{idProduct}=="fb01", SYMLINK+="fitbit", MODE="0666"

$ cat /etc/udev/conf.d/rules.d/98-kexec.rules

SUBSYSTEM=="cpu", ACTION=="online", PROGRAM="/bin/systemctl try-restart kdump.service" 

SUBSYSTEM=="cpu", ACTION=="offline", PROGRAM="/bin/systemctl try-restart kdump.service" 

SUBSYSTEM=="memory", ACTION=="add", PROGRAM="/bin/systemctl try-restart kdump.service" 

SUBSYSTEM=="memory", ACTION=="remove", PROGRAM="/bin/systemctl try-restart kdump.service"

$ cat 80-kvm.rules

KERNEL=="kvm", GROUP="kvm", MODE="0666"

$ cat 99-fuse.rules

KERNEL=="fuse", MODE="0666",OWNER="root",GROUP="root"

LAB 1

1. Create and implement a rule on your system that will create a symlink called myusb when a USB device is plugged in.

$ vim /etc/udev/rules.d/75-myusb.rules
SUBSYSTEM=="usb", SYMLINK+="myusb"

2. Plug in a USB device to your system. It can be a pendrive, mouse, webcam, etc.
Note: If you are running a virtual machine under a hypervisor, you will have to make sure the USB device is seen by the guest, which usually is just a mouse click which also disconnects it from the host.

3. Get a listing of the /dev directory and see if your symlink was created.

$ ls -lF /dev | grep myusb

4. Remove the USB device. (If it is a drive you should always umount it first for safety.)

5. See if your symbolic link still exists in /dev.

​$ ls -lF /dev | grep myusb