From 48a20e958259b2d832a28bfe7c3ab1dee0b5a6de Mon Sep 17 00:00:00 2001 From: Paul Guyot Date: Sat, 4 May 2024 21:16:50 +0200 Subject: [PATCH] Fix optimize with parted Signed-off-by: Paul Guyot --- cleanup_image.sh | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/cleanup_image.sh b/cleanup_image.sh index a422cec..77edb12 100644 --- a/cleanup_image.sh +++ b/cleanup_image.sh @@ -1,5 +1,5 @@ #!/bin/bash -set -uo pipefail +set -euxo pipefail loopdev=$1 mount=$2 @@ -20,10 +20,10 @@ 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 @@ -47,10 +47,15 @@ if [[ -d "${mount}" ]]; then 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_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 > $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