Skip to content

Commit

Permalink
Fix optimize_image (fix #87)
Browse files Browse the repository at this point in the history
Fix a miscalculaton of end of partition parameter for parted. It shall be the
last byte of the partition, not start byte + size. This made parted fail when
the partition wasn't shrunk by resize2fs.

Avoid calling parted if the partition isn't shrunk by resize2fs.

Fix a bug where `cleanup_image.sh` script was not aborting in case of errors,
thus not revealing a corruption of generated image as reported in #87.

Fix a bug where cleanup_image.sh failed using images with more than 10
partitions because of a lazy glob.

Signed-off-by: Paul Guyot <pguyot@kallisys.net>
  • Loading branch information
pguyot committed May 5, 2024
1 parent 7d172f0 commit 543dbd3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
25 changes: 24 additions & 1 deletion .github/workflows/test-optimize_image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,27 @@ jobs:
gzip /home/actions/temp/optimized-image.img
imgsize=`stat --printf="%s" /home/actions/temp/optimized-image.img.gz`
echo "imgsize=${imgsize}" >> $GITHUB_OUTPUT
slimmed:
runs-on: ubuntu-latest
outputs:
imgsize: ${{ steps.compress.outputs.imgsize }}
steps:
- uses: actions/checkout@v4
- uses: ./ # pguyot/arm-runner-action@HEAD
id: slimmed
with:
base_image: raspios_lite:2021-05-07
optimize_image: yes
commands: |
apt-get -y --purge autoremove ".*python.*"
cat /dev/random > /rand || rm /rand
- name: Compress image
id: compress
run: |
mv ${{ steps.slimmed.outputs.image }} /home/actions/temp/slimmed-image.img
gzip /home/actions/temp/slimmed-image.img
imgsize=`stat --printf="%s" /home/actions/temp/slimmed-image.img.gz`
echo "imgsize=${imgsize}" >> $GITHUB_OUTPUT
unoptimized:
runs-on: ubuntu-latest
outputs:
Expand All @@ -50,10 +71,12 @@ jobs:
echo "imgsize=${imgsize}" >> $GITHUB_OUTPUT
compare:
runs-on: ubuntu-latest
needs: [optimized, unoptimized]
needs: [optimized, unoptimized, slimmed]
steps:
- name: Test image sizes
run: |
echo "Optimized size: ${{needs.optimized.outputs.imgsize}}"
echo "Slimmed size: ${{needs.slimmed.outputs.imgsize}}"
echo "Unoptimized size: ${{needs.unoptimized.outputs.imgsize}}"
test $(( ${{needs.unoptimized.outputs.imgsize}} - ${{needs.optimized.outputs.imgsize}} )) > 300000000
test $(( ${{needs.optimized.outputs.imgsize}} - ${{needs.slimmed.outputs.imgsize}} )) > 5000000
21 changes: 13 additions & 8 deletions cleanup_image.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
set -uo pipefail
set -euxo pipefail

loopdev=$1
mount=$2
Expand All @@ -12,18 +12,18 @@ rm "${mount}/usr/bin/qemu-arm-static"
rm "${mount}/usr/bin/qemu-aarch64-static0"
rm "${mount}/usr/bin/qemu-aarch64-static"
[ -e "${mount}/etc/_ld.so.preload" ] && mv "${mount}/etc/_ld.so.preload" "${mount}/etc/ld.so.preload"
mv "${mount}/etc/_resolv.conf" "${mount}/etc/resolv.conf"
[ -e "${mount}/etc/_resolv.conf" ] && mv "${mount}/etc/_resolv.conf" "${mount}/etc/resolv.conf"

[[ -f "${mount}/tmp/commands.sh" ]] && rm "${mount}/tmp/commands.sh"
if [[ -d "${mount}" ]]; then

if [[ "${optimize}x" == "x" || "${optimize}x" == "yesx" ]]; then
if [[ -d "${mount}/boot" ]]; then
echo "Zero-filling unused blocks on boot filesystem..."
cat /dev/zero >"${mount}/boot/zero.fill" 2>/dev/null; sync; rm -f "${mount}/boot/zero.fill"
(cat /dev/zero >"${mount}/boot/zero.fill" 2>/dev/null || true); sync; rm -f "${mount}/boot/zero.fill"
fi
echo "Zero-filling unused blocks on root filesystem..."
cat /dev/zero >"${mount}/zero.fill" 2>/dev/null; sync; rm -f "${mount}/zero.fill"
(cat /dev/zero >"${mount}/zero.fill" 2>/dev/null || true); sync; rm -f "${mount}/zero.fill"
fi

umount "${mount}/dev/pts" || true
Expand All @@ -35,7 +35,7 @@ if [[ -d "${mount}" ]]; then

if [[ "${optimize}x" == "x" || "${optimize}x" == "yesx" ]]; then
rootfs_partnum=${rootpartition}
rootdev=$(ls "${loopdev}"*${rootfs_partnum})
rootdev="${loopdev}p${rootfs_partnum}"

echo "Resizing root filesystem to minimal size."
e2fsck -p -f "${rootdev}"
Expand All @@ -46,11 +46,16 @@ if [[ -d "${mount}" ]]; then
echo "Resizing rootfs partition."
rootfs_partstart=$(parted -m --script "${loopdev}" unit B print | grep "^${rootfs_partnum}:" | awk -F ":" '{print $2}' | tr -d 'B')
rootfs_partsize=$((${rootfs_blockcount} * ${rootfs_blocksize}))
rootfs_partend=$((${rootfs_partstart} + ${rootfs_partsize}))
rootfs_partend=$((${rootfs_partstart} + ${rootfs_partsize} - 1))
rootfs_partoldend=$(parted -m --script "${loopdev}" unit B print | grep "^${rootfs_partnum}:" | awk -F ":" '{print $3}' | tr -d 'B')
# 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
parted --script "${loopdev}" rm "${rootfs_partnum}"
parted --script "${loopdev}" unit B mkpart primary "${rootfs_partstart}" "${rootfs_partend}"
if [ "$rootfs_partoldend" -gt "$rootfs_partend" ]; then
parted --script "${loopdev}" rm "${rootfs_partnum}"
parted --script "${loopdev}" unit B mkpart primary "${rootfs_partstart}" "${rootfs_partend}"
else
echo "Rootfs partition not resized as it was not shrunk"
fi

free_space=$(parted -m --script "${loopdev}" unit B print free | tail -1)
if [[ "${free_space}" =~ "free" ]]; then
Expand Down

0 comments on commit 543dbd3

Please sign in to comment.