首页 > 代码库 > 【LVM】LVM自动扩容脚本
【LVM】LVM自动扩容脚本
当新增物理磁盘时,自动扩容到:/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
如果添加了物理磁盘,该脚本会自动比对哪些物理磁盘没有进行LVM扩展,并将未LVM扩展的磁盘扩展到/dev/vg0/data。
比如,新添加了两块物理磁盘,分别为/dev/sdd 和/dev/sde,执行该脚本后的结果:
[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扩展脚本如下:
#!/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"
来自为知笔记(Wiz)
附件列表
【LVM】LVM自动扩容脚本
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。