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

GodokNam   
   Á¶È¸ 5311   Ãßõ 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
2014-05   4968740   Á¤ÀºÁØ1
2015-12   1505064   ¹é¸Þ°¡
2013-10   26091   ȸ¿øK
2015-07   26029   ¼ÛÀÌ
2010-05   25993   ÀÌÁØÈ£1
2014-07   25946   ¹äÇѳ¢
2018-01   25785   catstyle
2015-08   25615   Àο¬
2023-06   25559   µ¿·Â
2013-02   25504   ¯¯¸Ç
2017-01   25478   õ¿Üõoo³ë¡¦
2013-10   25440   ȸ¿øK
2013-07   25253   ¸Ö¸°
2009-11   25211   ±èÀå¿ì
2009-05   25210   ÇÑÀÏÀÎ
2013-04   25201   °û¼øÇö
2013-02   25083   °û¼øÇö
2013-02   25083   HEUo±è¿ë¹Î
2013-05   25043   QS¿ÕÅëÅ°¼Õ¡¦
2013-01   25021   ¹Î¿í´Ô
2009-05   25020   TSHA
2016-09   24999   ¼­¿ïl°­ÇѸð