Skip to content

Commit

Permalink
Fix partition resize for MBR (home-assistant#1149)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
agners authored Dec 31, 2020
1 parent 581de22 commit 0188f24
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions buildroot-external/rootfs-overlay/usr/libexec/hassos-expand
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -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...
Expand Down

0 comments on commit 0188f24

Please sign in to comment.