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

GodokNam   
   Á¶È¸ 5327   Ãßõ 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   1527119   ¹é¸Þ°¡
2014-05   4991133   Á¤ÀºÁØ1
2020-05   6272   µö·¯´×¼­¹ö
2019-09   6276   µö·¯´×¼­¹ö
2019-10   6301   µö·¯´×¼­¹ö
2019-12   6302   µö·¯´×¼­¹ö
2019-10   6319   µö·¯´×¼­¹ö
2020-11   6324   ¸ð¾îÄð
2020-05   6351   Á¦¿ÂÇÁ·Î
2020-09   6364   ±¤ÁÖµ¿Å°È£Å×
2020-12   6399   ½ºÄµl¹ÎÇö±â
2020-08   6414   À庸¹è
2022-04   6434   ÇöÁø
2020-03   6441   È­Á¤Å¥»ï
2020-04   6457   PCMaster
2020-05   6459   µö·¯´×¼­¹ö
2019-08   6461   µö·¯´×¼­¹ö
2020-05   6475   µö·¯´×¼­¹ö
2019-07   6483   À̼±È£
2020-06   6516   µö·¯´×¼­¹ö
2018-12   6531   ¿¥ºê¸®¿À
2020-11   6534   È­Á¤Å¥»ï