CentOS6,7 LVM logical partitions automatic expansion Shell Scripting

Scenarios and known problems:
Apply to CentOS6 or CentOS7 (probably available on CentOS4 and earlier versions such as 5)
The root file system (extended file system) use LVM to manage, such as the mount command output /dev/mapper/vg_$hostname-lv_root on/type ext4 (RW) contains the Mapper keywords
Automatic expansion of the root file system, if you want to extend the file system, such as the existing business application data directory is not in the root partition, you will need to modify the VG_PATH_TO_EXTEND variable in the Shell script code (around line 78).
Only support partition formats such as ext2, ext3, ext4, XFS file systems
Might not support some customized CentOS system too much, but the most important step is similar to
Scripts only add SCSI disk support, such as the need to manage other disks, you need your own extension scripts
In order to simplify the script, avoiding multiple (the implementation of the present program does not write locks), pre-existing disk name is already set to the SDA, see ONLINE_SCSI_DISK_PRESENT 45th row variables in Shell script code
Due to the CentOS6 and CentOS7 differ in file system format of the default root file system, you need to determine whether for the XFS if XFS xfs_growfs option should be used without using the resize2fs.
Use resize2fs extension of ext2, ext3, ext4 file system format using xfs_growfs expanding XFS file system format
The same script is executed multiple times may be triggered by the same system error, you might consider using file locking to solve this problem
Programming method:
(Should be prepared or checked before Shell scripting environment) Gets a block device file names currently in use
Get new add SCSI disk filename
Gets a LVM volume group (VG), the file path to the volume group name will be expanded
Adds a new disk using Fdisk partition is created and formatted with the LVM format
To create a physical volume, pvcreate
Extending a volume group, vgextend
Adjusting the size of the logical volume, lvresize
To determine whether an XFS file system
Synchronize file systems, making expansion force
Returns the system disk usage
Shell code:
# 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
        if[[ ! -z $RETVAL ]]; then
        if[[ $RETVAL -ne0 ]]; then
                echoexecution failed! 
                echoexecution successfully! 
# lsblk --scsi
# lsblk --all
# 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 | grepdisk | grep-vfd | awk'{print $1}'xargs)
# For execution this script beyond twice
# 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 | grepdisk | grep-vfd | awk'{print $1}'xargsechosed"s/$ONLINE_SCSI_DISK_PRESENT//g")
# Construct disk file with full path
# Get VG Name
VG_Name=$(vgdisplay | grep'VG Name'awk'{print $NF}')
VG_PATH_TO_EXTEND=$(lvdisplay | grep'LV Path'awk'{print $NF}'greproot)
    # end-of-file contents and eof mark must start row1
    fdisk$ONLINE_SCSI_DISK_NEWADD_FILENAME >/dev/null2>&1<<eof
    pvcreate $LVM_OPERATION_DISK_FILENAME >/dev/null2>&1
    vgextend $VG_Name $LVM_OPERATION_DISK_FILENAME >/dev/null2>&1
    lvresize -l +100%FREE $VG_PATH_TO_EXTEND >/dev/null2>&1
# 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 
    whichxfs_info >/dev/null2>&1
    if[[ $? -ne0 ]]; then
        yum installxfsprogs -y >/dev/null2>&1
    # end Check xfs_info if is installed
    # Check VG_PATH_TO_EXTEND if is xfs filesystem
    xfs_info $VG_PATH_TO_EXTEND >/dev/null2>&1
    if[[ $? -ne0 ]]; then
        # is not xfs
        # is xfs
    # 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/null2>&1
        # is not xfs
        resize2fs $VG_PATH_TO_EXTEND >/dev/null2>&1
    lsblk --all

Leave a Reply

Your email address will not be published. Required fields are marked *