#!/bin/bash

### BEGIN INIT INFO
# Provides:          hobot
# Required-Start:    $all
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:
# Short-Description: Resize Root Filesystem
### END INIT INFO

do_resize_rootfs()
{
    local root_src=$(findmnt -n -o SOURCE / | sed 's~\[.*\]~~')

    if [ ! -f /etc/.do_resizefs_rootfs ];then
        echo "Start resize ${root_src}"
        resize2fs ${root_src}
        touch  /etc/.do_resizefs_rootfs
    fi
}

do_expand_partiton()
{
	if [ ! -f /etc/.do_expand_partiton ];then
	    # local rootsource=$(findmnt -n -o SOURCE / | sed 's~\[.*\]~~')
	    # local rootdevice=$(echo $rootsource | sed -e "s/^\/dev\///" | sed "s/p.*//")
	    # local rootdevicepath="/dev/$rootdevice"
	    # local partstart=$(parted $rootdevicepath unit s print -sm | tail -1 | cut -d: -f2 | sed 's/s//')
	    # (echo d; echo n; echo p; echo ; echo ; echo ; echo w;) | fdisk $rootdevicepath
        local ROOT_PART="$(findmnt / -o source -n)"
        local ROOT_DEV="/dev/$(lsblk -no pkname "$ROOT_PART")"

        local PART_NUM="$(echo "$ROOT_PART" | grep -o "[[:digit:]]*$")"

        local LAST_PART_NUM=$(parted "$ROOT_DEV" -ms unit s p | tail -n 1 | cut -f 1 -d:)
        if [ "$LAST_PART_NUM" -ne "$PART_NUM" ]; then
            echo "$ROOT_PART is not the last partition. Don't know how to expand"
            return 0
        fi

        # Get the starting offset of the root partition
        local PART_START=$(parted "$ROOT_DEV" -ms unit s p | grep "^${PART_NUM}" | cut -f 2 -d: | sed 's/[^0-9]//g')
        [ "$PART_START" ] || return 1

        # If the root partition already fills the disk, there is nothing to do:
        # mark it done so we do not retry expansion on every boot.
        local DISK_END=$(parted "$ROOT_DEV" -ms unit s p | grep "^/dev" | head -1 | cut -f 2 -d: | sed 's/[^0-9]//g')
        local PART_END=$(parted "$ROOT_DEV" -ms unit s p | grep "^${PART_NUM}" | cut -f 3 -d: | sed 's/[^0-9]//g')
        if [ -n "$PART_END" ] && [ -n "$DISK_END" ] && [ "$PART_END" -ge "$((DISK_END - 1))" ]; then
            touch /etc/.do_expand_partiton
            return 0
        fi

        # Delete the root partition and recreate it ending at the disk end,
        # keeping the same start sector so the filesystem data is preserved.
        # fdisk prints a warning that it cannot reload the partition table on a
        # mounted root fs, but it still writes the new table to disk.
        fdisk "$ROOT_DEV" <<EOF
p
d
$PART_NUM
n
p
$PART_NUM
$PART_START

p
w
EOF
        # Reload the partition table so the kernel sees the new partition size.
        partprobe "$ROOT_DEV"
        parted "${ROOT_DEV}" set ${PART_NUM} boot on
        # Only mark expansion done if the partition actually grew; otherwise
        # leave the marker unset so the next boot retries.
        local PART_END_NEW=$(parted "$ROOT_DEV" -ms unit s p | grep "^${PART_NUM}" | cut -f 3 -d: | sed 's/[^0-9]//g')
        if [ -n "$PART_END_NEW" ] && [ "$PART_END_NEW" -gt "$PART_END" ]; then
            touch /etc/.do_expand_partiton
        else
            echo "Partition expansion failed (end sector ${PART_END} -> ${PART_END_NEW}), will retry next boot"
            return 1
        fi
    fi
}

do_resize()
{
    local rootsource=$(findmnt -n -o SOURCE / | sed 's~\[.*\]~~')
    local rootpartnum=${rootsource#*p}
    if [ x${rootpartnum} == x"1" ] || [ x${rootpartnum} == x"2" ];then
        do_expand_partiton
    fi
    do_resize_rootfs
}

case "$1" in
	start)
        do_resize
        exit 0
    ;;
   	*)
		echo "Usage: $0 start"
		exit 0
	;;
esac

