Article Title: proficient in building initramfs stepbystep (8 ). Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
25. udev coldplug Mode
The kernel has detected the hardware device of the system at startup, and exported the hardware device information through the sysfs kernel Virtual File System. The sysfs file system is mounted to/sys by the system initialization script. Udev scans the sysfs file system and generates hotplug events based on hardware device information. udev then reads these events and generates corresponding hardware device files. Because there is no actual hardware plugging action, this process is called coldplug. Our init ramfs uses this mechanism to load the driver modules of hardware devices.
Udev needs the following three programs to complete coldplug operations:
Udevd -- as deamon, records hotplug events, queues them, and sends them to udev to avoid race conditions ).
Udevtrigger -- scan the sysfs file system to generate the hotplug event of the corresponding hardware device.
Udevsettle -- view the udev event queue and exit after all events in the queue are processed.
In the init ramfs init script, you can execute the following statement to implement the coldplug function:
Mkdir-p/dev/. udev/db
Udevd -- daemon
Mkdir-p/dev/. udev/queue
Udevtrigger
Udevsettle
Many documents mentioned that the udevd-daemon command must be executed before
Echo>/proc/sys/kernel/hotplug
Command. It is verified that the coldplug function is not required in our init ramfs environment.
26. Test: Use udev to automatically load the device driver module
After learning about the coldplug mechanism of udev, we will try to use udev to automatically load the device driver module and generate hardware file.
(1) copy the udevd, udevtrigger, and udevsettle programs from the/sbin directory to the sbin directory under the image directory, and use the ldd command to find the dynamic library files they need, copy to the lib directory under the image directory.
(2) modify the init script and add the coldplug function:
Note: before switching to the real root file system, you must kill the udevd process. Otherwise, it will conflict with the execution of the udev script in the real root file system. This is the role of the preceding killall udevd statement.
(3) Compile the udev rule File
The rule file is the soul of udev. Without the rule file, udev cannot automatically load the driver module of the hardware device. For simplicity, we directly use 40-modprobe.rules in CLFS to copy it to the etc/udev/rules. d directory under the image directory. Writing the udev rule file is beyond the scope of this article. I may write a special article later.
Note:
ENV {MODALIAS} = "? * ", RUN + ="/sbin/modprobe $ env {MODALIAS }"
The statement is the key to automatically load the driver module of the hardware device. It uses the modprobe command to load the corresponding kernel module according to the aliases data of the module recorded in the sysfs file system. For more information about the Module aliases, see the description in section 11.5.2.4. Module Loading in the CLFS Manual (CLFS-1.0.0-x86.
(4) Copy The modprobe command
As mentioned in the previous section, The modprobe command of busybox cannot be used normally, so we need to copy the modprobe command under the/sbin directory to the sbin directory under the image directory for udev to load the kernel module. Run the ldd command to check the dynamic library files required by the/sbin/modprobe command. If yes, copy the files to the image/lib directory. (My check result is that, except for libc6, no other dynamic libraries are required, so no copy is required)
Now, re-generate init ramfs and start the CLFS system. init ramfs can automatically load the drive module of the hard disk device. The system smoothly switches from init ramfs to the true CLFS root file system.