LVMLVM自动扩容脚本
Posted ssslinppp
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LVMLVM自动扩容脚本相关的知识,希望对你有一定的参考价值。
当新增物理磁盘时,自动扩容到:/dev/vg0/data
例如,如下是原始的:
[[email protected] ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb vg0 lvm2 a-- 10.00g 0
/dev/sdc vg0 lvm2 a-- 10.00g 0
[[email protected] ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg0 2 1 0 wz--n- 19.98g 0
[[email protected] ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
data vg0 -wi-ao---- 19.98g
[[email protected] ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/vg0-data ext3 19G 52M 18G 1% /data
如果添加了物理磁盘,该脚本会自动比对哪些物理磁盘没有进行LVM扩展,并将未LVM扩展的磁盘扩展到/dev/vg0/data。
比如,新添加了两块物理磁盘,分别为/dev/sdd 和/dev/sde,执行该脚本后的结果:
[[email protected] ~]# 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
[[email protected] ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg0 4 1 0 wz--n- 49.98g 0
[[email protected] ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
data vg0 -wi-ao---- 49.98g
[[email protected] ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/vg0-data ext3 50G 52M 47G 1% /data
自动LVM扩展脚本如下:
#!/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
#get Difference set
sort $fdisk_info $pvs_info $pvs_info | uniq -u > $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" ] && [ `echo $dev_disk | wc -L` == 8 ];then
# exec extendLVM
extendLV
fi
done
# display info
echo "After extendLVM... " >> $log_file_extendLvm
displayInfo
echo "extendLVM finish success" >> $log_file_extendLvm
echo "success"
附件列表
以上是关于LVMLVM自动扩容脚本的主要内容,如果未能解决你的问题,请参考以下文章