標籤:lvm自動擴容指令碼 lvm擴容指令碼 lvm管理 檔案鎖 lvm邏輯卷擴容
應用情境和已知存在的問題:
適用於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 scriptfunction 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 operationONLINE_SCSI_DISK_PRESENT=$(lsblk --all | grep disk | grep -v fd | awk ‘{print $1}‘ | xargs)# TODO# For execution this script beyond twiceONLINE_SCSI_DISK_PRESENT=sda# Find new scsi disk online# TODO figure it out why there is host0? echo "- - -" >/sys/class/scsi_host/host0/scanecho "- - -" >/sys/class/scsi_host/host1/scanecho "- - -" >/sys/class/scsi_host/host2/scan# Show new added scsi disk onlineONLINE_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 pathecho New Added SCSI Disk: $ONLINE_SCSI_DISK_NEWADD# Get VG NameVG_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; doONLINE_SCSI_DISK_NEWADD_FILENAME="/dev/"$BLOCK# end-of-file contents and eof mark must start row1fdisk $ONLINE_SCSI_DISK_NEWADD_FILENAME >/dev/null 2>&1<<eofnp1t8eweofcheck_execution_resultLVM_OPERATION_DISK_FILENAME=$ONLINE_SCSI_DISK_NEWADD_FILENAME"1"pvcreate $LVM_OPERATION_DISK_FILENAME >/dev/null 2>&1check_execution_resultvgextend $VG_Name $LVM_OPERATION_DISK_FILENAME >/dev/null 2>&1check_execution_resultlvresize -l +100%FREE $VG_PATH_TO_EXTEND >/dev/null 2>&1check_execution_result# resize2fs - ext2/ext3/ext4 file system resizer# xfs_growfs, xfs_info - expand an XFS filesystem#[[email protected] ~]# 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.#[[email protected] ~]##[[email protected] ~]# 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#[[email protected] ~]## Check xfs_info if is installed which xfs_info >/dev/null 2>&1if [[ $? -ne 0 ]]; thenyum install xfsprogs -y >/dev/null 2>&1fi# end Check xfs_info if is installed# Check VG_PATH_TO_EXTEND if is xfs filesystemxfs_info $VG_PATH_TO_EXTEND >/dev/null 2>&1if [[ $? -ne 0 ]]; then# is not xfsVG_PATH_TO_EXTEND_IS_NOT_XFS=0else # is xfsVG_PATH_TO_EXTEND_IS_NOT_XFS=1fi# 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 CentOS7if [[ $VG_PATH_TO_EXTEND_IS_NOT_XFS ]]; then# is xfsxfs_growfs $VG_PATH_TO_EXTEND >/dev/null 2>&1else# is not xfsresize2fs $VG_PATH_TO_EXTEND >/dev/null 2>&1ficheck_execution_resultdf -hlsblk --alldone
測試結果:
(1)添加磁碟前:
650) this.width=650;" title="image" style="border-top:0px;border-right:0px;border-bottom:0px;border-left:0px;" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M00/5B/30/wKiom1UBL3TACTpcAADsm6aUG9M856.jpg" height="167" />
(2)添加磁碟並執行指令碼後:
650) this.width=650;" title="image" style="border-top:0px;border-right:0px;border-bottom:0px;border-left:0px;" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M02/5B/2A/wKioL1UBMJTxxX98AAIex5okKck237.jpg" height="391" />
由此可見根分區已經由原先的36GB變為52GB,表示LVM擴容成功。
--END--
本文出自 “通訊,我的最愛” 部落格,請務必保留此出處http://dgd2010.blog.51cto.com/1539422/1619692
CentOS6、7 LVM邏輯卷分區自動擴容Shell指令碼編程思路與執行個體