initDB.sh初始化磁盘脚本centos7

2022/3/1 7:24:32

本文主要是介绍initDB.sh初始化磁盘脚本centos7,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

vim initDB.sh

#!/bin/bash
#auther wangxp

EXCLUDE_LIST='2,11'
EXCLUDE_DISK=sda
VG_NAME=vg0

function check {
if test ! $? -eq 0
then
echo "Error!"
exit 1
fi
}
lsblk |grep -v sda
#生成磁盘数组1
VDA_NUM=`lsblk -d -e $EXCLUDE_LIST -p|grep -v $EXCLUDE_DISK|grep -v 'NAME'|wc -l`
if test $VDA_NUM -ge 1;then
echo "-------------- New hard disk detected ---------------------"
for(( i=0; i< ${VDA_NUM} ; i++ ))
do
vda_name=`lsblk -d -e $EXCLUDE_LIST -p|grep -v $EXCLUDE_DISK|grep -v 'NAME'|awk "NR==($i+1)"|awk '{print $1}'`
vda_names[$i]=${vda_name}
done
else
exit 1
fi
#设置选择操作磁盘 VDA_NAME 磁盘名
PS3="Select an unmounted hard disk: "
select VDA_NAME in ${vda_names[@]}
do
if test -z $VDA_NAME
then
echo "Input error!"
exit 1
fi
break
done
#设置挂载点数量
read -p "Input the number of mount points: " POINT_NUM
while true;do
if test -z $POINT_NUM
then
echo "ERROR,input is null"
echo -n "Input the number of mount points: "
read POINT_NUM
else
break
fi
done

if test $POINT_NUM -ge 1
then
LV_MAX_SIZE=`lsblk -d -e $EXCLUDE_LIST -p| grep $VDA_NAME|awk '{print $4}'`
LV_FREE_SIZE=${LV_MAX_SIZE%G*}
for (( i=0;i<${POINT_NUM};i++ ))
do
j=$[$i+1]
echo "Set mount point /u0$j"
#最后一个lv默认为剩余空间
if test $[$POINT_NUM-$j] -eq 0
then
echo "set default lv size of lvu0$j ${LV_FREE_SIZE}G"
LV_SIZE=$LV_FREE_SIZE
else
#判断空间大小
read -p "Input size of u0$j(free size ${LV_FREE_SIZE}G): " LV_SIZE
while true;do
if test -z $LV_SIZE
then
echo "ERROR,input is null"
echo -n "Input size of u0$j(free size ${LV_FREE_SIZE}G): "
read LV_SIZE
elif test $LV_SIZE -ge $LV_FREE_SIZE
then
echo "ERROR,input size greater than or equal to ${LV_FREE_SIZE}G"
echo -n "Input size of u0$j(free size ${LV_FREE_SIZE}G): "
read LV_SIZE
elif test $LV_SIZE -le 0
then
echo "ERROR,input size less than or equal to 0G"
echo -n "Input size of u0$j(free size ${LV_FREE_SIZE}G): "
read LV_SIZE
else
break
fi
done
LV_FREE_SIZE=$[$LV_FREE_SIZE-$LV_SIZE]
fi
lv_sizes[$i]=$LV_SIZE
lv_names[$i]="u0${j}"
done
else
echo "Input illegal"
fi


echo '-----------------'
echo VDA_NAME=$VDA_NAME
echo VG_NAME=$VG_NAME
for (( i=0;i<${POINT_NUM};i++ ))
do
echo "mount point=/${lv_names[$i]},lv_size=${lv_sizes[$i]}G"
done
echo '-----------------'
read -p "Confirm configuration(yes/no)?" INPUT
while true;do
case $INPUT in
y|yes)
    #创建物理卷
    pvcreate $VDA_NAME
    check
    #创建逻辑卷组
    vgcreate $VG_NAME $VDA_NAME
    check
    #创建逻辑卷
    for (( i=0;i<${POINT_NUM};i++ ))
    do
    j=$[$i+1]
    if test $[$POINT_NUM-$j] -eq 0
    then
    lvcreate -l +100%free -n "lv${lv_names[$i]}" $VG_NAME
    check
    else
    lvcreate -L ${lv_sizes[$i]}G -n "lv${lv_names[$i]}" $VG_NAME
    check
    fi
    #创建挂载点
    mkdir "/${lv_names[$i]}"
    check
    #格式化逻辑卷
        mkfs -t xfs "/dev/$VG_NAME/lv${lv_names[$i]}"
    check
    #添加fstab开机启动
    fstab_lv=`cat /etc/fstab|grep "/dev/mapper/$VG_NAME-lv${lv_names[$i]}"`
    if test -z $fstab_lv
    then
    echo "/dev/mapper/$VG_NAME-lv${lv_names[$i]} /${lv_names[$i]}  xfs  defaults  0 0" >> /etc/fstab
    check
    fi
    done
    #重新加载fstab文件中的内容
    echo "mount all point......"
    mount -a
    lsblk
    break
;;
n|no)
        break
;;
*)
        echo "Input ERROR"
    echo -n "Confirm configuration(yes/no)?"
        read INPUT
;;
esac
done

 



这篇关于initDB.sh初始化磁盘脚本centos7的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程