Managing Embedded Linux Device Nodes with Mdev
What is Mdev?
Mdev is a lightweight device management tool provided by BusyBox, designed specifically for resource-constrained embedded systems. It combines the kernel’s devtmpfs functionality with user-space device management capabilities, offering features such as dynamic device node creation, hot-plug support, and device permission management.
Compared to a full udev system, Mdev is lighter and consumes fewer resources while still providing basic device management functionalities, making it ideal for embedded environments with limited memory and storage space.
The Relationship Between Mdev and Devtmpfs
Mdev is typically used in conjunction with the kernel’s devtmpfs functionality:
- Kernel Level: devtmpfs is responsible for creating device nodes early in the system boot process
- User Space: Mdev handles device hot-plug events and more complex device management needs
This division of labor allows the system to quickly have basic device nodes at boot time while dynamically responding to changes in devices.
Advantages of Mdev
- Low Resource Usage: Mdev is lighter compared to a full udev system
- Simple Configuration: Uses a straightforward configuration file syntax that is easy to understand and maintain
- Hot-Plug Support: Capable of handling dynamic plug-and-play events for devices
- High Integration: As part of BusyBox, it requires no additional installation
Kernel Configuration Requirements
Using Mdev requires enabling the following options in the kernel:
CONFIG_DEVTMPFS=y
CONFIG_HOTPLUG=y
The first option ensures that the kernel supports devtmpfs, while the second option enables hot-plug support. In our build script, this is achieved with the following commands:
./scripts/config --file "${LINUX_BUILD_DIR}/.config" --enable DEVTMPFS
./scripts/config --file "${LINUX_BUILD_DIR}/.config" --enable DEVTMPFS_MOUNT
BusyBox Configuration Requirements
To use Mdev, the following options need to be enabled when compiling BusyBox:
CONFIG_MDEV=y
CONFIG_FEATURE_MDEV_CONF=y
CONFIG_FEATURE_MDEV_RENAME=y
CONFIG_FEATURE_MDEV_RENAME_REGEXP=y
Our script enables these options with the following commands:
sed -i 's/# CONFIG_MDEV is not set/CONFIG_MDEV=y/' "${BUSYBOX_BUILD_DIR}/.config"
sed -i 's/# CONFIG_FEATURE_MDEV_CONF is not set/CONFIG_FEATURE_MDEV_CONF=y/' "${BUSYBOX_BUILD_DIR}/.config"
sed -i 's/# CONFIG_FEATURE_MDEV_RENAME is not set/CONFIG_FEATURE_MDEV_RENAME=y/' "${BUSYBOX_BUILD_DIR}/.config"
sed -i 's/# CONFIG_FEATURE_MDEV_RENAME_REGEXP is not set/CONFIG_FEATURE_MDEV_RENAME_REGEXP=y/' "${BUSYBOX_BUILD_DIR}/.config"
File System Configuration
Using Mdev requires configuring the following files:
1. Mdev Configuration File (/etc/mdev.conf)
The Mdev configuration file defines the rules for creating device nodes, setting permissions, and handling hot-plug scripts:
# Basic device nodes
null root:root 666
zero root:root 666
full root:root 666
random root:root 444
urandom root:root 444
tty root:tty 666
tty[0-9]* root:tty 660
# Serial devices
ttyS[0-9]* root:root 660
# Zynq serial
ttyPS0 root:root 660
# Block devices
ram([0-9]*) root:disk 660 >rd/%1
loop([0-9]+) root:disk 660 >loop/%1
sd[a-z].* root:disk 660 */lib/mdev/usbdisk
mmcblk([0-9]+)p([0-9]+) root:disk 660 >mmc/%1/%2
# Hot-plug script
$MODALIAS=.* root:root 660 @/sbin/modprobe "$MODALIAS"
2. System Startup Script (/etc/init.d/rcS)
The startup script needs to set up Mdev and mount the necessary file systems:
#!/bin/sh
export PATH=/bin:/sbin:/usr/bin
export TERM=vt100
# Mount virtual file systems
mount -t proc proc /proc
mount -t sysfs sysfs /sys
mount -t tmpfs tmpfs /tmp
# Set up Mdev
echo /sbin/mdev > /proc/sys/kernel/hotplug
mdev -s # Scan /sys and create device nodes
# Set hostname and other initialization tasks
hostname "$(cat /etc/hostname)"
3. Init Configuration File (/etc/inittab)
The init configuration file defines the system startup process:
::sysinit:/etc/init.d/rcS
ttyPS0::respawn:/bin/sh
::ctrlaltdel:/sbin/reboot
Practical Verification
We provide a complete build script that demonstrates how to configure and use Mdev:
-
Get the Code:
git clone https://gitee.com/znvm/elab.git cd elab -
Run the Script:
./run-device-mdev.sh -
Observe the Results: The script will automatically compile BusyBox and the Linux kernel with Mdev support, create the root file system, and boot the system in QEMU. After booting, you can check the contents of
<span>/dev</span>directory to confirm that the device nodes have been created correctly.
Notes
- The configuration file syntax for Mdev is relatively simple and does not support the complex rules found in udev
- For complex device renaming or permission settings, additional hot-plug scripts may need to be written
- Mdev’s hot-plug handling capabilities are limited and may not be as powerful as udev for complex device relationships
- Ensure that the kernel version supports hot-plug functionality
Conclusion
Mdev provides a lightweight yet fully functional device management solution for embedded Linux systems. It combines the kernel’s devtmpfs functionality with user-space device management capabilities, ensuring fast system boot times while providing dynamic device management capabilities.
With proper configuration, Mdev can meet the management needs of most embedded devices, making it an ideal choice for resource-constrained environments.
Visit https://gitee.com/znvm/elab to view the complete code and running scripts, and experience the convenience of Mdev device management firsthand. You can validate it using the QEMU emulator without the need for actual hardware.
Follow us for more exciting content on embedded Linux development in the future!
#Linux #EmbeddedLinux #Busybox #LinuxKernel #mdev #DeviceManagement