From fbc553dc0ea66a141dc05a81e39c4bf64cc98ea2 Mon Sep 17 00:00:00 2001 From: Hangfan Li Date: Mon, 15 Dec 2025 18:58:14 +0800 Subject: [PATCH] support custom disk alignment --- tools/image_layout_helper.sh | 5 ++++ tools/partition_table_helper/guid.sh | 45 ++++++++++++++++++---------- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/tools/image_layout_helper.sh b/tools/image_layout_helper.sh index 6afc73e..9d5bc32 100644 --- a/tools/image_layout_helper.sh +++ b/tools/image_layout_helper.sh @@ -7,6 +7,11 @@ parse_image_layout_impl_output_format_partitioned_disk() { exit 1 fi echo "PARTITION_TABLE_TYPE=${PARTITION_TABLE_TYPE}" + + if [ -n "${DISK_PARTITION_ALIGNMENT:-}" ]; then + echo "DISK_PARTITION_ALIGNMENT=${DISK_PARTITION_ALIGNMENT}" + fi + if ! is_function "parse_image_layout_impl_partitioned_disk_table_${PARTITION_TABLE_TYPE}"; then loge "PARTITION_TABLE_TYPE \"${PARTITION_TABLE_TYPE}\" not implemented" exit 1 diff --git a/tools/partition_table_helper/guid.sh b/tools/partition_table_helper/guid.sh index 54ba1cb..7d586ee 100644 --- a/tools/partition_table_helper/guid.sh +++ b/tools/partition_table_helper/guid.sh @@ -1,9 +1,21 @@ # Assuming sector size is 512B -readonly GUID_TABLE_SECTOR_SIZE=512 +readonly GUID_TABLE_SECTOR_SIZE_DEFAULT=512 # Default alignment of the offset of each partition -readonly GUID_TABLE_ALIGNMENT="$(( 2048 * GUID_TABLE_SECTOR_SIZE ))" +readonly GUID_TABLE_ALIGNMENT_DEFAULT="$(( 2048 * GUID_TABLE_SECTOR_SIZE_DEFAULT ))" # Reserved size for partition table header -readonly GUID_TABLE_RESV_SIZE="$(( 34 * GUID_TABLE_SECTOR_SIZE ))" +readonly GUID_TABLE_RESV_SIZE_DEFAULT="$(( 34 * GUID_TABLE_SECTOR_SIZE_DEFAULT ))" + +get_guid_part_alignment() { + if [ -n "${DISK_PARTITION_ALIGNMENT:-}" ]; then + if [ "0" -ne "$(( DISK_PARTITION_ALIGNMENT % GUID_TABLE_SECTOR_SIZE_DEFAULT ))" ]; then + loge "Inavlid DISK_PARTITION_ALIGNMENT: \"${DISK_PARTITION_ALIGNMENT}\"" + exit 1 + fi + printf "%d" "${DISK_PARTITION_ALIGNMENT}" + else + printf "%d" "${GUID_TABLE_ALIGNMENT_DEFAULT}" + fi +} parse_image_layout_impl_partitioned_disk_table_GUID_part_usage_MOUNTPOINT() { logi "Parsing mountpoint usage" @@ -56,9 +68,11 @@ parse_image_layout_impl_partitioned_disk_table_GUID_part_usage_BLOB() { parse_image_layout_impl_partitioned_disk_table_GUID() { logi "Parsing GUID table" + + guid_table_alignment="$(get_guid_part_alignment)" # reserve space for table header - curr_part_offset=${GUID_TABLE_RESV_SIZE} - curr_part_offset="$(( ((curr_part_offset - 1) / GUID_TABLE_ALIGNMENT + 1) * GUID_TABLE_ALIGNMENT ))" + curr_part_offset=${GUID_TABLE_RESV_SIZE_DEFAULT} + curr_part_offset="$(( ((curr_part_offset - 1) / guid_table_alignment + 1) * guid_table_alignment ))" has_flex_size_part=false for part_idx in $(seq 1 99); do if eval "[ -z \"\${PARTITION_${part_idx}_USAGE+x}\" ]"; then @@ -77,7 +91,7 @@ parse_image_layout_impl_partitioned_disk_table_GUID() { part_offset_in_byte="$(numfmt --from iec "${part_offset_in_byte%B}")" if [ "${part_offset_in_byte}" -ne 0 ]; then # Ensure alignment first - aligned_part_offset="$(( ((part_offset_in_byte - 1) / GUID_TABLE_ALIGNMENT + 1) * GUID_TABLE_ALIGNMENT ))" + aligned_part_offset="$(( ((part_offset_in_byte - 1) / guid_table_alignment + 1) * guid_table_alignment ))" if [ "${aligned_part_offset}" -ne "${part_offset_in_byte}" ]; then logw "Force aligning ${part_offset_in_byte} to ${aligned_part_offset}" fi @@ -103,7 +117,7 @@ parse_image_layout_impl_partitioned_disk_table_GUID() { echo "PARTITION_${part_idx}_SIZE=${part_size_in_byte}" # Then increase the part offset to next available position - curr_part_offset="$(( ((curr_part_offset + part_size_in_byte - 1) / GUID_TABLE_ALIGNMENT + 1) * GUID_TABLE_ALIGNMENT ))" + curr_part_offset="$(( ((curr_part_offset + part_size_in_byte - 1) / guid_table_alignment + 1) * guid_table_alignment ))" # PARTITION_xx_NAME echo "PARTITION_${part_idx}_NAME=$(eval echo \$\{PARTITION_"${part_idx}"_NAME:-\})" @@ -162,9 +176,9 @@ produce_image_GUID() { fi curr_offset="$(( last_part_offset + last_part_size ))" # Ensure the last partition owns the last usable sector - curr_offset="$(( ((curr_offset - 1) / GUID_TABLE_SECTOR_SIZE + 1) * GUID_TABLE_SECTOR_SIZE ))" - final_disk_size="$(( curr_offset + GUID_TABLE_RESV_SIZE ))" - final_disk_size="$(( ((final_disk_size - 1) / GUID_TABLE_SECTOR_SIZE + 1) * GUID_TABLE_SECTOR_SIZE ))" + curr_offset="$(( ((curr_offset - 1) / GUID_TABLE_SECTOR_SIZE_DEFAULT + 1) * GUID_TABLE_SECTOR_SIZE_DEFAULT ))" + final_disk_size="$(( curr_offset + GUID_TABLE_RESV_SIZE_DEFAULT ))" + final_disk_size="$(( ((final_disk_size - 1) / GUID_TABLE_SECTOR_SIZE_DEFAULT + 1) * GUID_TABLE_SECTOR_SIZE_DEFAULT ))" # Prepare the partition table OUTPUT_IMAGE="${OUTPUT_PREFIX}.img" @@ -175,8 +189,9 @@ produce_image_GUID() { # sgdisk operates on files that may have different sector size with out target image SECTOR_SIZE="$(sgdisk -p "${OUTPUT_IMAGE}" | sed -rn 's/Sector size \(logical\): ([0-9]+) bytes/\1/p')" # Make sure our offset alignment satisfies sector size - if [ "$(( (GUID_TABLE_ALIGNMENT / SECTOR_SIZE) * SECTOR_SIZE ))" -ne "${GUID_TABLE_ALIGNMENT}" ]; then - loge "Unsupported alignment between GUID_TABLE_ALIGNMENT (${GUID_TABLE_ALIGNMENT}) and SECTOR_SIZE (${SECTOR_SIZE})" + guid_table_alignment="$(get_guid_part_alignment)" + if [ "$(( (guid_table_alignment / SECTOR_SIZE) * SECTOR_SIZE ))" -ne "${guid_table_alignment}" ]; then + loge "Unsupported alignment between guid_table_alignment (${guid_table_alignment}) and SECTOR_SIZE (${SECTOR_SIZE})" exit 1 fi @@ -190,7 +205,7 @@ produce_image_GUID() { fi curr_part_offset="PARTITION_${part_idx}_OFFSET" curr_part_offset="${!curr_part_offset}" - logcmd sgdisk --new "${part_idx}:$(( curr_part_offset / SECTOR_SIZE )):+$(( curr_part_size / SECTOR_SIZE ))" "${OUTPUT_IMAGE}" + logcmd sgdisk --set-alignment="$(( guid_table_alignment / SECTOR_SIZE ))" --new "${part_idx}:$(( curr_part_offset / SECTOR_SIZE )):+$(( curr_part_size / SECTOR_SIZE ))" "${OUTPUT_IMAGE}" curr_part_uuid="PARTITION_${part_idx}_UUID" curr_part_uuid="${!curr_part_uuid}" ./partition_table_helper/gptcfg.sh "${OUTPUT_IMAGE}" set-uuid "${part_idx}" "${curr_part_uuid}" @@ -212,8 +227,8 @@ produce_image_GUID() { logcmd dd conv=notrunc,sparse \ if="${curr_part_content}" \ of="${OUTPUT_IMAGE}" \ - bs="${GUID_TABLE_SECTOR_SIZE}" \ - seek="$(( curr_part_offset / GUID_TABLE_SECTOR_SIZE ))" + bs="${GUID_TABLE_SECTOR_SIZE_DEFAULT}" \ + seek="$(( curr_part_offset / GUID_TABLE_SECTOR_SIZE_DEFAULT ))" done logi "${OUTPUT_IMAGE} written." }