Skip to content

Commit 8b4c941

Browse files
committed
compress at end
1 parent 0a35a9f commit 8b4c941

File tree

1 file changed

+63
-1
lines changed

1 file changed

+63
-1
lines changed

mount.sh

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,69 @@ if [ "$rubik" == true ]; then
277277
FINAL_IMAGE="photonvision_rubikpi3"
278278

279279
else
280-
# Export the final image as an enivronment variable
280+
# Final image resize and cleanup (for standard images only)
281+
echo "=== Shrinking image to minimal size ==="
282+
283+
# Create loop device with partition scanning
284+
loopdev=$(sudo losetup --find --show --partscan "$ROOTFS_IMG")
285+
echo "Created loopback device: ${loopdev}"
286+
287+
# Determine root partition (typically partition 2)
288+
rootfs_partnum=2
289+
rootdev="${loopdev}p${rootfs_partnum}"
290+
291+
# Check partition table type
292+
part_type=$(sudo blkid -o value -s PTTYPE "${loopdev}")
293+
echo "Image is using ${part_type} partition table"
294+
295+
# Minimize root filesystem
296+
echo "Resizing root filesystem to minimal size"
297+
sudo e2fsck -p -f "${rootdev}"
298+
sudo resize2fs -M "${rootdev}"
299+
300+
# Get filesystem dimensions
301+
rootfs_blocksize=$(sudo tune2fs -l "${rootdev}" | grep "^Block size" | awk '{print $NF}')
302+
rootfs_blockcount=$(sudo tune2fs -l "${rootdev}" | grep "^Block count" | awk '{print $NF}')
303+
304+
echo "Block size: ${rootfs_blocksize}, Block count: ${rootfs_blockcount}"
305+
306+
# Calculate new partition boundaries
307+
rootfs_partstart=$(sudo parted -m --script "${loopdev}" unit B print | grep "^${rootfs_partnum}:" | awk -F ":" '{print $2}' | tr -d 'B')
308+
rootfs_partsize=$((rootfs_blockcount * rootfs_blocksize))
309+
rootfs_partend=$((rootfs_partstart + rootfs_partsize - 1))
310+
rootfs_partoldend=$(sudo parted -m --script "${loopdev}" unit B print | grep "^${rootfs_partnum}:" | awk -F ":" '{print $3}' | tr -d 'B')
311+
312+
# Shrink partition if needed (parted bug requires interactive mode for shrinking)
313+
if [ "$rootfs_partoldend" -gt "$rootfs_partend" ]; then
314+
echo "Shrinking partition from ${rootfs_partoldend} to ${rootfs_partend} bytes"
315+
echo y | sudo parted ---pretend-input-tty "${loopdev}" unit B resizepart "${rootfs_partnum}" "${rootfs_partend}"
316+
else
317+
echo "Rootfs partition not resized (no shrinkage detected)"
318+
fi
319+
320+
# Check for free space at end of disk
321+
free_space=$(sudo parted -m --script "${loopdev}" unit B print free | tail -1)
322+
if [[ "${free_space}" =~ "free" ]]; then
323+
initial_image_size=$(stat -L --printf="%s" "${ROOTFS_IMG}")
324+
image_size=$(echo "${free_space}" | awk -F ":" '{print $2}' | tr -d 'B')
325+
326+
# For GPT, reserve space for secondary GPT (33 sectors = 16896 bytes)
327+
if [[ "${part_type}" == "gpt" ]]; then
328+
image_size=$((image_size + 16896))
329+
fi
330+
331+
echo "Shrinking image from ${initial_image_size} to ${image_size} bytes"
332+
sudo truncate -s "${image_size}" "${ROOTFS_IMG}"
333+
334+
# Fix secondary GPT after truncation
335+
if [[ "${part_type}" == "gpt" ]]; then
336+
sudo sgdisk -e "${ROOTFS_IMG}"
337+
fi
338+
fi
339+
340+
# Cleanup loop device
341+
sudo losetup --detach "${loopdev}"
342+
echo "=== Image shrinking complete ==="
281343
FINAL_IMAGE="$ROOTFS_IMG"
282344
fi
283345

0 commit comments

Comments
 (0)