#!/bin/bash
#auther chentongsan,time 2019-05-30
#Description: 适用于系统根分区扩容,第一次、第二次、第三次……..适合用于centos 6、centos 7版本
#扫描总线,识别新增添加的磁盘。
echo "- - -" > /sys/class/scsi_host/host0/scan
echo "- - -" > /sys/class/scsi_host/host1/scan
echo "- - -" > /sys/class/scsi_host/host2/scan
#检测新加磁盘的盘符、扩展VG及磁盘大小
DIVISOR=1073741824
DISK_NAME=fdisk -l | grep "Disk /dev/sd" | awk -F '[ :]+' '{print $2}'
for disk in ${DISK_NAME[*]};do
df_disk=df -h | grep $disk | wc -l
if [[ $df_disk -eq 0 ]];then
pvs_disk=pvs | grep $disk | wc -l
if [[ $pvs_disk -eq 0 ]];then
new_disk_size=fdisk -l | grep "$disk" | awk -F '[ ,:]+' '{print $5}'
new_disk_size_G=$(($new_disk_size/$DIVISOR))
echo "检测到新磁盘:$disk 容量为:${new_disk_size_G}G"
echo "开始创建PV、扩展VG"
#创建PV、扩展VG
pvcreate $disk
VG_NAME=lvdisplay | grep "VG Name" | awk '{print $3}'
LV_PATH=lvdisplay | grep "LV Path" | awk '{print $3}'
vgextend $VG_NAME $disk
lvresize -l +100%FREE $LV_PATH
resize2fs $LV_PATH
echo "$LV_PATH / ext4 defaults 0 0" >> /etc/fstab
fi
fi
done
echo "根分区扩容完成,请检查确认!"