#!/bin/bash
# Grows the root partition and filesystem to fill the disk.
# Runs once on first boot, then disables itself.

ROOT_DEV=$(findmnt -no SOURCE /) || { echo "Cannot find root device"; exit 1; }
DISK=$(lsblk -ndo PKNAME "$ROOT_DEV")
PART_NUM=$(cat /sys/class/block/$(basename "$ROOT_DEV")/partition 2>/dev/null)

if [ -z "$DISK" ] || [ -z "$PART_NUM" ]; then
    echo "Cannot determine disk layout (DISK=$DISK PART_NUM=$PART_NUM)"
    exit 1
fi

growpart "/dev/$DISK" "$PART_NUM" || true
partprobe "/dev/$DISK"
resize2fs "$ROOT_DEV"

systemctl disable grow-rootfs.service
