CentOS7LVM logical volume partition automatic resizing Shell script
Application scenarios and known problems:
Applicable to CentOS6 or CentOS7 (may be applicable to earlier versions such as CentOS4 or 5)
The root file system (Extended File System) is managed using LVM, such as the mount command output "/dev/mapper/vg _ $ hostname-lv_root on/type ext4 (rw) "contains" mapper "keywords
Automatically expand the root file system. If you want to expand other file systems, for example, some business application data directories are not in the root partition, You need to modify the VG_PATH_TO_EXTEND variable (about 78 rows) in the Shell script code ).
Only ext2, ext3, ext4, xfs, and other partition formats are supported.
Some customized CentOS systems may not be supported, but the core steps are similar.
The script only supports scsi disks. To manage other disks, you need to expand the script by yourself.
To simplify the script and avoid executing it multiple times (this program does not write the execution lock), the existing disk name has been set to sda. For details, see the ONLINE_SCSI_DISK_PRESENT variable in line 1 of the Shell script code.
Considerations:
Because CentOS6 and CentOS7 have different file system formats in the default root file system, you need to determine whether it is xfs. If it is xfs, you should use xfs_growfs instead of simply using resize2fs.
Use resize2fs to expand the file systems in ext2, ext3, and ext4 formats, and use xfs_growfs to expand the file systems in xfs format
If the same script is executed multiple times in the same system, you can use the file lock to solve this problem.
Programming logic:
(You should prepare or check the Shell script runtime environment before) Get the name of the block device file in use
Get the file name of the newly added scsi disk
Obtain the file path of the LVM volume group name (vg) and the extended volume group name
Use fdisk to create partitions for newly added disks and format them in LVM format.
Create a physical volume, pvcreate
Extended volume group, vgextend
Adjust logical volume size, lvresize
Determine whether it is an xfs File System
Synchronize the file system to make the expansion take effect
Back to system disk usage
Shell code:
#!/bin/bash#Usage:AutomaticexpandlvwithLVMmanageddisk#Setp1:AddHardDiskorStoragetoComputingunit#Setp2:Executethisscriptwithrootprivilege#Setp3:Mindinfoofthisscriptexecutionresult#Opentherefrigeratordoor,gettheshellscriptexecutionenvironmentready#Puttheelephantintotherefrigerator,howtheshellscriptsworks#Closetherefrigeratordoor,checkouttheresultofexecution#Simetimes,wehavetopullnewelephantorelephantdungouthere,unsetvariablesofshellscriptfunctioncheck_execution_result(){if[[!-z$RETVAL]];thenunsetRETVALfiRETVAL=$?if[[$RETVAL-ne0]];thenechoexecutionfailed!exit$RETVALelseechoexecutionsuccessfully!fiunsetRETVAL}#lsblk--scsi#lsblk--all#NAMEMAJ:MINRMSIZEROTYPEMOUNTPOINT#fd02:014K0disk#sda8:0040G0disk#├─sda18:10500M0part/boot#└─sda28:2039.5G0part#├─centos-swap253:003.9G0lvm[SWAP]#└─centos-root253:1035.6G0lvm/#sdb8:16016G0disk#sr011:016.6G0rom#Showpresentscsidiskonline#Q:Whyuse"xargs"here?#A:Convertthetextfrommulti-linesingle-columnintosingle-linemulti-column,forsedoperationONLINE_SCSI_DISK_PRESENT=$(lsblk--all|grepdisk|grep-vfd|awk'{print$1}'|xargs)#TODO#ForexecutionthisscriptbeyondtwiceONLINE_SCSI_DISK_PRESENT=sda#Findnewscsidiskonline#TODOfigureitoutwhythereishost0?echo"---">/sys/class/scsi_host/host0/scanecho"---">/sys/class/scsi_host/host1/scanecho"---">/sys/class/scsi_host/host2/scan#ShownewaddedscsidiskonlineONLINE_SCSI_DISK_NEWADD=$(lsblk--all|grepdisk|grep-vfd|awk'{print$1}'|xargsecho|sed"s/$ONLINE_SCSI_DISK_PRESENT//g")#ConstructdiskfilewithfullpathechoNewAddedSCSIDisk:$ONLINE_SCSI_DISK_NEWADD#GetVGNameVG_Name=$(vgdisplay|grep'VGName'|awk'{print$NF}')VG_PATH_TO_EXTEND=$(lvdisplay|grep'LVPath'|awk'{print$NF}'|greproot)forBLOCKin$ONLINE_SCSI_DISK_NEWADD;doONLINE_SCSI_DISK_NEWADD_FILENAME="/dev/"$BLOCK#end-of-filecontentsandeofmarkmuststartrow1fdisk$ONLINE_SCSI_DISK_NEWADD_FILENAME>/dev/null2>&1<<eofnp1t8eweofcheck_execution_resultLVM_OPERATION_DISK_FILENAME=$ONLINE_SCSI_DISK_NEWADD_FILENAME"1"pvcreate$LVM_OPERATION_DISK_FILENAME>/dev/null2>&1check_execution_resultvgextend$VG_Name$LVM_OPERATION_DISK_FILENAME>/dev/null2>&1check_execution_resultlvresize-l+100%FREE$VG_PATH_TO_EXTEND>/dev/null2>&1check_execution_result#resize2fs-ext2/ext3/ext4filesystemresizer#xfs_growfs,xfs_info-expandanXFSfilesystem#[root@hlc7172009~]#resize2fs/dev/mapper/centos-root#resize2fs1.42.9(28-Dec-2013)#resize2fs:Badmagicnumberinsuper-blockwhiletryingtoopen/dev/mapper/centos-root#Couldn'tfindvalidfilesystemsuperblock.#[root@hlc7172009~]##[root@hlc7172009~]#xfs_growfs$VG_PATH_TO_EXTEND#meta-data=/dev/mapper/centos-rootisize=256agcount=4,agsize=2334208blks#=sectsz=512attr=2,projid32bit=1#=crc=0#data=bsize=4096blocks=9336832,imaxpct=25#=sunit=0swidth=0blks#naming=version2bsize=4096ascii-ci=0ftype=0#log=internalbsize=4096blocks=4559,version=2#=sectsz=512sunit=0blks,lazy-count=1#realtime=noneextsz=4096blocks=0,rtextents=0#datablockschangedfrom9336832to13530112#[root@hlc7172009~]##Checkxfs_infoifisinstalledwhichxfs_info>/dev/null2>&1if[[$?-ne0]];thenyuminstallxfsprogs-y>/dev/null2>&1fi#endCheckxfs_infoifisinstalled#CheckVG_PATH_TO_EXTENDifisxfsfilesystemxfs_info$VG_PATH_TO_EXTEND>/dev/null2>&1if[[$?-ne0]];then#isnotxfsVG_PATH_TO_EXTEND_IS_NOT_XFS=0else#isxfsVG_PATH_TO_EXTEND_IS_NOT_XFS=1fi#endCheckVG_PATH_TO_EXTENDifisxfsfilesystem#TODOCentOS7defaultfilesystemisxfs,sowecancheckitoutbyOSifisCentOS7if[[$VG_PATH_TO_EXTEND_IS_NOT_XFS]];then#isxfsxfs_growfs$VG_PATH_TO_EXTEND>/dev/null2>&1else#isnotxfsresize2fs$VG_PATH_TO_EXTEND>/dev/null2>&1ficheck_execution_resultdf-hlsblk--alldoneTest results:
(1) Before adding a disk:
(2) After adding a disk and executing the script:
It can be seen that the root partition has changed from 36 GB to 52 GB, indicating that the LVM is successfully resized.