Debian Codegraph @

scripts/debuerreotype-minimizing-config

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 aptVersion="$("$thisDir/.apt-version.sh" "$targetDir")"
23 dpkgVersion="$("$thisDir/.apt-version.sh" "$targetDir" 'dpkg')"
24
25 # https://github.com/docker/docker/blob/d6f4fe9e38b60f63e429fff7ffced9c26cbf8236/contrib/mkimage/debootstrap#L63-L177
26
27 # prevent init scripts from running during install/update
28 cat > "$targetDir/usr/sbin/policy-rc.d" <<-'EOF'
29 #!/bin/sh
30
31 # For most Docker users, "apt-get install" only happens during "docker build",
32 # where starting services doesn't work and often fails in humorous ways. This
33 # prevents those failures by stopping the services from attempting to start.
34
35 exit 101
36 EOF
37 chmod 0755 "$targetDir/usr/sbin/policy-rc.d"
38
39 # prevent upstart scripts from running during install/update (but only if the "upstart" package exists)
40 if "$thisDir/debuerreotype-chroot" "$targetDir" apt-get install -qq -s upstart &> /dev/null; then
41 "$thisDir/debuerreotype-chroot" "$targetDir" dpkg-divert --local --rename --add /sbin/initctl > /dev/null
42 cp -a "$targetDir/usr/sbin/policy-rc.d" "$targetDir/sbin/initctl"
43 sed -i 's/^exit.*/exit 0/' "$targetDir/sbin/initctl"
44 fi
45
46 # force dpkg not to call sync() after package extraction (speeding up installs)
47 if [ -d "$targetDir/etc/dpkg/dpkg.cfg.d" ] && dpkg --compare-versions "$dpkgVersion" '>=' '1.15.8.6~'; then
48 # --debian-eol lenny and older do not include /etc/dpkg/dpkg.cfg.d
49 # force-unsafe-io was added in dpkg 1.15.8.6: https://salsa.debian.org/dpkg-team/dpkg/-/commit/929a9c4808c79781469987585f78f07df7f1d484
50 cat > "$targetDir/etc/dpkg/dpkg.cfg.d/docker-apt-speedup" <<-'EOF'
51 # For most Docker users, package installs happen during "docker build", which
52 # doesn't survive power loss and gets restarted clean afterwards anyhow, so
53 # this minor tweak gives us a nice speedup (much nicer on spinning disks,
54 # obviously).
55
56 force-unsafe-io
57 EOF
58 chmod 0644 "$targetDir/etc/dpkg/dpkg.cfg.d/docker-apt-speedup"
59 fi
60
61 if [ -d "$targetDir/etc/apt/apt.conf.d" ]; then
62 # TODO make some (all?) of these conditional based on the version of APT that added the feature
63 # (perhaps it's finally time for an "apt-version-cmp.sh" helper script to test whether APT is X or newer one version component at a time? "dpkg --compare-versions"!!!)
64
65 # update "autoremove" configuration to be aggressive about removing suggests deps that weren't manually installed
66 cat > "$targetDir/etc/apt/apt.conf.d/docker-autoremove-suggests" <<-'EOF'
67 # Since Docker users are looking for the smallest possible final images, the
68 # following emerges as a very common pattern:
69
70 # RUN apt-get update \
71 # && apt-get install -y <packages> \
72 # && <do some compilation work> \
73 # && apt-get purge -y --auto-remove <packages>
74
75 # By default, APT will actually _keep_ packages installed via Recommends or
76 # Depends if another package Suggests them, even and including if the package
77 # that originally caused them to be installed is removed. Setting this to
78 # "false" ensures that APT is appropriately aggressive about removing the
79 # packages it added.
80
81 # https://www.debian.org/doc/manuals/aptitude/ch02s05s05.en.html#configApt-AutoRemove-SuggestsImportant
82 Apt::AutoRemove::SuggestsImportant "false";
83 EOF
84 chmod 0644 "$targetDir/etc/apt/apt.conf.d/docker-autoremove-suggests"
85 "$thisDir/.fix-apt-comments.sh" "$aptVersion" "$targetDir/etc/apt/apt.conf.d/docker-autoremove-suggests"
86
87 # keep us lean by effectively running "apt-get clean" after every install
88 aptGetClean='"rm -f /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin || true";'
89 cat > "$targetDir/etc/apt/apt.conf.d/docker-clean" <<-EOF
90 # Since for most Docker users, package installs happen in "docker build" steps,
91 # they essentially become individual layers due to the way Docker handles
92 # layering, especially using CoW filesystems. What this means for us is that
93 # the caches that APT keeps end up just wasting space in those layers, making
94 # our layers unnecessarily large (especially since we'll normally never use
95 # these caches again and will instead just "docker build" again and make a brand
96 # new image).
97
98 # Ideally, these would just be invoking "apt-get clean", but in our testing,
99 # that ended up being cyclic and we got stuck on APT's lock, so we get this fun
100 # creation that's essentially just "apt-get clean".
101 DPkg::Post-Invoke { $aptGetClean };
102 APT::Update::Post-Invoke { $aptGetClean };
103
104 Dir::Cache::pkgcache "";
105 Dir::Cache::srcpkgcache "";
106
107 # Note that we do realize this isn't the ideal way to do this, and are always
108 # open to better suggestions (https://github.com/debuerreotype/debuerreotype/issues).
109 EOF
110 chmod 0644 "$targetDir/etc/apt/apt.conf.d/docker-clean"
111 "$thisDir/.fix-apt-comments.sh" "$aptVersion" "$targetDir/etc/apt/apt.conf.d/docker-clean"
112
113 cat > "$targetDir/etc/apt/apt.conf.d/docker-gzip-indexes" <<-'EOF'
114 # Since Docker users using "RUN apt-get update && apt-get install -y ..." in
115 # their Dockerfiles don't go delete the lists files afterwards, we want them to
116 # be as small as possible on-disk, so we explicitly request that Apt keep them
117 # compressed on-disk too instead of decompressing them.
118
119 # For comparison, an "apt-get update" layer without this on a pristine
120 # "debian:wheezy" base image was "29.88 MB", where with this it was only
121 # "8.273 MB".
122
123 Acquire::GzipIndexes "true";
124 EOF
125 # https://github.com/debuerreotype/debuerreotype/issues/41
126 isDebianJessie="$([ -f "$targetDir/etc/os-release" ] && source "$targetDir/etc/os-release" && [ "${ID:-}" = 'debian' ] && [ "${VERSION_ID:-}" = '8' ] && echo '1')" || :
127 if [ -n "$isDebianJessie" ] || [[ "$aptVersion" == 0.* ]] || dpkg --compare-versions "$aptVersion" '<<' '1.0.9.2~'; then
128 cat >> "$targetDir/etc/apt/apt.conf.d/docker-gzip-indexes" <<-'EOF'
129
130 # https://salsa.debian.org/apt-team/apt/commit/b0f4b486e6850c5f98520ccf19da71d0ed748ae4; released in src:apt 1.0.9.2, 2014-10-02
131 # prior to src:apt 1.0.9.2, "Acquire::GzipIndexes" _only_ applied to gzip-compressed list files, so we need to prefer those on older releases
132 Acquire::CompressionTypes::Order:: "gz";
133 EOF
134 if [ -n "$isDebianJessie" ]; then
135 cat >> "$targetDir/etc/apt/apt.conf.d/docker-gzip-indexes" <<-'EOF'
136 # see also https://github.com/debuerreotype/debuerreotype/issues/41 (details of a bug that's apparently specific to Debian Jessie)
137 EOF
138 fi
139 fi
140 chmod 0644 "$targetDir/etc/apt/apt.conf.d/docker-gzip-indexes"
141 "$thisDir/.fix-apt-comments.sh" "$aptVersion" "$targetDir/etc/apt/apt.conf.d/docker-gzip-indexes"
142
143 # remove apt-cache translations for faster "apt-get update"
144 cat > "$targetDir/etc/apt/apt.conf.d/docker-no-languages" <<-'EOF'
145 # In Docker, we don't often need the "Translations" files, so we're just wasting
146 # time and space by downloading them, and this inhibits that. For users that do
147 # need them, it's a simple matter to delete this file and "apt-get update". :)
148
149 Acquire::Languages "none";
150 EOF
151 chmod 0644 "$targetDir/etc/apt/apt.conf.d/docker-no-languages"
152 "$thisDir/.fix-apt-comments.sh" "$aptVersion" "$targetDir/etc/apt/apt.conf.d/docker-no-languages"
153 fi