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

GodokNam   
   Á¶È¸ 5289   Ãßõ 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 9/77
2015-12   1484951   ¹é¸Þ°¡
2014-05   4948283   Á¤ÀºÁØ1
06-29   5125   »ßµ¹À̽½ÇÄÀÌ
2020-11   5135   ¹«¾Æ
2019-08   5175   µö·¯´×¼­¹ö
2020-04   5186   ±âÈÆ
2019-07   5193   Å«½º
2019-08   5195   µö·¯´×¼­¹ö
2022-03   5200   ´©¸Þ
2021-09   5215   PCMaster
2019-08   5225   µö·¯´×¼­¹ö
2020-12   5225   È­Á¤Å¥»ï
2019-12   5251   PCMaster
2019-07   5265   µö·¯´×¼­¹ö
2021-06   5270   Á¦¿ÂÇÁ·Î
2019-10   5279   µö·¯´×¼­¹ö
2021-10   5279   ÇöÁø
2019-08   5280   µö·¯´×¼­¹ö
2020-05   5289   µö·¯´×¼­¹ö
2020-12   5290   GodokNam
2021-01   5310   ¼úÀÌ
2019-11   5333   µö·¯´×¼­¹ö