Skip to content

Commit 7f28809

Browse files
committed
Save all files in /var/lib/dpkg/info
1 parent 61fc13e commit 7f28809

3 files changed

Lines changed: 27 additions & 8 deletions

File tree

install_and_cache_pkgs.sh

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ manifest_main=""
6262
log "Package list:"
6363
for package in ${packages}; do
6464
manifest_main="${manifest_main}${package},"
65-
log "- ${package}"
65+
test "${debug}" == "true" && log "- ${package}"
6666
done
6767
write_manifest "main" "${manifest_main}" "${cache_dir}/manifest_main.log"
6868

@@ -95,7 +95,7 @@ installed_packages=$(get_installed_packages "${install_log_filepath}")
9595
log "Installed package list:"
9696
for installed_package in ${installed_packages}; do
9797
# Reformat for human friendly reading.
98-
log "- $(echo ${installed_package} | awk -F\= '{print $1" ("$2")"}')"
98+
test "${debug}" == "true" && log "- $(echo ${installed_package} | awk -F\= '{print $1" ("$2")"}')"
9999
done
100100

101101
log_empty_line
@@ -108,13 +108,16 @@ for installed_package in ${installed_packages}; do
108108
# Sanity test in case APT enumerates duplicates.
109109
if test ! -f "${cache_filepath}"; then
110110
read package_name package_ver < <(get_package_name_ver "${installed_package}")
111-
log " * Caching ${package_name} to ${cache_filepath}..."
111+
test "${debug}" == "true" && log " * Caching ${package_name} to ${cache_filepath}..."
112112

113+
# Get the entry in /var/lib/dpkg/status
114+
mkdir -p /var/tmp/dpkg-restore || :
115+
sed -n -r -e "/Package:\s+${package_name}$/,/^$/p" /var/lib/dpkg/status > /var/tmp/dpkg-restore/${package_name}.status
116+
113117
# Pipe all package files (no folders), including symlinks, their targets, and installation control data to Tar.
114118
tar -cf "${cache_filepath}" -C / --verbatim-files-from --files-from <(
115-
{ dpkg -L "${package_name}" &&
116-
{ get_install_script_filepath "/" "${package_name}" "preinst" ;
117-
get_install_script_filepath "/" "${package_name}" "postinst" ; } ; } |
119+
{ dpkg -L "${package_name}" && echo "/var/tmp/dpkg-restore/${package_name}.status" &&
120+
get_install_script_filepaths "/" "${package_name}" ; } |
118121
while IFS= read -r f; do
119122
if test -f "${f}" -o -L "${f}"; then
120123
get_tar_relpath "${f}"
@@ -128,7 +131,7 @@ for installed_package in ${installed_packages}; do
128131
done
129132
)
130133

131-
log " done (compressed size $(du -h "${cache_filepath}" | cut -f1))."
134+
test "${debug}" == "true" && log " done (compressed size $(du -h "${cache_filepath}" | cut -f1))."
132135
fi
133136

134137
# Comma delimited name:ver pairs in the all packages manifest.

lib.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ function execute_install_script {
2727
test "${debug}" == "true" && log "- Executing ${install_script_filepath}..." || :
2828
# Don't abort on errors; dpkg-trigger will error normally since it is
2929
# outside its run environment.
30+
export DPKG_MAINTSCRIPT_NAME="${3}"
3031
sudo sh ${echocmd} ${install_script_filepath} ${4} || true
3132
test "${debug}" == "true" && log " done"
3233
fi
@@ -49,6 +50,11 @@ function get_install_script_filepath {
4950
test "${filepath}" && echo "${filepath}"
5051
}
5152

53+
function get_install_script_filepaths {
54+
# Filename includes arch (e.g. amd64).
55+
ls -1 ${1}var/lib/dpkg/info/${2}:*.* ${1}var/lib/dpkg/info/${2}.* 2> /dev/null
56+
}
57+
5258
###############################################################################
5359
# Gets a list of installed packages from a Debian package installation log.
5460
# Arguments:

restore_pkgs.sh

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,22 @@ mapfile -t packages <"${manifest_all}"
5050
cached_filecount="${#packages[@]}"
5151
log "Restoring ${cached_filecount} packages from cache..."
5252

53+
restore_dpkg_status() {
54+
echo "" >> "${1}/var/lib/dpkg/status"
55+
cat "${1}/var/tmp/dpkg-restore/${2}.status" >> "${1}/var/lib/dpkg/status"
56+
}
57+
5358
for package in "${packages[@]}"; do
5459
cached_filepath="${cache_dir}/${package}.tar"
5560
test "${debug}" == "true" && log "- ${package} restoring..." || :
5661
sudo tar -xf "${cached_filepath}" -C "${cache_restore_root}" > /dev/null
5762
test "${debug}" == "true" && log " done" || :
58-
63+
package_name="${package%%=*}"
64+
if test -f "${cache_restore_root}/var/tmp/dpkg-restore/${pkg}.status" ; then
65+
sudo bash -c "$(declare -f restore_dpkg_status); restore_dpkg_status ${cache_restore_root} ${package_name} ;"
66+
else
67+
log "Status file ${cache_restore_root}/var/tmp/dpkg-restore/${pkg}.status not found"
68+
fi
5969
# Execute install scripts if available.
6070
if test ${execute_install_scripts} == "true"; then
6171
# May have to add more handling for extracting pre-install script before extracting all files.

0 commit comments

Comments
 (0)