diff --git a/.github/workflows/action-tests.yml b/.github/workflows/action-tests.yml index 2fb8ce3..6e66a94 100644 --- a/.github/workflows/action-tests.yml +++ b/.github/workflows/action-tests.yml @@ -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 diff --git a/install_and_cache_pkgs.sh b/install_and_cache_pkgs.sh index 1a544ad..006fda7 100755 --- a/install_and_cache_pkgs.sh +++ b/install_and_cache_pkgs.sh @@ -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 diff --git a/restore_pkgs.sh b/restore_pkgs.sh index 4556265..ef9418d 100755 --- a/restore_pkgs.sh +++ b/restore_pkgs.sh @@ -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