From 5ea7121dc2a59a9cc31165934d6cb00c494c7390 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20M=C3=BCller?= Date: Fri, 13 Mar 2026 06:34:01 -0700 Subject: [PATCH] devices: build init binary in Cargo OUT_DIR MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As per previous discussion [0], adjust the build script to build the init binary in Cargo's OUT_DIR, which is meant precisely for use cases like this. This way, we don't leak a random binary into the source tree, where it would have to be removed manually. [0] https://github.com/containers/libkrun/pull/578#discussion_r2925181142 Signed-off-by: Daniel Müller --- .gitignore | 1 - src/devices/build.rs | 11 +++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index c0b4e6771..054c7d46a 100644 --- a/.gitignore +++ b/.gitignore @@ -7,7 +7,6 @@ __pycache__ *.pyc *~ /libkrun.pc -init/init init/nitro/init examples/chroot_vm examples/launch-tee diff --git a/src/devices/build.rs b/src/devices/build.rs index 813d76f41..0d5cc0c97 100644 --- a/src/devices/build.rs +++ b/src/devices/build.rs @@ -6,7 +6,9 @@ fn build_default_init() -> PathBuf { let manifest_dir = PathBuf::from(std::env::var_os("CARGO_MANIFEST_DIR").unwrap()); let libkrun_root = manifest_dir.join("../.."); let init_src = libkrun_root.join("init/init.c"); - let init_bin = libkrun_root.join("init/init"); + + let out_dir = PathBuf::from(std::env::var_os("OUT_DIR").unwrap()); + let init_bin = out_dir.join("init"); println!("cargo:rerun-if-env-changed=CC_LINUX"); println!("cargo:rerun-if-env-changed=CC"); @@ -45,7 +47,12 @@ fn build_default_init() -> PathBuf { fn main() { let init_binary_path = std::env::var_os("KRUN_INIT_BINARY_PATH") .map(PathBuf::from) - .unwrap_or_else(build_default_init); + .unwrap_or_else(|| { + let init_path = build_default_init(); + // SAFETY: The build script is single threaded. + unsafe { std::env::set_var("KRUN_INIT_BINARY_PATH", &init_path) }; + init_path + }); println!( "cargo:rustc-env=KRUN_INIT_BINARY_PATH={}", init_binary_path.display()