-
Notifications
You must be signed in to change notification settings - Fork 297
Expand file tree
/
Copy pathdocker.rs
More file actions
259 lines (217 loc) · 8.39 KB
/
docker.rs
File metadata and controls
259 lines (217 loc) · 8.39 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
259
//! Docker-based kernel build support.
//!
//! Provides a real, working Dockerfile for building a minimal Linux kernel
//! from source inside Docker. This enables reproducible, CI-friendly kernel
//! builds without requiring a local toolchain.
use std::path::{Path, PathBuf};
use std::process::Command;
use crate::config::MICROVM_KERNEL_CONFIG;
use crate::error::KernelError;
/// Default Linux kernel version to build.
pub const DEFAULT_KERNEL_VERSION: &str = "6.8.12";
/// Generate the Dockerfile content for building a Linux kernel.
///
/// The Dockerfile:
/// 1. Starts from Alpine 3.19 (small, fast package install)
/// 2. Installs the full GCC build toolchain + kernel build dependencies
/// 3. Downloads the specified kernel version from kernel.org
/// 4. Copies the RVF microVM kernel config
/// 5. Runs `make olddefconfig` to fill in defaults, then builds bzImage
/// 6. Multi-stage build: final image contains only the bzImage
pub fn generate_dockerfile(kernel_version: &str) -> String {
format!(
r#"# RVF MicroVM Kernel Builder
# Builds a minimal Linux kernel for Firecracker / QEMU microvm
# Generated by rvf-kernel
FROM alpine:3.19 AS builder
# Install kernel build dependencies
RUN apk add --no-cache \
build-base \
linux-headers \
bc \
flex \
bison \
elfutils-dev \
openssl-dev \
perl \
python3 \
cpio \
gzip \
wget \
xz
# Download and extract kernel source
ARG KERNEL_VERSION={kernel_version}
RUN wget -q "https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-${{KERNEL_VERSION}}.tar.xz" && \
tar xf "linux-${{KERNEL_VERSION}}.tar.xz" && \
rm "linux-${{KERNEL_VERSION}}.tar.xz"
# Copy kernel configuration
COPY kernel.config "linux-${{KERNEL_VERSION}}/.config"
WORKDIR "/linux-${{KERNEL_VERSION}}"
# Apply config defaults and build
RUN make olddefconfig && \
make -j"$(nproc)" bzImage
# Extract just the bzImage in a scratch stage
FROM scratch
COPY --from=builder "/linux-{kernel_version}/arch/x86/boot/bzImage" /bzImage
"#
)
}
/// Context directory structure for a Docker kernel build.
pub struct DockerBuildContext {
/// Path to the build context directory.
pub context_dir: PathBuf,
/// Kernel version being built.
pub kernel_version: String,
}
impl DockerBuildContext {
/// Prepare a Docker build context directory with the Dockerfile and kernel config.
///
/// Creates:
/// - `<context_dir>/Dockerfile`
/// - `<context_dir>/kernel.config`
pub fn prepare(context_dir: &Path, kernel_version: Option<&str>) -> Result<Self, KernelError> {
let version = kernel_version.unwrap_or(DEFAULT_KERNEL_VERSION);
std::fs::create_dir_all(context_dir)?;
// Write Dockerfile
let dockerfile = generate_dockerfile(version);
std::fs::write(context_dir.join("Dockerfile"), dockerfile)?;
// Write kernel config
std::fs::write(context_dir.join("kernel.config"), MICROVM_KERNEL_CONFIG)?;
Ok(Self {
context_dir: context_dir.to_path_buf(),
kernel_version: version.to_string(),
})
}
/// Execute the Docker build and extract the resulting bzImage.
///
/// Requires Docker to be installed and accessible. The build may take
/// 10-30 minutes depending on CPU and network speed.
///
/// Returns the bzImage bytes on success.
pub fn build(&self) -> Result<Vec<u8>, KernelError> {
let image_tag = format!("rvf-kernel-build:{}", self.kernel_version);
// Build the Docker image
let build_status = Command::new("docker")
.args([
"build",
"-t",
&image_tag,
"--build-arg",
&format!("KERNEL_VERSION={}", self.kernel_version),
".",
])
.current_dir(&self.context_dir)
.status()
.map_err(|e| KernelError::DockerBuildFailed(format!("failed to run docker: {e}")))?;
if !build_status.success() {
return Err(KernelError::DockerBuildFailed(format!(
"docker build exited with status {}",
build_status
)));
}
// Clean up any leftover container from a previous run
let _ = Command::new("docker")
.args(["rm", "-f", "rvf-kernel-extract"])
.output();
// Create a temporary container to copy out the bzImage.
// The image is FROM scratch (no shell), so we pass a dummy
// entrypoint that won't be executed — docker create only
// creates the container filesystem, it doesn't run anything.
let create_output = Command::new("docker")
.args([
"create",
"--name",
"rvf-kernel-extract",
"--entrypoint",
"",
&image_tag,
"/bzImage",
])
.output()
.map_err(|e| KernelError::DockerBuildFailed(format!("docker create failed: {e}")))?;
if !create_output.status.success() {
let stderr = String::from_utf8_lossy(&create_output.stderr);
return Err(KernelError::DockerBuildFailed(format!(
"docker create failed: {stderr}"
)));
}
let bzimage_path = self.context_dir.join("bzImage");
let cp_status = Command::new("docker")
.args([
"cp",
"rvf-kernel-extract:/bzImage",
&bzimage_path.to_string_lossy(),
])
.status()
.map_err(|e| KernelError::DockerBuildFailed(format!("docker cp failed: {e}")))?;
// Clean up the temporary container (best-effort)
let _ = Command::new("docker")
.args(["rm", "rvf-kernel-extract"])
.status();
if !cp_status.success() {
return Err(KernelError::DockerBuildFailed(
"docker cp failed to extract bzImage".into(),
));
}
let bzimage = std::fs::read(&bzimage_path)?;
if bzimage.is_empty() {
return Err(KernelError::DockerBuildFailed(
"extracted bzImage is empty".into(),
));
}
Ok(bzimage)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn dockerfile_contains_required_steps() {
let dockerfile = generate_dockerfile(DEFAULT_KERNEL_VERSION);
// Must start from Alpine
assert!(dockerfile.contains("FROM alpine:3.19 AS builder"));
// Must install build dependencies
assert!(dockerfile.contains("build-base"));
assert!(dockerfile.contains("flex"));
assert!(dockerfile.contains("bison"));
assert!(dockerfile.contains("elfutils-dev"));
assert!(dockerfile.contains("openssl-dev"));
// Must download kernel source
assert!(dockerfile.contains("cdn.kernel.org"));
assert!(dockerfile.contains(DEFAULT_KERNEL_VERSION));
// Must copy config
assert!(dockerfile.contains("COPY kernel.config"));
// Must run make
assert!(dockerfile.contains("make olddefconfig"));
assert!(dockerfile.contains("make -j"));
assert!(dockerfile.contains("bzImage"));
// Must use multi-stage build
assert!(dockerfile.contains("FROM scratch"));
assert!(dockerfile.contains("COPY --from=builder"));
}
#[test]
fn dockerfile_uses_custom_version() {
let dockerfile = generate_dockerfile("6.9.1");
assert!(dockerfile.contains("6.9.1"));
}
#[test]
fn prepare_creates_files() {
let dir = tempfile::TempDir::new().unwrap();
let ctx = DockerBuildContext::prepare(dir.path(), None).unwrap();
assert!(dir.path().join("Dockerfile").exists());
assert!(dir.path().join("kernel.config").exists());
assert_eq!(ctx.kernel_version, DEFAULT_KERNEL_VERSION);
// Verify kernel.config content
let config = std::fs::read_to_string(dir.path().join("kernel.config")).unwrap();
assert!(config.contains("CONFIG_64BIT=y"));
assert!(config.contains("CONFIG_VIRTIO_PCI=y"));
}
#[test]
fn prepare_with_custom_version() {
let dir = tempfile::TempDir::new().unwrap();
let ctx = DockerBuildContext::prepare(dir.path(), Some("6.6.30")).unwrap();
assert_eq!(ctx.kernel_version, "6.6.30");
let dockerfile = std::fs::read_to_string(dir.path().join("Dockerfile")).unwrap();
assert!(dockerfile.contains("6.6.30"));
}
}