标签:ext gre one move image img logical size 增加
任务:需要在nagios中监控aws的一些虚拟主机,检测/data分区容量,一旦该分区容量低于某个值,就执行脚本利用aws命令指令去创建卷,挂载卷到/data分区容量不足的主机上,然后在主机上利用lvm自动扩充/data分区的容量。利用aws命令去创建挂载磁盘的脚本由lead编写,我需要写lvm自动扩充的脚本。脚本如下:
#!/bin/bash
exec &>/tmp/.lvm #将所有正确和错误输出记录到/tmp/.lvm,追加记录就用>>/tmp/.lvm
#decteing /data partion #检测有没有data分区存在,如果没有就退出脚本
df|grep -i ‘/data‘ || {
echo "no found /data"
exit 1
}
#Judge /data is which vulume #取出/data分区对应的vg名称赋给VG变量,如果VG变量为空值就退出脚本
VG=df -h|grep -i "/data"|cut -d/ -f 4|cut -d\- -f 1
[ ! -n "$VG" ] && {
echo ‘no found $VG‘
exit 1
}
#decte new whick disk #检测新增加磁盘文件名是什么,如果新增了多块,则取最后一块
PRESENT=sudo lsblk --all | grep disk | grep nvme | awk ‘{print $1}‘
do
NUM=fdisk -l | grep $i | wc -l
DISK_PVS="pvs|grep $i
"
REQ=$?
if [ "$NUM" == 1 -a "$REQ" == "1" ]
then
echo "$i is new add disk" >> ${LOG_FILE}
DEV=/dev/$i
fi
done
[ ! -n "$DEV" ] && { #判断dev变量是否为空
echo ‘no found $DEV to extended‘ |tee -a ${LOG_FILE}
exit 1
}
#create new pv #创建pv操作
sudo pvcreate ${DEV} || {
echo "pv create faild"
exit 1
}
#extend vg size #扩充VG大小
sudo vgextend ${VG} ${DEV} || {
echo "vg extend failed"
exit 1
}
#lvextend /data partion #lv卷扩充操作
LV=df|grep -i ‘/data‘|awk ‘{print $1}‘
#截取/data分区lvm卷名
[ ! -n "$LV" ] && {
echo ‘no found $LV‘
exit 1
}
sudo lvextend -l +100%FREE ${LV} || { #扩充/data分区lvm卷
echo "lv extend failed"
exit 1
}
#extend filesystem #扩充文件系统大小,如果是ext4分区,执行resize2fs命令.....
FILESYSTEM=mount | grep "${LV}"|awk ‘{print $5}‘
case $FILESYSTEM in
ext4)
sudo resize2fs ${LV}
;;
xfs)
sudo xfs_growfs ${LV}
;;
*)
echo "unkown filesystem"
;;
esac
在虚拟机中测试如下:
1./data分区大小:/dev/mapper/test-test--lv 40G 48M 38G 1% /data
2.添加一块磁盘并执行脚本:/dev/mapper/test-test--lv 69G 52M 66G 1% /data
3.日志记录如下:
[root@localhost shell]# cat /tmp/.lvm
/dev/mapper/test-test--lv 41153760 49080 39375144 1% /data
nvme0n3 is new add disk
Physical volume "/dev/nvme0n3" successfully created
Volume group "test" successfully extended
Size of logical volume test/test-lv changed from 49.98 GiB (12796 extents) to 69.98 GiB (17916 extents).
Logical volume test-lv successfully resized.
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/mapper/test-test--lv is mounted on /data; on-line resizing required
old_desc_blocks = 5, new_desc_blocks = 9
The filesystem on /dev/mapper/test-test--lv is now 18345984 blocks long.
在nagios中测试,只要检测到/data分区容量不足某个阀值,就触发一系列脚本完成空间自动扩容
这是在lead那里测试的,所以没有记录,因为要反复测试多次,添加不同的aws磁盘类型,扩充lvm后需要将空间缩小,磁盘移除等操作,这样才方便测试,可通过如下脚本将刚添加的磁盘移除:
#!/bin/bash
if [ $# -ne 2 ];then
echo "Usage: $0 reduce2size reducesize ,i.e: 40G 500G"
exit 10
fi
LV=df|grep -i ‘/data‘|awk ‘{print $1}‘
VG=df -h|grep -i "/data"|cut -d/ -f 4|cut -d\- -f 1
sudo umount /data/ || exit 1
sudo e2fsck -f ${LV} || exit 3
sudo resize2fs ${LV} $1 || exit 2
sudo e2fsck -f ${LV} || exit 3
sudo lvreduce -L -$2 ${LV} || exit 4
#VGREDUCE=sudo pvs|grep -i ${VG}|grep nvme|awk ‘NR==1{print $1}‘
VGREDUCE=‘/dev/nvme1n1‘ #这里写要移除的磁盘文件名
sudo vgdisplay -v | grep ${VGREDUCE} || exit 5
sudo vgreduce ${VG} ${VGREDUCE} || exit 6
sudo pvs|grep ${VGREDUCE} || exit 7
sudo pvremove ${VGREDUCE} || exit 8
sudo mount ${LV} /data || exit 9
注:这是在ext4文件系统下做的测试,xfs文件系统好像不能缩小空间,只能增大
标签:ext gre one move image img logical size 增加
原文地址:https://blog.51cto.com/pkimin3/2399583