#!/bin/bash
set -euo pipefail

path=/growroot
udevadm settle

#Following function takes name as argument e.g. coreos.inst.install_dev and finds its value from
#cmdline (which is passed at boot time)
cmdline=( $(</proc/cmdline) )
karg() {
    local name="$1" value=""
    for arg in "${cmdline[@]}"; do
        if [[ "${arg%%=*}" == "${name}" ]]; then
            value="${arg#*=}"
        fi
    done
    echo "${value}"
}

# Get installation device
installation_device="$(karg coreos.inst.install_dev)"
if [ "${installation_device##*/}" = "${installation_device}" ]; then
    # karg contains no slashes.  Prepend "/dev/" for compatibility.
    installation_device="/dev/${installation_device}"
fi

#Get number of partitions avaiable for the installation_device and choose last 
#partition to grow till available disk size.
device=${installation_device##*/}
partition=$(grep -c "${device}"[0-9] /proc/partitions)
growpart $installation_device $partition
echo "growpart executed on "$installation_device" "$partition" " 

crypt=$(lsblk -ln -o NAME,TYPE | grep crypt | awk '{print $1}')
if [[ -z ${crypt} ]]; then
    exit 1
fi
cryptsetup resize ${crypt}
lvm pvresize /dev/mapper/${crypt}

lvm vgchange -ay rootvg

if [[ ! -e /dev/disk/by-label/root ]]; then
    dm=$(blkid --label root)
    if [[ -z ${dm} ]]; then
        exit 1
    fi
    ln -s ${dm} /dev/disk/by-label/root
fi

mkdir -p ${path}
mount -t xfs /dev/disk/by-label/root ${path}
