网站建设百度云,注册了微信小程序怎么登录,网站建设氺首选金手指14,能够做外贸的网站有哪些一、概述mdev是busybox提供的一个工具#xff0c;用在嵌入式系统中#xff0c;相当于简化版的udev#xff0c;作用是在系统启动和热插拔或动态加载驱动程序时#xff0c;自动创建设备节点。文件系统中的/dev目录下的设备节点都是由mdev创建的。在加载驱动过程中#xff0c…一、概述mdev是busybox提供的一个工具用在嵌入式系统中相当于简化版的udev作用是在系统启动和热插拔或动态加载驱动程序时自动创建设备节点。文件系统中的/dev目录下的设备节点都是由mdev创建的。在加载驱动过程中根据驱动程序在/dev下自动创建设备节点。以下内容摘自busybox-1.23.1的mdev.txt文件Mdev has two primary uses: initial population and dynamic updates. Bothrequire sysfs support in the kernel and have it mounted at /sys. For dynamicupdates, you also need to have hotplugging enabled in your kernel.Heres a typical code snippet from the init script:[0] mount -t proc proc /proc[1] mount -t sysfs sysfs /sys[2] echo /sbin/mdev /proc/sys/kernel/hotplug[3] mdev -sAlternatively, without procfs the above becomes:[1] mount -t sysfs sysfs /sys[2] sysctl -w kernel.hotplug/sbin/mdev[3] mdev -sOf course, a more full setup would entail executing this before the previouscode snippet:[4] mount -t tmpfs -o size64k,mode0755 tmpfs /dev[5] mkdir /dev/pts[6] mount -t devpts devpts /dev/ptsThe simple explanation here is that [1] you need to have /sys mounted beforeexecuting mdev. Then you [2] instruct the kernel to execute /sbin/mdev whenevera device is added or removed so that the device node can be created or destroyed.Then you [3] seed /dev with all the device nodes that were created while the systemwas booting.For the full setup, you want to [4] make sure /dev is a tmpfs filesystem(assuming youre running out of flash). Then you want to [5] create the/dev/pts mount point and finally [6] mount the devpts filesystem on it.二、用法这篇博文所涉及到的用法很简单1、在/etc/init.d/rcS脚本里有“mdev -s”解释在系统启动时通过执行“mdev -s”扫描/sys/class和/sys/block在目录中查找dev文件。例如/sys/class/tty/tty0/dev它的内容为4:0即主设备号是4次设备号是0dev的上一级目录为设备名这里是tty0。/sys/class/下的每个文件夹都代表着一个子系统。2、在/etc/init.d/rcS脚本里有“echo /sbin/mdev /proc/sys/kernel/hotplug”即是把/sbin/mdev写到/proc/sys/kernel/hotplug文件里解释当有热插拔事件产生时内核会调用/proc/sys/kernel/hotplug文件里指定的应用程序来处理热插拔事件根据mdev.txt的说明可知在使用mdev之前要满足下面的条件mount -t proc proc /procmount -t sysfs sysfs /sysmount -t tmpfs -o size64k,mode0755 tmpfs /devmkdir /dev/ptsmount -t devpts devpts /dev/pts说明所有的设备都可以在/sys/class下找到这个文件夹下的每一个文件夹下代表了一类设备表示类设备的文件夹下也有文件夹这些文件夹代表设备。如/sys/class/test/test_dev/ test代表类如net、tty、soundtest_dev代表某个设备它的名字和/dev下的设备节点名字是一样的三、内核源码分析分析一下相关内核源码看上面提到的功能是如何实现的。平时我们添加驱动时如果想自动创建设备节点调用的函数是class_create和device_create。class_create是创建类设备就是在/sys/class/创建一个文件夹这个文件夹代表一类设备这个文件夹里会包含device_create创建的设备也是一个文件夹。下面就从device_create入手看是怎么实现自动创建设备节点的。源码基于linux-2.6.30.4内核struct device *device_create(struct class *class, struct device *parent,dev_t devt, void *drvdata, const char *fmt, ...)dev device_create_vargs(class, parent, devt, drvdata, fmt, vargs);retval device_register(dev);return device_add(dev);kobject_uevent(dev-kobj, KOBJ_ADD);return kobject_uevent_env(kobj, action, NULL); // action KOBJ_ADDconst char *action_string kobject_actions[action]; // action_string add……//把相关信息存到环境变量里ACTION代表操作类型DEVPATH为设备在class下存在的路径SUBSYSTEM为class_create创建的设备类//ACTIONadd DEVPATH/class/test/test_dev , SUBSYSTEMtestretval add_uevent_var(env, ACTION%s, action_string);if (retval)goto exit;retval add_uevent_var(env, DEVPATH%s, devpath);if (retval)goto exit;retval add_uevent_var(env, SUBSYSTEM%s, subsystem);if (retval)goto exit;……if (uevent_helper[0]){argv [0] uevent_helper;argv [1] (char *)subsystem;argv [2] NULL;//内核空间调用用户空间程序调用的程序由argv [0] uevent_helper指定retval call_usermodehelper(argv[0], argv,env-envp, UMH_WAIT_EXEC);下面看看uevent_helper是谁定义如下char uevent_helper[UEVENT_HELPER_PATH_LEN] CONFIG_UEVENT_HELPER_PATH;去.config中查看CONFIG_UEVENT_HELPER_PATH/sbin/hotplug但是去/sbin目录下查看并没有hotplug这个文件所以肯定不是这个文件起作用于是在上面的if (uevent_helper[0])里加了一句调试信息打印uevent_helper内核启动相关打印信息如下uevent_helper is /sbin/hotpluguevent_helper is /sbin/hotplugs3c2410-rtc s3c2410-rtc: setting system clock to 2015-04-30 08:12:15 UTC (1430381535)yaffs: dev is 32505858 name is mtdblock2yaffs: passed flags yaffs: Attempting MTD mount on 31.2, mtdblock2yaffs: auto selecting yaffs2block 646 is badyaffs_read_super: isCheckpointed 0VFS: Mounted root (yaffs filesystem) on device 31:2.Freeing init memory: 240KStart Qtopia-2.2.0uevent_helper is /sbin/mdevuevent_helper is /sbin/mdev看到没刚开始确实是/sbin/hotplug但后来就变成了/sbin/mdev。很据上面信息我们知道是在文件系统启动的过程中发生改变的。文件系统启动过程中改变mdev的只有“echo /sbin/mdev /proc/sys/kernel/hotplug”也确实是这个导致了uevent_helper的改变。涉及到的数据在/kernel/sysctl.c下#if defined(CONFIG_HOTPLUG) defined(CONFIG_NET){.ctl_name KERN_HOTPLUG,.procname hotplug,.data uevent_helper,.maxlen UEVENT_HELPER_PATH_LEN,.mode 0644,.proc_handler proc_dostring,.strategy sysctl_string,},#endif至于为什么“echo /sbin/mdev /proc/sys/kernel/hotplug”能改变uevent_helper就是proc虚拟文件系统的内容了这里不讨论。其实设置mdev有三种方法总结如下1、编译内核的时候直接配置CONFIG_UEVENT_HELPER_PATH并且在之后的启动中不去修改uevent_helper那么uevent_helper代表的程序就是CONFIG_UEVENT_HELPER_PATH指定的程序2、不管CONFIG_UEVENT_HELPER_PATH配置与否或如何设置通过echo /sbin/mdev /sys/kernel/uevent_helper修改uevent_helper的内容这个指令将会调用内核函数uevent_helper_store。过程涉及sysfs虚拟文件系统的内容这里不讨论。改变之后/proc/sys/kernel/hotplug里的内容也会立即发生改变3、不管CONFIG_UEVENT_HELPER_PATH配置与否或如何设置通过echo /sbin/mdev /proc/sys/kernel/hotplug修改uevent_helper的内容.它的修改也会导致/sys/kernel/uevent_helper里的内容立即改变对于上述的2、3两种方法都是通过用户层的接口直接uevent_helper所以谁在后面谁起作用三、busybox源码分析内核源码的最后是调用uevent_helper指定的用户程序这个用户程序通常是mdev那么mdev如何做的呢来看一下busybox的源码。源码基于busybox-1.23.1int mdev_main(int argc UNUSED_PARAM, char **argv)xchdir(/dev); // 先把目录改变到/dev下if (argv[1] strcmp(argv[1], -s) 0) { // 在文件系统启动的时候会调用 mdev -s创建所有驱动设备节点putenv((char*)ACTIONadd); // mdev -s 的动作是创建设备节点所以为addif (access(/sys/class/block, F_OK) ! 0) { // 当/sys/class/block目录不存在时才扫描/sys/block/* Scan obsolete /sys/block only if /sys/class/block* doesnt exist. Otherwise well have dupes.* Also, do not complain if it doesnt exist.* Some people configure kernel to have no blockdevs.*/recursive_action(/sys/block,ACTION_RECURSE | ACTION_FOLLOWLINKS | ACTION_QUIET,fileAction, dirAction, temp, 0);}/** 这个函数是递归函数它会扫描/sys/class目录下的所有文件如果发现dev文件将按照* /etc/mdev.conf文件进行相应的配置。如果没有配置文件那么直接创建设备节点* 最终调用的创建函数是 make_device*/recursive_action(/sys/class,ACTION_RECURSE | ACTION_FOLLOWLINKS,fileAction, dirAction, temp, 0);}else{// 获得环境变量环境变量是内核在调用mdev之前设置的env_devname getenv(DEVNAME); /* can be NULL */G.subsystem getenv(SUBSYSTEM);action getenv(ACTION);env_devpath getenv(DEVPATH);snprintf(temp, PATH_MAX, /sys%s, env_devpath);make_device(env_devname, temp, op);}由以上代码分析可知无论对于何种操作最后都是调用make_device来创建节点看一下这个函数static void make_device(char *device_name, char *path, int operation)int major, minor, type, len;char *path_end path strlen(path); //path_end指定path结尾处major -1;if (operation OP_add) {strcpy(path_end, /dev); // 往path结尾处拷贝“/dev”这时path/sys/class/test/test_dev/devlen open_read_close(path, path_end 1, SCRATCH_SIZE - 1); // 打开并读取/sys/class/test/test_dev/dev*path_end \0;if (len 1) {if (!ENABLE_FEATURE_MDEV_EXEC)return;} else if (sscanf(path_end 1, %u:%u, major, minor) 2) { //从/sys/class/test/test_dev/dev获得主次设备号dbg1(dev %u,%u, major, minor);} else {major -1;}}if (operation OP_add major 0) // 如果是add即创建节点mknod(node_name, rule-mode | type, makedev(major, minor)) // 最终用mknod函数在/dev下创建设备节点if (operation OP_remove major -1) // 如果是remove即删除节点unlink(node_name);创建节点最后无非还是调用mknod当然在class_create和device_create自动创建设备节点时也会在/sys/class下自动创建和删除相关设备类和设备这是sysfs的驱动内容这里不讲