-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDockerfile
More file actions
258 lines (216 loc) · 8.53 KB
/
Dockerfile
File metadata and controls
258 lines (216 loc) · 8.53 KB
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
# syntax=docker/dockerfile:1.4
# =============================================================================
# Multi-stage Dockerfile for Building Custom Linux Distribution
# =============================================================================
FROM debian:sid-slim AS builder-base
ARG LINUX_VERSION=master
ARG BUSYBOX_VERSION=master
ARG SYSLINUX_VERSION=6.03
ARG DEBIAN_FRONTEND=noninteractive
ARG BUILD_JOBS=
LABEL maintainer="handbuilt-linux-project"
LABEL description="Custom Linux distribution builder"
LABEL version="1.0.0"
# Install build dependencies
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
apt-get update && \
apt-get install -yq --no-install-recommends \
build-essential \
bzip2 \
git \
make \
gcc \
libncurses-dev \
flex \
bison \
bc \
cpio \
libelf-dev \
libssl-dev \
syslinux-common \
dosfstools \
genisoimage \
wget \
curl \
ca-certificates \
xz-utils && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
WORKDIR /build
# -----------------------------------------------------------------------------
# Stage: source-downloader
# -----------------------------------------------------------------------------
FROM builder-base AS source-downloader
ARG LINUX_VERSION=master
ARG BUSYBOX_VERSION=master
ARG SYSLINUX_VERSION=6.03
RUN mkdir -p /build/initramfs && \
mkdir -p /build/myiso/isolinux && \
mkdir -p /build/sources && \
mkdir -p /build/cache
WORKDIR /build/sources
# Clone Linux (shallow, branch/tag from ARG)
RUN --mount=type=cache,target=/build/cache \
if [ ! -d /build/cache/linux ]; then \
git clone --depth 1 --branch "${LINUX_VERSION}" https://github.com/torvalds/linux.git linux || \
git clone --depth 1 https://github.com/torvalds/linux.git linux; \
cp -r linux /build/cache/linux; \
else \
cp -r /build/cache/linux linux; \
fi
# Clone BusyBox (shallow, branch/tag from ARG)
WORKDIR /build/sources
RUN --mount=type=cache,target=/build/cache \
if [ ! -d /build/cache/busybox ]; then \
git clone --depth 1 --branch "${BUSYBOX_VERSION}" https://git.busybox.net/busybox busybox || \
git clone --depth 1 https://git.busybox.net/busybox busybox; \
cp -r busybox /build/cache/busybox; \
else \
cp -r /build/cache/busybox busybox; \
fi
# Download and extract Syslinux (tries multiple URLs)
WORKDIR /build/sources
RUN set -eux; \
tried=0; \
for url in \
"https://mirrors.edge.kernel.org/pub/linux/utils/boot/syslinux/syslinux-${SYSLINUX_VERSION}.tar.gz" \
"https://www.kernel.org/pub/linux/utils/boot/syslinux/syslinux-${SYSLINUX_VERSION}.tar.gz" \
"https://mirrors.edge.kernel.org/pub/linux/utils/boot/syslinux/Testing/${SYSLINUX_VERSION}/syslinux-${SYSLINUX_VERSION}.tar.gz"; do \
echo "Trying $url"; \
if curl -fsSL -o syslinux.tar.gz "$url"; then \
tried=1; \
break; \
else \
echo "Failed to download from $url"; \
fi; \
done; \
if [ "$tried" -ne 1 ]; then \
echo "ERROR: Unable to download syslinux ${SYSLINUX_VERSION}" >&2; \
exit 22; \
fi; \
tar xzf syslinux.tar.gz && rm syslinux.tar.gz && mv "syslinux-${SYSLINUX_VERSION}" syslinux
# -----------------------------------------------------------------------------
# Stage: kernel-builder
# -----------------------------------------------------------------------------
FROM builder-base AS kernel-builder
ARG BUILD_JOBS
ARG LINUX_VERSION=master
COPY --from=source-downloader /build/sources/linux /build/linux
# If you have a linux.config in repo, it will be copied by the build context
COPY linux.config /build/linux/.config
WORKDIR /build/linux
RUN make olddefconfig && \
make -j"${BUILD_JOBS:-$(nproc)}" && \
(strip --strip-debug arch/x86/boot/bzImage 2>/dev/null || true)
# -----------------------------------------------------------------------------
# Stage: busybox-builder
# -----------------------------------------------------------------------------
FROM builder-base AS busybox-builder
ARG BUILD_JOBS
ARG BUSYBOX_VERSION=master
COPY --from=source-downloader /build/sources/busybox /build/busybox
COPY busybox.config /build/busybox/.config
WORKDIR /build/busybox
# Use a robust config, build, install; create an explicit tarball artifact and verify it
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
RUN set -eux; \
mkdir -p /build/initramfs; \
# configure (prefer existing .config, accept defaults non-interactively)
if [ -f .config ]; then \
if ! yes "" | make oldconfig; then \
make defconfig; \
fi; \
else \
make defconfig; \
fi; \
# build
make -j"${BUILD_JOBS:-$(nproc)}"; \
# install into explicit path
make CONFIG_PREFIX=/build/initramfs install; \
# sanity checks — fail early with diagnostics if artifacts missing
if [ ! -x /build/initramfs/bin/busybox ]; then \
echo "ERROR: busybox install did not produce /build/initramfs/bin/busybox"; \
echo "workspace /build contents:"; ls -la /build || true; \
echo "busybox tree:"; ls -la /build/initramfs || true; \
exit 1; \
fi; \
# shrink binary if possible (non-fatal)
strip --strip-all /build/initramfs/bin/busybox || true; \
# create a single tarball artifact for reliable COPY by BuildKit
tar -C /build -czf /build/initramfs.tar.gz initramfs; \
ls -la /build/initramfs.tar.gz
# -----------------------------------------------------------------------------
# Stage: initramfs-builder
# -----------------------------------------------------------------------------
FROM builder-base AS initramfs-builder
# Copy the tarball artifact and extract it, then add/overwrite init
COPY --from=busybox-builder /build/initramfs.tar.gz /build/initramfs.tar.gz
WORKDIR /build
RUN set -eux; \
mkdir -p /build; \
tar -C /build -xzf /build/initramfs.tar.gz && rm /build/initramfs.tar.gz; \
ls -la /build/initramfs
COPY init.sh /build/initramfs/init
RUN chmod +x /build/initramfs/init
WORKDIR /build/initramfs
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
RUN find . -print0 | cpio --null --create --format=newc | gzip -9 > /build/initramfs.cpio.gz
# -----------------------------------------------------------------------------
# Stage: iso-builder
# -----------------------------------------------------------------------------
FROM builder-base AS iso-builder
ARG SYSLINUX_VERSION=6.03
COPY --from=source-downloader /build/sources/syslinux /build/syslinux
COPY --from=kernel-builder /build/linux/arch/x86/boot/bzImage /build/myiso/bzImage
COPY --from=initramfs-builder /build/initramfs.cpio.gz /build/myiso/initramfs
WORKDIR /build
RUN cp /build/syslinux/bios/core/isolinux.bin /build/myiso/isolinux/ && \
cp /build/syslinux/bios/com32/elflink/ldlinux/ldlinux.c32 /build/myiso/isolinux/
COPY syslinux.cfg /build/myiso/isolinux/isolinux.cfg
WORKDIR /build
RUN mkisofs \
-J \
-R \
-o output.iso \
-b isolinux/isolinux.bin \
-c isolinux/boot.cat \
-no-emul-boot \
-boot-load-size 4 \
-boot-info-table \
myiso
# -----------------------------------------------------------------------------
# Stage: export-stage
# -----------------------------------------------------------------------------
FROM scratch AS export-stage
COPY --from=iso-builder /build/output.iso /output.iso
COPY --from=kernel-builder /build/linux/arch/x86/boot/bzImage /bzImage
COPY --from=initramfs-builder /build/initramfs.cpio.gz /initramfs
# -----------------------------------------------------------------------------
# Stage: runtime
# -----------------------------------------------------------------------------
FROM debian:sid-slim AS runtime
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
apt-get update && \
apt-get install -yq --no-install-recommends \
qemu-system-x86 \
qemu-utils && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
RUN groupadd -r distro && \
useradd -r -g distro -d /distro -s /bin/bash distro && \
mkdir -p /distro && \
chown -R distro:distro /distro
COPY --from=iso-builder /build/output.iso /distro/output.iso
COPY --from=kernel-builder /build/linux/arch/x86/boot/bzImage /distro/bzImage
COPY --from=initramfs-builder /build/initramfs.cpio.gz /distro/initramfs
COPY --chown=distro:distro build.sh /distro/build.sh
RUN chmod +x /distro/build.sh
WORKDIR /distro
USER distro
ENV DISTRO_HOME=/distro
ENV PATH="${DISTRO_HOME}:${PATH}"
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD [ -f "/distro/output.iso" ] || exit 1
CMD ["/bin/bash"]