主题 : 开发板上如何实现u盘自动挂载到某个目录 复制链接 | 浏览器收藏 | 打印
级别: 新手上路
UID: 8124
精华: 0
发帖: 17
金钱: 170 两
威望: 115 点
综合积分: 34 分
注册时间: 2009-08-11
最后登录: 2009-09-23
楼主  发表于: 2009-09-04 14:34

 开发板上如何实现u盘自动挂载到某个目录

我这个礼拜实现了用udev+hal+dbus的方式实现了: 当有热插拔设备的时候这个热插拔事件通过Udev向Hal发送Netlink socket消息,Hal在经过对消息的解析,最后将热插拔事件通知Dbus。
但是Hal这个开源软件比较大, 而且依赖于glib,所以想把它抽掉。
所以我想请教一下大家怎样在开发板上实现u盘自动挂载并通知应用程序的方法。
人总是在绝望的边缘 才能看清楚自己软弱的样子
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
1楼  发表于: 2009-09-04 16:25
我想这个功能用udev自己应该也可以实现,这是Angstrom里面的做法:

1)在udev的rules里加上下面几行
复制代码
  1. # Media automounting
  2. SUBSYSTEM=="block", ACTION=="add"    RUN+="/etc/udev/scripts/mount.sh"
  3. SUBSYSTEM=="block", ACTION=="remove" RUN+="/etc/udev/scripts/mount.sh"


2)创建脚本/etc/udev/scripts/mount.sh
复制代码
  1. #!/bin/sh
  2. #
  3. # Called from udev
  4. # Attemp to mount any added block devices
  5. # and remove any removed devices
  6. #
  7. MOUNT="/bin/mount"
  8. PMOUNT="/usr/bin/pmount"
  9. UMOUNT="/bin/umount"
  10. name="`basename "$DEVNAME"`"
  11. for line in `cat /etc/udev/mount.blacklist | grep -v ^#`
  12. do
  13.     if ( echo "$DEVNAME" | grep -q "$line" )
  14.     then
  15.         logger "udev/mount.sh" "[$DEVNAME] is blacklisted, ignoring"
  16.         exit 0
  17.     fi
  18. done
  19. automount() {    
  20.     ! test -d "/media/$name" && mkdir -p "/media/$name"
  21.     
  22.     if ! $MOUNT -t auto -o sync $DEVNAME "/media/$name"
  23.     then
  24.         #logger "mount.sh/automount" "$MOUNT -t auto $DEVNAME \"/media/$name\" failed!"
  25.         rm_dir "/media/$name"
  26.     else
  27.         logger "mount.sh/automount" "Auto-mount of [/media/$name] successful"
  28.         touch "/tmp/.automount-$name"
  29.     fi
  30. }
  31.     
  32. rm_dir() {
  33.     # We do not want to rm -r populated directories
  34.     if test "`find "$1" | wc -l | tr -d " "`" -lt 2 -a -d "$1"
  35.     then
  36.         ! test -z "$1" && rm -r "$1"
  37.     else
  38.         logger "mount.sh/automount" "Not removing non-empty directory [$1]"
  39.     fi
  40. }
  41. if [ "$ACTION" = "add" ] && [ -n "$DEVNAME" ]; then
  42.     if [ -x "$PMOUNT" ]; then
  43.         $PMOUNT $DEVNAME 2> /dev/null
  44.     elif [ -x $MOUNT ]; then
  45.             $MOUNT $DEVNAME 2> /dev/null
  46.     fi
  47.     
  48.     # If the device isn't mounted at this point, it isn't configured in fstab
  49.     # 20061107: Small correction: The rootfs partition may be called just "rootfs" and not by
  50.     #         its true device name so this would break. If the rootfs is mounted on two places
  51.     #        during boot, it confuses the heck out of fsck. So Im auto-adding the root-partition
  52.     #        to /etc/udev/mount.blacklist via postinst
  53.     cat /proc/mounts | awk '{print $1}' | grep -q "^$DEVNAME$" || automount
  54.     
  55. fi
  56. if [ "$ACTION" = "remove" ] && [ -x "$UMOUNT" ] && [ -n "$DEVNAME" ]; then
  57.     for mnt in `cat /proc/mounts | grep "$DEVNAME" | cut -f 2 -d " " `
  58.     do
  59.         $UMOUNT $mnt
  60.     done
  61.     
  62.     # Remove empty directories from auto-mounter
  63.     test -e "/tmp/.automount-$name" && rm_dir "/media/$name"
  64. fi


这样,当block设备载入或卸载时,系统会自动尝试将其挂载到/media目录下的对应设备名的目录下
"If you have an apple and I have an apple and we exchange apples, then you and I will
still each have one apple. But if you have an idea and I have an idea and we exchange
these ideas, then each of us will have two ideas."
级别: 新手上路
UID: 8124
精华: 0
发帖: 17
金钱: 170 两
威望: 115 点
综合积分: 34 分
注册时间: 2009-08-11
最后登录: 2009-09-23
2楼  发表于: 2009-09-04 17:16
感谢楼主回复!

我在/etc/udev/rules.d 下面加了一个10-usb.rules的文件,内容是:
KERNEL=="sda", SUBSYSTEM=="block" RUN+="/root/usbmount.sh"

我在root下面加了usbmount.sh 内容是:
#!/bin/bash
LOG=/var/log/usb-hotplug.log
#lap=$(date --rfc-3339=ns)
#echo "$lop: $DEVPATH requesting $ACTION" >> $LOG
echo "kluter auto mount"
if[ $ACTION == "add" ]
    then
        mount -t vfat -o umask=000,noatime,async,codepage=936,iocharset=gb2312 \
            /dev/sda /var/tmp
elif[ $ACTION == "remove" ]
    then
    umount -l /var/tmp
fi

可是当板子跑起来的时候我插入u盘, 还是不能自动mount,照你说的方法也试过了,还是不行,而且我感觉脚本根本就没有跑起来,因为echo的字串没有印出来,/var/log下面也没有生成usb-hotplug.log日志。

我用的文件系统是光盘自带的。难道是规则文件没有被udevd识别么?
人总是在绝望的边缘 才能看清楚自己软弱的样子
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
3楼  发表于: 2009-09-04 17:18
那就找原因啊,为什么脚本没有跑起来
"If you have an apple and I have an apple and we exchange apples, then you and I will
still each have one apple. But if you have an idea and I have an idea and we exchange
these ideas, then each of us will have two ideas."
级别: 新手上路
UID: 8124
精华: 0
发帖: 17
金钱: 170 两
威望: 115 点
综合积分: 34 分
注册时间: 2009-08-11
最后登录: 2009-09-23
4楼  发表于: 2009-09-04 17:20
嗯,我再找找看
人总是在绝望的边缘 才能看清楚自己软弱的样子
级别: 新手上路
UID: 8124
精华: 0
发帖: 17
金钱: 170 两
威望: 115 点
综合积分: 34 分
注册时间: 2009-08-11
最后登录: 2009-09-23
5楼  发表于: 2009-09-06 21:09
还是挂不上,郁闷~
人总是在绝望的边缘 才能看清楚自己软弱的样子
级别: 新手上路
UID: 8124
精华: 0
发帖: 17
金钱: 170 两
威望: 115 点
综合积分: 34 分
注册时间: 2009-08-11
最后登录: 2009-09-23
6楼  发表于: 2009-09-06 23:05
现在我觉得很奇怪的是,我写了一个简单的脚本:
#!/bin/sh
echo "kluter auto mount"
if [ $ACTION = "add" ]; then
    /bin/mount /dev/sda /var/tmp
elif [ $ACTION = "remove" ]; then
    /bin/umount -l /var/tmp
fi

我照shell编程里面说的#chmod +x usb.sh   然后./usb.sh 结果报错:-/bin/sh: ./usb.sh: not found

我又 #sh usb.sh 又报错:
kluter auto mount
usb.sh: line 5: syntax error: "elif" unexpected (expecting "then")

搞不懂了,我都是按照shell编程里面的语法。
人总是在绝望的边缘 才能看清楚自己软弱的样子
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
7楼  发表于: 2009-09-07 11:01
我不知道你的shell出了什么问题,但你至少需要把
if [ $ACTION = "add" ]; then
改成
if [ “$ACTION” = "add" ]; then
"If you have an apple and I have an apple and we exchange apples, then you and I will
still each have one apple. But if you have an idea and I have an idea and we exchange
these ideas, then each of us will have two ideas."
级别: 新手上路
UID: 8124
精华: 0
发帖: 17
金钱: 170 两
威望: 115 点
综合积分: 34 分
注册时间: 2009-08-11
最后登录: 2009-09-23
8楼  发表于: 2009-09-07 18:05
谢谢,这个确实是语法错误
不过我现在在脚本里只写了一个ls命令,在PC上可以跑,但是开发板上跑不了
我觉得应该是开发板shell有问题。
人总是在绝望的边缘 才能看清楚自己软弱的样子
级别: 新手上路
UID: 69033
精华: 0
发帖: 31
金钱: 155 两
威望: 31 点
综合积分: 62 分
注册时间: 2012-04-30
最后登录: 2012-08-22
9楼  发表于: 2012-08-13 16:18
HAL这个开源的东西 去哪里下啊??我写了一个qt检测U盘插拔的东西,在板子上运行时提示:"?????????QDBusInterface?" "The name org.freedesktop.Hal was not provided by any
.service files"
Object::connect: No such signal QDBusAbstractInterface::DeviceAdded(QString)
Object::connect:  (receiver name: 'Widget')
Object::connect: No such signal QDBusAbstractInterface::DeviceRemoved(QString)
Object::connect:  (receiver name: 'Widget')
这什么意思啊??是不是没有HAL???