Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support optimizing (shrinking) bootable partitions on images with GPT partition tables #117

Merged
merged 6 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test-gpt-partition-table.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ jobs:
image_additional_mb: 2048
cpu: cortex-a53
commands: |
test $(df -h --output=size / | tail -n 1) = '2.8G'
test $(df -h --output=size / | tail -n 1) = '2.8G'
25 changes: 25 additions & 0 deletions .github/workflows/test-shrink-bootable-partition.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Test shrink_bootable_partition
on:
push:
branches:
- 'main'
- 'releases/**'
pull_request:
workflow_dispatch:

jobs:
optimized:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ./ # pguyot/arm-runner-action@HEAD
id: shrink
with:
base_image: https://github.com/Joshua-Riek/ubuntu-rockchip/releases/download/v2.2.1/ubuntu-24.04-preinstalled-server-arm64-orangepi-5.img.xz
optimize_image: yes
commands: |
echo "Shrink Bootable Partition"
- name: Check image
run: |
parted --script ${{ steps.shrink.outputs.image }} print free | tail -n2 | head -n1 | grep -c "boot, esp"

15 changes: 13 additions & 2 deletions cleanup_image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ if [[ -d "${mount}" ]]; then
if [[ "${optimize}x" == "x" || "${optimize}x" == "yesx" ]]; then
rootfs_partnum=${rootpartition}
rootdev="${loopdev}p${rootfs_partnum}"

part_type=$(blkid -o value -s PTTYPE "${loopdev}")
echo "Image is using ${part_type} partition table"

echo "Resizing root filesystem to minimal size."
e2fsck -p -f "${rootdev}"
Expand All @@ -63,8 +66,7 @@ if [[ -d "${mount}" ]]; then
# parted --script "${loopdev}" unit B resizepart "${rootfs_partnum}" "${rootfs_partend}"
# Can't use resizepart for shrinking with --script (parted bug#22167) => must rm then mkpart
if [ "$rootfs_partoldend" -gt "$rootfs_partend" ]; then
parted --script "${loopdev}" rm "${rootfs_partnum}"
parted --script "${loopdev}" unit B mkpart primary "${rootfs_partstart}" "${rootfs_partend}"
echo y | parted ---pretend-input-tty "${loopdev}" unit B resizepart "${rootfs_partnum}" "${rootfs_partend}"
else
echo "Rootfs partition not resized as it was not shrunk"
fi
Expand All @@ -73,8 +75,17 @@ if [[ -d "${mount}" ]]; then
if [[ "${free_space}" =~ "free" ]]; then
initial_image_size=$(stat -L --printf="%s" "${image}")
image_size=$(echo "${free_space}" | awk -F ":" '{print $2}' | tr -d 'B')
if [[ "${part_type}" == "gpt" ]]; then
# for GPT partition table, leave space at the end for the secondary GPT
# it requires 33 sectors, which is 16896 bytes
image_size=$((image_size + 16896))
fi
echo "Shrinking image from ${initial_image_size} to ${image_size} bytes."
truncate -s "${image_size}" "${image}"
if [[ "${part_type}" == "gpt" ]]; then
# use sgdisk to fix the secondary GPT after truncation
sgdisk -e "${image}"
fi
fi
fi
rmdir "${mount}" || true
Expand Down
Loading