LINUX lvm hdd È®Àå ½ºÅ©¸³Æ®

GodokNam   
   Á¶È¸ 5317   Ãßõ 0    

hdd_extend.sh (4.3K), Down : 4, 2020-12

인터넷에서 돌아 다니는 확장 스크립트 짜집기 하였습니다.


CentOS 7.x 버전에서 테스트 되었으며

/dev/sda 만 있는 경우 테스트 되었습니다.

/dev/sdb 까지 있는 경우는 테스트 안됨.


감사합니다.


#-----------------------------------------------------------------------------------------------------------------------------

#!/bin/bash

##################################################################################

# Get OS Type

##################################################################################

OS_ID=`cat /etc/os-release | grep -E "^ID=" | sed -e 's/ID=//g' | sed -e 's/"//g'`

OS_VER=`cat /etc/os-release | grep -E "^VERSION_ID=" | sed -e 's/VERSION_ID=//g' | sed -e 's/"//g'`

OS_TYPE=$OS_ID"-"$OS_VER

echo "OS_TYPE = $OS_TYPE"


function resize_disk_2 {

    local DISK=$1

    local PV_NAME_CNT=`pvdisplay | grep -A 1 $DISK | grep "PV Name" | awk '{print $3}' | wc -l`

    local PT_NAME=`pvdisplay | grep -A 1 $DISK | grep "PV Name" | awk '{print $3}' | sed -n "$PV_NAME_CNT"p`

    local PT_NUM=`echo $PT_NAME | awk '{print $1}' | rev | cut -c -1`

    PT_TYPE=`mount | grep ^/dev | awk '{if($3 == "/") print $5}'`

    FILE_SYSTEM=`mount | grep ^/dev | awk '{if($3 == "/") print $1}'`

    PARTED_SUPPORT_RESIZEPART=`parted --help | grep "resizepart" | awk '{print $1}'`

    echo "========== resize_disk_2 =============="

    echo "DISK[$DISK], PV_NAME_CNT[$PV_NAME_CNT], PT_NAME[$PT_NAME], PT_NUM[$PT_NUM], PT_TYPE[$PT_TYPE], FILE_SYSTEM[$FILE_SYSTEM]"

    if [ "$PARTED_SUPPORT_RESIZEPART" = "resizepart" ]; then

        echo "[parted] program supports resizepart."

    else

        echo "[parted] program does not support resizepart. so, update processing parted program..."

        case $OS_TYPE in

            "ubuntu-18.04") 

                apt-get -y install --only-upgrade parted

                ;;

            "centos-7") 

                yum -y update parted

                ;;

            *)

                echo "Check OS."

                #exit 1

                ;;

        esac


        PARTED_SUPPORT_RESIZEPART=`parted --help | grep "resizepart" | awk '{print $1}'`

    fi


    #exit


    if [ "$PARTED_SUPPORT_RESIZEPART" = "resizepart" ]; then

        echo "[Before] [df -Th] --------------------------------------"

        df -Th

        parted $DISK --script resizepart $PT_NUM 100%

        pvresize $PT_NAME

        lvextend -l +100%FREE $FILE_SYSTEM


        if [ $PT_TYPE = "ext4" ]; then

            resize2fs $FILE_SYSTEM

        elif [ $PT_TYPE = "xfs" ]; then

            xfs_growfs $FILE_SYSTEM

        fi

        echo "[After] [df -Th] ---------------------------------------"

        df -Th

    else

        echo "-------------------------------------------------------"

        echo "Please, update parted program."

        echo "-------------------------------------------------------"


        # Because you can create up to 4 partitions, this is not recommended.

        PT_NUM=$(($PT_NUM+1))

        START=`parted -s $DISK print free | grep Free | grep GB | awk '{print $1}'`

        END=`parted -s $DISK print free | grep Free | grep GB | awk '{print $2}'`

        echo "START[$START], END[$END], PT_NUM[$PT_NUM]"

        parted $DISK -s mkpart primary $START $END

        #parted /dev/sda -s set $PT_NUM lvm

        echo "fdisk -l $DISK"

        fdisk -l $DISK

        pvcreate $DISK$PT_NUM

        

        # if VG_NAME is multiple ????

        VG_NAME=`vgdisplay | grep "VG Name" | awk '{print $3}'`

        vgextend $VG_NAME $DISK$PT_NUM

        lvextend -l +100%FREE $FILE_SYSTEM

        

        if [ $PT_TYPE = "ext4" ]; then

            resize2fs $FILE_SYSTEM

        elif [ $PT_TYPE = "xfs" ]; then

            xfs_growfs $FILE_SYSTEM

        fi

        echo "fdisk -l $DISK"

        fdisk -l $DISK

    fi

}


FDISK=""

for DISK in `fdisk -l 2>&1 | grep "Disk" | grep '/dev/sd' | awk '{print $2}' | sed -e 's/://g'`; do

    DISK_LABEL_TYPE=`fdisk -l $DISK | grep "label type" | awk '{print $NF}'`

    if [ $DISK_LABEL_TYPE = "dos" ]; then

        FDISK_SIZE=`fdisk -l $DISK | grep "Disk" | grep $DISK | awk '{print $3}'`

        echo "FDISK_SIZE : $FDISK_SIZE"

        PV_NAME_CNT=`pvdisplay | grep -A 1 $DISK | grep "PV Name" | awk '{print $3}' | wc -l`

        PT_NAME=`pvdisplay | grep -A 1 $DISK | grep "PV Name" | awk '{print $3}' | sed -n "$PV_NAME_CNT"p`

        PT_SIZE=`fdisk -l $PT_NAME | grep "Disk" | grep $PT_NAME | awk '{print $3}'`

        PT_SIZE_IS_MB=`fdisk -l $PT_NAME | grep "Disk" | grep $PT_NAME | awk '{print $4}' | cut -d"," -f1 | awk '{if($1 = "MB") print "1"; else print "0";}'`

        echo "PT_NAME[$PT_NAME], PT_SIZE[$PT_SIZE]"

        SIZE_COMP_RES=0

        if [ $PT_SIZE_IS_MB = "1" ]; then

            SIZE_COMP_RES=`echo "$FDISK_SIZE $PT_SIZE" | awk '{if($1 > $2/1024) print "1"; else print "0";}'`

        else

            SIZE_COMP_RES=`echo "$FDISK_SIZE $PT_SIZE" | awk '{if($1 > $2) print "1"; else print "0";}'`

        fi

        if [ $SIZE_COMP_RES != 0 ]; then

            START=`parted -s $DISK print free | grep Free | grep GB | awk '{print $1}'`

            END=`parted -s $DISK print free | grep Free | grep GB | awk '{print $2}'`

            if [ "$START" != "" -a "$END" != "" ]; then

                resize_disk_2 $DISK

            else

                echo "df -Th"

                df -Th

            fi

        fi

    fi

done

ªÀº±Û Àϼö·Ï ½ÅÁßÇÏ°Ô.


PDS
Á¦¸ñPage 14/77
2015-12   1510276   ¹é¸Þ°¡
2014-05   4974002   Á¤ÀºÁØ1
2015-08   21263   Villanus
2015-08   17522   ±è½Â±Ç
2015-12   13911   º¹½Ç°­¾ÆÁö
2015-12   10591   ¼Ò¸Á»ç¶û71
2015-12   15186   ¼Ò¸Á»ç¶û71
2015-12   16688   ¼Ò¸Á»ç¶û71
2015-12   17661   ¼Ò¸Á»ç¶û71
2015-12   17785   ¼Ò¸Á»ç¶û71
2015-12   20575   ¼Ò¸Á»ç¶û71
2016-01   13634   ¹Ú¹®Çü
2016-01   18400   DoubleSH
2016-01   13903   ÆĶõ²Þ
2016-01   14785   ¾çÇü¼®
2016-01   16293   ¾çÇü¼®
2016-01   17227   ¾çÇü¼®
2016-01   17092   Coke
2016-01   16116   Çູ´ÜÁö
2016-02   15453   À׿©°¡vain
2016-02   15790   õ¸¶°Ç»§
2016-02   18176   ¸¸¼®±º