In this lesson, you will learn how the Linux kernel automates the population of the /dev directory to bridge the gap between kernel-space drivers and user-space applications.
The Problem: Bridging User and Kernel Space
When a kernel driver registers a character device (like a sensor or a UART port), the kernel assigns it a major and minor number. However, user-space applications cannot simply "guess" these numbers to open a file descriptor. They need a representative file—a device node—located in the /dev directory to communicate with the driver.
Manual Node Creation
Historically, developers used the mknod command to create these nodes manually:
mknod /dev/my_sensor c 240 0
This tells the kernel: create a character device (c) at /dev/my_sensor with major number 240 and minor number 0. In embedded systems with hundreds of devices, manual creation is error-prone, inflexible, and creates a significant maintenance burden.
The Solution: Devtmpfs
devtmpfs is a small, specialized virtual filesystem that automatically manages the creation and removal of device nodes. When a kernel driver calls device_create(), devtmpfs intercepts the request and instantly creates the corresponding entry in /dev with the correct permissions and ownership.
To leverage this, you must ensure your kernel configuration has CONFIG_DEVTMPFS and CONFIG_DEVTMPFS_MOUNT enabled. When these are active, the kernel handles the heavy lifting during the boot process:
- Kernel Boot: The kernel mounts
devtmpfsat/devearly in the initialization phase. - Driver Loading: As hardware drivers initialize and register their presence (e.g., via a Platform Driver), the kernel automatically generates the device node.
- User Access: User-space applications can immediately open
/dev/node_namewithout requiring manual intervention from aninitscript.
This mechanism ensures that your /dev directory remains perfectly synchronized with the hardware actually present on your board, regardless of whether a module is loaded or a device is hot-plugged.
Key Takeaway: devtmpfs eliminates the need for manual mknod calls by dynamically populating the /dev directory as drivers register, ensuring a seamless interface between hardware drivers and user-space applications.