/********************************************************************* * mdev USB disk auto mount demo * 说明: * 之前有分析过mdev,这次富林写了一个demo,跟一下,不过呢,在看过之后 * 觉得,需要优化,后面提供了一种兼容eMMC、SD、U盘、SATA自动挂载的方式, * 在功能上可以进步的进行优化,其中的eMMC、SD卡功能需要加强区分,这份脚本 * 中没有进一步识别区分,这份参考来自参考文档第二点链接。 * * 2017-8-21 深圳 龙华樟坑村 曾剑锋 ********************************************************************/ 一、参考文档: 1. MDEV Primer http://www.cnblogs.com/zengjfgit/p/5133307.html 2. [Linux] Auto mount 自動掛載 usb disk & SD card http://www.awaysu.acsite.org/linux/linux-auto-mount-usb-disk-sd-card/ 3. SD card automount for Embedded Linux http://infosoure.blogspot.com/2011/04/sd-card-automount-for-embedded-linux.html 4. How to mount removable media in /media/<LABEL> automatically when inserted, without any desktop environment? https://unix.stackexchange.com/questions/44454/how-to-mount-removable-media-in-media-label-automatically-when-inserted-with 二、代码阅读: 1. /etc/mdev.conf ... sda1 0:0 0660 @/etc/usb_disk1_insert sda 0:0 0660 $/etc/usb_disk1_remove sdb1 0:0 0660 @/etc/usb_disk2_insert sdb 0:0 0660 $/etc/usb_disk2_remove ... 2. /etc/usb_disk1_insert #!/bin/sh if [ ! -e /media/sda1 ]; then mkdir /media/sda1 -p fi mount -t vfat /dev/sda1 /media/sda1 3. /etc/usb_disk1_remove #!/bin/sh umount /media/sda1 if [ -d /media/sda1 ]; then rm /media/sda1 -rf fi 4. /etc/usb_disk2_insert #!/bin/sh if [ ! -e /media/sdb1 ]; then mkdir /media/sdb1 -p fi mount -t vfat /dev/sdb1 /media/sdb1 5. /etc/usb_disk2_remove #!/bin/sh umount /media/sdb1 if [ -d /media/sdb1 ]; then rm /media/sdb1 -rf fi 三、更好的处理方式: 1. /etc/mdev.conf ... sd[a-z][0-9] 0:0 600 *(/etc/init.d/automount.sh $MDEV &) mmcblk0p[0-9] 0:0 600 *(/etc/init.d/automount.sh $MDEV &) ... 2. /etc/init.d/automount.sh #!/bin/sh # 挂载路径 MOUNT_PATH="" # 信息输出串口 MSG_PATH="/dev/ttyS0" # 设备节点为空检查,退出 if [ $1 == "" ]; then echo "[AutoMount] automount.sh parameter is none" > $MSG_PATH exit 0 # eMMC或者SD卡挂载 elif [ $1 == "mmcblk0p1" ]; then MOUNT_PATH="/mnt/sdcard" # 其他类型的盘挂载 else name=$1 check=${name%[1-9]} vendor=`cat /sys/block/$check/device/vendor` # SATA挂载 if [ $vendor == "ATA" ]; then MOUNT_PATH="/mnt/hdd/"$name # U盘挂载 else MOUNT_PATH="/mnt/usb/"$name fi fi # 将挂载点信息输出到串口 echo "[AutoMount] mount path : $MOUNT_PATH" >> $MSG_PATH # 获取当前需要挂载、卸载的分区格式 FORMAT=`/sbin/fdisk -l | grep $1` # 查看是否已经有挂载设备了,如果已经挂载,那当前次就要卸载 OLD_MOUNT_PATH=`mount | grep $MOUNT_PATH` # 未挂载,那么当前次就是需要进行挂载 if [ "$OLD_MOUNT_PATH" == "" ]; then # 如果挂载点不存在,那么就自动创建挂载点 if [ ! -x $MOUNT_PATH ]; then mkdir -p $MOUNT_PATH fi # 针对格式进行挂载 for token in $FORMAT do if [ $token == "FAT32" ]; then mount -t vfat /dev/$1 $MOUNT_PATH echo "[AutoMount] mount -t vfat /dev/$1 $MOUNT_PATH" > $MSG_PATH elif [ $token == "FAT16" ]; then mount -t vfat /dev/$1 $MOUNT_PATH echo "[AutoMount] mount -t vfat /dev/$1 $MOUNT_PATH" > $MSG_PATH elif [ $token == "HPFS/NTFS" ]; then if [ -f /usr/app/ntfs-3g ]; then echo "[AutoMount] /usr/app/ntfs-3g /dev/"$1" "$MOUNT_PATH > /var/log/automount/mount.log export LD_LIBRARY_PATH=/lib/:${LD_LIBRARY_PATH} /usr/app/ntfs-3g /dev/$1 $MOUNT_PATH echo "[AutoMount] /usr/app/ntfs-3g /dev/$1 $MOUNT_PATH" > $MSG_PATH else mount -t ntfs /dev/$1 $MOUNT_PATH echo "[AutoMount] mount -t ntfs /dev/$1 $MOUNT_PATH" > $MSG_PATH fi # 没有针对的格式,直接进行挂载 elif [ $token == "Linux" ]; then mount /dev/$1 $MOUNT_PATH echo "[AutoMount] mount /dev/$1 $MOUNT_PATH" > $MSG_PATH fi done # 自动卸载 else umount $MOUNT_PATH echo "[AutoMount] umount "$MOUNT_PATH > $MSG_PATH fi 3. 插入U盘,输出信息: [rootzengjf init.d]# [ 1233.252058] usb 1-1.2: new high-speed USB device number 5 using musb-hdrc [ 1233.372824] usb 1-1.2: New USB device found, idVendor=0781, idProduct=556b [ 1233.380358] usb 1-1.2: New USB device strings: Mfr=1, Product=2, SerialNumber=3 [ 1233.388402] usb 1-1.2: Product: Cruzer Edge [ 1233.393128] usb 1-1.2: Manufacturer: SanDisk [ 1233.397922] usb 1-1.2: SerialNumber: 2005395413192CA2C0DB [ 1233.411004] usb-storage 1-1.2:1.0: USB Mass Storage device detected [ 1233.450259] scsi host2: usb-storage 1-1.2:1.0 [ 1234.455868] scsi 2:0:0:0: Direct-Access SanDisk Cruzer Edge 1.27 PQ: 0 ANSI: 6 [ 1234.476271] sd 2:0:0:0: [sdc] 15431338 512-byte logical blocks: (7.90 GB/7.36 GiB) [ 1234.523098] sd 2:0:0:0: [sdc] Write Protect is off [ 1234.548813] sd 2:0:0:0: [sdc] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA [ 1234.603919] sdc: sdc1 [ 1234.625862] sd 2:0:0:0: [sdc] Attached SCSI removable disk [AutoMount] mount path : /mnt/usb/sdc1 [ 1234.922506] FAT-fs (sdc1): Volume was not properly unmounted. Some data may be corrupt. Please run fsck. [AutoMount] mount -t vfat /dev/sdc1 /mnt/usb/sdc1