#!/usr/bin/env bash set -Eeuo pipefail thisDir="$(dirname "$(readlink -vf "$BASH_SOURCE")")" source "$thisDir/.constants.sh" \ '' \ 'rootfs' eval "$dgetopt" while true; do flag="$1"; shift dgetopt-case "$flag" case "$flag" in --) break ;; *) eusage "unknown flag '$flag'" ;; esac done targetDir="${1:-}"; shift || eusage 'missing target-dir' [ -n "$targetDir" ] dpkgVersion="$("$thisDir/.apt-version.sh" "$targetDir" 'dpkg')" if ! { [ -d "$targetDir/etc/dpkg/dpkg.cfg.d" ] && dpkg --compare-versions "$dpkgVersion" '>=' '1.15.8.6~'; }; then # --debian-eol lenny and older do not include /etc/dpkg/dpkg.cfg.d # path-exclude/include was added in dpkg 1.15.8: https://salsa.debian.org/dpkg-team/dpkg/-/commit/4694cd64089bc72975d8ba6fbe51339023eb2e8c echo >&2 "note: skipping $self: dpkg version ($dpkgVersion) too old to support path-exclude" exit fi # https://github.com/debuerreotype/debuerreotype/issues/10 shopt -s nullglob extraSpecialDirectories=( "$targetDir"/usr/share/man/man[0-9]/ ) shopt -u nullglob IFS=$'\n'; set -o noglob slimExcludes=( $(grep -vE '^#|^$' "$thisDir/.slimify-excludes" | sort -u) ) slimIncludes=( $(grep -vE '^#|^$' "$thisDir/.slimify-includes" | sort -u) ) set +o noglob; unset IFS dpkgCfgFile="$targetDir/etc/dpkg/dpkg.cfg.d/docker" mkdir -p "$(dirname "$dpkgCfgFile")" { echo '# This is the "slim" variant of the Debian base image.' echo '# Many files which are normally unnecessary in containers are excluded,' echo '# and this configuration file keeps them that way.' } > "$dpkgCfgFile" findMatchIncludes=() for slimInclude in "${slimIncludes[@]}"; do [ "${#findMatchIncludes[@]}" -eq 0 ] || findMatchIncludes+=( '-o' ) findMatchIncludes+=( -path "$slimInclude" ) done findMatchIncludes=( '(' "${findMatchIncludes[@]}" ')' ) for slimExclude in "${slimExcludes[@]}"; do { echo echo "# dpkg -S '$slimExclude'" if dpkgOutput="$("$thisDir/debuerreotype-chroot" "$targetDir" dpkg -S "$slimExclude" 2>&1)"; then echo "$dpkgOutput" | sed 's/: .*//g; s/, /\n/g' | sort -u | xargs else echo "$dpkgOutput" fi | fold -w 76 -s | sed 's/^/# /' echo "path-exclude $slimExclude" } >> "$dpkgCfgFile" if [[ "$slimExclude" == *'/*' ]]; then if [ -d "$targetDir/$(dirname "$slimExclude")" ]; then # use two passes so that we don't fail trying to remove directories from $slimIncludes # this is our best effort at implementing https://sources.debian.net/src/dpkg/stretch/src/filters.c/#L96-L97 in shell # step 1 -- delete everything that doesn't match "$slimIncludes" and isn't a directory or a symlink "$thisDir/debuerreotype-chroot" "$targetDir" \ find "$(dirname "$slimExclude")" \ -depth -mindepth 1 \ -not \( -type d -o -type l \) \ -not "${findMatchIncludes[@]}" \ -exec rm -f '{}' ';' # step 2 -- repeatedly delete any dangling symlinks and empty directories until there aren't any # (might have a dangling symlink in a directory which then makes it empty, or a symlink to an empty directory) while [ "$( "$thisDir/debuerreotype-chroot" "$targetDir" \ find "$(dirname "$slimExclude")" \ -depth -mindepth 1 \( -empty -o -xtype l \) \ -exec rm -rf '{}' ';' -printf '.' \ | wc -c )" -gt 0 ]; do true; done fi else "$thisDir/debuerreotype-chroot" "$targetDir" rm -f "$slimExclude" fi done { echo for slimInclude in "${slimIncludes[@]}"; do echo "path-include $slimInclude" done } >> "$dpkgCfgFile" chmod 0644 "$dpkgCfgFile" # https://github.com/debuerreotype/debuerreotype/issues/10 if [ "${#extraSpecialDirectories[@]}" -gt 0 ]; then mkdir -p "${extraSpecialDirectories[@]}" fi