Files
operating-system/buildroot-external/rootfs-overlay/usr/libexec/hassos-expand
Stefan Agner 7959113c97 Use systemd-growfs (#1133)
* Use systemd-growfs instead of resize2fs (#1106)

Since systemd 236 systemd has a built-in file system growing mechanism.
The mechanism relies on the kernels online file system resize
capabilities instead of the external resize2fs utility. Online resizing
is supposedly much faster since the kernel takes care of things.

This also makes sure that external file systems get resized which
previously have not been taken care of.

* Drop HA OS specific file system resizing

Since we have systemd-growfs in place now we can drop our file system
resizing code.

* Make sure /dev/disk/by-label/hassos-data is present after resizing

Note: systemd will retry mnt-data.mount later, so at least in theory
this shouldn't really matter. However, the journal has a lot of churn
due to that reordering.
2020-12-28 23:46:55 +01:00

69 lines
2.5 KiB
Bash
Executable File

#!/bin/sh
# ==============================================================================
# HassOS partition expander
# ==============================================================================
set -e
DEVICE_CHILD="$(findfs LABEL="hassos-data")"
DEVICE_CHILD_NAME="$(basename "${DEVICE_CHILD}")"
DEVICE_ROOT_NAME="$(lsblk -no pkname "${DEVICE_CHILD}")"
DEVICE_ROOT="/dev/${DEVICE_ROOT_NAME}"
PART_NUM="$(cat "/sys/class/block/${DEVICE_CHILD_NAME}/partition")"
# Get partition label type
PART_TABLE="$(sfdisk -lqJ "${DEVICE_ROOT}")"
PART_LABEL="$(echo "${PART_TABLE}" | jq -r '.partitiontable.label')"
if [ "${PART_LABEL}" = "gpt" ]; then
echo "[INFO] Detected GPT partition label"
# We cannot use .partitiontable.lastlba from the json output as we might
# still have the backup GPT not at the end of the disk. Calculate last
# usable LBA using disk size
LAST_USABLE_LBA=$(( $(cat "/sys/class/block/${DEVICE_ROOT_NAME}/size") - 34 ))
else
echo "[INFO] Detected MBR partition label"
LAST_USABLE_LBA=$(cat "/sys/class/block/${DEVICE_ROOT_NAME}/size")
fi
# Calculate end of data partition
JQ_FILTER=".partitiontable.partitions[] | select ( .node == \"${DEVICE_CHILD}\" ) | .start + .size"
DATA_PARTITION_END="$(echo "${PART_TABLE}" | jq "${JQ_FILTER}")"
# Need resize? Ignore everything less than 8MB since that could be partition
# alignment rounding...
UNUSED_BLOCKS=$(( LAST_USABLE_LBA - DATA_PARTITION_END ))
if [ "${UNUSED_BLOCKS}" -le "16384" ]; then
echo "[INFO] No resize of data partition needed"
exit 0
fi
echo "[INFO] Update hassos-data partition ${PART_NUM}"
if [ "${PART_LABEL}" = "gpt" ]; then
sgdisk -e "${DEVICE_ROOT}"
sgdisk -d "${PART_NUM}" \
-n "${PART_NUM}:0:0" \
-c "${PART_NUM}:hassos-data" \
-t "${PART_NUM}:0FC63DAF-8483-4772-8E79-3D69D8477DE4" \
-u "${PART_NUM}:a52a4597-fa3a-4851-aefd-2fbe9f849079" \
"${DEVICE_ROOT}"
sgdisk -v "${DEVICE_ROOT}"
else
echo ", +" | sfdisk -N "${PART_NUM}" "${DEVICE_ROOT}" --force
sfdisk -V "${DEVICE_ROOT}"
fi
# Reload partition table
partx -u "${DEVICE_ROOT}"
udevadm settle
# Make sure /dev/disk/by-label/hassos-data is actually present before exiting.
# This avoids a race condition with mnt-data.mount which might fail to bind
# otherwise:
# Bound to unit dev-disk-by\x2dlabel-hassos\x2ddata.device, but unit isn't active.
if ! systemctl start "dev-disk-by\\x2dlabel-hassos\\x2ddata.device"; then
echo "[ERROR] Data partition not found!"
exit 1
fi
echo "[INFO] Finished hassos-data partition resizing"