Debian Codegraph @

scripts/debuerreotype-slimify

raw
1 #!/usr/bin/env bash
2 set -Eeuo pipefail
3
4 thisDir="$(dirname "$(readlink -vf "$BASH_SOURCE")")"
5 source "$thisDir/.constants.sh" \
6 '<target-dir>' \
7 'rootfs'
8
9 eval "$dgetopt"
10 while true; do
11 flag="$1"; shift
12 dgetopt-case "$flag"
13 case "$flag" in
14 --) break ;;
15 *) eusage "unknown flag '$flag'" ;;
16 esac
17 done
18
19 targetDir="${1:-}"; shift || eusage 'missing target-dir'
20 [ -n "$targetDir" ]
21
22 dpkgVersion="$("$thisDir/.apt-version.sh" "$targetDir" 'dpkg')"
23
24 if ! { [ -d "$targetDir/etc/dpkg/dpkg.cfg.d" ] && dpkg --compare-versions "$dpkgVersion" '>=' '1.15.8.6~'; }; then
25 # --debian-eol lenny and older do not include /etc/dpkg/dpkg.cfg.d
26 # path-exclude/include was added in dpkg 1.15.8: https://salsa.debian.org/dpkg-team/dpkg/-/commit/4694cd64089bc72975d8ba6fbe51339023eb2e8c
27 echo >&2 "note: skipping $self: dpkg version ($dpkgVersion) too old to support path-exclude"
28 exit
29 fi
30
31 # https://github.com/debuerreotype/debuerreotype/issues/10
32 shopt -s nullglob
33 extraSpecialDirectories=( "$targetDir"/usr/share/man/man[0-9]/ )
34 shopt -u nullglob
35
36 IFS=$'\n'; set -o noglob
37 slimExcludes=( $(grep -vE '^#|^$' "$thisDir/.slimify-excludes" | sort -u) )
38 slimIncludes=( $(grep -vE '^#|^$' "$thisDir/.slimify-includes" | sort -u) )
39 set +o noglob; unset IFS
40
41 dpkgCfgFile="$targetDir/etc/dpkg/dpkg.cfg.d/docker"
42 mkdir -p "$(dirname "$dpkgCfgFile")"
43 {
44 echo '# This is the "slim" variant of the Debian base image.'
45 echo '# Many files which are normally unnecessary in containers are excluded,'
46 echo '# and this configuration file keeps them that way.'
47 } > "$dpkgCfgFile"
48
49 findMatchIncludes=()
50 for slimInclude in "${slimIncludes[@]}"; do
51 [ "${#findMatchIncludes[@]}" -eq 0 ] || findMatchIncludes+=( '-o' )
52 findMatchIncludes+=( -path "$slimInclude" )
53 done
54 findMatchIncludes=( '(' "${findMatchIncludes[@]}" ')' )
55
56 for slimExclude in "${slimExcludes[@]}"; do
57 {
58 echo
59 echo "# dpkg -S '$slimExclude'"
60 if dpkgOutput="$("$thisDir/debuerreotype-chroot" "$targetDir" dpkg -S "$slimExclude" 2>&1)"; then
61 echo "$dpkgOutput" | sed 's/: .*//g; s/, /\n/g' | sort -u | xargs
62 else
63 echo "$dpkgOutput"
64 fi | fold -w 76 -s | sed 's/^/# /'
65 echo "path-exclude $slimExclude"
66 } >> "$dpkgCfgFile"
67
68 if [[ "$slimExclude" == *'/*' ]]; then
69 if [ -d "$targetDir/$(dirname "$slimExclude")" ]; then
70 # use two passes so that we don't fail trying to remove directories from $slimIncludes
71 # this is our best effort at implementing https://sources.debian.net/src/dpkg/stretch/src/filters.c/#L96-L97 in shell
72
73 # step 1 -- delete everything that doesn't match "$slimIncludes" and isn't a directory or a symlink
74 "$thisDir/debuerreotype-chroot" "$targetDir" \
75 find "$(dirname "$slimExclude")" \
76 -depth -mindepth 1 \
77 -not \( -type d -o -type l \) \
78 -not "${findMatchIncludes[@]}" \
79 -exec rm -f '{}' ';'
80
81 # step 2 -- repeatedly delete any dangling symlinks and empty directories until there aren't any
82 # (might have a dangling symlink in a directory which then makes it empty, or a symlink to an empty directory)
83 while [ "$(
84 "$thisDir/debuerreotype-chroot" "$targetDir" \
85 find "$(dirname "$slimExclude")" \
86 -depth -mindepth 1 \( -empty -o -xtype l \) \
87 -exec rm -rf '{}' ';' -printf '.' \
88 | wc -c
89 )" -gt 0 ]; do true; done
90 fi
91 else
92 "$thisDir/debuerreotype-chroot" "$targetDir" rm -f "$slimExclude"
93 fi
94 done
95 {
96 echo
97 for slimInclude in "${slimIncludes[@]}"; do
98 echo "path-include $slimInclude"
99 done
100 } >> "$dpkgCfgFile"
101 chmod 0644 "$dpkgCfgFile"
102
103 # https://github.com/debuerreotype/debuerreotype/issues/10
104 if [ "${#extraSpecialDirectories[@]}" -gt 0 ]; then
105 mkdir -p "${extraSpecialDirectories[@]}"
106 fi