udev 和mdev 是两个使用uevent 机制处理热插拔问题的用户空间程序,两者的实现机理不同。udev 是基于netlink 机制的,它在系统启动时运行了一个deamon 程序udevd,通过监听内核发送的uevent 来执行相应的热拔插动作,包括创建/删除设备节点,加载/卸载驱动模块等等。mdev 是基于uevent_helper 机制的,它在系统启动时修改了内核中的uevnet_helper 变量(通过写/proc/sys/kernel/hotplug),值为“/sbin/mdev”。这样内核产生uevent
时会调用uevent_helper 所指的用户级程序,也就是mdev,来执行相应的热拔插动作。udev 使用的netlink 机制在有大量uevent 的场合效率高,适合用在PC 机上;而mdev 使用的uevent_helper 机制实现简单,适合用在嵌入式系统中。另外要说明的一点是,uevent_helper 的初始值在内核编译时时可配置的,默认值为/sbin/hotplug。如果想修改它的值,写/proc/sys/kernel/hotplug 文件就可以了,例如:
echo “/sbin/mdev” > /proc/sys/kernel/hotplug
补充一点:如果使用的是udevd,那么uevent_helper变量应为空,即
echo “ ” > /proc/sys/kernel/hotplug
refer to
http://sxw0624.blog.163.com/blog/static/200168038201272451049893/
http://bbs.csdn.net/topics/300056887
http://blog.csdn.net/walkingman321/article/details/5917737
1.udev1.启动方式
一般是在inittab的rcS的服务启动列表里面去启动
/etc/
root@freescale /etc/rc.d$ cat rcS
#!/bin/sh
# minimal startup script, will work with msh (this is best available in
# MMUless format).
# load the configuration information
. /etc/rc.d/rc.conf //服务配置文件
mode=${1:-start}
if [ $mode = "start" ]
then
services=$cfg_services
else
services=$cfg_services_r
fi
cfg_services=${2:-$services}
echo "cfg:"
echo $cfg_services
# run the configured sequence
for i in $cfg_services
do
if [ -x /etc/rc.d/init.d/$i ]
then
/etc/rc.d/init.d/$i $mode //调用各个服务
fi
done
if [ $# -ge 2 ]
then
exit 0
fi
# show all kernel log messages
#echo 8 > /proc/sys/kernel/printk
# run rc.local if present and executable
if [ -x /etc/rc.d/rc.local ]
then
/etc/rc.d/rc.local $mode
fi
root@freescale /etc/rc.d$ cat rc.conf
all_services="mount-proc-sys mdev udev hostname devfsd depmod modules filesystems syslog network inetd portmap dropbear sshd boa smb dhcpd settime fslgnome watchdog bluetooth gtk2 pango"
all_services_r="pango gtk2 bluetooth watchdog fslgnome settime dhcpd smb boa sshd dropbear portmap inetd network syslog filesystems modules depmod devfsd hostname udev mdev mount-proc-sys"
cfg_services="mount-proc-sys hostname depmod modules filesystems inetd "
cfg_services_r=" inetd filesystems modules depmod hostname mount-proc-sys"
export HOSTNAME="freescale"
export NTP_SERVER=""
export MODLIST=""
export RAMDIRS="/tmp /var"
export TMPFS="tmpfs"
export TMPFS_SIZE="512k"
export READONLY_FS=""
export INETD_ARGS=""
export BOA_ARGS=""
export SMBD_ARGS=""
export NMBD_ARGS=""
export DHCP_ARG=""
export DEPLOYMENT_STYLE="JFFS2"
export SYSCFG_DHCPC_CMD="udhcpc -b -i "
export DROPBEAR_ARGS=""
root@freescale /etc/rc.d/init.d$ cat udev
#!/bin/sh
PATH=$PATH:/sbin:/bin
if [ ! -x /sbin/udevd ]
then
exit 0
fi
case "$1" in
start)
echo "" > /proc/sys/kernel/hotplug
mount -n -o mode=0755 -t tmpfs tmpfs /dev
# Create static device nodes in /dev
mknod /dev/console c 5 1
mknod /dev/null c 1 3
echo "Starting the hotplug events dispatcher udevd"
udevd --daemon
echo "Synthesizing initial hotplug events"
udevtrigger
udevsettle --timeout=300
mkdir /dev/pts
mount -n -t devpts devpts /dev/pts
mkdir /dev/shm
;;
stop)
;;
reload)
udevcontrol --reload_rules
;;
*)
echo "Usage: /etc/rc.d/init.d/udev {start|stop|reload}"
echo
exit 1
;;
esac
exit 0
如果不需要启动时启动,需要在rc.conf中删除掉对应的服务项目
手动启动udev:
/etc/rc.d/init.d/udev start
启动时,主要启动了
udevd --daemon
udevtrigger
2.mdevbusybox带一个简化版,mdev ,通过mdev -s扫描设备文件,相当于sbin/udevstart;通过echo /sbin/mdev > /proc/sys/kernel/hotplug启用自动扫描,相当于/sbin/udevd --daemon
[root@hemei]# cat /etc/init.d/rcS
#! /bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:
runlevel=S
prevlevel=N
umask 022
export PATH runlevel prevlevel
#
# Trap CTRL-C &c only in this shell so we can interrupt subprocesses.
#
trap ":" INT QUIT TSTP
/bin/hostname hemei
/bin/mount -n -t proc none /proc
/bin/mount -n -t sysfs none /sys
/bin/mount -n -t usbfs none /proc/bus/usb
/bin/mount -t ramfs none /dev
echo /sbin/mdev > /proc/sys/kernel/hotplug
/sbin/mdev -s
/bin/hotplug
# mounting file system specified in /etc/fstab
mkdir -p /dev/pts
mkdir -p /dev/shm
/bin/mount -n -t devpts none /dev/pts -o mode=0622
/bin/mount -n -t tmpfs tmpfs /dev/shm
/bin/mount -n -t ramfs none /tmp
#/bin/mount -n -t ramfs none /var
mkdir -p /var/empty
mkdir -p /var/log
mkdir -p /var/log/boa
mkdir -p /var/lock
mkdir -p /var/run
mkdir -p /var/tmp
/sbin/hwclock -s -f /dev/rtc
syslogd
/sbin/ifconfig lo 127.0.0.1
ifconfig eth0 hw ether 10:23:45:67:89:ab
/sbin/ifconfig eth0 192.168.1.168
/sbin/route add default gw 192.168.1.1
sbin/telnetd -l /bin/login
ip link set can0 up type can bitrate 125000
export PATH=$PATH:/usr/local/mysql/bin
export LD_LIBRARY_PATH=/usr/local/mysql/lib/mysql:$LD_LIBRARY_PATH
export LD_PRELOAD=/usr/lib/preloadable_libiconv.so
/usr/local/mysql/bin/mysqld_safe --user=root &
[root@hemei]# cat /etc/mdev.conf
# system all-writable devices
full 0:0 0666
null 0:0 0666
ptmx 0:0 0666
random 0:0 0666
tty 0:0 0666
zero 0:0 0666
# console devices
tty[0-9]* 0:5 0660
vc/[0-9]* 0:5 0660
# serial port devices
s3c2410_serial0 0:5 0666 =ttySAC0
s3c2410_serial1 0:5 0666 =ttySAC1
s3c2410_serial2 0:5 0666 =ttySAC2
s3c2410_serial3 0:5 0666 =ttySAC3
# loop devices
loop[0-9]* 0:0 0660 =loop/
# i2c devices
i2c-0 0:0 0666 =i2c/0
i2c-1 0:0 0666 =i2c/1
# frame buffer devices
fb[0-9] 0:0 0666
# input devices
mice 0:0 0660 =input/
mouse.* 0:0 0660 =input/
event.* 0:0 0660 =input/
ts.* 0:0 0660 =input/
# rtc devices
rtc0 0:0 0644 >rtc
rtc[1-9] 0:0 0644
# misc devices
mmcblk0p1 0:0 0600 =sdcard */bin/hotplug
sda1 0:0 0600 =udisk * /bin/hotplug
# sound card
controlC[0-9] 0:0 0664 =snd/
pcmC[0-9]D[0-9][cp] 0:0 0664 =snd/
timer 0:0 0664 =snd/
seq 0:0 0664 =snd/
[root@hemei]# cat /bin/hotplug
#!/bin/sh
case $MDEV in
sda1)
DEVNAME=udisk
MOUNTPOINT=/udisk
;;
mmcblk0p1)
DEVNAME=sdcard
MOUNTPOINT=/sdcard
;;
*)
exit 0
;;
esac
case $ACTION in
remove)
/bin/umount $MOUNTPOINT || true
rmdir $MOUNTPOINT >/dev/null 2>&1 || true
;;
*)
/bin/mkdir $MOUNTPOINT > /dev/null 2>&1 || true
/bin/mount -o sync -o noatime -o nodiratime -t vfat /dev/$DEVNAME $MOUNTPOINT > /dev/null 2>&1 || true
;;
esac
exit 0
http://www.embedu.org/Column/Column167.htm
http://blog.csdn.net/hugerat/article/details/3437099