2021-02-04 09:45:00 +01:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
2022-01-19 15:50:19 +01:00
|
|
|
# Credits to hassos-expand original script
|
|
|
|
# https://github.com/home-assistant/operating-system/
|
|
|
|
|
2021-02-04 09:45:00 +01:00
|
|
|
DEVICE_ROOTFS="$(findfs UUID=c0932a41-44cf-463b-8152-d43188553ed4)"
|
2022-01-19 15:50:19 +01:00
|
|
|
DEVICE_ROOTFS_NAME="$(basename "${DEVICE_ROOTFS}")"
|
2021-02-04 09:45:00 +01:00
|
|
|
DEVICE="/dev/$(lsblk -no pkname "${DEVICE_ROOTFS}")"
|
2022-01-19 15:50:19 +01:00
|
|
|
PART_NUM="$(cat "/sys/class/block/${DEVICE_ROOTFS_NAME}/partition")"
|
|
|
|
PART_TABLE="$(sfdisk -lqJ "${DEVICE}")"
|
|
|
|
|
|
|
|
if sfdisk --verify "${DEVICE}" 2>&1 | grep "The backup GPT table is not on the end of the device."; then
|
|
|
|
echo "[INFO] Moving GPT backup header to the end"
|
|
|
|
sfdisk --relocate gpt-bak-std "${DEVICE}"
|
|
|
|
|
|
|
|
# Reload partition label to get correct .partitiontable.lastlba
|
|
|
|
PART_TABLE="$(sfdisk -lqJ "${DEVICE}")"
|
|
|
|
fi
|
|
|
|
LAST_USABLE_LBA="$(echo "${PART_TABLE}" | jq -r '.partitiontable.lastlba')"
|
|
|
|
|
|
|
|
echo "[INFO] Last usable logical block ${LAST_USABLE_LBA}"
|
2021-02-04 09:45:00 +01:00
|
|
|
|
2022-01-19 15:50:19 +01:00
|
|
|
JQ_FILTER=".partitiontable.partitions[] | select ( .node == \"${DEVICE_ROOTFS}\" ) | .start + .size"
|
|
|
|
ROOTFS_PARTITION_END="$(echo "${PART_TABLE}" | jq "${JQ_FILTER}")"
|
|
|
|
echo "[INFO] Rootfs partition end block ${ROOTFS_PARTITION_END}"
|
2021-02-04 09:45:00 +01:00
|
|
|
|
2022-01-19 15:50:19 +01:00
|
|
|
UNUSED_BLOCKS=$(( LAST_USABLE_LBA - DATA_PARTITION_END ))
|
|
|
|
if [ "${UNUSED_BLOCKS}" -le "16384" ]; then
|
2021-02-04 09:45:00 +01:00
|
|
|
echo "[INFO] No resize of rootfs partition needed"
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2022-01-19 15:50:19 +01:00
|
|
|
echo "[INFO] Update rootfs partition ${PART_NUM}"
|
|
|
|
echo ", +" | sfdisk --no-reread --no-tell-kernel -N "${PART_NUM}" "${DEVICE}"
|
|
|
|
sfdisk -V "${DEVICE}"
|
|
|
|
partx -u "${DEVICE}"
|
|
|
|
udevadm settle
|
|
|
|
partprobe "${DEVICE}"
|
|
|
|
|
|
|
|
echo "[INFO] Resizing the rootfs partition"
|
|
|
|
resize2fs "${DEVICE_ROOTFS}"
|
|
|
|
echo "[OK]"
|