设计思路:
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
bashdevName=("vdb" "vdc" "vdd" "vde") for a in {0..3} do echo "/dev/${devName[$a]}" # 判断磁盘是否存在 ret0=`fdisk -l |grep /dev/${devName[$a]}` if [ "$ret0" == "" ];then echo "/dev/${devName[$a]} is not exit." continue; fi echo "$a" echo "/dev/${devName[$a]}1" if [[ -b "/dev/${devName[$a]}1" ]];then echo "这块磁盘已经分区,退出,请管理员检查" else echo "开始进行分区操作" echo "parted /dev/${devName[$a]} print" parted /dev/${devName[$a]} print echo "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]}1 echo "pvcreate /dev/${devName[$a]}1" pvcreate /dev/${devName[$a]}1 -y echo "mkdir /zol" if [ ! -d /zol ];then mkdir -p /zol fi echo "add /dev/${devName[$a]}1 to vgzol" vg=`vgs | awk '{if(NR>1){print $1}}' | grep "vgzol"` if [ "$vg" != "" ];then vgextend vgzol /dev/${devName[$a]}1 -f -y else vgcreate vgzol /dev/${devName[$a]}1 -f fi echo "add /dev/${devName[$a]}1 to lvzol" lv=`lvs | awk '{if(NR>1){print $1}}' | grep "lvzol"` if [ "$lv" != "" ];then lvresize -l +100%FREE -r /dev/vgzol/lvzol -f else lvcreate -n lvzol -L +2048G /dev/vgzol -y mkfs.xfs /dev/vgzol/lvzol mount /dev/vgzol/lvzol /zol echo "/dev/vgzol/lvzol /zol xfs defaults 0 0" >> /etc/fstab fi echo "df -h" df -h echo "----------------------------------------------------------------------------------" fi done