概要说明:
- /dev/mapper/vg0-data挂载在目录/data下;
- 当新增物理磁盘时,需要自动对/dev/mapper/vg0-data磁盘时进行扩容(自动化脚本);
当新增物理磁盘时,自动扩容到:/dev/vg0/data
未新增物理磁盘时的系统情况:
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb vg0 lvm2 a-- 10.00g 0
/dev/sdc vg0 lvm2 a-- 10.00g 0
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg0 2 1 0 wz--n- 19.98g 0
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
data vg0 -wi-ao---- 19.98g
[root@localhost ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/vg0-data ext3 19G 52M 18G 1% /data
新增物理磁盘:
新添加了两块物理磁盘,分别为/dev/sdd 和/dev/sde,希望执行自动扩容脚本后,可以对/dev/vg0/data扩容;
下面是扩容后的结果:
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb vg0 lvm2 a-- 10.00g 0
/dev/sdc vg0 lvm2 a-- 10.00g 0
/dev/sdd vg0 lvm2 a-- 15.00g 0
/dev/sde vg0 lvm2 a-- 15.00g 0
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg0 4 1 0 wz--n- 49.98g 0
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
data vg0 -wi-ao---- 49.98g
[root@localhost ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/vg0-data ext3 50G 52M 47G 1% /data
自动LVM扩展脚本如下:
- 该脚本会自动比对哪些物理磁盘没有进行LVM扩展,并将未扩展的磁盘扩展到/dev/vg0/data;
- 不会扩展sda和hda磁盘;
#!/bin/bash
#################################################
# Create by ssslinppp
# 201699 15:16:58
##################################################
log_file_extendLvm="/data/logs/log_file_extendLvm.log"
echo "====================================================" > $log_file_extendLvm
echo "Begin to extendLVM at `date "+%Y-%m-%d %H:%M:%S"`" >> $log_file_extendLvm
vg_name="vg0"
lv_name="/dev/vg0/data"
fdisk_info="/data/fdisk_info"
pvs_info="/data/pvs_info"
diffSet="/data/diffSet"
displayInfo()
{
echo "---cmd: df -Th--------------------- " >> $log_file_extendLvm
df -Th /data >> $log_file_extendLvm 2>&1
echo "---cmd: pvs------------------------ " >> $log_file_extendLvm
pvs >> $log_file_extendLvm 2>&1
echo "---------------------------- " >> $log_file_extendLvm
}
getUnLvmDisk()
{
fdisk -l | grep /dev/[sh]d.: | awk -F ' ' '{print $2}' | awk -F ':' '{print $1}' > $fdisk_info
# pvs | grep /dev/[sh]d. | awk -F ' ' '{print $1}' > $pvs_info
pvscan | grep /dev/[sh]d. | awk -F ' ' '{print $2}' > $pvs_info
#get Difference set
# sort $fdisk_info $pvs_info $pvs_info | uniq -u > $diffSet
grep -F -v -f $pvs_info $fdisk_info > $diffSet
echo "-----------------------------" >> $log_file_extendLvm
echo "fdisk_info is :" >> $log_file_extendLvm
cat $fdisk_info >> $log_file_extendLvm
echo "pvs_info is :" >> $log_file_extendLvm
cat $pvs_info >> $log_file_extendLvm
echo "diffSet is:" >> $log_file_extendLvm
cat $diffSet >> $log_file_extendLvm
echo "----------------------------" >> $log_file_extendLvm
rm -rf $fdisk_info
rm -rf $pvs_info
}
extendLV()
{
# example: change "sdb" to "/dev/sdb"
dev_disk=`echo $dev_disk|sed '/dev/!s/^/\/dev\//'`
if [ ! -e $dev_disk ];then
echo "[ERROR] $dev_disk do not exists" >> $log_file_extendLvm
echo "error"
exit -1
fi
if [ `pvs | grep $dev_disk | wc -l` = "1" ];then
echo "[ERROR] $dev_disk(as PE) has already extend to $vg_name" >> $log_file_extendLvm
echo "error"
exit -2
fi
# step 1 :extend vg
echo "step 1- exec cmd: vgextend vg0 $dev_disk" >> $log_file_extendLvm
vgextend -f vg0 $dev_disk >> $log_file_extendLvm 2>&1
# step 2 :extend lv
echo "step 2- exec cmd: lvextend -l 100%VG $lv_name" >> $log_file_extendLvm
lvextend -l 100%VG $lv_name >> $log_file_extendLvm 2>&1
# step 3: resize2fs /dev/vg0/data
echo "step 3- exec cmd: resize2fs $lv_name" >> $log_file_extendLvm
resize2fs $lv_name >> $log_file_extendLvm 2>&1
}
# display info
echo "Before extendLVM..." >> $log_file_extendLvm
displayInfo
getUnLvmDisk
cat $diffSet | while read dev_disk
do
if [ $dev_disk != "/dev/sda" ] && [ $dev_disk != "/dev/hda" ] && [ `echo $dev_disk | wc -L` == 8 ];then
# exec extendLVM
# echo "+++++++++++++++++ [$dev_disk]"
extendLV
fi
done
# display info
echo "After extendLVM... " >> $log_file_extendLvm
displayInfo
echo "extendLVM finish success" >> $log_file_extendLvm
echo "success"
附件列表
转载请标明出处:http://www.cnblogs.com/ssslinppp/