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

GodokNam   
   Á¶È¸ 5296   Ãßõ 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 13/77
2014-05   4955031   Á¤ÀºÁØ1
2015-12   1491531   ¹é¸Þ°¡
2021-03   5813   »ßµ¹À̽½ÇÄÀÌ
2021-02   6724   »õÇϾᱸ¸§
2021-02   5628   dateno1
2021-02   8900   ±îÄ¡»ê°³²ÜÅÊ
2021-02   7918   ½ºÄµl¹ÎÇö±â
2021-01   7259   ½ºÄµl¹ÎÇö±â
2021-01   6186   ½ºÄµl¹ÎÇö±â
2021-01   6154   ½ºÄµl¹ÎÇö±â
2021-01   9214   ÇãÀα¸¸¶Æ¾
2021-01   5589   dateno1
2021-01   4901   »ç¶ûÇ϶ó
2021-01   3491   »ç¶ûÇ϶ó
2021-01   3457   »ç¶ûÇ϶ó
2021-01   5321   ¼úÀÌ
2021-01   6052   ±ô¹Ú±ô¹Ú°¡
2021-01   4981   È­Á¤Å¥»ï
2020-12   4723   Rainwalk
2020-12   5232   È­Á¤Å¥»ï
2020-12   4974   ¾î²Ù±â
2020-12   5297   GodokNam