Course contentsShow
← Linux
Lesson 1777 of 1,81915. Kernel Concepts for Systems AdministratorsFree lesson

Hotplug event handling for hardware

You will implement rules for automated driver loading when hardware is detected.

In this lesson, you will learn how the Linux kernel notifies user space when hardware is connected and how you can automate driver loading through udev rules.

The Hotplug Lifecycle

When you plug a device (like a USB drive or a specialized PCIe card) into a running system, the kernel detects the electrical change on the bus. It then probes the device to gather metadata, such as its Vendor ID and Product ID. The kernel does not typically "load" a driver by itself; instead, it generates a uevent—a message sent from the kernel to user space via a netlink socket—announcing that a new device has appeared.

Automating with Udev

The udev daemon listens for these uevents. It matches the device’s metadata against a set of predefined rules stored in /etc/udev/rules.d/. When a match occurs, udev can execute actions, such as changing permissions, creating symbolic links, or—most importantly for administrators—triggering the loading of a kernel module.

Implementing a Rule

Suppose you have a custom hardware device that requires a specific kernel module named custom_hw_driver. Since the kernel doesn't know this module is required, you must create a rule to load it automatically when the device is detected.

Create a file named /etc/udev/rules.d/99-custom-hardware.rules:

# Match the vendor and product ID of the hardware
ACTION=="add", SUBSYSTEM=="pci", ATTRS{vendor}=="0x1234", ATTRS{device}=="0xabcd", RUN+="/sbin/modprobe custom_hw_driver"

In this rule:

  • ACTION=="add" ensures the command only fires when the device is plugged in.
  • SUBSYSTEM=="pci" narrows the scope to your specific bus type.
  • ATTRS{...} extracts the hardware identifiers to ensure the rule only targets your specific device.
  • RUN+= tells udev to execute the command to insert the module into the running kernel.

Once the module is loaded, the kernel will perform its standard initialization sequence for that driver, and the device will become ready for use. By leveraging udev, you transition from manual module management to a reactive, policy-driven hardware environment.

Key Takeaway: Hotplug handling relies on the kernel emitting a uevent upon hardware detection, which user-space udev rules then intercept to trigger administrative tasks like module loading.