Understanding mdev and Dynamic /dev Node Creation in Embedded Linux (Kernel 2.6.31/MIPS)


2 views

mdev is BusyBox's minimalistic replacement for udev, designed for embedded systems. It handles two primary functions:

  1. Device node creation/removal during hotplug events
  2. Initial population of /dev during system startup (via mdev -s)
# Typical mdev initialization in rcS:
echo "/sbin/mdev" > /proc/sys/kernel/hotplug
mdev -s

In your case with only /dev/console initially, nodes are created through:

Cold Plug (System Boot)

mdev -s scans /sys/class and /sys/block to create static device nodes:

# Example mdev -s operation flow:
1. Walk /sys/class/net/eth0 → create /dev/eth0
2. Walk /sys/block/mtdblock0 → create /dev/mtdblock0

Hot Plug (Runtime Events)

When kernel detects new devices, it executes your stbhotplug script:

# Simplified hotplug event processing:
kernel → /proc/sys/kernel/hotplug → /sbin/stbhotplug → mdev

For your UBI-related issues (/dev/ubi_ctrl, /dev/ubi1), consider:

Timing Issues

UBI devices may initialize after mdev -s runs. Solution:

# Add to rcS after mdev -s:
sleep 1
UBI_DEVICES=$(ls /sys/class/ubi/)
for dev in $UBI_DEVICES; do
    mdev -s /sys/class/ubi/$dev
done

mdev.conf Configuration

Ensure proper permissions in /etc/mdev.conf:

# Example entry for UBI devices:
ubi[0-9]* 0:0 666
ubi_ctrl 0:0 666

To diagnose device creation issues:

# Monitor udev events:
echo 1 > /proc/sys/kernel/printk
mdev -df

# Check kernel messages for ubi initialization:
dmesg | grep ubi

For more complex systems, consider:

  • Pre-creating static device nodes in rootfs
  • Using tmpfs for /dev with proper initialization sequence
  • Implementing device existence checks in application code

When working with an embedded Linux system (kernel 2.6.31 on MIPS using BusyBox), the mdev utility plays a crucial role in device management. As a lightweight alternative to udev, mdev handles:

1. Dynamic device node creation/removal
2. Hotplug event processing
3. Basic device permissions management

In your case, the initialization sequence shows two critical operations:

echo "/sbin/stbhotplug" > /proc/sys/kernel/hotplug
mdev -s

The first line registers your custom hotplug handler, while mdev -s scans /sys and creates device nodes for all currently detected hardware.

The issue with missing /dev/ubi_ctrl or /dev/ubi1 typically occurs when:

1. Kernel modules load after mdev initialization
2. The hotplug handler isn't properly triggering mdev
3. Filesystem permissions prevent node creation

Here's an improved initialization approach:

#!/bin/sh

# Set hotplug handler
echo "/sbin/mdev" > /proc/sys/kernel/hotplug

# Initial population of /dev
mdev -s

# Ensure UBI devices are ready
UBI_CTRL_MAJOR=$(grep ubi_ctrl /proc/devices | awk '{print $1}')
[ -n "$UBI_CTRL_MAJOR" ] && mknod /dev/ubi_ctrl c $UBI_CTRL_MAJOR 0

# Alternative approach using static device table
if [ -f /etc/mdev.conf ]; then
    # Example mdev.conf entry for UBI devices
    echo "ubi[0-9]* 0:0 660 */sbin/stbhotplug" >> /etc/mdev.conf
fi

When devices fail to appear, try:

# Check kernel messages
dmesg | grep ubi

# Verify device major numbers
grep -E 'ubi|mtd' /proc/devices

# Manually trigger events
echo add > /sys/class/misc/ubi_ctrl/uevent

For reliable operation, consider creating a static device table in /etc/mdev.conf:

# UBI control device
ubi_ctrl c 10:63 root:root 660

# MTD devices
mtd[0-9]* c 90:0 root:root 660

# Hotplug actions
sd[a-z][0-9]* root:root 660 */etc/hotplug/sd.script