编译和烧录SPI-NOR-Flash的V3s核心板(5):自动挂载/卸载U盘

在上一篇:编译和烧录SPI-NOR-Flash的V3s核心板(4):USB扩展中,遗留了一个话题,就是在USB接口插入U盘时,如何让系统自动地挂载U盘的分区,当U盘拔出时,自动地卸载U盘的分区。这里简要地说明一下。

我的板子使用mdev而不是udev实现USB接口设备的热插拔。废话不说,直接上最终配置。

步骤1:登录到核心板

使用核心板的DEBUG口连接到核心板,只有这种连接方式才能看到USB设备接入时的提示信息。如果您不关心这些提示信息的话,也可以使用telnet或者ssh登录到核心板。

步骤2:修改/etc/init.d/rcS文件

在/etc/init.d/rcS文件的最后加上一句话:echo /sbin/mdev > /proc/sys/kernel/hotplug,如下图

#!/bin/sh


# Start all init scripts in /etc/init.d
# executing them in numerical order.
#
for i in /etc/init.d/S??* ;do

     # Ignore dangling symlinks (if any).
     [ ! -f "$i" ] && continue

     case "$i" in
        *.sh)
            # Source shell script for speed.
            (
                trap - INT QUIT TSTP
                set start
                . $i
            )
            ;;
        *)
            # No sh extension, so fork subprocess.
            $i start
            ;;
    esac
done

echo /sbin/mdev > /proc/sys/kernel/hotplug

酱紫的话,就可以使操作系统在启动所有启动脚本后,更新/proc/sys/kernel/hotplug文件。/proc目录在内存中,是在系统启动时创建的,因此在 /etc/init.d/rcS 中的这条指令使得 hotplug(热插拔) 的功能可用。

步骤3:编辑 hotplug 脚本

对于一个热插拔设备,不考虑其具体功能如何,都会由插入(insert)和拔出(remove)两个事件。我们现在考虑对U盘的热插拔响应,因此对这两个事件编写响应代码如下:

文件位置:/etc/hotplug
# dir /etc/hotplug/*.sh 
-rwxr-xr-x    1 root     root           303 Jul 28 10:13 /etc/hotplug/insert.sh*
-rwxr-xr-x    1 root     root            97 Jul 28 10:23 /etc/hotplug/remove.sh*

/etc/hotplug/insert.sh
# 
# 
# cat /etc/hotplug/insert.sh 
if [ -n "$1" ] ; then
    if [ -b /dev/$1 ]; then
        if [ ! -d /mnt ]; then
            mkdir -p /mnt
        fi

        if [ ! -d /mnt/$1 ]; then
            mkdir -p /mnt/$1
        fi

        mount /dev/$1 /mnt/$1

        if [ $? -ne 0 ]; then
            rm -rf /mnt/$1
        fi
    fi
fi

说明:这个脚本其实蛮简单,如果mdev传递进来一个参数,例如:sda1,就为这个设备建立一个挂载点,我这里的挂载点分配在 /mnt 目录下,sda1 设备将被挂载到 /mnt/sda1 下,sda2 设备将被挂载到 /mnt/sda2 下,等等。

/etc/hotplug/remove.sh
# 
# cat /etc/hotplug/remove.sh 
mntpoint=$(mount | grep $1 | cut -d' ' -f3)
umount $mntpoint
rm -rf $mntpoint
echo "$mntpoint unmounted"
# 
# 

说明:这个脚本根据 mdev 传递进来的参数,首先 umount,然后再把这个设备所分配的 /mnt/ 子目录删除。

请注意:

  • 这两个文件的开头没有 #!/bin/sh,不需要吓棒就可以被mdev所调用
  • 要将这两个文件都 chmod +x+x+x 才可以正常生效,要不的话就会出现 Permission denied 错误
编辑 mdev.conf
# find / -name mdev.conf
/etc/mdev.conf
#
#
# cat /etc/mdev.conf
sd[a-z][0-9]      0:0 666        @(/etc/hotplug/insert.sh $MDEV $SUBSYSTEM)
sd[a-z]           0:0 666        $(/etc/hotplug/remove.sh $MDEV $SUBSYSTEM)
ub[a-z][0-9]      0:0 666        @(/etc/hotplug/insert.sh $MDEV $SUBSYSTEM)
ub[a-z]           0:0 666        $(/etc/hotplug/remove.sh $MDEV $SUBSYSTEM)
mmcblk[0-9]p[0-9] 0:0 666        @(/etc/hotplug/insert.sh $MDEV $SUBSYSTEM)
mmcblk[0-9]       0:0 666        $(/etc/hotplug/remove.sh $MDEV $SUBSYSTEM)
#
#

这个脚本使用正则表达式分别对不同类型的U盘(可能是U盘,SD卡或者emmc卡)进行挂载。

验证

下图是U盘的一次热插拔的响应,可以看到设备正常挂载,并且能读写了。

#
#
[24067.423682] usb 1-1: new high-speed USB device number 11 using ehci-platform
[24067.573701] usb 1-1: device descriptor read/64, error -71
[24067.843685] usb 1-1: device descriptor read/64, error -71
[24068.103689] usb 1-1: new high-speed USB device number 12 using ehci-platform
[24068.253681] usb 1-1: device descriptor read/64, error -71
[24068.523678] usb 1-1: device descriptor read/64, error -71
[24068.783680] usb 1-1: new high-speed USB device number 13 using ehci-platform
[24069.233680] usb 1-1: device not accepting address 13, error -71
[24069.593683] usb 1-1: new high-speed USB device number 14 using ehci-platform
[24070.043679] usb 1-1: device not accepting address 14, error -71
[24070.049722] usb usb1-port1: unable to enumerate USB device
[24070.523681] usb 2-1: new full-speed USB device number 8 using ohci-platform
[24070.776720] usb 2-1: not running at top speed; connect to a high speed hub
[24070.794732] usb 2-1: New USB device found, idVendor=05e3, idProduct=0727
[24070.801464] usb 2-1: New USB device strings: Mfr=3, Product=4, SerialNumber=2
[24070.808649] usb 2-1: Product: USB Storage
[24070.812655] usb 2-1: Manufacturer: Generic
[24070.816770] usb 2-1: SerialNumber: 000000000250
[24070.824199] usb-storage 2-1:1.0: USB Mass Storage device detected
[24070.844638] scsi host0: usb-storage 2-1:1.0
[24071.930823] scsi 0:0:0:0: Direct-Access     Generic  STORAGE DEVICE   0250 PQ: 0 ANSI: 0
[24071.940878] sd 0:0:0:0: Attached scsi generic sg0 type 0
[24072.088785] sd 0:0:0:0: [sda] 15204352 512-byte logical blocks: (7.78 GB/7.25 GiB)
[24072.105821] sd 0:0:0:0: [sda] Write Protect is off
[24072.119793] sd 0:0:0:0: [sda] No Caching mode page found
[24072.125200] sd 0:0:0:0: [sda] Assuming drive cache: write through
[24072.191818]  sda: sda1
[24072.227902] sd 0:0:0:0: [sda] Attached SCSI removable disk
[24072.324807] FAT-fs (sda1): Volume was not properly unmounted. Some data may be corrupt. Please run fsck.

# 
# 
# 
# dir /mnt/sda1
total 12
drwxrwxrwx    4 root     root          4096 Jan  1  1970 ./
drwxr-xr-x    3 1000     1000             0 Jul 28 19:04 ../
drwxrwxrwx    2 root     root          4096 Jul 25 01:52 System Volume Information/
drwxrwxrwx    2 root     root          4096 Jul 25 11:23 ddd/
# 
# 
# cat /mnt/sda1/ddd/test.txt
Hello world!

# 
# 
# 
# [24102.731126] usb 2-1: USB disconnect, device number 8

# 
# 
# 
# dir /mnt
total 0
drwxr-xr-x    2 1000     1000             0 Jul 28 19:04 ./
drwxr-xr-x   18 root     root             0 Jan  1  1970 ../
# 
# 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

硬核老骆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值