#! /bin/bash
# If you provide no parameters, then we will do the pre-reboot part of the
# disk expansion.
# After reboot, you can run with "continue" as a parameter to just do the
# resize2fs.
devicenode=$(df / | awk '/dev/ {print $1}')
devicebase=$(lsblk -no pkname $devicenode)
if [ "$1" == "continue" ] ; then
    grep ${devicebase}5 /proc/swaps > /dev/null
    if [ $? ] ; then
        #Need to create swap with same UUID to avoid confusing fstab
        SWAPUUID=$(awk -F "[ =]" '/swap/ {if (substr($0,1,1) != "#") print $2}' < /etc/fstab)
        echo -e \\n\*\*\* Recreating swap file on /dev/${devicebase}5 with UUID=$SWAPUUID
        mkswap -U $SWAPUUID /dev/${devicebase}5
        echo -e \\n\*\*\* Enabling swap
        swapon -a
    else
        echo You already have a swap configured, not recreating it.
    fi
    PARTSIZE=$(sfdisk -l /dev/${devicebase} 2>/dev/null | awk '/\/dev\/'${devicebase}'1/ {print $6}')
    echo -e \\n\*\*\* /dev/${devicebase}1 partition is now $PARTSIZE
    echo \*\*\* Expanding filesystem on /dev/${devicebase}1
    resize2fs /dev/${devicebase}1
    FSSIZE=$(df -h /dev/${devicebase}1 | awk '/\/dev\/'${devicebase}'1/ {print $2}')
    echo -e \\n\*\*\* /dev/${devicebase}1 filesystem is now $FSSIZE
    exit 0
fi
# How much free space do we have?  If it's less than 1GB, I won't even try
VALID=$(sfdisk -lq 2>/dev/null | awk '/\/dev\/'${devicebase}'/ { drives = drives substr($1,length($1),1) ; if ($2 == "*") {drives = drives $7} else {drives = drives $6} } END { print drives }')
if [ "$VALID" != "18325582" ] ; then
    echo Unrecognized partition table.  We expect /dev/${devicebase}1 \(type 83\), /dev/${devicebase}2 \(type 5\) and /dev/${devicebase}5 \(type 82\).  Please contact technical support for assistance.
    exit -1
fi
echo \*\*\* Available unused space on disk
sfdisk -F /dev/${devicebase} 2>/dev/null
SPACE=$(sfdisk -F /dev/${devicebase} 2>/dev/null | awk '/Unpartitioned/ {print $6}')
if [ $SPACE -lt 1000000000 ] ; then
    echo -e \\nThere is only $SPACE bytes of free space.  Please reboot to detect the disk expansion.
    exit -1
fi
#Print current partition table
echo -e \\n\*\*\* Existing partition table
sfdisk -l /dev/${devicebase} 2>/dev/null
INCREASE=$(sfdisk -l /dev/${devicebase} 2>/dev/null | awk '/\/dev\/'${devicebase}':/ {diskend = $7} /\/dev\/'${devicebase}'2/ {swapsize = $4; swapstart = $2} END {print int((diskend - swapstart - swapsize)/4096)*4096}')
PARTSIZE=$(sfdisk -l /dev/${devicebase} 2>/dev/null | awk '/\/dev\/'${devicebase}'1/ {print $6}')
FSSIZE=$(df -h /dev/${devicebase}1 | awk '/\/dev\/'${devicebase}'1/ {print $2}')
echo -e \\n\*\*\* Existing /dev/${devicebase}1 partition is $PARTSIZE, filesystem is $FSSIZE
echo \*\*\* This script will expand the partition and filesystem by $(($SPACE/1073741824))GB
read -p "Are you sure you want to proceed? (y/N)" answer
if [[ "$answer" != [Yy] ]] ; then
    echo Aborting.
    exit -1
fi
echo \*\*\* Moving swap partition at /dev/${devicebase}2 out by $INCREASE sectors.
swapoff -a
sfdisk -q --delete /dev/${devicebase} 5 2>/dev/null
# Performing the move on /dev/${devicebase}2
echo +$INCREASE | sfdisk /dev/${devicebase} -q -N 2 --no-reread 2>/dev/null
echo -e \\n\*\*\* Recreating swap partition /dev/${devicebase}5
echo ",,82" | sfdisk -q -N 5 /dev/${devicebase} --no-reread 2>/dev/null
echo -e \\n\*\*\* Expanding partition /dev/${devicebase}1 into free space
echo ',+' | sfdisk -q /dev/${devicebase} -N 1 --no-reread 2>/dev/null
PARTSIZE=$(sfdisk -l /dev/${devicebase} 2>/dev/null | awk '/\/dev\/'${devicebase}'1/ {print $6}')
echo -e \\n\*\*\* /dev/${devicebase}1 partition is now $PARTSIZE
echo \*\*\* Final partition table:
sfdisk -l /dev/${devicebase} 2>/dev/null
echo -e \\n\*\*\* Please reboot and run the following command:
echo $0 continue