#!/bin/bash
# dib-lint: disable=dibdebugtrace

set -exu
set -o pipefail

root_dev=$(df -P / | tail -n 1 | awk '/.*/ { print $1 }')

disk=$(find /sys/dev/block -name $(udevadm info --query=all --name $root_dev | grep ID_PART_ENTRY_DISK | cut -d= -f2))
disk="/dev/$(source ${disk}/uevent; echo $DEVNAME)"

part_no=$(udevadm info --query=all --name $root_dev | grep ID_PART_ENTRY_NUMBER | cut -d= -f2)

set +e
growpart $disk $part_no
# Error code 1 means no change
if [ "$?" -le 1 ]; then
    # always return true because this might not work if were are non ext4
    resize2fs $root_dev || true
fi
