#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

Cent=`cat /etc/issue | awk '{print $1}' | sed '/^$/d' | grep Kernel`
OtherOs=`cat /etc/issue | awk '{print $1}' | sed '/^$/d'`


checkext4=`df -Th | grep ext4 | awk '{print $2}'`
#checkxfs=`df -Th | grep xfs | awk '{print $2}'``

if [ "$checkext4" = "" ];then
    echo -e "\e[1;31m---Only ext4 system file is allow to expand!!!--- \e[0m"
    exit;
fi


debianAll() {
device=`cat /proc/partitions | awk '{print $4}' | grep -v 'name' | grep -v '[0-9]$' | grep -v '^$'`
sdaNum=`cat /proc/partitions | grep sda | wc -l`
path="/dev/"
check1=`rpm -qa cloud-utils-growpart`
check2=`rpm -qa gdisk`


if [ "$sdaNum" -eq 4 ];then
    sed -i '/none/s/^/&#/g' /etc/fstab
    fdisk /dev/sda << EOF
p
d
5
d
2
p
d
p
n
p
1


wq
EOF
    mkdir -p /opt/resize/ && cd /opt/resize/ && touch resize.sh && chmod 755 resize.sh

cat > resize.sh << EOF
#!/bin/bash
sudo resize2fs /dev/sda1
sleep 2
sudo rm -rf /opt/resize
sudo sed -ie '/resize.sh/d' /etc/rc.local
EOF

    echo '/opt/resize/resize.sh' >> /etc/rc.local && chmod +x /etc/rc.local
    echo -e "\033[32m---All Done---\033[0m";


elif [ "$path""$device" != "" ];then

    if [ "$check1" = "" ];then
        apt install cloud-guest-utils-y
    fi

    if [ "$check2" = "" ];then
        apt install gdisk -y
    fi

    growpart /dev/sda 1
    resize2fs "$path"sda1
    partx -u "$path"sda1
    echo -e "\033[32m---All Done---\033[0m"
fi

}

ubuntu16(){
sdaNum=`cat /proc/partitions | grep sda | wc -l`
device=`cat /proc/partitions | awk '{print $4}' | grep sda1`
check1=`dpkg --list | grep cloud-guest-utils`
check2=`dpkg --list | grep gdisk`
path="/dev/"

if [ "$sdaNum" -eq 4 ]
then
    sed -i '/none/s/^/&#/g' /etc/fstab
    fdisk /dev/sda << EOF
p
d
5
d
2
d
n
p
1


wq
EOF
    mkdir -p /opt/resize/ && cd /opt/resize/ && touch resize.sh && chmod 755 resize.sh

cat > resize.sh << EOF
#!/bin/bash
sudo resize2fs /dev/sda1
sleep 2
sudo rm -rf /opt/resize
sudo sed -ie '/resize.sh/d' /etc/rc.local
EOF

    echo '/opt/resize/resize.sh' >> /etc/rc.local && chmod +x /etc/rc.local
    echo -e "\033[32m---All Done---\033[0m";


elif [ "$device" = "sda1" ];then
    if [ "$check1" = "" ];then
        apt install cloud-guest-utils-y
    fi

    if [ "$check2" = "" ];then
        apt install gdisk -y
    fi

    growpart /dev/sda 1
    resize2fs "$path"sda1
    partx -u "$path"sda1

    echo -e "\033[32m---All Done---\033[0m";

fi
}

ubuntu1820() {
device=`cat /proc/partitions | awk '{print $4}' | grep sda1`
checExpect=`which expect`
path="/dev/"
check1=`dpkg --list | grep cloud-guest-utils`
check2=`dpkg --list | grep gdisk`

if [ "$check1" = "" ]
then
    apt install cloud-guest-utils-y
fi

if [ "$check2" = "" ]
then
    apt install gdisk -y
fi

if [ "$checExpect" = "" ]
then
    apt-get install expect -y > /dev/null 2>&1
fi

expect << EOF
spawn parted -l
expect {
"Fix/Ignore?" {send "Fix\n";exp_continue}
eof
}
EOF

if [ "$device" = "sda1" ]
then
    growpart /dev/sda 1
    resize2fs "$path"sda1
    partx -u "$path"sda1

    echo -e "\033[32m---All Done---\033[0m";

fi
}


centos6() {
device=`cat /proc/partitions | awk '{print $4}' | grep -v 'name' | grep -v '[0-9]$' | grep -v '^$'`
check1=`rpm -qa cloud-utils-growpart`
check2=`rpm -qa gdisk`

if [ "$check1" = "" ];then
    yum install cloud-utils-growpart gdisk -y >> /dev/null 2>&1
fi

if [ "$check2" = "" ];then
    yum install gdisk -y >> /dev/null 2>&1
fi

if [ "$path""$device" != "" ];then

    growpart /dev/sda 1

    echo -e "\033[32m---All Done---\033[0m"

    mkdir -p /opt/resize/ && cd /opt/resize/ && touch resize.sh && chmod 755 resize.sh
    cat > resize.sh << EOF
#!/bin/bash
resize2fs /dev/sda1
sleep 2
rm -rf /opt/resize
sed -ie '/resize.sh/d' /etc/rc.local
EOF

    echo '/opt/resize/resize.sh' >> /etc/rc.local && chmod +x /etc/rc.local

fi

}

centos7() {
device=`cat /proc/partitions | awk '{print $4}' | grep -v 'name' | grep -v '[0-9]$' | grep -v '^$'`
path="/dev/"
check1=`rpm -qa cloud-utils-growpart`
check2=`rpm -qa gdisk`

if [ "$check1" = "" ];then
    yum install cloud-utils-growpart gdisk -y
fi

if [ "$check2" = "" ];then
    yum install gdisk -y
fi

if [ "$path""$device" != "" ];then

    growpart /dev/sda 1
    resize2fs "$path"sda1
    partx -u "$path"sda1

    echo -e "\033[32m---All Done---\033[0m"

fi
}

centos8() {
device=`cat /proc/partitions | awk '{print $4}' | grep -v 'name' | grep -v '[0-9]$' | grep -v '^$'`
path="/dev/"
check1=`rpm -qa cloud-utils-growpart`
check2=`rpm -qa gdisk`

if [ "$check1" = "" ];then
    dnf install cloud-utils-growpart gdisk -y
fi

if [ "$check2" = "" ];then
    dnf install cloud-utils-growpart gdisk -y
fi

if [ "$path""$device" != "" ];then

    growpart /dev/sda 1
    resize2fs "$path"sda1
    partx -u "$path"sda1

    echo -e "\033[32m---All Done---\033[0m"

fi
}


if [ "$Cent" = "Kernel" ];then
    checkOsVer=`rpm --eval '%{centos_ver}'`
    case $checkOsVer in
    6)
    centos6
    exit;;

    7)
    centos7
    exit;;

    8)
    centos8
    exit;;
    esac

elif [ "$OtherOs" = "Debian" ];then
    debianAll


elif [ "$OtherOs" = "Ubuntu" ];then
    checkVerUbun=`cat /etc/os-release | grep VERSION_ID | awk '{split($0,R,"=");print R[2]}' | sed 's/"//g'`
    case $checkVerUbun in
    16.04)
    ubuntu16
    exit;;

    18.04)
    ubuntu1820
    exit;;

    20.04)
    ubuntu1820
    exit;;
    esac
else
    echo "No os found."

fi
