-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dockerfile.debian
466 lines (437 loc) · 30.9 KB
/
Dockerfile.debian
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# Host Docker image.
###############################################################################
FROM --platform=${TARGETPLATFORM} debian:bullseye-slim AS host
# Set environment variables.
ENV LANG=POSIX
ENV LANGUAGE=POSIX
ENV LC_ALL=POSIX
ENV TZ=UTC
# Install debootstrap.
RUN apt-get update && apt-get install -y debootstrap xz-utils && apt-get clean
# Install host certificates.
RUN apt-get update && apt-get install -y wget ca-certificates && apt-get clean
# Prepare target.
ENV CHTEMP="/mnt/chtemp"
ENV CHROOT="/mnt/chroot"
ENV CHUSER="/mnt/chuser"
ENV CHCERT="/mnt/chcert"
RUN mkdir -p "${CHTEMP}/tmp" && chmod 1777 "${CHTEMP}/tmp"
RUN mkdir -p "${CHROOT}/tmp" && chmod 1777 "${CHROOT}/tmp"
# Define arguments.
ARG ARCH
ARG VERSION
ARG TARGETPLATFORM
# Apply debootstrap.
RUN case "${TARGETPLATFORM}" in \
linux/386) \
ARCH=i386 \
;; \
linux/amd64) \
ARCH=amd64 \
;; \
*) \
echo "E: unsupported target platform: ${TARGETPLATFORM}" \
exit 1 \
;; \
esac; \
case "${VERSION}" in \
4|etch) \
ARCHIVE=http://archive.debian.org/debian \
VERSION=etch \
INCLUDE=$(echo " \
dash \
patch \
") \
EXCLUDE=$(echo " \
bsdutils \
dselect \
e2fslibs \
e2fsprogs \
hostname \
initscripts \
libblkid1 \
libdevmapper1.02 \
libslang2 \
libss2 \
libuuid1 \
lsb-base \
mount \
ncurses-base \
ncurses-bin \
procps \
sysvinit \
util-linux \
" | xargs) \
;; \
5|lenny) \
ARCHIVE=http://archive.debian.org/debian \
VERSION=lenny \
INCLUDE=$(echo " \
dash \
patch \
") \
EXCLUDE=$(echo " \
bsdutils \
dselect \
e2fslibs \
e2fsprogs \
gcc-4.2-base \
hostname \
initscripts \
libblkid1 \
libdevmapper1.02.1 \
libcap1 \
libslang2 \
libss2 \
libuuid1 \
lsb-base \
mount \
ncurses-base \
ncurses-bin \
procps \
sysvinit \
util-linux \
" | xargs) \
;; \
6|squeeze) \
ARCHIVE=http://archive.debian.org/debian \
VERSION=squeeze \
INCLUDE=$(echo " \
dash \
patch \
") \
EXCLUDE=$(echo " \
bsdutils \
dselect \
e2fslibs \
e2fsprogs \
hostname \
initscripts \
libblkid1 \
libss2 \
libuuid1 \
lsb-base \
mount \
ncurses-base \
ncurses-bin \
procps \
sysvinit \
util-linux \
" | xargs) \
;; \
7|wheezy) \
ARCHIVE=http://archive.debian.org/debian \
VERSION=wheezy \
INCLUDE=$(echo " \
dash \
patch \
") \
EXCLUDE=$(echo " \
bsdutils \
dselect \
e2fslibs \
e2fsprogs \
hostname \
initscripts \
libblkid1 \
libmount1 \
libss2 \
libuuid1 \
lsb-base \
mount \
ncurses-base \
ncurses-bin \
procps \
sysvinit \
util-linux \
xz-utils \
" | xargs) \
;; \
8|jessie) \
ARCHIVE=http://archive.debian.org/debian \
VERSION=jessie \
INCLUDE=$(echo " \
dash \
patch \
") \
EXCLUDE=$(echo " \
bsdutils \
dselect \
e2fslibs \
e2fsprogs \
gcc-4.8-base \
hostname \
init \
initscripts \
libblkid1 \
libmount1 \
libsmartcols1 \
libss2 \
libuuid1 \
lsb-base \
mount \
ncurses-base \
ncurses-bin \
systemd \
systemd-sysv \
util-linux \
" | xargs) \
;; \
9|stretch) \
ARCHIVE=http://archive.debian.org/debian \
VERSION=stretch \
INCLUDE=$(echo " \
dash \
patch \
") \
EXCLUDE=$(echo " \
bsdutils \
e2fslibs \
e2fsprogs \
hostname \
libblkid1 \
libfdisk1 \
libmount1 \
libsmartcols1 \
libss2 \
libuuid1 \
mount \
ncurses-base \
ncurses-bin \
sysvinit-utils \
util-linux \
" | xargs) \
;; \
10|buster) \
ARCHIVE=http://deb.debian.org/debian \
VERSION=buster \
INCLUDE=$(echo " \
dash \
patch \
") \
EXCLUDE=$(echo " \
bsdutils \
e2fsprogs \
fdisk \
hostname \
libblkid1 \
libfdisk1 \
libmount1 \
libsmartcols1 \
libss2 \
libuuid1 \
mount \
ncurses-base \
ncurses-bin \
sysvinit-utils \
util-linux \
" | xargs) \
;; \
esac; \
debootstrap --arch=${ARCH} --variant=minbase --no-check-gpg \
--exclude="${EXCLUDE}" --include="${INCLUDE}" \
${VERSION} ${CHROOT} ${ARCHIVE}
# Copy host timezone to target.
RUN echo ${TZ} > ${CHROOT}/etc/timezone
# Copy `/etc/profile` from outside and delete root profiles in target.
COPY files/etc/profile ${CHROOT}/etc/profile
RUN rm -f ${CHROOT}/root/.profile
RUN rm -f ${CHROOT}/root/.bashrc
# Add special configuration for `apt`.
COPY files/etc/apt ${CHROOT}/etc/apt
# Add updates repository to target.
RUN chroot ${CHROOT} sh -c ' \
distro_name=$(cat /etc/apt/sources.list | head -n1 | cut -d" " -f3) \
; case ${VERSION} in \
4|5|6|7|8|9) ARCHIVE=http://archive.debian.org/debian-security ;; \
*) ARCHIVE=http://deb.debian.org/debian-security ;; \
esac \
; echo "deb ${ARCHIVE} ${distro_name}/updates main" \
>> /etc/apt/sources.list \
'
# Upgrade target packages if possible.
RUN chroot ${CHROOT} sh -c ' \
chmod 1777 /tmp \
; apt-get update && apt-get upgrade -y && apt-get clean \
'
# Replace bash with dash (cheat the system with a symlink if needed).
RUN chroot ${CHROOT} sh -c ' \
echo "dash dash/sh boolean true" | debconf-set-selections \
; yes | dpkg-reconfigure dash \
; echo "Yes, do as I say!" | \
apt-get remove --purge -y --force-yes bash \
'
# Fix `ldd` to be dash-compliant.
RUN sed -i 's|$"|"|; s|#! /bin/bash|#! /bin/sh|' ${CHROOT}/usr/bin/ldd
RUN sed -i 's|if \(set -o pipefail 2> /dev/null; then\)|if [ ! -z "${BASH_VERSION%%.*}" ] \&\& [ "${BASH_VERSION%%.*}" -ge 3 ] \&\& \1|' ${CHROOT}/usr/bin/ldd
# Remove login and libpam modules if still there.
RUN chroot ${CHROOT} sh -c ' \
pkgs="login libpam0g libpam-modules libpam-runtime" \
; case "${VERSION}" in \
4) pkgs="${pkgs} libdb4.3 libcap1" ;; \
5) pkgs="${pkgs} libdb4.6" ;; \
6) pkgs="${pkgs} libdb4.8" ;; \
7) pkgs="${pkgs} libdb5.1" ;; \
8) pkgs="${pkgs} libdb5.3" ;; \
*) pkgs="" ;; \
esac \
; echo "#! /bin/sh" > /var/lib/dpkg/info/libpam-runtime.prerm \
; echo "Yes, do as I say!" | \
apt-get remove --purge -y --force-yes ${pkgs} \
'
# Remove passwd if still there.
RUN chroot ${CHROOT} sh -c ' \
case "${VERSION}" in \
4|5) pkgs="passwd" ;; \
7|8) pkgs="libsemanage-common libsemanage1 libustr-1.0-1" ;; \
9) pkgs="libtinfo5" ;; \
10) pkgs="libtinfo6" ;; \
*) pkgs="" ;; \
esac \
; if [ ! -z "${pkgs}" ]; then \
echo "Yes, do as I say!" | \
apt-get remove --purge -y --force-yes ${pkgs}; \
fi \
'
# Copy `getopt` program manually extracted from `util-linux`.
RUN chroot ${CHROOT} sh -c ' \
apt-get update \
; apt-get install -y --download-only util-linux \
; mkdir -p /usr/bin /usr/share/doc/util-linux \
; dpkg-deb -x /var/cache/apt/archives/util-linux*.deb /tmp \
; cp /tmp/usr/bin/getopt /usr/bin/ \
; cp /tmp/usr/share/doc/util-linux/copyright /usr/share/doc/util-linux/ \
; rm -rf /tmp/* \
'
# Convert some hardlinks into symlinks.
RUN chroot ${CHROOT} sh -c ' \
perlpath=$(find /usr/bin -type f -name "perl5*") \
; if [ ! -z "${perlpath}" ]; then \
rm -rf /usr/bin/perl; \
ln -s ${perlpath} /usr/bin/perl; \
fi \
'
# Configure CA certificates location for hypothetical `git` and `wget`.
COPY files/etc/gitconfig ${CHROOT}/etc/gitconfig
COPY files/etc/wgetrc ${CHROOT}/etc/wgetrc
# Remove bulky files in target unless mandatory.
RUN chroot ${CHROOT} sh -c ' \
# Clean docs, lintian files, locales and manpages.
sh /etc/apt/apt.conf.d.scripts/99docker_dpkg_postinvoke.sh \
# Remove unused folders.
; rm -rf \
/etc/cron.* \
/etc/logrotate.d \
/usr/share/emacs \
/usr/share/vim \
/usr/share/zoneinfo/* \
# Remove games folders.
; rm -rf \
/usr/games \
/usr/local/games \
# Remove apt cache and logs.
; find \
/var/cache/apt \
/var/lib/apt/lists \
/var/log \
-type f | xargs rm -f \
; find / -regex ".*[~-]$" | xargs rm -rf \
# Remove non-deterministic files.
; rm -rf \
/etc/apt/trustdb.gpg \
/var/cache/ldconfig/aux-cache \
# Remove specific binary file on Debian Lenny.
; rm -rf /usr/bin/oldfind \
# Remove specific contents of `resolv.conf` and leave it empty.
; cp /dev/null /etc/resolv.conf \
# Remove specific system folder contents.
; rm -rf /dev/* /proc/* /sys/* \
'
# Fix non-determinism due to modification times.
ARG SOURCE_DATE_EPOCH=0
RUN sed -i 's/^\(\([^:]*:\)\{2\}\)\([^:]*:\)\(.*\)$/\1'"$((${SOURCE_DATE_EPOCH} / 86400))"':\4/g' ${CHROOT}/etc/shadow
RUN find -L "${CHROOT}" "${CHTEMP}" | xargs touch -cham -d "@${SOURCE_DATE_EPOCH}"
# Retrieve CA certificates for later.
RUN true \
; CALINK="https://curl.se/ca/cacert-2023-12-12.pem" \
; CAFILE="${CHCERT}/etc/ssl/cert.pem" \
; mkdir -p "$(dirname ${CAFILE})" \
; wget -O "${CAFILE}" -S "${CALINK}" && chmod 444 "${CAFILE}" \
; find -L "${CHCERT}" -type d | xargs touch -cham -d "@${SOURCE_DATE_EPOCH}"
# Retrieve static `wget` for later.
RUN case "${TARGETPLATFORM}" in \
linux/386) \
WGORIG="wget_busybox_i686_x86_gcc_Linux" \
;; \
linux/amd64) \
WGORIG="wget_busybox_amd_x86_64_gcc_Linux" \
;; \
*) \
echo "E: unsupported target platform: ${TARGETPLATFORM}" \
exit 1 \
;; \
esac \
; ROOTBASE="https://raw.githubusercontent.com/Azathothas/Static-Binaries" \
; ROOTVERS="baseutils_2023_12_15_07_02_58_AM" \
; ROOTDATE=1702623778 \
; CHBINDIR="${CHUSER}/usr/local/bin" \
; CHDOCDIR="${CHUSER}/usr/local/share/doc" \
; WGBIN="$(echo $WGORIG | cut -d_ -f1)" \
; WGURL="${ROOTBASE}/${ROOTVERS}/${WGBIN}/${WGORIG}" \
; WGDST="${CHBINDIR}/${WGORIG}" \
; WGLNK="${CHBINDIR}/${WGBIN}" \
; mkdir -p "${CHBINDIR}" \
; wget -O "${WGDST}" -S "${WGURL}" && chmod 777 "${WGDST}" \
; cd "${CHBINDIR}" && ln -s "${WGORIG}" "${WGBIN}" \
; WGTAG="$(wget -O- ${ROOTBASE}/${ROOTVERS}/${WGBIN}/version.txt 2>/dev/null | tr _ .)" \
; WGCPY="${CHDOCDIR}/busybox/copyright" \
; mkdir -p "$(dirname ${WGCPY})" \
; { \
echo "The BusyBox package was assembled from sources obtained from:" ;\
echo " https://busybox.net/downloads/busybox-${WGTAG}.tar.bz2" ;\
echo "" ;\
echo "BusyBox is an aggregate of multiple packages. These packages are" ;\
echo "copyrighted by their respective authors." ;\
echo "" ;\
echo "License:" ;\
echo "" ;\
echo " This package is free software; you can redistribute it and/or modify" ;\
echo " it under the terms of the GNU General Public License as published by" ;\
echo " the Free Software Foundation; version 2 dated June, 1991." ;\
echo "" ;\
echo " This package is distributed in the hope that it will be useful," ;\
echo " but WITHOUT ANY WARRANTY; without even the implied warranty of" ;\
echo " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the" ;\
echo " GNU General Public License for more details." ;\
echo "" ;\
echo " You should have received a copy of the GNU General Public License" ;\
echo " along with this package; if not, write to the Free Software" ;\
echo " Foundation, Inc., 51 Franklin St, Fifth Floor, Boston," ;\
echo " MA 02110-1301, USA." ;\
echo "" ;\
echo "On Debian GNU/Linux systems, the complete text of the GNU General" ;\
echo "Public License can be found in \`/usr/share/common-licenses/GPL-2'." ;\
} > "${WGCPY}" \
; find -L "${CHUSER}" -type f | xargs touch -cham -d "@${ROOTDATE}" \
; find -L "${CHUSER}" -type d | xargs touch -cham -d "@${SOURCE_DATE_EPOCH}"
###############################################################################
# Target Docker image.
###############################################################################
FROM --platform=${TARGETPLATFORM} scratch AS tinybases-debian
# Set environment variables.
ENV ENV="/etc/profile"
ENV LANG=POSIX
ENV LANGUAGE=POSIX
ENV LC_ALL=POSIX
ENV PAGER=cat
ENV TZ=UTC
# Copy rootfs from host.
COPY --from=host /mnt/chroot /
COPY --from=host --chmod=1777 /mnt/chtemp /
# Inject the static `wget` into target.
COPY --from=host /mnt/chuser /
# Inject the CA certificates into target.
COPY --from=host /mnt/chcert /
# Launch shell.
CMD ["sh"]
###############################################################################