From 0188f24a0db9bbca4a2e4e5b25f1cf6813f24ede Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Thu, 31 Dec 2020 14:58:21 +0100 Subject: [PATCH] Fix partition resize for MBR (#1149) Partition handling for disks with 4k sectors broke partition resizing when using MBR disk label. It seems that sfdisk doesn't calculate the last LBA for diks with MBR label. Calculate the last usable LBA ourselfs in the MBR case. --- .../rootfs-overlay/usr/libexec/hassos-expand | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/buildroot-external/rootfs-overlay/usr/libexec/hassos-expand b/buildroot-external/rootfs-overlay/usr/libexec/hassos-expand index 77db8a771..f3fef3ad2 100755 --- a/buildroot-external/rootfs-overlay/usr/libexec/hassos-expand +++ b/buildroot-external/rootfs-overlay/usr/libexec/hassos-expand @@ -13,6 +13,7 @@ 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')" +echo "[INFO] Checking if expanding data partition on ${DEVICE_CHILD} is necessary" if [ "${PART_LABEL}" = "gpt" ]; then echo "[INFO] Detected GPT partition label on ${DEVICE_ROOT}" @@ -24,15 +25,21 @@ if [ "${PART_LABEL}" = "gpt" ]; then # Reload partition label to get correct .partitiontable.lastlba PART_TABLE="$(sfdisk -lqJ "${DEVICE_ROOT}")" fi + LAST_USABLE_LBA="$(echo "${PART_TABLE}" | jq -r '.partitiontable.lastlba')" else echo "[INFO] Detected MBR partition label on ${DEVICE_ROOT}" -fi -LAST_USABLE_LBA="$(echo "${PART_TABLE}" | jq -r '.partitiontable.lastlba')" + # For MBR, we have to calculate the last usable sector by ourselfs + DEVICE_SIZE=$(blockdev --getsize64 "${DEVICE_ROOT}") + SECTOR_SIZE=$(echo "${PART_TABLE}" | jq -r '.partitiontable.sectorsize') + LAST_USABLE_LBA="$((DEVICE_SIZE / SECTOR_SIZE))" +fi +echo "[INFO] Last usable logical block ${LAST_USABLE_LBA}" # Calculate end of data partition JQ_FILTER=".partitiontable.partitions[] | select ( .node == \"${DEVICE_CHILD}\" ) | .start + .size" DATA_PARTITION_END="$(echo "${PART_TABLE}" | jq "${JQ_FILTER}")" +echo "[INFO] Data partition end block ${DATA_PARTITION_END}" # Need resize? Ignore free space if its less than 8MB/64MB (4k sectors) since # that could be partition alignment rounding...