CSchantz Wiki

Resize Disk Script

Resize Disk Bat Script

nano resize_disk.sh
#!/bin/bash
# VMware Disk resize Script for AlmaLinux, CentOS and Ubuntu

GREEN="\e[32m"
RED="\e[31m"
YELLOW="\e[33m"
ENDCOLOR="\e[0m"

# Determine if redhat or debian based
if [ -f /etc/centos-release ] || [ -f /etc/redhat-release ]; then
    OS=redhat
elif [ -f /etc/lsb-release ]; then
    OS=debian
else
    echo -e "${RED}ERROR! Make Sure Distro is RPM or APT Based${ENDCOLOR}"
    exit 1
fi

echo -e "${YELLOW}This tool will only work for /dev/sda. If you need to resize another drive please see the waypoint page${ENDCOLOR}\n"

# Rescan /dev/sda
echo 1>/sys/class/block/sda/device/rescan

# Resize Drive and FS

if [[ $(lvdisplay 2>/dev/null| head -1) == *'--- Logical volume ---'* ]]; then
    # using lvm (no current templates using this. Kept for legacy/future support)
    # ex: sda3
    drivepartition=$(lsblk -oMOUNTPOINT,PKNAME -P | grep 'MOUNTPOINT="/"' | cut -d '"' -f 4)
    # confirm
    echo -e "\n${YELLOW}Drive Layout${ENDCOLOR}"
    lsblk | pr -to5
    echo -e "${GREEN}Resize $drivepartition?${ENDCOLOR}"
    select confirm_part in "Yes" "No"; do
        case $confirm_part in
            Yes )
                # ex: 3
                partition=${drivepartition: -1}
                # ex: sda
                drive=${drivepartition: -1}
                # ex: /dev/mapper/centos-root
                dmpath=$(df -hT | grep /$ | cut -d ' ' -f 1)
                parted /dev/"$drive" resizepart "$partition" -- -1
                pvresize /dev/"$drivepartition"
                lvresize -r -l +95%FREE "$dmpath"
                break
                ;;
            No ) echo -e "${RED}Resize Partition manually${ENDCOLOR}";break;;
        esac
    done
elif [[ ! $(lvdisplay 2>/dev/null| head -1) == *'--- Logical volume ---'* ]]; then
    # lvdisplay not installed (centos and alma)
    # ex: sda
    drive=$(lsblk -oMOUNTPOINT,PKNAME -P | grep 'MOUNTPOINT="/"' | cut -d '"' -f 4)
    # ex: 3
    echo -e "\n${YELLOW}Running parted to get partition information${ENDCOLOR}"
    partition=$(printf "%s" "$(echo I| parted /dev/"$drive" -l partition table)" | tail -1 | awk '{$1=$1};1' | cut -d ' ' -f 1)
    echo -e "${YELLOW}Ignoring the error from parted output above${ENDCOLOR}"
    # confirm
    echo -e "\n${YELLOW}Drive Layout${ENDCOLOR}"
    lsblk | pr -to5
    echo -e "${GREEN}Resize /dev/$drive$partition?${ENDCOLOR}"
    select confirm_part in "Yes" "No"; do
        case $confirm_part in
            Yes )
                # The command inside the if statement runs
                if [[ ! $(parted -s /dev/"$drive" resizepart "$partition" -- -1) ]]; then
                    # if the above command fails, the below stuff runs
                    # current templates require this exception. Legacy templates do not
                    if [ $OS == "redhat" ]; then
                        yum install gdisk -y
                    elif [ $OS == "debian" ]; then
                        apt install -y gdisk
                    else
                        echo -e "${RED}ERROR! Make Sure Distro is RPM or APT Based${ENDCOLOR}"
                        exit 1
                    fi
                    sgdisk -e /dev/"$drive"
                    sgdisk -d "$partition" /dev/"$drive"
                    sgdisk -N "$partition" /dev/"$drive"
                    partprobe
                fi
                resize2fs /dev/"$drive$partition"
                break
                ;;
            No ) echo -e "${RED}Resize Partition manually${ENDCOLOR}";break;;
        esac
    done
else
    # not using lvm (ubuntu)
    # ex: sda
    drive=$(lsblk -oMOUNTPOINT,PKNAME -P | grep 'MOUNTPOINT="/"' | cut -d '"' -f 4)
    # ex: 3
    partition=$(printf "%s" "$(parted /dev/"$drive" -l partition table)" | tail -1 | awk '{$1=$1};1' | cut -d ' ' -f 1)
    # confirm
    echo -e "\n${YELLOW}Drive Layout${ENDCOLOR}"
    lsblk | pr -to5
    echo -e "${GREEN}Resize /dev/$drive$partition?${ENDCOLOR}"
    select confirm_part in "Yes" "No"; do
        case $confirm_part in
            Yes )
                # The command inside the if statement runs
                if [[ ! $(parted -s /dev/"$drive" resizepart "$partition" -- -1) ]]; then
                    # if the above command fails, the below stuff runs
                    # current templates require this exception. Legacy templates do not
                    if [ $OS == "redhat" ]; then
                        yum install gdisk -y
                    elif [ $OS == "debian" ]; then
                        apt install -y gdisk
                    else
                        echo -e "${RED}ERROR! Make Sure Distro is RPM or APT Based${ENDCOLOR}"
                        exit 1
                    fi
                    sgdisk -e /dev/"$drive"
                    sgdisk -d "$partition" /dev/"$drive"
                    sgdisk -N "$partition" /dev/"$drive"
                    partprobe
                fi
                resize2fs /dev/"$drive$partition"
                break
                ;;
            No ) echo -e "${RED}Resize Partition manually${ENDCOLOR}";break;;
        esac
    done
fi