分享

UDEV实现自动挂载

 云将东游 2015-10-12

  1、开启/etc/rc.d/init.d/udev 服务
/etc/rc.d/rc.conf 文件cfg_services 中增加udev。
2、自动挂载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 /etc/udev/rules.d/11-add-usb.rules
添加如下内容:
ACTION!="add",GOTO="farsight"
KERNEL=="sd[a-z][0-9]",RUN+="/home/mountusb.sh %k"
KERNEL=="sd[a-z]",RUN+="/home/mountusb.sh %k"
LABEL="farsight"

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

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

修改文件权限为其添加可执行的权限。
这样就实现了U盘的自动挂载,下面附上U盘的卸载规则文件和sd卡的文件

Usb卸载
vim etc/udev/rules.d/11-add-remove.rules

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

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

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 /mnt/sd
sync

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

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


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多