Linux下批量挂载硬盘

  • 全新环境下正常运行,旧环境需要谨慎使用,要考虑如lv是否已存在,挂载点是否已存在等因素;
  • 挂载对象为数据盘,除开系统盘所在的磁盘;(机械,SSD,NVME_SSD);
  • SSD做成lvm并格式化为xfs,然后写入/etc/fstab并挂载;
    • SATA-SSD挂载点为: /ssd1;
    • NVME-SSD挂载点为:/ssd2;
  • 每个机械硬盘单独挂载
    • 挂载点为/datax;1)
  • 添加参数 mkfs.xfs -n ftype=1 2)

auto_mount.sh
#!/bin/bash
#Author:MRCO
#2017-04-12
 
cd `dirname $0`
 
function data_disks_list(){
    #查找系统盘
    sys_disk=`df -h|grep -w '/boot'|awk '{print $1}'|sed 's/.$//g'|uniq`
    #全部磁盘
    all_disks=`lsblk -dnp -e 9,11,180,253 --output KNAME,SIZE,RM,RO,ROTA|egrep -v 'mapper|\/dev\/md'|awk '$3=='0' && $4=='0''|sort`
    data_disks=`echo "$all_disks"|grep -v $sys_disk`
    echo "$data_disks" > data_disks.list
    echo -ne "-------------------------grab data_disks list success--------------\n"
}
 
 
function ssd_sata(){
    cp /etc/fstab /tmp/fstab.`date +%F-%H_%M_%S`
    disks=`cat data_disks.list | awk '{if ($5 == '0') print $1 }'|grep -v 'nvme'|xargs`
    lv='/dev/vg-ssd/lv-ssd'
    if [[ -n ${disks} ]];then
        if [[ ! -a ${lv} ]];then
            mountdir="/ssd1"
            wipefs -af ${disks}
            pvcreate ${disks}
            vgcreate vg-ssd ${disks}
            lvcreate -l +100%FREE -n lv-ssd vg-ssd
            mkfs.xfs -n ftype=1 -fq ${lv}
            [[ ! -d ${mountdir} ]] && mkdir -p ${mountdir}
            umount -l ${mountdir} 2> /dev/null
            echo "${lv}    ${mountdir}    xfs    defaults,noatime    0 0" >> /etc/fstab
            mount -a
            echo -ne "-------------------------SATA_SSD mount success-------------------------\n"
        else
            echo -ne "-------------------------$lv exists,quit-------------------------\n"
            exit
        fi
    else
        echo -ne "-------------------------NO SSD FOUND-------------------------\n"
    fi
}
 
 
function ssd_nvme(){
    cp /etc/fstab /tmp/fstab.`date +%F-%H_%M_%S`
    disks=`cat data_disks.list | awk '{if ($5 == '0') print $1 }'|grep 'nvme'|xargs`
    lv='/dev/vg-nvme/lv-nvme'
    mountdir="/ssd2"
    if [[ -n ${disks} ]];then
        if [[ ! -a $lv ]];then
            wipefs -af ${disks}
            pvcreate ${disks}
            vgcreate vg-nvme ${disks}
            lvcreate -l +100%FREE -n lv-nvme vg-nvme
            mkfs.xfs -n ftype=1 -fq ${lv}
            [[ ! -d ${mountdir} ]] && mkdir -p ${mountdir}
            umount -l ${mountdir} 2> /dev/null
            echo "${lv}    ${mountdir}    xfs    defaults,noatime    0 0" >> /etc/fstab
            mount -a
            echo -ne "-------------------------NVME_SSD mount success-------------------------\n"
        else
            echo -ne "-------------------------$lv exists,quit-------------------------\n"
            exit
        fi
    else
        echo -ne "-------------------------NO NVME_SSD FOUND-------------------------\n"
    fi
}
 
 
function hdd_disk(){
    cp /etc/fstab /tmp/fstab.`date +%F-%H_%M_%S`
    disks=`cat data_disks.list | awk '{if ($5 == '1') print $1 }'|sort|xargs`
    mountdir='/data'
    #判断硬盘列表是否为空
    if [[ -n ${disks} ]];then
        wipefs -af ${disks}
        disk_num=`cat data_disks.list | awk '{if ($5 == '1') print $1 }' | wc -l`
        var=1
        #循环挂载单个HDD
        while [[ $var -le $disk_num ]];do
            for i in $disks;do
                mkfs.xfs -n ftype=1 -fq $i
                uuid=`blkid -s UUID $i | awk '{print $2}'`
                [[ ! -d ${mountdir}${var} ]] && mkdir -p ${mountdir}${var}
                umount -l ${mountdir}${var} 2> /dev/null
                echo "$uuid    ${mountdir}${var}    xfs    defaults,noatime    0 0" >> /etc/fstab
                var=$((var + 1))
            done
        done
        mount -a
        echo -ne "-------------------------HDD mount success-------------------------\n"
    else
        echo -ne "-------------------------NO HDD FOUND-------------------------\n"
    fi
}
 
 
function help_me(){
    echo
    echo "使用说明:该脚本用于自动判断磁盘类型(SATA-SSD/NVME-SSD/HDD)并格式化,挂载."
    echo
    echo "命令参数:"
    echo "-l: 查找出系统盘以外的磁盘,并输出到data_disks.list"
    echo "-s: 格式化全部SATA类型的ssd并挂载."
    echo "-n: 格式化全部nvme类型(PCI-E)的ssd并挂载."
    echo "-h: 格式化全部机械磁盘并挂载."
    echo "-a: 处理全部数据盘,包括SATA/NVME类型的SSD和机械盘."
}
 
 
#调用函数
if [[ $# -gt 0 ]];then
    while getopts "lsnha" opt; do
        case $opt in
 
            l)
                (data_disks_list);;
            s)
                (data_disks_list)
                (ssd_sata);;
            n)
                (data_disks_list)
                (ssd_nvme);;
            h)
                (data_disks_list)
                (hdd_disk);;
            a)
                (data_disks_list)
                (ssd_sata)
                (ssd_nvme)
                (hdd_disk);;
        esac
    done
else
    help_me
fi

  • linux/others/linux批量挂载硬盘.txt
  • 最后更改: 2019/06/20 09:52
  • 由 mrco