Skip to content

Commit 644ee1a

Browse files
committed
Adjust tarball creation to be reproducible
1 parent e704abc commit 644ee1a

File tree

8 files changed

+146
-25
lines changed

8 files changed

+146
-25
lines changed

Dockerfile-builder.template

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ RUN set -eux; \
1313
musl-dev \
1414
patch \
1515
tzdata \
16+
# busybox's tar ironically does not maintain mtime of directories correctly (which we need for SOURCE_DATE_EPOCH / reproducibility)
17+
tar \
1618
;
1719
{{ ) else ( -}}
1820
FROM debian:bookworm-slim
@@ -234,22 +236,36 @@ RUN set -eux; \
234236
curl -fL -o busybox.tar.bz2 "https://busybox.net/downloads/$tarball"; \
235237
echo "$BUSYBOX_SHA256 *busybox.tar.bz2" | sha256sum -c -; \
236238
gpg --batch --verify busybox.tar.bz2.sig busybox.tar.bz2; \
237-
mkdir -p /usr/src/busybox; \
238-
tar -xf busybox.tar.bz2 -C /usr/src/busybox --strip-components 1; \
239-
rm busybox.tar.bz2*
239+
# Alpine... 😅
240+
mkdir -p /usr/src; \
241+
tar -xf busybox.tar.bz2 -C /usr/src "busybox-$BUSYBOX_VERSION"; \
242+
mv "/usr/src/busybox-$BUSYBOX_VERSION" /usr/src/busybox; \
243+
rm busybox.tar.bz2*; \
244+
\
245+
# save the tarball's filesystem timestamp persistently (in case building busybox modifies it) so we can use it for reproducible rootfs later
246+
SOURCE_DATE_EPOCH="$(stat -c '%Y' /usr/src/busybox | tee /usr/src/busybox.SOURCE_DATE_EPOCH)"; \
247+
date="$(date -d "@$SOURCE_DATE_EPOCH" '+%Y%m%d%H%M.%S')"; \
248+
touch -t "$date" /usr/src/busybox.SOURCE_DATE_EPOCH; \
249+
# for logging validation/edification
250+
date --date "@$SOURCE_DATE_EPOCH" --rfc-2822
240251

241252
WORKDIR /usr/src/busybox
242253

243254
RUN set -eux; \
244255
\
256+
# build date/time gets embedded in the BusyBox binary -- SOURCE_DATE_EPOCH should override that
257+
SOURCE_DATE_EPOCH="$(cat /usr/src/busybox.SOURCE_DATE_EPOCH)"; \
258+
export SOURCE_DATE_EPOCH; \
259+
# (has to be set in the config stage for making sure "AUTOCONF_TIMESTAMP" is embedded correctly)
260+
\
245261
setConfs=' \
246262
CONFIG_AR=y \
247263
CONFIG_FEATURE_AR_CREATE=y \
248264
CONFIG_FEATURE_AR_LONG_FILENAMES=y \
249265
# CONFIG_LAST_SUPPORTED_WCHAR: see https://github.com/docker-library/busybox/issues/13 (UTF-8 input)
250266
CONFIG_LAST_SUPPORTED_WCHAR=0 \
251267
{{ if env.variant == "glibc" then ( -}}
252-
# As long as we rely on libnss (see below), we have to have libc.so anyhow, so we've removed CONFIG_STATIC here... :cry:
268+
# As long as we rely on libnss (see below), we have to have libc.so anyhow, so we've removed CONFIG_STATIC here... 😭
253269
{{ ) else ( -}}
254270
CONFIG_STATIC=y \
255271
{{ ) end -}}
@@ -361,6 +377,7 @@ RUN set -eux; \
361377
{{ ) else "" end -}}
362378
chroot rootfs /bin/getconf _NPROCESSORS_ONLN; \
363379
\
380+
# TODO make this create symlinks instead so the output tarball is cleaner (but "-s" outputs absolute symlinks which is kind of annoying to deal with -- we should also consider letting busybox determine the "install paths"; see "busybox --list-full")
364381
chroot rootfs /bin/busybox --install /bin
365382
366383
# install a few extra files from buildroot (/etc/passwd, etc)

build.sh

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,17 @@ for dir; do
1313
(
1414
set -x
1515
docker build -t "$base-builder" -f "$dir/Dockerfile.builder" "$dir"
16-
docker run --rm "$base-builder" tar cC rootfs . | xz -T0 -z9 > "$dir/busybox.tar.xz"
16+
docker run --rm "$base-builder" \
17+
tar \
18+
--create \
19+
--directory rootfs \
20+
--numeric-owner \
21+
--transform 's,^./,,' \
22+
--sort name \
23+
--mtime /usr/src/busybox.SOURCE_DATE_EPOCH --clamp-mtime \
24+
. \
25+
| xz -T0 -z9 > "$dir/busybox.tar.xz"
26+
sha256sum "$dir/busybox.tar.xz"
1727
docker build -t "$base-test" "$dir"
1828
docker run --rm "$base-test" sh -xec 'true'
1929

latest-1/glibc/Dockerfile.builder

Lines changed: 19 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

latest-1/musl/Dockerfile.builder

Lines changed: 20 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

latest-1/uclibc/Dockerfile.builder

Lines changed: 18 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

latest/glibc/Dockerfile.builder

Lines changed: 19 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

latest/musl/Dockerfile.builder

Lines changed: 20 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

latest/uclibc/Dockerfile.builder

Lines changed: 18 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)