Application scenarios and known problems: for 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, for example, mount command output & ldquo;/dev/mapper/vg _ $ hostname-lv_rooton/typeext4 (rw) & rdquo; contains & ldquo; ma 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: 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; 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<
/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 --alldone
Test 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.