linux dev目录创建,linux下的mdev和dev目录下设备节点自动创建

454228

mdev是简化版的udev,适合于嵌入式的应用埸合。其具有使用简单的特点。它的作用热插拔或动态加载驱动程序时,自动产生驱动程序所需的节点文件。根据我的使用情况记录下我的使用过程:(1)我的busybox是1.19.4版本。在编译时加上对mdev的支持    Linux System Utilities  --->                mdev                     Support /etc/mdev.conf                 Support command execution at device addition/removal(2)在启动时加上使用mdev的命令,下面2句    /bin/echo /sbin/mdev > /proc/sys/kernel/hotplug    mdev -s  我的上面2句开始放在inittab中,但是这样的话u盘在上电的时候不能正常挂载,但是设备启动以后可以正常挂载和卸载。后来放在rcS中,可以正常工作了。但是在系统启动流程上是一样的,这点我很疑惑。 在执行上面2句以前保证已经挂载了proc,sys文件系统。(3)编写mdev.conf,我的这个文件内容如下:Sd[a-z][0-9] 0:0 660 *(/etc/automount.sh $MDEV)而具体执行的脚本是automount.sh文件,内容附上,希望对做同样工作的人或多或少有些帮助,可以减少斟酌和验证时间,当然我的不一定就正确啊,有错误之处,希望大家共同交流,不要拍砖。#!/bin/sh# mount the /dev/sd* to /mnt/docif [ "$1" == "" ]; thenexit 1fimounted=`mount | grep $1 | wc -l`if [ ! -d /mnt/work ]; thenexit 1fi# If mounted, we will umount itif [ $mounted -ge 1 ]; thenif ! umount "/mnt/work"; thenumount -l /mnt/workexit 1fielsefsck.ext3 /dev/$1 -pif ! mount "/dev/$1" "/mnt/work" -o sync ; thenexit 1fifiexit 0(4)当然mdev.conf也不是必须的,我的之所以需要这个文件,是为了挂载和卸载u盘,如果只是需要动态建立设备节点,这个文件就不需要了。 要在加载驱动的时候再dev目录下动态建立设备节点其实只需要下面两个函数:class_create和device_create下面也把我的代码关键部分提供给大家:static int RtControl_Init (void){ module_class = class_create(THIS_MODULE, "module");if(IS_ERR(module_class)){Result = PTR_ERR(module_class);printk("create module_class failed!\n");goto ERR_CREATE_CLASS;} for (i= 0; i { Devs[i].nDevId = MKDEV(Devs[i].Id, 0); Result = register_chrdev_region(Devs[i].Id, 1, Devs[i].DevName); if (Result { ECError("Could not register the specific major number of device : %s!\n",Devs[i].DevName); goto ERR_REG_ID; } cdev_init(&Devs[i].cDevice, &Devs[i].DevOps); Devs[i].Device.owner = THIS_MODULE; Devs[i].Device.ops = &Devs[i].DevOps; Result = cdev_add(&Devs[i].Device, Devs[i].Id, 1); if (Result { ECError("Could not add the device to system!\n", Devs[i].DevName); goto ERR_ADD_DEV; } ECPrint("Register device : successfully!\n", Devs[i].DevName, Devs[i].Id);device_create(module_class, NULL, Devs[i].Id, NULL, Devs[i].DevName);printk("device_create %s success!\n", Devs[i].DevName); } return iResult;ERR_ADD_DEV:。。。ERR_REG_ID:。。。ERR_CREATE_CLASS:。。。}static void Control_Cleanup (void){。。。 for (i= 0; i { if (Devs[i].Id)device_destroy(module_class, Devs[i].Id); cdev_del(&Devs[i].Device); if (Devs[i].Id) unregister_chrdev_region(Devs[i].Id, 1); }class_destroy(module_class); return;}module_init(RtControl_Init);module_exit(RtControl_Cleanup);然后编译ko文件,insmod的时候就可以在dev目录下建立设备节点,确实挺方便

11-13 20:45

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值