设计思路:
1、创建磁盘分区变量集合
devName=("vdb" "vdc" "vdd" "vde")
2、创建for循环,磁盘分区数量 /dev/vdb0~3,用于在for循环中if判断磁盘分区是否已经存在
if 判断条件 [[ -b "目录名称"]]
if [[ -b "/dev/${devName[$a]}1" ]];then
3、如磁盘分区不存在就创建磁盘分区 PV、VG、LV
在root用户下执行
cat diskcfg.sh
devName=("vdb" "vdc" "vdd" "vde") for a in {0..3} doecho "/dev/${devName[$a]}"# 判断磁盘是否存在ret0=`fdisk -l |grep /dev/${devName[$a]}`if [ "$ret0" == "" ];thenecho "/dev/${devName[$a]} is not exit."continue;fiecho "$a"echo "/dev/${devName[$a]}1"if [[ -b "/dev/${devName[$a]}1" ]];thenecho "这块磁盘已经分区,退出,请管理员检查"elseecho "开始进行分区操作"echo "parted /dev/${devName[$a]} print"parted /dev/${devName[$a]} printecho "parted /dev/${devName[$a]} "mklabel gpt""parted /dev/${devName[$a]} "mklabel gpt ignore yes"echo "parted /dev/${devName[$a]} "mkpart primary xfs 1 -1""parted /dev/${devName[$a]} "mkpart primary xfs 1 -1 ignore"echo "parted /dev/${devName[$a]} "quit""parted /dev/${devName[$a]} "quit"echo "mkfs.xfs /dev/${devName[$a]}1"mkfs.xfs -f /dev/${devName[$a]}1echo "pvcreate /dev/${devName[$a]}1"pvcreate /dev/${devName[$a]}1 -yecho "mkdir /zol"if [ ! -d /zol ];thenmkdir -p /zolfiecho "add /dev/${devName[$a]}1 to vgzol"vg=`vgs | awk '{if(NR>1){print $1}}' | grep "vgzol"`if [ "$vg" != "" ];thenvgextend vgzol /dev/${devName[$a]}1 -f -yelsevgcreate vgzol /dev/${devName[$a]}1 -ffiecho "add /dev/${devName[$a]}1 to lvzol"lv=`lvs | awk '{if(NR>1){print $1}}' | grep "lvzol"`if [ "$lv" != "" ];thenlvresize -l +100%FREE -r /dev/vgzol/lvzol -felselvcreate -n lvzol -L +2048G /dev/vgzol -ymkfs.xfs /dev/vgzol/lvzolmount /dev/vgzol/lvzol /zolecho "/dev/vgzol/lvzol /zol xfs defaults 0 0" >> /etc/fstabfiecho "df -h"df -hecho "----------------------------------------------------------------------------------"fi done