Udev实现U盘或SD卡的自动挂载

 
并且udev还会根据/etc/udev/rules.d中的规则文件实现一些相应的功能。

下面我们介绍一下如何实现设备节点的自动创建及U盘或SD卡的自动挂载。

一、.udev的移植

因为文件系统中默认是没有对udev进行支持的,所以我们移植一个udev。

1.下载UDEV源码udev-080.tar.bz2,并解压

网址:http://www.us.kernel.org/pub/linux/utils/kernel/hotplug

2.交叉编译。

修改Makefile,具体修改如下:
         CROSS = arm-softfloat-linux-gnu-
         保存退出。
         执行命令:make 进行编译,然后执行arm-softfloat-linux-gnu-strip udev udevd udevstart udevinfo udevtest。并拷贝这些文件到rootfs/bin目录下面

3.添加udev的支持

下面三种方法功能相同
         (1)并修改etc/init.d/rcS脚本,然后添加如下命令:
         /bin/mount -t sysfs sysfs /sys
         (首先创建这个目录)
         /bin/mount -t tmpfs tmpfs /dev
         /bin/udevd --daemon
         /bin/udevstart
         (2)如果linuxrc是二进制文件的话
         rm /linuxrc
         vi /linuxrc
         添加如下内容
         /bin/mount -t sysfs sysfs /sys
         (首先创建这个目录)
         /bin/mount -t tmpfs tmpfs /dev
         /bin/udevd --daemon
         /bin/udevstart
         exec /sbin/init
         (3)修改/etc/fstab为
         #device mount-point type options dump fsck order
         proc /proc proc defaults 0 0
         tmpfs /tmp tmpfs defaults 0 0
         sysfs /sys sysfs defaults 0 0
         tmpfs /dev tmpfs defaults 0 0
         修改/etc/init.d/rcS,添加如下内容
         /bin/udevd --daemon
         /bin/udevstart
         有这上面写些,重新启动系统后,我们的文件系统就有了自动创建节点的功能了。

二、自动挂载U盘或SD卡

1.在/etc下创建udev目录

2.在/etc/udev下穿件目录rules.d和文件udev.conf

3.在udev.conf中添加如下内容

# udev.conf
         # The initial syslog(3) priority: "err", "info", "debug" or its
         # numerical equivalent. For runtime debugging, the daemons internal
         # state can be changed with: "udevcontrol log_priority=<value>".
         udev_log="err"

4.在rules.d下创建规则文件

如实现U盘自动挂载
         Vim 11-add-usb.rules

添加如下内容
         ACTION!="add",GOTO="farsight"
         KERNEL=="sd[a-z][0-9]",RUN+="/sbin/mount-usb.sh %k"
         LABEL="farsight"

这个文件中ACTION后是说明是什么事件,KERNEL后是说明是什么设备比如sda1,mmcblk0p1等,RUN这个设备插入后去执行哪个程序%k是传入这个程序的参数,这里%k=KERNEL的值也就是sda1等。

在/sbin/下创建mount-usb.sh文件添加如下内容
         #!/bin/sh
         /bin/mount -t vfat /dev/$1 /tmp
         sync

修改文件权限为其添加可执行的权限。

这样就实现了U盘的自动挂载,下面附上U盘的卸载规则文件和sd卡的文件

Usb卸载

11-add-remove.rules
         ACTION !="remove",GOTO="farsight"
         SUBSYSTEM!="block",GOTO="farsight"
         KERNEL=="sd[a-z][0-9]",RUN+="/sbin/umount-usb.sh"
         LABEL="farsight"

umount-usb.sh
         #!/bin/sh
         sync
         umount /tmp/

SD卡挂载

ACTION!="add",GOTO="farsight"
         KERNEL=="mmcblk[0-9]p[0-9]",RUN+="/sbin/mount-sd.sh %k"
         LABEL="farsight"

mount-sd.sh
         #!/bin/sh
         /bin/mount -t vfat /dev/$1 /tmp
         Sync

SD卡卸载
         ACTION !="remove",GOTO="farsight"
         SUBSYSTEM!="block",GOTO="farsight"
         KERNEL=="mmcblk[0-9]p[0-9]",RUN+="/sbin/umount-sd.sh"
         LABEL="farsight"

umount-sd.sh
         #!/bin/sh
         sync
         umount /tmp/

引文来源   Udev实现U盘或SD卡的自动挂载_hp_roc的空间_百度空间
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值