mdev 把sd卡节点固定到特定目录

该博客介绍了如何在Linux系统中利用busybox的mdev工具实现热插拔支持,并通过配置mdev.conf文件及脚本autosd.sh自动挂载mmcblk设备到/mnt目录下。内容包括设置用户、组和权限,处理 mmcblk 设备的挂载与卸载,以及创建/dev/hd/sda节点的链接。
摘要由CSDN通过智能技术生成

成功后在/dev 下会有/dev/hd/sda节点 ,可以mount 到mnt/sda

1、使用busybox mdev 

 

//通知kernel支持热插拔

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

//起动mdev 是个进展 在ps下可见
mdev -s

起mdev.conf 内容如下

#
# This is a sample mdev.conf
#

# Provide user, group, and mode information for devices.  If a regex matches
# the device name provided by sysfs, use the appropriate user:group and mode
# instead of the default 0:0 660.
#
# Syntax:
# [-]devicename_regex user:group mode [=path]|[>path]|[!] [@|$|*cmd args...]
# [-]$ENVVAR=regex    user:group mode [=path]|[>path]|[!] [@|$|*cmd args...]
# [-]@maj,min[-min2]  user:group mode [=path]|[>path]|[!] [@|$|*cmd args...]
#
# [-]: do not stop on this match, continue reading mdev.conf
# =: move, >: move and create a symlink
# !: do not create device node
# @|$|*: run@cmd if $ACTION=add,  $cmd if $ACTION=remove, *cmd in all cases

# support module loading on hotplug
$MODALIAS=.*    root:root 660 @modprobe "$MODALIAS"

#mmcblk[0-9]p[0-9] root:root 660 */etc/mdev-script/autosd.sh
mmcblk[0-2] root:root 660 */etc/mdev-script/autosd.sh
#sd[a-z] root:root 660 */etc/mdev-script/autoscsi.sh

 

autosd.sh 内容如下  

#!/bin/sh
 
MNT_PATH=/mnt

MNT_DIR=
SD1_DIR=sda
SD2_DIR=sd2
SD3_DIR=sd3
EMMC1_DIR=emmc1
EMMC2_DIR=emmc2
CACHE_DIR=cache
DRIVE_A=""
DRIVE_B=""

my_umount()
{
    FOLDER=`grep "/dev/$1" /proc/mounts | cut -d ' ' -f 2`
    if [ ! -z "$FOLDER" ]; then
        umount -l "$FOLDER";
    fi
}
 
my_mount()
{
    MMCBUSNUM=`echo $DEVPATH | cut -d '/' -f 4`
    if [ "$MMCBUSNUM" == "nt96660_mmc.0" ] || [ "$MMCBUSNUM" == "f0420000.mmc" ]; then
        MNT_DIR=$SD1_DIR
    elif [ "$MMCBUSNUM" == "nt96660_mmc.1" ] || [ "$MMCBUSNUM" == "f0500000.mmc" ]; then
        MNT_DIR=$SD2_DIR
    else
        MNT_DIR=$SD3_DIR
    fi

    if [ -b /dev/$1 ]; then
        MOUNTDEV="/dev/$1"
        if [ -b "/dev/$1p1" ]; then
            MOUNTDEV="/dev/$1p1"
        fi
    fi    

    time_offset_sig=`date +%z | cut -c 1`
    time_offset_h=`date +%z | cut -c 2-3`
    time_offset_m=`date +%z | cut -c 4-5`
    time_offset=`expr $local_time - $utc_time`
    if [ $time_offset_sig == + ]; then
        time_offset_sig="";
    fi
    time_offset_total_m=$time_offset_sig`expr $time_offset_h \* 60 + $time_offset_m`
    
    mkdir -p "${MNT_PATH}/${MNT_DIR}" || exit 1
    fat_type=`blkid "$MOUNTDEV" | awk -F'TYPE=' '{print $NF}'`
    if [ "${fat_type}" == "\"vfat\"" ]; then
        if ! mount -o dirsync,time_offset=$time_offset_total_m "$MOUNTDEV" "${MNT_PATH}/${MNT_DIR}" 2>&1 | tee -a /tmp/mountstat; then
            echo "$MOUNTDEV $MNT_PATH/$MNT_DIR ignore defaults 0 0" >> /tmp/.nvt_mounts
            exit 1
        fi
    elif [ "${fat_type}" == "\"exfat\"" ]; then
        if ! mount -t exfat "$MOUNTDEV" "${MNT_PATH}/${MNT_DIR}" 2>&1 | tee -a /tmp/mountstat; then
            echo "$MOUNTDEV $MNT_PATH/$MNT_DIR ignore defaults 0 0" >> /tmp/.nvt_mounts
            continue
        fi
    else
        echo "$MOUNTDEV FAIL" >> /tmp/.nvt_mounts
        exit 1
    fi
}

check_mmc_ready()
{
    MMCBUSPATH="/sys/bus/mmc/devices/"
    x=0
    timeout=100
    echo "check_mmc_ready"
    while [ "$x" -lt "$timeout" -a ! -d $MMCBUSPATH ]; do
        echo "MMCBUSPATH is not exist."
        x=$((x+1))
        sleep .1
    done
    if [ "$x" -ge "$timeout" ]; then
        echo "check_mmc_ready timeout"
        return  -1;
    fi
    echo "MMCBUSPATH:$MMCBUSPATH"
    return 0;
}

create_link()
{
    if [ -b /dev/$1 ]; then
        echo dev=$1
        if [ -d "/dev/hd/" ];then
            rm -rf /dev/hd/
        fi
        mkdir -p /dev/hd/
        ln -s /dev/$1 /dev/hd/sda
        for part in $(ls /dev/$1p*)
        do
        if [ -b $part ]; then
            ln -s $part /dev/hd/sda1
            break;
        fi
        done
    fi    
}

remove_link()
{
    rm -rf /dev/hd
}

if [ -z $DEVPATH ]; then
    # This is for boot stage handling
    MMCBUSPATH="/sys/bus/mmc/devices/"
    check_mmc_ready
    ret=$?
    if [ $ret != 0 ]; then
        echo "MMCBUSPATH exist"
        exit;
    fi
    
    MMCDEVLIST=`ls $MMCBUSPATH`

    for n in $MMCDEVLIST
    do
        # Check if it is not SD device
        SD_TYPE=`cat $MMCBUSPATH/$n/type`
        echo "SD_TYPE:$SD_TYPE"
        if [ $SD_TYPE == SDIO ]; then
            continue
        fi

        if [ $SD_TYPE == MMC ]; then
            # To check if it's the emmc storage device
            if [ -f $MMCBUSPATH/$n/bga ]; then
                BGA=`cat $MMCBUSPATH/$n/bga`
                if [ 1 == $BGA ]; then
                    # Get the block device name
                    BLOCKDEV=`ls $MMCBUSPATH/$n/block`
                    create_link $BLOCKDEV
                fi
            fi
            continue
        fi

        # Get the block device name
        BLOCKDEV=`ls $MMCBUSPATH/$n/block`
        if [ -b /dev/$BLOCKDEV ]; then
            create_link $BLOCKDEV
        else
            continue
        fi
    done
else
    # This is for booted up stage
    case "${ACTION}" in
    add|"")
        remove_link 
        create_link ${MDEV}
        ;;
    remove)
        remove_link 
        ;;
    esac
fi
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值