CentOS6, 7 LVM Logical Volume partitioning automatic expansion shell script programming ideas and examples

Source: Internet
Author: User
Tags disk usage

Application scenarios and Known issues:

    1. For CentOS6 or CentOS7 (may apply to CentOS4 or earlier versions of 5)

    2. The root filesystem (extended file system) is managed using LVM, such as the Mount command output "/dev/mapper/vg_$hostname-lv_root on/type ext4 (rw)" containing "mapper" keywords

    3. Automatic expansion of the root file system, if you want to extend other file systems, such as some business application Data directory is not in the root partition, you need to modify the shell script code in the Vg_path_to_extend variable (about 78 lines).

    4. Only file systems that support Ext2, ext3, Ext4, XFS, and other partitioned formats

    5. Some of the many custom CentOS systems may not be supported, but the core steps are similar

    6. Only SCSI disk support is added to the script, and you need to expand the script yourself if you need to manage additional disks

    7. In order to simplify the script, avoid executing multiple times (this program does not write execution lock), the previously existing disk name has been set to SDA, see the shell script code in line 45th of the online_scsi_disk_present variable

Consider the point:

Because CentOS6 and CentOS7 differ in the file system format of the default root file system, it is necessary to determine whether XFS, or XFS, should use XFS_GROWFS instead of blindly using RESIZE2FS.

Use RESIZE2FS to extend the ext2, ext3, ext4 format file system using XFS_GROWFS to extend the XFS format file system

The same script can cause errors to be executed multiple times on the same system, consider using a file lock to solve this problem

Programming Ideas:

    1. (Before you should prepare or check the shell Script Runtime Environment) get the block device file name currently in use

    2. Get the file name of the newly added SCSI disk

    3. Gets the file path of the LVM volume Group name (VG), the volume group name to be extended

    4. Create a partition with Fdisk and format it in LVM using the new add disk

    5. Create a physical volume, pvcreate

    6. Extended Volume group, vgextend

    7. Adjust the logical volume size, lvresize

    8. Determine if the XFS file system

    9. Synchronize the file system to make the expansion effective

    10. Return system disk Usage

Shell code:

#!/bin/bash# usage: automatic expand lv with lvm managed disk#  SETP&NBSP;1:&NBSP;ADD&NBSP;HARD&NBSP;DISK&NBSP;OR&NBSP;STORAGE&NBSP;TO&NBSP;COMPUTING&NBSP;UNIT#SETP&NBSP;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&NBSP;BLOCK&Nbsp;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


Test results:

(1) before adding a disk:

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) After adding the disk and executing the script:

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" />

This shows that the root partition has changed from the original 36GB to 52GB, indicating that the LVM expansion was successful.

--end--

This article is from "Communication, My Favorites" blog, please make sure to keep this source http://dgd2010.blog.51cto.com/1539422/1619692

CentOS6, 7 LVM Logical Volume partitioning automatic expansion shell script programming ideas and examples

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.