CentOS6、7 LVM逻辑卷分区自动扩容Shell脚本编程思路与实例
时间:2016-05-21 12:00 来源:linux.it.net.cn 作者:IT
应用场景和已知存在的问题:
-
适用于CentOS6或CentOS7(可能适用于CentOS4或5等早些版本)
-
根文件系统(被扩展的文件系统)采用LVM进行管理,例如mount命令输出“/dev/mapper/vg_$hostname-lv_root on / type ext4 (rw)”中含有“mapper”关键词
-
自动扩容根文件系统,如果想扩展其他文件系统,例如有的业务应用数据目录不在根分区中,则需要修改Shell脚本代码中的VG_PATH_TO_EXTEND变量(约78行)。
-
仅支持ext2、ext3、ext4、xfs等分区格式的文件系统
-
可能不支持某些过多自定义的CentOS系统,但核心步骤相似
-
脚本中仅添加了scsi磁盘支持,如需要管理其他磁盘,则需要自己扩充脚本
-
为了简化脚本,避免执行多次(本程序没有写执行锁),先前已经存在的磁盘名已经设定为sda,见Shell脚本代码中第45行的ONLINE_SCSI_DISK_PRESENT变量
考虑点:
由于CentOS6和CentOS7在默认根文件系统的文件系统格式存在差异,需要判断是否为xfs,如果是xfs则应该使用xfs_growfs而不是一味的使用resize2fs。
使用resize2fs扩展ext2、ext3、ext4格式的文件系统,使用xfs_growfs扩展xfs格式的文件系统
同一脚本在同一系统多次被执行可能引发的错误,可以考虑使用文件锁来解决这个问题
编程思路:
-
(之前应该准备或检查Shell脚本运行环境)获取当前使用中的块设备文件名
-
获取新添加scsi磁盘的文件名
-
获取LVM卷组名(vg)、将被扩展的卷组名的文件路径
-
将新添加磁盘使用fdisk创建分区并格式化为LVM格式
-
创建物理卷,pvcreate
-
扩展卷组,vgextend
-
调节逻辑卷大小,lvresize
-
判断是否是xfs文件系统
-
同步文件系统,使得扩容生效
-
返回系统磁盘使用情况
Shell代码:
#!/bin/bash
# Usage: Automatic expand lv with LVM managed disk
# Setp 1: Add Hard Disk or Storage to Computing unit
# Setp 2: Execute this script with root privilege
# Setp 3: Mind info of this script execution result
# Open the refrigerator door, get the shell script execution environment ready
# Put the elephant into the refrigerator, how the shell scripts works
# Close the refrigerator door, check out the result of execution
# Simetimes, we have to pull new elephant or elephant dung out here, unset variables of shell script
function check_execution_result(){
if [[ ! -z $RETVAL ]]; then
unset RETVAL
fi
RETVAL=$?
if [[ $RETVAL -ne 0 ]]; then
echo execution failed!
exit $RETVAL
else
echo execution successfully!
fi
unset RETVAL
}
# lsblk --scsi
# lsblk --all
# NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
# fd0 2:0 1 4K 0 disk
# sda 8:0 0 40G 0 disk
# ├─sda1 8:1 0 500M 0 part /boot
# └─sda2 8:2 0 39.5G 0 part
# ├─centos-swap 253:0 0 3.9G 0 lvm [SWAP]
# └─centos-root 253:1 0 35.6G 0 lvm /
# sdb 8:16 0 16G 0 disk
# sr0 11:0 1 6.6G 0 rom
# Show present scsi disk online
# Q: Why use "xargs" here?
# A: Convert the text from multi-line single-column into single-line multi-column, for sed operation
ONLINE_SCSI_DISK_PRESENT=$(lsblk --all | grep disk | grep -v fd | awk '{print $1}' | xargs)
# TODO
# For execution this script beyond twice
ONLINE_SCSI_DISK_PRESENT=sda
# Find new scsi disk online
# TODO figure it out why there is host0?
echo "- - -" >/sys/class/scsi_host/host0/scan
echo "- - -" >/sys/class/scsi_host/host1/scan
echo "- - -" >/sys/class/scsi_host/host2/scan
# Show new added scsi disk online
ONLINE_SCSI_DISK_NEWADD=$(lsblk --all | grep disk | grep -v fd | awk '{print $1}' | xargs echo | sed "s/$ONLINE_SCSI_DISK_PRESENT//g")
# Construct disk file with full path
echo New Added SCSI Disk: $ONLINE_SCSI_DISK_NEWADD
# Get VG Name
VG_Name=$(vgdisplay | grep 'VG Name' | awk '{print $NF}')
VG_PATH_TO_EXTEND=$(lvdisplay | grep 'LV Path' | awk '{print $NF}' | grep root)
for BLOCK in $ONLINE_SCSI_DISK_NEWADD; do
ONLINE_SCSI_DISK_NEWADD_FILENAME="/dev/"$BLOCK
# end-of-file contents and eof mark must start row1
fdisk $ONLINE_SCSI_DISK_NEWADD_FILENAME >/dev/null 2>&1<<eof
n
p
1
t
8e
w
eof
check_execution_result
LVM_OPERATION_DISK_FILENAME=$ONLINE_SCSI_DISK_NEWADD_FILENAME"1"
pvcreate $LVM_OPERATION_DISK_FILENAME >/dev/null 2>&1
check_execution_result
vgextend $VG_Name $LVM_OPERATION_DISK_FILENAME >/dev/null 2>&1
check_execution_result
lvresize -l +100%FREE $VG_PATH_TO_EXTEND >/dev/null 2>&1
check_execution_result
# resize2fs - ext2/ext3/ext4 file system resizer
# xfs_growfs, xfs_info - expand an XFS filesystem
#[root@hlc7172009 ~]# resize2fs /dev/mapper/centos-root
#resize2fs 1.42.9 (28-Dec-2013)
#resize2fs: Bad magic number in super-block while trying to open /dev/mapper/centos-root
#Couldn't find valid filesystem superblock.
#[root@hlc7172009 ~]#
#[root@hlc7172009 ~]# xfs_growfs $VG_PATH_TO_EXTEND
#meta-data=/dev/mapper/centos-root isize=256 agcount=4, agsize=2334208 blks
# = sectsz=512 attr=2, projid32bit=1
# = crc=0
#data = bsize=4096 blocks=9336832, imaxpct=25
# = sunit=0 swidth=0 blks
#naming =version 2 bsize=4096 ascii-ci=0 ftype=0
#log =internal bsize=4096 blocks=4559, version=2
# = sectsz=512 sunit=0 blks, lazy-count=1
#realtime =none extsz=4096 blocks=0, rtextents=0
#data blocks changed from 9336832 to 13530112
#[root@hlc7172009 ~]#
# Check xfs_info if is installed
which xfs_info >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
yum install xfsprogs -y >/dev/null 2>&1
fi
# end Check xfs_info if is installed
# Check VG_PATH_TO_EXTEND if is xfs filesystem
xfs_info $VG_PATH_TO_EXTEND >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
# is not xfs
VG_PATH_TO_EXTEND_IS_NOT_XFS=0
else
# is xfs
VG_PATH_TO_EXTEND_IS_NOT_XFS=1
fi
# end Check VG_PATH_TO_EXTEND if is xfs filesystem
# TODO CentOS7 default filesystem is xfs, so we can check it out by OS if is CentOS7
if [[ $VG_PATH_TO_EXTEND_IS_NOT_XFS ]]; then
# is xfs
xfs_growfs $VG_PATH_TO_EXTEND >/dev/null 2>&1
else
# is not xfs
resize2fs $VG_PATH_TO_EXTEND >/dev/null 2>&1
fi
check_execution_result
df -h
lsblk --all
done
测试结果:
(1)添加磁盘前:
(2)添加磁盘并执行脚本后:
由此可见根分区已经由原先的36GB变为52GB,表示LVM扩容成功。
--END--
(责任编辑:IT)
应用场景和已知存在的问题:
考虑点:由于CentOS6和CentOS7在默认根文件系统的文件系统格式存在差异,需要判断是否为xfs,如果是xfs则应该使用xfs_growfs而不是一味的使用resize2fs。 使用resize2fs扩展ext2、ext3、ext4格式的文件系统,使用xfs_growfs扩展xfs格式的文件系统 同一脚本在同一系统多次被执行可能引发的错误,可以考虑使用文件锁来解决这个问题 编程思路:
Shell代码:#!/bin/bash # Usage: Automatic expand lv with LVM managed disk # Setp 1: Add Hard Disk or Storage to Computing unit # Setp 2: Execute this script with root privilege # Setp 3: Mind info of this script execution result # Open the refrigerator door, get the shell script execution environment ready # Put the elephant into the refrigerator, how the shell scripts works # Close the refrigerator door, check out the result of execution # Simetimes, we have to pull new elephant or elephant dung out here, unset variables of shell script function check_execution_result(){ if [[ ! -z $RETVAL ]]; then unset RETVAL fi RETVAL=$? if [[ $RETVAL -ne 0 ]]; then echo execution failed! exit $RETVAL else echo execution successfully! fi unset RETVAL } # lsblk --scsi # lsblk --all # NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT # fd0 2:0 1 4K 0 disk # sda 8:0 0 40G 0 disk # ├─sda1 8:1 0 500M 0 part /boot # └─sda2 8:2 0 39.5G 0 part # ├─centos-swap 253:0 0 3.9G 0 lvm [SWAP] # └─centos-root 253:1 0 35.6G 0 lvm / # sdb 8:16 0 16G 0 disk # sr0 11:0 1 6.6G 0 rom # Show present scsi disk online # Q: Why use "xargs" here? # A: Convert the text from multi-line single-column into single-line multi-column, for sed operation ONLINE_SCSI_DISK_PRESENT=$(lsblk --all | grep disk | grep -v fd | awk '{print $1}' | xargs) # TODO # For execution this script beyond twice ONLINE_SCSI_DISK_PRESENT=sda # Find new scsi disk online # TODO figure it out why there is host0? echo "- - -" >/sys/class/scsi_host/host0/scan echo "- - -" >/sys/class/scsi_host/host1/scan echo "- - -" >/sys/class/scsi_host/host2/scan # Show new added scsi disk online ONLINE_SCSI_DISK_NEWADD=$(lsblk --all | grep disk | grep -v fd | awk '{print $1}' | xargs echo | sed "s/$ONLINE_SCSI_DISK_PRESENT//g") # Construct disk file with full path echo New Added SCSI Disk: $ONLINE_SCSI_DISK_NEWADD # Get VG Name VG_Name=$(vgdisplay | grep 'VG Name' | awk '{print $NF}') VG_PATH_TO_EXTEND=$(lvdisplay | grep 'LV Path' | awk '{print $NF}' | grep root) for BLOCK in $ONLINE_SCSI_DISK_NEWADD; do ONLINE_SCSI_DISK_NEWADD_FILENAME="/dev/"$BLOCK # end-of-file contents and eof mark must start row1 fdisk $ONLINE_SCSI_DISK_NEWADD_FILENAME >/dev/null 2>&1<<eof n p 1 t 8e w eof check_execution_result LVM_OPERATION_DISK_FILENAME=$ONLINE_SCSI_DISK_NEWADD_FILENAME"1" pvcreate $LVM_OPERATION_DISK_FILENAME >/dev/null 2>&1 check_execution_result vgextend $VG_Name $LVM_OPERATION_DISK_FILENAME >/dev/null 2>&1 check_execution_result lvresize -l +100%FREE $VG_PATH_TO_EXTEND >/dev/null 2>&1 check_execution_result # resize2fs - ext2/ext3/ext4 file system resizer # xfs_growfs, xfs_info - expand an XFS filesystem #[root@hlc7172009 ~]# resize2fs /dev/mapper/centos-root #resize2fs 1.42.9 (28-Dec-2013) #resize2fs: Bad magic number in super-block while trying to open /dev/mapper/centos-root #Couldn't find valid filesystem superblock. #[root@hlc7172009 ~]# #[root@hlc7172009 ~]# xfs_growfs $VG_PATH_TO_EXTEND #meta-data=/dev/mapper/centos-root isize=256 agcount=4, agsize=2334208 blks # = sectsz=512 attr=2, projid32bit=1 # = crc=0 #data = bsize=4096 blocks=9336832, imaxpct=25 # = sunit=0 swidth=0 blks #naming =version 2 bsize=4096 ascii-ci=0 ftype=0 #log =internal bsize=4096 blocks=4559, version=2 # = sectsz=512 sunit=0 blks, lazy-count=1 #realtime =none extsz=4096 blocks=0, rtextents=0 #data blocks changed from 9336832 to 13530112 #[root@hlc7172009 ~]# # Check xfs_info if is installed which xfs_info >/dev/null 2>&1 if [[ $? -ne 0 ]]; then yum install xfsprogs -y >/dev/null 2>&1 fi # end Check xfs_info if is installed # Check VG_PATH_TO_EXTEND if is xfs filesystem xfs_info $VG_PATH_TO_EXTEND >/dev/null 2>&1 if [[ $? -ne 0 ]]; then # is not xfs VG_PATH_TO_EXTEND_IS_NOT_XFS=0 else # is xfs VG_PATH_TO_EXTEND_IS_NOT_XFS=1 fi # end Check VG_PATH_TO_EXTEND if is xfs filesystem # TODO CentOS7 default filesystem is xfs, so we can check it out by OS if is CentOS7 if [[ $VG_PATH_TO_EXTEND_IS_NOT_XFS ]]; then # is xfs xfs_growfs $VG_PATH_TO_EXTEND >/dev/null 2>&1 else # is not xfs resize2fs $VG_PATH_TO_EXTEND >/dev/null 2>&1 fi check_execution_result df -h lsblk --all done 测试结果:(1)添加磁盘前:
(2)添加磁盘并执行脚本后:
由此可见根分区已经由原先的36GB变为52GB,表示LVM扩容成功。 --END-- (责任编辑:IT) |