mosaik 0.3.13

A Rust runtime for building self-organizing, leaderless distributed systems.
Documentation
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
//! Shared infrastructure for TDX image builders.

use {
	super::{
		BuilderOutput,
		helpers::{detect_profile, env_or, find_target_dir},
		kernel::auto_download_kernel,
		mrtd,
		ovmf::obtain_ovmf,
		scripts::{generate_launch_script, generate_self_extracting_script},
	},
	crate::tee::tdx::Measurement,
	std::{
		env,
		fs::{self, File},
		path::{Path, PathBuf},
		process::Command,
	},
};

/// Fields shared by all TDX image builders.
pub(super) struct CommonConfig {
	pub custom_vmlinuz: Option<Vec<u8>>,
	pub custom_ovmf: Option<Vec<u8>>,
	pub ssh_keys: Vec<String>,
	pub ssh_forward: Option<(u16, u16)>,
	pub default_cpus: u32,
	pub default_memory: String,
	pub bundle_runner: bool,
	pub extra_files: Vec<(PathBuf, String)>,
	pub extra_kernel_modules: Vec<PathBuf>,
	pub kernel_modules_dir: Option<PathBuf>,
	pub kernel_version: Option<String>,
	pub kernel_abi: Option<String>,
	pub ovmf_version: Option<String>,
	pub artifacts_output_path: Option<PathBuf>,
	pub args: Vec<String>,
	pub env_vars: Vec<(String, String)>,
}

impl Default for CommonConfig {
	fn default() -> Self {
		Self {
			custom_vmlinuz: None,
			custom_ovmf: None,
			ssh_keys: Vec::new(),
			ssh_forward: None,
			default_cpus: 4,
			default_memory: "4G".to_string(),
			bundle_runner: true,
			extra_files: Vec::new(),
			extra_kernel_modules: Vec::new(),
			kernel_modules_dir: None,
			kernel_version: None,
			kernel_abi: None,
			ovmf_version: None,
			artifacts_output_path: None,
			args: Vec::new(),
			env_vars: Vec::new(),
		}
	}
}

/// Resolved build environment from Cargo.
pub(super) struct BuildContext {
	pub out_dir: PathBuf,
	pub cache_dir: PathBuf,
	pub crate_name: String,
	pub profile: &'static str,
	pub target_dir: PathBuf,
}

impl BuildContext {
	pub fn resolve() -> Self {
		let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
		let cache_dir = out_dir.join("tdx-image-cache");
		fs::create_dir_all(&cache_dir).unwrap();
		Self {
			out_dir,
			cache_dir,
			crate_name: env::var("CARGO_PKG_NAME").unwrap(),
			profile: detect_profile(),
			target_dir: find_target_dir(),
		}
	}
}

impl CommonConfig {
	/// Generate shell `export` lines for environment variables.
	pub fn env_block(&self) -> String {
		self
			.env_vars
			.iter()
			.map(|(k, v)| {
				let escaped = v.replace('\'', "'\\''");
				format!("export {k}='{escaped}'")
			})
			.collect::<Vec<_>>()
			.join("\n")
	}

	/// Generate the shell arguments string for the binary.
	///
	/// Returns `"$@"` when no explicit args are configured, so the
	/// kernel command-line pass-through is preserved.
	pub fn args_string(&self) -> String {
		if self.args.is_empty() {
			"\"$@\"".to_string()
		} else {
			self
				.args
				.iter()
				.map(|a| {
					let escaped = a.replace('\'', "'\\''");
					format!("'{escaped}'")
				})
				.collect::<Vec<_>>()
				.join(" ")
		}
	}

	/// Resolve the artifacts output directory.
	pub fn resolve_artifacts_dir(
		&self,
		ctx: &BuildContext,
		distro: &str,
	) -> PathBuf {
		let dir = match self.artifacts_output_path {
			Some(ref p) if p.is_absolute() => p.clone(),
			Some(ref p) => ctx.target_dir.join(p),
			None => ctx
				.target_dir
				.join(ctx.profile)
				.join("tdx-artifacts")
				.join(&ctx.crate_name)
				.join(distro),
		};
		fs::create_dir_all(&dir).unwrap();
		dir
	}
}

/// Returns `true` if the build should be skipped (recursion guard
/// or `TDX_IMAGE_SKIP=1`).
pub(super) fn check_skip_build() -> bool {
	if env::var("__TDX_IMAGE_INNER_BUILD").is_ok() {
		return true;
	}
	println!("cargo:rerun-if-env-changed=TDX_IMAGE_SKIP");
	println!("cargo:rerun-if-env-changed=TDX_IMAGE_EXTRA_FILES");
	println!("cargo:rerun-if-env-changed=TDX_IMAGE_KERNEL_MODULES");
	if env_or("TDX_IMAGE_SKIP", "0") == "1" {
		eprintln!("TDX_IMAGE_SKIP=1 — skipping initramfs build");
		return true;
	}
	false
}

/// Acquire the kernel image and auto-download module directory.
///
/// Returns `(kernel_vmlinuz, auto_modules_dir)`.
pub(super) fn acquire_kernel(
	config: &CommonConfig,
	kernel_cache_dir: &Path,
) -> (Option<PathBuf>, Option<PathBuf>) {
	println!("cargo:rerun-if-env-changed=TDX_IMAGE_KERNEL");
	println!("cargo:rerun-if-env-changed=TDX_IMAGE_KERNEL_VERSION");

	let kernel_vmlinuz: Option<PathBuf>;
	let mut auto_modules_dir: Option<PathBuf> = None;

	if let Some(ref custom_vmlinuz) = config.custom_vmlinuz {
		let path = kernel_cache_dir.join("custom-vmlinuz");
		fs::write(&path, custom_vmlinuz).unwrap();
		kernel_vmlinuz = Some(path);
	} else if let Ok(kernel_path) = env::var("TDX_IMAGE_KERNEL") {
		eprintln!("==> Using user-provided kernel: {kernel_path}");
		kernel_vmlinuz = Some(PathBuf::from(&kernel_path));
	} else {
		let (vmlinuz, modules_dir) = auto_download_kernel(
			kernel_cache_dir,
			config.kernel_version.as_deref(),
			config.kernel_abi.as_deref(),
		);
		kernel_vmlinuz = vmlinuz;
		auto_modules_dir = modules_dir;
	}

	let has_explicit_modules_dir = config.kernel_modules_dir.is_some()
		|| env::var("TDX_IMAGE_KERNEL_MODULES_DIR").is_ok()
		|| !config.extra_kernel_modules.is_empty();

	if auto_modules_dir.is_none() && !has_explicit_modules_dir {
		eprintln!(
			"==> No modules directory — auto-downloading Ubuntu kernel modules for \
			 TDX support..."
		);
		let (_, modules_dir) = auto_download_kernel(
			kernel_cache_dir,
			config.kernel_version.as_deref(),
			config.kernel_abi.as_deref(),
		);
		auto_modules_dir = modules_dir;
	}

	(kernel_vmlinuz, auto_modules_dir)
}

/// Resolve the effective kernel modules directory from builder
/// config, environment, or auto-downloaded modules.
pub(super) fn resolve_modules_dir(
	config: &CommonConfig,
	auto_modules_dir: Option<&Path>,
) -> Option<PathBuf> {
	println!("cargo:rerun-if-env-changed=TDX_IMAGE_KERNEL_MODULES_DIR");

	config
		.kernel_modules_dir
		.clone()
		.or_else(|| {
			env::var("TDX_IMAGE_KERNEL_MODULES_DIR")
				.map(PathBuf::from)
				.ok()
		})
		.or_else(|| auto_modules_dir.map(Path::to_path_buf))
}

/// Post-CPIO build pipeline: gzip, kernel copy, OVMF, measurements,
/// launch scripts, and `BuilderOutput` construction.
#[allow(clippy::too_many_lines)]
pub(super) fn finalize_build(
	config: &CommonConfig,
	ctx: &BuildContext,
	cpio_path: &Path,
	artifacts_dir: &Path,
	kernel_vmlinuz: Option<&Path>,
) -> BuilderOutput {
	let output_filename = format!("{}-initramfs.cpio.gz", ctx.crate_name);
	let final_path = artifacts_dir.join(&output_filename);

	// gzip → final output
	eprintln!("==> Compressing...");
	let gz_path = ctx.out_dir.join(&output_filename);

	let gz_file = File::create(&gz_path).unwrap();
	let status = Command::new("gzip")
		.args(["-9", "-n", "-c"])
		.arg(cpio_path)
		.stdout(gz_file)
		.status()
		.expect("failed to run gzip — is it installed?");

	assert!(status.success(), "gzip compression failed");

	fs::copy(&gz_path, &final_path).unwrap();

	// Report initramfs
	let cpio_size = fs::metadata(cpio_path).map(|m| m.len()).unwrap_or(0);
	let gz_size = fs::metadata(&final_path).map(|m| m.len()).unwrap_or(0);

	println!(
		"cargo:warning=initramfs: {} (cpio {:.1} MB → gz {:.1} MB)",
		final_path.display(),
		cpio_size as f64 / 1_048_576.0,
		gz_size as f64 / 1_048_576.0,
	);

	println!(
		"cargo:rustc-env=TDX_INITRAMFS_PATH={}",
		final_path.display()
	);

	let _ = fs::remove_file(cpio_path);

	// Copy kernel to output directory
	let kernel_output = artifacts_dir.join(format!("{}-vmlinuz", ctx.crate_name));

	if let Some(vmlinuz) = kernel_vmlinuz {
		if vmlinuz.exists() {
			fs::copy(vmlinuz, &kernel_output).unwrap();
			let ksize = fs::metadata(&kernel_output).map(|m| m.len()).unwrap_or(0);
			println!(
				"cargo:warning=kernel: {} ({:.1} MB)",
				kernel_output.display(),
				ksize as f64 / 1_048_576.0,
			);
			println!(
				"cargo:rustc-env=TDX_KERNEL_PATH={}",
				kernel_output.display()
			);
		}
	} else {
		eprintln!(
			"==> No kernel available — set TDX_IMAGE_KERNEL or let auto-download \
			 handle it"
		);
	}

	// Generate launch-tdx.sh
	generate_launch_script(
		&ctx.crate_name,
		artifacts_dir,
		config.default_cpus,
		&config.default_memory,
		config.ssh_forward,
	);

	// Obtain OVMF and precompute measurements
	let kernel_cache_dir = ctx.cache_dir.join("kernel");
	let ovmf_output = artifacts_dir.join(format!("{}-ovmf.fd", ctx.crate_name));

	let ovmf_data = obtain_ovmf(
		&config.custom_ovmf,
		&kernel_cache_dir,
		config.ovmf_version.as_deref(),
	);

	let mut mrtd_value = [0u8; 48];
	let mut rtmr1_value = [0u8; 48];
	let mut rtmr2_value = [0u8; 48];

	if let Some(ref data) = ovmf_data {
		fs::write(&ovmf_output, data).unwrap();
		println!(
			"cargo:warning=OVMF: {} ({:.1} MB)",
			ovmf_output.display(),
			data.len() as f64 / 1_048_576.0,
		);

		eprintln!("==> Precomputing MRTD...");
		match mrtd::compute_mrtd(data) {
			Ok(digest) => {
				mrtd_value = digest;
				let hex = hex::encode(digest);
				println!("cargo:warning=MRTD: {hex}");
				println!("cargo:rustc-env=TDX_EXPECTED_MRTD={hex}");

				let mrtd_path =
					artifacts_dir.join(format!("{}-mrtd.hex", ctx.crate_name));
				fs::write(&mrtd_path, &hex).unwrap();
				println!("cargo:warning=MRTD written to: {}", mrtd_path.display());
			}
			Err(e) => {
				println!("cargo:warning=MRTD computation failed: {e}");
			}
		}
	}

	// RTMR[1]
	if kernel_output.exists() && final_path.exists() {
		eprintln!("==> Precomputing RTMR[1]...");
		let kernel_data = fs::read(&kernel_output).unwrap();
		let initrd_data = fs::read(&final_path).unwrap();
		rtmr1_value =
			mrtd::compute_rtmr1(&kernel_data, &initrd_data, &config.default_memory);
		let hex = hex::encode(rtmr1_value);
		println!("cargo:warning=RTMR[1]: {hex}");
		println!("cargo:rustc-env=TDX_EXPECTED_RTMR1={hex}");

		let rtmr1_path =
			artifacts_dir.join(format!("{}-rtmr1.hex", ctx.crate_name));
		fs::write(&rtmr1_path, &hex).unwrap();
		println!("cargo:warning=RTMR[1] written to: {}", rtmr1_path.display(),);
	}

	// RTMR[2]
	if final_path.exists() {
		eprintln!("==> Precomputing RTMR[2]...");
		let initrd_data = fs::read(&final_path).unwrap();
		let cmdline = "console=ttyS0 ip=dhcp";
		rtmr2_value = mrtd::compute_rtmr2(cmdline, &initrd_data);
		let hex = hex::encode(rtmr2_value);
		println!("cargo:warning=RTMR[2]: {hex}");
		println!("cargo:rustc-env=TDX_EXPECTED_RTMR2={hex}");

		let rtmr2_path =
			artifacts_dir.join(format!("{}-rtmr2.hex", ctx.crate_name));
		fs::write(&rtmr2_path, &hex).unwrap();
		println!("cargo:warning=RTMR[2] written to: {}", rtmr2_path.display());
	}

	// Self-extracting bundle
	generate_self_extracting_script(
		&ctx.crate_name,
		artifacts_dir,
		&ctx.out_dir,
		&kernel_output,
		&final_path,
		&ovmf_output,
		config.default_cpus,
		&config.default_memory,
		config.ssh_forward,
	);

	let bundle_path =
		artifacts_dir.join(format!("{}-run-qemu.sh", ctx.crate_name));

	BuilderOutput {
		initramfs_path: final_path,
		kernel_path: kernel_output,
		ovmf_path: ovmf_output,
		bundle_path,
		mrtd: Measurement::from(mrtd_value),
		rtmr1: Measurement::from(rtmr1_value),
		rtmr2: Measurement::from(rtmr2_value),
	}
}