container: remove old image when upgrading at startup

One unique feature of Infix OS is that you can embed your OCI archive in
the rootfs.squashfs.  This way your container instance does not need to
download anything from the network.  To upgrade you drop in a new image
and rebuild Infix, when the new Infix boots the container script 'setup'
command will recognize that the OCI archive has changed and will reload
it into the container store.

This patch is an improvement of the way too generic 'podman image prune'
command used previously.  Instead of looking for any dangling image, we
now surgically remove the old image.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2025-10-23 15:23:54 +02:00
parent f53d26bf34
commit da5b4bcfff
+17 -2
View File
@@ -997,6 +997,12 @@ case $cmd in
fi
fi
# Capture old image ID before recreation (for surgical cleanup after)
old_image_id=""
if podman container exists "$name"; then
old_image_id=$(podman inspect "$name" --format '{{.ImageID}}' 2>/dev/null)
fi
# Save our PID in case we get stuck here and someone wants to
# stop us, e.g., due to reconfiguration or reboot.
pidfile=$(pidfn "${name}")
@@ -1033,8 +1039,17 @@ case $cmd in
fi
rm -f "$pidfile"
cnt=$(podman image prune -f | wc -l)
log "Pruned $cnt image(s)"
# Remove the old image if it's not used by any other containers
if [ -n "$old_image_id" ]; then
# Check if the old image is still in use by any containers
if ! podman ps -a --format '{{.ImageID}}' | grep -q "^${old_image_id}$"; then
log "Removing old image $old_image_id"
podman rmi "$old_image_id" 2>/dev/null || true
else
log "Old image $old_image_id still in use by other containers, keeping it"
fi
fi
;;
shell)
if [ -z "$name" ]; then