Raspberry Pi Image Compression and Distribution
Overview
Creating an optimized and minimized image for the Raspberry Pi 5 is a critical step when moving a system from development to deployment. A 1:1 clone of a 64 GB SD card or NVMe SSD is impractical as it contains empty space and is unwieldy to transfer. A multi-step process is recommended to keep the image as small and professional as possible.
Step 1: Clean the System (Preparation)
Before taking the image, the running system must be cleared of unnecessary ballast and system-specific data. This reduces the image size and prevents security issues (e.g., identical SSH keys on all distributed devices).
Preparation Script (prepare_image.sh)
Execute the following script on the Raspberry Pi to clean logs, caches, temporary files, and remove system-specific IDs and SSH host keys.
#!/bin/bash
# ==============================================================================
# Script: prepare_image.sh
# Purpose: Clean and prepare a Raspberry Pi 5 system for image distribution
# (removes caches, logs, SSH keys, and system IDs).
# Note: Must be run with sudo/root privileges!
# ==============================================================================
# Colors for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# Check if script is run as root
if [ "$EUID" -ne 0 ]; then
echo -e "${RED}Error: This script must be run with sudo or as root!${NC}"
exit 1
fi
echo -e "${YELLOW}=== Starting system cleanup for image preparation ===${NC}"
# 1. Clean package manager
echo -e "${GREEN}[1/5] Cleaning APT package cache...${NC}"
apt-get clean
apt-get autoremove -y
# 2. Delete temporary files and logs
echo -e "${GREEN}[2/5] Deleting temporary files and logfiles...${NC}"
# Empty/delete system logs
find /var/log -type f -exec truncate -s 0 {} \;
rm -rf /var/log/*.gz /var/log/*.[0-9] /var/log/*-????????
# Empty temporary directories
rm -rf /tmp/* /tmp/.* 2>/dev/null
rm -rf /var/tmp/* /var/tmp/.* 2>/dev/null
# 3. Remove SSH host keys (Security relevant step!)
echo -e "${GREEN}[3/5] Removing SSH host keys (will be regenerated on next boot)...${NC}"
rm -f /etc/ssh/ssh_host_*
# 4. Reset unique system IDs (Machine-ID)
echo -e "${GREEN}[4/5] Resetting Machine-ID...${NC}"
if [ -f /etc/machine-id ]; then
truncate -s 0 /etc/machine-id
fi
if [ -f /var/lib/dbus/machine-id ]; then
rm -f /var/lib/dbus/machine-id
# Restore symbolic link if necessary (often expected by systemd)
ln -sf /etc/machine-id /var/lib/dbus/machine-id
fi
# 5. Clear bash history for all users
echo -e "${GREEN}[5/5] Clearing Bash history...${NC}"
# History of current root user
cat /dev/null > ~/.bash_history
history -c
# History for normal users
for user_home in /home/*; do
if [ -d "$user_home" ]; then
if [ -f "$user_home/.bash_history" ]; then
cat /dev/null > "$user_home/.bash_history"
fi
fi
done
echo -e "${YELLOW}======================================================${NC}"
echo -e "${GREEN}Cleanup completed successfully!${NC}"
echo -e "${YELLOW}The Raspberry Pi will SHUT DOWN in 10 seconds.${NC}"
echo -e "${YELLOW}Afterwards, you can remove the SD card/SSD to pull the image.${NC}"
echo -e "${YELLOW}======================================================${NC}"
# Countdown before shutdown
for i in {10..1}; do
echo -n "$i... "
sleep 1
done
echo ""
# Safely shut down system
shutdown -h nowImportant: Do not reboot the Raspberry Pi before creating the image, as the temporary IDs and SSH keys will be regenerated immediately.
Step 2: Create the Raw Image
Remove the SD card or NVMe SSD from the Raspberry Pi 5 and connect it to a Linux machine (or Mac/Windows with WSL). Create a 1:1 clone using dd:
sudo dd if=/dev/sdX of=my_system.img bs=4M status=progress*(Replace /dev/sdX with the correct path to your drive).*
This image will be the exact size of the physical drive, regardless of actual data usage.
Step 3: Shrink the Image (PiShrink)
Use PiShrink to cut off empty space. It shrinks the filesystem and partition to the absolute minimum and injects a script that automatically resizes the partition on the first boot on the target device.
1. Download PiShrink
wget https://raw.githubusercontent.com/Drewsif/PiShrink/master/pishrink.sh
chmod +x pishrink.sh
sudo mv pishrink.sh /usr/local/bin2. Shrink and Compress
The -z flag compresses the image directly with gzip after shrinking.
sudo pishrink.sh -z my_system.imgResult: A 64 GB image with 8 GB of actual data will be converted into a my_system.img.gz file of roughly 2 to 3 GB, which is ideal for distribution via Raspberry Pi Imager or BalenaEtcher.
Alternative: Infrastructure as Code (Best Practice)
For complex software systems, constant cloning and cleaning can become error-prone. A long-term alternative is to build the image automatically from scratch:
Pi-Gen: The official tool from the Raspberry Pi Foundation. You can define custom "Stages" where complex software is installed during the build process.
Ansible or Docker: Distribute a minimal Raspberry Pi OS "Lite" along with a script that automatically pulls required Docker containers or executes an Ansible playbook on the first boot.
As an alternative to manual cloning/cleaning/shrinking: Our production CI/CD approach, which aligns with the "Infrastructure as Code" concept mentioned in the article:
At pib-rocks, we already use CustomPiOS in combination with GitHub Actions to build the image fully automatically from scratch, instead of cloning an existing system and cleaning it up afterward. This eliminates the risks described in Step 1 (forgotten SSH keys, logs, IDs) right from the start, since a "dirty" system is never cloned.
Key points of our workflow:
Trigger strategy: Automatic builds on push (
develop,master,release/*,feature/*), on version tags, and on a schedule (daily fordevelop, weekly formain).Base setup: Instead of starting from an existing system, a fresh Raspberry Pi OS Bookworm (ARM64) image is downloaded as the starting point.
CustomPiOS as the build framework: We clone guysoft/CustomPiOS and define our own modules (stages) – similar to Pi-Gen, but using CustomPiOS's module structure. The actual setup runs via a
start_chroot_script, which is executed as a CustomPiOS module inside the chroot during the build:Unpacks filesystem overlays (
/home/pi,/root,/boot) with correct ownership.Installs required packages (
PIBBACKEND_PKGS) and removes unnecessary pre-installed games/office software (Aisleriot, LibreOffice, Thunderbird, etc.).Downloads and runs the actual
setup-pib.shscript from GitHub.Starts
dockerdmanually (--iptables=false --bridge=none) to load already prepared Docker images.Finally enables a
doitpi_firstboot.servicefor tasks that can only run on real hardware.Docker integration already during the build: Instead of pulling containers on first boot, we download all required Docker images (pib-backend, cerebra/angular-app) during the CI build itself, save them as
.tar.gz, and place them directly into the image's filesystem. In the chroot script, these are then loaded viadocker loadand deleted afterward. This means the target device doesn't need to download anything from the internet on first start – ideal for offline deployment.Storage optimization on the runner: Since GitHub Actions runners have limited disk space, we remove unnecessary pre-installed toolchains beforehand (Android SDK, Go, CodeQL, etc.) and clean up Docker caches.
Automated releases: Depending on the branch/trigger, weekly, daily, pre-, or final releases are automatically created with the finished
.imgas an artifact.Advantage over the manual PiShrink workflow: No manual interaction with real hardware required, fully reproducible, version-controlled (Git), and tested through the CI pipeline before an image is ever distributed.
Possible downside: The final compression step (xz) is currently commented out – analogous to PiShrink's
-z/gzip orxz --extreme, this could be added to reduce artifact size before it's uploaded as a release.Open item to verify: Switching to Trixie
It would also be worth checking whether the entire build (including
start_chroot_script) works with Trixie (Debian 13) instead of the currently used Bookworm – not to be confused with "Trixi", though it is the same Debian foundation. Specifically to be clarified:Whether
PIBBACKEND_PKGSstill has the same package names/versions under Trixie (especially Docker packages, since Debian often ships older versions than Docker itself).Compatibility of
cgroupfs-umount/cgroupfs-mountwith Trixie's default cgroup v2 behavior.Whether
dockerd --bridge=nonecontinues to run stably in the QEMU chroot under Trixie (kernel/iptables version differences).Availability of an official ARM64 Trixie image as a base at the time of testing.
If anyone has already tested the setup script against a Trixie base image, a report of experience here would be helpful.
Repository: pib-rocks/setupimage (feature/docker) – already in productive use in the pib-rocks GitHub.