From 0a72e2e8d92b9e3abe651977b8891b3481e0b83f Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Sun, 4 Oct 2026 02:59:08 +0200 Subject: [PATCH] feat: Added wrapper crate for including ipxe binaries --- .gitmodules | 3 +++ Cargo.lock | 34 +++++++++++++++++++++++++++ Cargo.toml | 3 ++- ipxe/Cargo.toml | 10 ++++++++ ipxe/build.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++ ipxe/ipxe | 1 + ipxe/src/embed.ipxe | 5 ++++ ipxe/src/lib.rs | 3 +++ src/main.rs | 6 ++++- 9 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 .gitmodules create mode 100644 ipxe/Cargo.toml create mode 100644 ipxe/build.rs create mode 160000 ipxe/ipxe create mode 100644 ipxe/src/embed.ipxe create mode 100644 ipxe/src/lib.rs diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..58b8f04 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "ipxe/ipxe"] + path = ipxe/ipxe + url = git@github.com:rkubes/ipxe diff --git a/Cargo.lock b/Cargo.lock index 1329704..94224f9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -166,6 +166,12 @@ dependencies = [ "serde_core", ] +[[package]] +name = "build-print" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8e6738dfb11354886f890621b4a34c0b177f75538023f7100b608ab9adbd66b" + [[package]] name = "bumpalo" version = "3.20.3" @@ -292,6 +298,7 @@ dependencies = [ "clap_complete", "futures", "gix-discover", + "ipxe", "minijinja", "netascii", "num_enum", @@ -1017,6 +1024,25 @@ dependencies = [ "icu_properties", ] +[[package]] +name = "include_dir" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "923d117408f1e49d914f1a379a309cffe4f18c05cf4e3d12e613a15fc81bd0dd" +dependencies = [ + "include_dir_macros", +] + +[[package]] +name = "include_dir_macros" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7cab85a7ed0bd5f0e76d93846e0147172bed2e2d3f859bcc33a8d9699cad1a75" +dependencies = [ + "proc-macro2", + "quote", +] + [[package]] name = "indexmap" version = "2.14.2" @@ -1033,6 +1059,14 @@ version = "2.12.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "791930b43c0d5973160d90a8f3894509f2b273430f5c5c73b668636d0287c5c0" +[[package]] +name = "ipxe" +version = "0.1.0" +dependencies = [ + "build-print", + "include_dir", +] + [[package]] name = "is_terminal_polyfill" version = "1.70.2" diff --git a/Cargo.toml b/Cargo.toml index ab8d057..6e7eb7c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ publish = ["infra"] license = "GPL-3.0-or-later" [workspace] -members = ["xtask"] +members = ["ipxe", "xtask"] [workspace.dependencies] schemars = { version = "1.2.2", features = ["semver1"] } @@ -35,6 +35,7 @@ serde_json = { workspace = true } serde_yaml = "0.9.34" thiserror = "2.0.21" tokio = { version = "1.52.1", features = ["full"] } +ipxe = { path = "./ipxe" } [lib] test = false diff --git a/ipxe/Cargo.toml b/ipxe/Cargo.toml new file mode 100644 index 0000000..2220d92 --- /dev/null +++ b/ipxe/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "ipxe" +version = "0.1.0" +edition = "2024" + +[build-dependencies] +build-print = "1.0.1" + +[dependencies] +include_dir = "0.7.4" diff --git a/ipxe/build.rs b/ipxe/build.rs new file mode 100644 index 0000000..720fbe3 --- /dev/null +++ b/ipxe/build.rs @@ -0,0 +1,57 @@ +use std::env; +use std::path::PathBuf; +use std::process::{Command, Stdio}; + +use build_print::error; + +fn main() { + let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()) + .canonicalize() + .unwrap(); + let src_dir = manifest_dir.join("ipxe").join("src"); + + let mut command = Command::new("make"); + command + .stdout(Stdio::piped()) + .current_dir(&src_dir) + .env_remove("DEBUG") + .env_remove("PROFILE") + .args([ + "-j", + &std::thread::available_parallelism() + .unwrap() + .get() + .to_string(), + ]) + .arg(format!( + "EMBED={}", + manifest_dir + .join("src") + .join("embed.ipxe") + .to_string_lossy(), + )); + + let files = ["bin/ipxe.pxe", "bin-x86_64-efi/ipxe.efi"].map(PathBuf::from); + for file in &files { + command.arg(file); + } + + let output = command.output().unwrap(); + if !output.status.success() { + error!("{}", String::from_utf8_lossy(&output.stderr)); + } + + let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap()) + .canonicalize() + .unwrap() + .join("ipxe"); + + std::fs::remove_dir_all(&out_dir).ok(); + for file in &files { + let input = src_dir.join(file); + let output = out_dir.join(file.file_name().unwrap()); + + std::fs::create_dir_all(output.parent().unwrap()).unwrap(); + std::fs::copy(input, output).unwrap(); + } +} diff --git a/ipxe/ipxe b/ipxe/ipxe new file mode 160000 index 0000000..8892af6 --- /dev/null +++ b/ipxe/ipxe @@ -0,0 +1 @@ +Subproject commit 8892af68a254d36f778345d5324ec74fb8adbab8 diff --git a/ipxe/src/embed.ipxe b/ipxe/src/embed.ipxe new file mode 100644 index 0000000..f24cea6 --- /dev/null +++ b/ipxe/src/embed.ipxe @@ -0,0 +1,5 @@ +#!ipxe + +dhcp + +echo "Hello, World!" diff --git a/ipxe/src/lib.rs b/ipxe/src/lib.rs new file mode 100644 index 0000000..8b8b128 --- /dev/null +++ b/ipxe/src/lib.rs @@ -0,0 +1,3 @@ +use include_dir::{Dir, include_dir}; + +pub static IPXE_DIR: Dir<'_> = include_dir!("$OUT_DIR/ipxe"); diff --git a/src/main.rs b/src/main.rs index ce27253..09fccfc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,6 +11,7 @@ use crete::tftp::ServerError; use crete::{get_configs_path, get_repo_path, set_repo_path, tftp}; use futures::StreamExt; use futures::future::join_all; +use ipxe::IPXE_DIR; use minijinja::context; use regex::Regex; use thiserror::Error; @@ -115,7 +116,10 @@ async fn main() -> Result<(), Error> { let re = re.clone(); async move { - if let Some(caps) = re.captures(&filename) { + const IPXE_PATTERN: &str = "ipxe/"; + if let Some(filename) = filename.strip_prefix(IPXE_PATTERN) { + Some(IPXE_DIR.get_file(filename)?.contents().to_owned()) + } else if let Some(caps) = re.captures(&filename) { let cluster = &caps["cluster"]; let node = &caps["node"];