git: 78fa6965e91e - main - vmimage.subr: Don't log missing files from pkgs

From: Colin Percival <cperciva_at_FreeBSD.org>
Date: Sun, 16 Nov 2025 01:57:59 UTC
The branch main has been updated by cperciva:

URL: https://cgit.FreeBSD.org/src/commit/?id=78fa6965e91ec52c24f602250a9d7ec465b9a6ba

commit 78fa6965e91ec52c24f602250a9d7ec465b9a6ba
Author:     Colin Percival <cperciva@FreeBSD.org>
AuthorDate: 2025-11-16 01:19:50 +0000
Commit:     Colin Percival <cperciva@FreeBSD.org>
CommitDate: 2025-11-16 01:19:50 +0000

    vmimage.subr: Don't log missing files from pkgs
    
    When installing "extra" packages (aka those built from the ports tree),
    we record everything being installed in METALOG.pkg; the contents of
    that file is appended to METALOG before we generate the filesystem.
    
    There are two cases when files recorded in METALOG.pkg will no longer
    exist by the time we create the final disk image:
    1. If a pkg bug results in false dependencies being installed which
    are later removed by "pkg autoremove", and
    2. If the pkg we build and install from /usr/ports is older than the
    pkg on pkg.freebsd.org, and pkg gets upgraded automatically as part of
    installing extra packages.
    
    The ultimate issue in both cases is that there's no mechanism for
    removing entries from METALOG when we run 'pkg delete'.
    
    Address this build breakage by checking, line by line, if filesystem
    objects mentioned in METALOG.pkg exist before appending them to METALOG.
    
    Fixes:  6a13aeac3c1f ("vmimage.subr: pkg autoremove after pkg install")
    MFC after:      immediately (needed for 15.0-RC1)
---
 release/tools/vmimage.subr | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/release/tools/vmimage.subr b/release/tools/vmimage.subr
index 7035b107ae22..03d92850f2a3 100644
--- a/release/tools/vmimage.subr
+++ b/release/tools/vmimage.subr
@@ -292,8 +292,15 @@ vm_extra_pkg_rmcache() {
 buildfs() {
 	local md tmppool
 
+	# Copy entries from METALOG.pkg into METALOG, but first check to
+	# make sure that filesystem objects still exist; some things may
+	# have been logged which no longer exist if a package was removed.
 	if [ -f ${DESTDIR}/METALOG.pkg ]; then
-		cat ${DESTDIR}/METALOG.pkg >> ${DESTDIR}/METALOG
+		while read F REST; do
+			if [ -e ${DESTDIR}/${F} ]; then
+				echo "${F} ${REST}" >> ${DESTDIR}/METALOG
+			fi
+		done < ${DESTDIR}/METALOG.pkg
 	fi
 
 	if [ -n "${NO_ROOT}" ]; then