/*********************************************************************
* mdev USB disk auto mount demo
* 说明:
* 之前有分析过mdev,这次富林写了一个demo,跟一下,不过呢,在看过之后
* 觉得,需要优化,后面提供了一种兼容eMMC、SD、U盘、SATA自动挂载的方式,
* 在功能上可以进步的进行优化,其中的eMMC、SD卡功能需要加强区分,这份脚本
* 中没有进一步识别区分,这份参考来自参考文档第二点链接。
*
* 2017-8-21 深圳 龙华樟坑村 曾剑锋
********************************************************************/ 一、参考文档:
. MDEV Primer
http://www.cnblogs.com/zengjfgit/p/5133307.html
. [Linux] Auto mount 自動掛載 usb disk & SD card
http://www.awaysu.acsite.org/linux/linux-auto-mount-usb-disk-sd-card/
. SD card automount for Embedded Linux
http://infosoure.blogspot.com/2011/04/sd-card-automount-for-embedded-linux.html
. 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 二、代码阅读:
. /etc/mdev.conf
...
sda1 : @/etc/usb_disk1_insert
sda : $/etc/usb_disk1_remove
sdb1 : @/etc/usb_disk2_insert
sdb : $/etc/usb_disk2_remove
...
. /etc/usb_disk1_insert
#!/bin/sh if [ ! -e /media/sda1 ]; then
mkdir /media/sda1 -p
fi mount -t vfat /dev/sda1 /media/sda1
. /etc/usb_disk1_remove
#!/bin/sh umount /media/sda1 if [ -d /media/sda1 ]; then
rm /media/sda1 -rf
fi
. /etc/usb_disk2_insert
#!/bin/sh if [ ! -e /media/sdb1 ]; then
mkdir /media/sdb1 -p
fi mount -t vfat /dev/sdb1 /media/sdb1
. /etc/usb_disk2_remove
#!/bin/sh umount /media/sdb1 if [ -d /media/sdb1 ]; then
rm /media/sdb1 -rf
fi 三、更好的处理方式:
. /etc/mdev.conf
...
sd[a-z][-] : *(/etc/init.d/automount.sh $MDEV &)
mmcblk0p[-] : *(/etc/init.d/automount.sh $MDEV &)
...
. /etc/init.d/automount.sh
#!/bin/sh # 挂载路径
MOUNT_PATH=""
# 信息输出串口
MSG_PATH="/dev/ttyS0" # 设备节点为空检查,退出
if [ $ == "" ]; then
echo "[AutoMount] automount.sh parameter is none" > $MSG_PATH
exit
# eMMC或者SD卡挂载
elif [ $ == "mmcblk0p1" ]; then
MOUNT_PATH="/mnt/sdcard"
# 其他类型的盘挂载
else
name=$
check=${name%[-]}
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 $`
# 查看是否已经有挂载设备了,如果已经挂载,那当前次就要卸载
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/$ $MOUNT_PATH
echo "[AutoMount] mount -t vfat /dev/$1 $MOUNT_PATH" > $MSG_PATH
elif [ $token == "FAT16" ]; then
mount -t vfat /dev/$ $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/"$" "$MOUNT_PATH > /var/log/automount/mount.log
export LD_LIBRARY_PATH=/lib/:${LD_LIBRARY_PATH}
/usr/app/ntfs-3g /dev/$ $MOUNT_PATH
echo "[AutoMount] /usr/app/ntfs-3g /dev/$1 $MOUNT_PATH" > $MSG_PATH
else
mount -t ntfs /dev/$ $MOUNT_PATH
echo "[AutoMount] mount -t ntfs /dev/$1 $MOUNT_PATH" > $MSG_PATH
fi
# 没有针对的格式,直接进行挂载
elif [ $token == "Linux" ]; then
mount /dev/$ $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
. 插入U盘,输出信息:
[rootzengjf init.d]# [ 1233.252058] usb -1.2: new high-speed USB device number using musb-hdrc
[ 1233.372824] usb -1.2: New USB device found, idVendor=, idProduct=556b
[ 1233.380358] usb -1.2: New USB device strings: Mfr=, Product=, SerialNumber=
[ 1233.388402] usb -1.2: Product: Cruzer Edge
[ 1233.393128] usb -1.2: Manufacturer: SanDisk
[ 1233.397922] usb -1.2: SerialNumber: 2005395413192CA2C0DB
[ 1233.411004] usb-storage -1.2:1.0: USB Mass Storage device detected
[ 1233.450259] scsi host2: usb-storage -1.2:1.0
[ 1234.455868] scsi :::: Direct-Access SanDisk Cruzer Edge 1.27 PQ: ANSI:
[ 1234.476271] sd :::: [sdc] -byte logical blocks: (7.90 GB/7.36 GiB)
[ 1234.523098] sd :::: [sdc] Write Protect is off
[ 1234.548813] sd :::: [sdc] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
[ 1234.603919] sdc: sdc1
[ 1234.625862] sd :::: [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

mdev USB disk auto mount demo的更多相关文章

  1. How to create a "BOOT USB DISK" for EXSI6.0

    1 准备工作 opensuse 13.2ESXi ISO文件  //vmware 官网下载 VMware-VMvisor-Installer-5.1.0-799733.x86_64.iso,XXXXX ...

  2. how to auto open demo and create it in a new codesandbox

    how to auto open demo and create it in a new codesandbox markdown & iframe https://ant.design/do ...

  3. Openwrt TF Card Auto Mount&Check (4)

    1      Scope of Document This document describes tf card bootup auto do file system check when mount ...

  4. ubuntu auto mount自动挂载硬盘

    Ubuntu 挂载的文章在网上也不少,推荐一个: http://wenku.baidu.com/link?url=N2c7axijp_KYaYkt2CrZFNZPzzS8xBHLQSTUcI2F85I ...

  5. VC++ 6.0 C8051F340 USB PC侧通信 Demo

    // HelloWorld.cpp : Defines the entry point for the console application. // /*********************** ...

  6. 将ISO镜像文件制作成USB disk

    制作USB Live盘有Universal USB Installer.UNetbootin.WinSetupFromUSB.LinuxLive USB Creator.YUMI(Your Unive ...

  7. OpenWrt中开启usb存储和samba服务

    在从官网安装的WNDR3800 15.05.1版本OpenWrt中, 不带usb存储支持以及samba, 需要另外安装 1. 启用usb支持 USB Basic Support https://wik ...

  8. mount命令汇总(转载)

    (一)挂接命令(mount) 首先,介绍一下挂接(mount)命令的使用方法,mount命令参数非常多,这里主要讲一下今天我们要用到的. 命令格式: mount [-t vfstype] [-o op ...

  9. 在GNU/Linux下使用命令行自动挂载与卸载USB磁盘

    在命令行环境下如果每次都是靠手动敲入mount与umount命令来挂载与卸载USB磁盘是件很麻烦的事情.尤其是mount命令的参数非常多.比如,磁盘的分区类型(vfat.ntfs等),挂载的目录节点, ...

随机推荐

  1. Python学习札记(十七) 高级特性3 列表生成式

    参考:列表生成式 Note 1.List Comprehensions,即列表生成式,是Python中内置的非常强大的list生成式. eg.生成一个列表:[1*1, 2*2, ..., 10*10] ...

  2. RNAseq 流程

    https://github.com/twbattaglia/RNAseq-workflow

  3. ScrambleString, 爬行字符串,动态规划

    问题描述: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty su ...

  4. IE与DOM的事件监听

    IE 事件监听 标准DOM不支持   注意几点: window.onload attachEvent detachEvent 标准DOM监听,ie不支持

  5. [C#]委托实例分析(附源码)

    一直都听说C#中的委托与事件非常重要,都没有什么切身的体会,而这次通过做一个WinForm二次开发的项目才真正感觉到了委托与事件的犀利之处. 1.C#中的事件和委托的作用? 事件代表一个组件能够被关注 ...

  6. jquery基础 笔记二

    动态创建元素 关于使用HTML DOM创建元素本文不做详细介绍, 下面举一个简单的例子: //使用Dom标准创建元素 var select = document.createElement(" ...

  7. Spring入门1. IoC入门实例

    Spring入门1. IoC入门实例 Reference:Java EE轻量级解决方案——S2SH 前言: 之前学习过关于Spring的一点知识,曾经因为配置出现问题,而总是被迫放弃学习这些框架技术, ...

  8. poj 1182 食物链(高级的带权并查集)

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 76486   Accepted: 22742 Description ...

  9. LeetCode OJ:Letter Combinations of a Phone Number(数字字母组合)

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  10. Java发送短信

    1.接口使用介绍 发送短信肯定需要使用第三方接口,Java本身是肯定不能直接发送短信的.第三方接口有很多,这里直接找个正规靠谱一点的学习一下 这里使用了中国网建(http://sms.webchine ...