-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDockerfile.qemu
More file actions
194 lines (174 loc) · 6.9 KB
/
Dockerfile.qemu
File metadata and controls
194 lines (174 loc) · 6.9 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
# Dockerfile for building a static qemu-system-x86_64 binary (libc-compatible)
# Target: microvm development environments
FROM ubuntu:25.10 AS builder
# Build arguments
ARG QEMU_VERSION=10.2.0
ARG JOBS=8
ARG DEBIAN_FRONTEND=noninteractive
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
# Configure apt to keep downloaded packages for cache mounts
RUN rm -f /etc/apt/apt.conf.d/docker-clean && \
echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache
# Install build dependencies
RUN --mount=type=cache,sharing=locked,id=qemu-aptlib,target=/var/lib/apt \
--mount=type=cache,sharing=locked,id=qemu-aptcache,target=/var/cache/apt \
apt-get update && apt-get upgrade -y && \
apt-get --no-install-recommends install -y \
# Core build tools
build-essential \
meson \
ninja-build \
pkg-config \
python3 \
python3-venv \
python3-tomli \
bash \
coreutils \
curl \
ca-certificates \
xz-utils \
# Required libraries (dev packages for static linking)
libglib2.0-dev \
libglib2.0-0 \
zlib1g-dev \
libpixman-1-dev \
# For virtio and networking
linux-libc-dev \
libcap-ng-dev \
libseccomp-dev \
libseccomp2 \
# Additional useful dependencies
librdmacm-dev \
acpica-tools \
libbpf-dev \
libpmem-dev \
libaio-dev \
liburing-dev \
git \
flex \
bison \
autoconf \
automake \
libtool \
libffi-dev \
libelf-dev \
libdw-dev \
valgrind \
linux-libc-dev \
patch && \
apt-get clean && rm -rf /var/lib/apt/lists/*
WORKDIR /build
# Download and extract QEMU source (cached across builds)
# Store both tarball and extracted source in cache mount for maximum reuse
RUN --mount=type=cache,sharing=locked,id=qemu-src-${QEMU_VERSION},target=/var/cache/qemu \
if [ ! -f "/var/cache/qemu/qemu-${QEMU_VERSION}.tar.xz" ]; then \
echo "Downloading QEMU ${QEMU_VERSION} source..."; \
curl -fsSL "https://download.qemu.org/qemu-${QEMU_VERSION}.tar.xz" -o "/var/cache/qemu/qemu-${QEMU_VERSION}.tar.xz"; \
else \
echo "Using cached QEMU ${QEMU_VERSION} source tarball"; \
fi && \
if [ ! -d "/var/cache/qemu/qemu-${QEMU_VERSION}" ]; then \
echo "Extracting QEMU source..."; \
tar -xf "/var/cache/qemu/qemu-${QEMU_VERSION}.tar.xz" -C /var/cache/qemu; \
else \
echo "Using cached extracted QEMU source"; \
fi
# Configure, build, and install QEMU with incremental build support
# Only run configure if Makefile doesn't exist
# Combine all steps in single RUN to ensure cache mount consistency
RUN --mount=type=cache,sharing=locked,id=qemu-src-${QEMU_VERSION},target=/var/cache/qemu \
--mount=type=cache,sharing=locked,id=qemu-build-${QEMU_VERSION},target=/build/qemu-build \
cd qemu-build && \
if [ ! -f Makefile ]; then \
echo "Running QEMU configure (first build)..."; \
/var/cache/qemu/qemu-${QEMU_VERSION}/configure \
--prefix=/opt/qemu \
--target-list=x86_64-softmmu \
--enable-tools \
--enable-lto \
--enable-strip \
--disable-containers \
--disable-docs \
--disable-debug-info \
--disable-werror \
--disable-sdl \
--disable-gtk \
--disable-opengl \
--disable-virglrenderer \
--disable-spice \
--disable-vnc \
--disable-alsa \
--disable-coreaudio \
--disable-oss \
--disable-pa \
--disable-jack \
--disable-sndio \
--enable-kvm \
--enable-virtfs \
--enable-linux-aio \
--enable-linux-io-uring \
--enable-cap-ng \
--enable-seccomp \
--disable-slirp \
--disable-gio; \
else \
echo "Using cached QEMU build (Makefile exists, skipping configure)"; \
fi && \
make -j${JOBS} && \
make install
# Copy BIOS/firmware files and verify
# Also copy source to regular directory for later COPY commands (cache mount not available in later stages)
RUN --mount=type=cache,sharing=locked,id=qemu-src-${QEMU_VERSION},target=/var/cache/qemu \
cp -a /var/cache/qemu/qemu-${QEMU_VERSION}/pc-bios/* /opt/qemu/share/qemu/ && \
if [ ! -f /opt/qemu/share/qemu/bios-256k.bin ]; then \
install -m 0644 /var/cache/qemu/qemu-${QEMU_VERSION}/pc-bios/bios-256k.bin /opt/qemu/share/qemu/bios-256k.bin; \
fi && \
test -f /opt/qemu/share/qemu/bios-256k.bin && \
ls -la /opt/qemu/share/qemu && \
mkdir -p /build/pc-bios && \
cp /var/cache/qemu/qemu-${QEMU_VERSION}/pc-bios/kvmvapic.bin /build/pc-bios/ && \
cp /var/cache/qemu/qemu-${QEMU_VERSION}/pc-bios/vgabios-stdvga.bin /build/pc-bios/
# Verify binary type and show dynamic library dependencies
RUN file /opt/qemu/bin/qemu-system-x86_64 && \
echo "Dynamic library dependencies:" && \
ldd /opt/qemu/bin/qemu-system-x86_64
# ============================================
# Final minimal image with binary and dependencies
# ============================================
FROM ubuntu:25.10 AS runtime
ARG DEBIAN_FRONTEND=noninteractive
# Configure apt to keep downloaded packages for cache mounts
RUN rm -f /etc/apt/apt.conf.d/docker-clean && \
echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache
# Install only runtime libraries required by QEMU
RUN --mount=type=cache,sharing=locked,id=qemu-runtime-aptlib,target=/var/lib/apt \
--mount=type=cache,sharing=locked,id=qemu-runtime-aptcache,target=/var/cache/apt \
apt-get update && \
apt-get --no-install-recommends install -y \
libglib2.0-0 \
libpixman-1-0 \
libcap-ng0 \
libseccomp2 \
libaio1 \
liburing2 \
libpmem1 \
librdmacm1t64 \
zlib1g && \
apt-get clean && rm -rf /var/lib/apt/lists/*
# Copy QEMU binaries
COPY --from=builder /opt/qemu/bin/qemu-system-x86_64 /usr/local/bin/qemu-system-x86_64
# Copy BIOS/firmware files needed for boot
COPY --from=builder /opt/qemu/share/qemu/ /usr/share/spin-stack/qemu/
ENTRYPOINT ["/usr/local/bin/qemu-system-x86_64"]
# ============================================
# Alternative: Extract binaries to host
# ============================================
FROM scratch AS extract
# Copy QEMU binaries
COPY --from=builder /opt/qemu/bin/qemu-system-x86_64 /bin/qemu-system-x86_64
# Copy BIOS/firmware files needed for boot
COPY --from=builder /opt/qemu/share/qemu/bios.bin /share/spin-stack/qemu/bios.bin
COPY --from=builder /opt/qemu/share/qemu/bios-256k.bin /share/spin-stack/qemu/bios-256k.bin
COPY --from=builder /opt/qemu/share/qemu/pvh.bin /share/spin-stack/qemu/pvh.bin
COPY --from=builder /build/pc-bios/kvmvapic.bin /share/spin-stack/qemu/kvmvapic.bin
COPY --from=builder /build/pc-bios/vgabios-stdvga.bin /share/spin-stack/qemu/vgabios-stdvga.bin