Fix dpkg not knowing about packages after cache restore

After cache restore, dpkg had no record of the installed packages because:
1. Only preinst/postinst scripts were cached from /var/lib/dpkg/info/,
   missing .list, .md5sums, .conffiles, and other metadata files
2. The dpkg status database (/var/lib/dpkg/status) was never updated

This meant dpkg -s, apt list --installed, and anything checking package
state would not see the restored packages.

Fix:
- Cache all /var/lib/dpkg/info/<package>.* files (not just install scripts)
- Save each package's dpkg status entry to a .dpkg-status file
- On restore, append status entries to /var/lib/dpkg/status (skipping
  packages that are already registered)

Adds tests verifying dpkg -s reports packages as installed after both
fresh install and cache restore.

Co-developed-by: Claude Code v2.1.58 (claude-opus-4-6)
This commit is contained in:
Rob Taylor 2026-03-10 18:30:37 +00:00
parent f6d423e255
commit 5bddecf9cd
3 changed files with 85 additions and 4 deletions

View file

@ -473,3 +473,50 @@ jobs:
packages: libvips
version: ${{ github.run_id }}-${{ github.run_attempt }}-virtual_package
debug: ${{ env.DEBUG }}
# === dpkg Registration Tests ===
dpkg_status_install:
runs-on: ubuntu-latest
name: "dpkg knows about packages after install (phase 1)."
steps:
- uses: actions/checkout@v4
- name: Execute
id: execute
uses: ./
with:
packages: rolldice
version: ${{ github.run_id }}-${{ github.run_attempt }}-dpkg_status
debug: ${{ env.DEBUG }}
- name: Verify dpkg knows the package
run: |
dpkg -s rolldice | grep -q 'Status: install ok installed'
echo "dpkg reports rolldice as installed after fresh install."
shell: bash
dpkg_status_restore:
needs: dpkg_status_install
runs-on: ubuntu-latest
name: "dpkg knows about packages after cache restore (phase 2)."
steps:
- uses: actions/checkout@v4
- name: Execute
id: execute
uses: ./
with:
packages: rolldice
version: ${{ github.run_id }}-${{ github.run_attempt }}-dpkg_status
debug: ${{ env.DEBUG }}
- name: Verify cache hit
run: test "${{ steps.execute.outputs.cache-hit }}" = "true"
shell: bash
- name: Verify dpkg knows the package after cache restore
run: |
dpkg -s rolldice | grep -q 'Status: install ok installed'
echo "dpkg reports rolldice as installed after cache restore."
shell: bash
- name: Verify the binary works
run: |
rolldice 2d6
echo "rolldice binary works after cache restore."
shell: bash

View file

@ -101,11 +101,15 @@ for installed_package in ${installed_packages}; do
read package_name package_ver < <(get_package_name_ver "${installed_package}")
log " * Caching ${package_name} to ${cache_filepath}..."
# Pipe all package files (no folders), including symlinks, their targets, and installation control data to Tar.
# Pipe all package files (no folders), including symlinks, their targets,
# and all dpkg metadata (info files) to Tar.
tar -cf "${cache_filepath}" -C / --verbatim-files-from --files-from <(
{ dpkg -L "${package_name}" &&
get_install_script_filepath "" "${package_name}" "preinst" &&
get_install_script_filepath "" "${package_name}" "postinst" ; } |
# Include all dpkg info files for this package (list, md5sums,
# conffiles, triggers, preinst, postinst, prerm, postrm, etc.)
# so dpkg recognizes the package after cache restore.
ls -1 /var/lib/dpkg/info/${package_name}.* 2>/dev/null &&
ls -1 /var/lib/dpkg/info/${package_name}:*.* 2>/dev/null ; } |
while IFS= read -r f; do
if test -f "${f}" -o -L "${f}"; then
get_tar_relpath "${f}"
@ -119,6 +123,9 @@ for installed_package in ${installed_packages}; do
done
)
# Save the dpkg status entry so we can register the package on restore.
dpkg -s "${package_name}" > "${cache_dir}/${installed_package}.dpkg-status" 2>/dev/null || true
log " done (compressed size $(du -h "${cache_filepath}" | cut -f1))."
fi

View file

@ -50,7 +50,7 @@ for cached_filepath in ${cached_filepaths}; do
sudo tar -xf "${cached_filepath}" -C "${cache_restore_root}" > /dev/null
log " done"
# Execute install scripts if available.
# Execute install scripts if available.
if test ${execute_install_scripts} == "true"; then
# May have to add more handling for extracting pre-install script before extracting all files.
# Keeping it simple for now.
@ -59,3 +59,30 @@ for cached_filepath in ${cached_filepaths}; do
fi
done
log "done"
log_empty_line
# Register packages with dpkg so they appear as installed.
# The tar extraction restores dpkg info files (list, md5sums, etc.) but the
# main status database (/var/lib/dpkg/status) also needs updating.
dpkg_status_dir="${cache_dir}"
status_files=$(ls -1 "${dpkg_status_dir}"/*.dpkg-status 2>/dev/null || true)
if test -n "${status_files}"; then
log "Registering restored packages with dpkg..."
for status_file in ${status_files}; do
pkg_name=$(head -1 "${status_file}" | sed 's/^Package: //')
# Skip if dpkg already knows about this package (e.g., it was pre-installed).
if dpkg -s "${pkg_name}" > /dev/null 2>&1; then
existing_status=$(dpkg -s "${pkg_name}" 2>/dev/null | grep '^Status:' | head -1)
if echo "${existing_status}" | grep -q 'install ok installed'; then
log "- ${pkg_name} already registered, skipping."
continue
fi
fi
# Append the status entry (with blank line separator) to the dpkg database.
echo "" | sudo tee -a "${cache_restore_root}var/lib/dpkg/status" > /dev/null
cat "${status_file}" | sudo tee -a "${cache_restore_root}var/lib/dpkg/status" > /dev/null
log "- ${pkg_name} registered."
done
log "done"
fi