Skip to content

Commit aba978c

Browse files
committed
fix(driver-vm): satisfy branch checks
1 parent 5943739 commit aba978c

4 files changed

Lines changed: 60 additions & 47 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/openshell-driver-vm/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ flate2 = "1"
4444
sha2 = "0.10"
4545
zstd = "0.13"
4646

47+
[dev-dependencies]
48+
temp-env = "0.3"
49+
4750
# smol-rs/polling drives the BSD/macOS parent-death detection in
4851
# procguard via kqueue's EVFILT_PROC / NOTE_EXIT filter. We could use
4952
# it on Linux too (via epoll + pidfd) but sticking with

crates/openshell-driver-vm/src/rootfs.rs

Lines changed: 55 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ pub fn validate_sandbox_rootfs(rootfs: &Path) -> Result<(), String> {
221221
/// this exact version; a mismatch causes `modprobe` failures at boot.
222222
///
223223
/// Keep in sync with:
224-
/// - `tasks/scripts/vm/build-nvidia-modules.sh` (KERNEL_TREE path)
224+
/// - `tasks/scripts/vm/build-nvidia-modules.sh` (`KERNEL_TREE` path)
225225
/// - `openshell-vm-sandbox-init.sh` `setup_gpu()` expected version
226226
const GUEST_KERNEL_VERSION: &str = "6.12.76";
227227

@@ -271,13 +271,8 @@ pub fn inject_gpu_modules(rootfs: &Path, state_dir: &Path) -> Result<(), String>
271271

272272
for ko in &ko_files {
273273
let dest = modules_dst.join(ko.file_name().unwrap());
274-
let bytes_copied = fs::copy(ko, &dest).map_err(|e| {
275-
format!(
276-
"copy {} -> {}: {e}",
277-
ko.display(),
278-
dest.display()
279-
)
280-
})?;
274+
let bytes_copied = fs::copy(ko, &dest)
275+
.map_err(|e| format!("copy {} -> {}: {e}", ko.display(), dest.display()))?;
281276
tracing::info!(
282277
module = %ko.file_name().unwrap().to_string_lossy(),
283278
size_bytes = bytes_copied,
@@ -323,7 +318,7 @@ fn warn_missing_gpu_userspace(rootfs: &Path) {
323318
/// 1. `OPENSHELL_GPU_MODULES_DIR` env var (explicit override)
324319
/// 2. `<state_dir>/gpu-modules/` (operator pre-provisioned)
325320
/// 3. `<project_root>/target/libkrun-build/nvidia-modules/` (build tree,
326-
/// discovered relative to the driver executable)
321+
/// discovered relative to the driver executable)
327322
/// 4. Host `/lib/modules/<GUEST_KERNEL_VERSION>/kernel/drivers/nvidia/`
328323
fn resolve_gpu_modules_dir(state_dir: &Path) -> Result<PathBuf, String> {
329324
if let Ok(dir) = std::env::var("OPENSHELL_GPU_MODULES_DIR") {
@@ -379,9 +374,11 @@ fn resolve_gpu_modules_dir(state_dir: &Path) -> Result<PathBuf, String> {
379374
/// `OPENSHELL_GPU_MODULES_DIR` or pre-provision `<state_dir>/gpu-modules/`.
380375
fn discover_build_tree_modules() -> Option<PathBuf> {
381376
#[cfg(unix)]
382-
if unsafe { libc::getuid() } == 0 {
383-
tracing::debug!("build-tree GPU module discovery running as root; \
384-
prefer OPENSHELL_GPU_MODULES_DIR in production");
377+
if nix::unistd::Uid::effective().is_root() {
378+
tracing::debug!(
379+
"build-tree GPU module discovery running as root; \
380+
prefer OPENSHELL_GPU_MODULES_DIR in production"
381+
);
385382
}
386383
let exe = std::env::current_exe().ok()?;
387384
// exe is typically target/{debug,release}/openshell-driver-vm
@@ -398,7 +395,9 @@ fn discover_build_tree_modules() -> Option<PathBuf> {
398395
// Also try CWD-relative (for `cargo run` or `mise run` from project root).
399396
let cwd_candidate = PathBuf::from("target/libkrun-build/nvidia-modules");
400397
if dir_has_ko_files(&cwd_candidate) {
401-
let abs = cwd_candidate.canonicalize().unwrap_or(cwd_candidate.clone());
398+
let abs = cwd_candidate
399+
.canonicalize()
400+
.unwrap_or_else(|_| cwd_candidate.clone());
402401
tracing::info!(
403402
path = %abs.display(),
404403
"auto-discovered GPU modules relative to CWD"
@@ -422,10 +421,13 @@ fn dir_has_ko_files(dir: &Path) -> bool {
422421
let path = entry.path();
423422
match path.extension().and_then(|e| e.to_str()) {
424423
Some("ko") => has_uncompressed = true,
425-
Some("zst" | "xz") => {
426-
if path.file_stem().and_then(|s| std::path::Path::new(s).extension()).is_some_and(|ext| ext == "ko") {
427-
has_compressed = true;
428-
}
424+
Some("zst" | "xz")
425+
if path
426+
.file_stem()
427+
.and_then(|s| Path::new(s).extension())
428+
.is_some_and(|ext| ext == "ko") =>
429+
{
430+
has_compressed = true;
429431
}
430432
_ => {}
431433
}
@@ -458,18 +460,16 @@ fn inject_gpu_firmware(rootfs: &Path, modules_dir: &Path) {
458460
}
459461

460462
// Try version-matched firmware next to the modules directory.
461-
let fw_parent = modules_dir
462-
.parent()
463-
.map(|p| p.join("nvidia-firmware"));
464-
465-
if let Some(ref fw_dir) = fw_parent {
466-
if fw_dir.is_dir() {
467-
if let Err(e) = copy_dir_contents(fw_dir, &fw_dst) {
468-
tracing::warn!(error = %e, "failed to copy version-matched firmware");
469-
} else {
470-
tracing::info!(src = %fw_dir.display(), "injected GPU firmware (version-matched)");
471-
return;
472-
}
463+
let fw_parent = modules_dir.parent().map(|p| p.join("nvidia-firmware"));
464+
465+
if let Some(ref fw_dir) = fw_parent
466+
&& fw_dir.is_dir()
467+
{
468+
if let Err(e) = copy_dir_contents(fw_dir, &fw_dst) {
469+
tracing::warn!(error = %e, "failed to copy version-matched firmware");
470+
} else {
471+
tracing::info!(src = %fw_dir.display(), "injected GPU firmware (version-matched)");
472+
return;
473473
}
474474
}
475475

@@ -489,7 +489,9 @@ fn inject_gpu_firmware(rootfs: &Path, modules_dir: &Path) {
489489
tracing::warn!(
490490
"no NVIDIA GSP firmware found; GPU guests may fail to initialize. \
491491
Place firmware in {:?} or host /lib/firmware/nvidia/",
492-
fw_parent.as_deref().unwrap_or(Path::new("(unknown)"))
492+
fw_parent
493+
.as_deref()
494+
.unwrap_or_else(|| Path::new("(unknown)"))
493495
);
494496
}
495497

@@ -517,13 +519,13 @@ fn rootfs_has_firmware_bins(fw_dir: &Path) -> bool {
517519
/// images install `kmod` but lack the convenience symlinks in `/usr/sbin`.
518520
fn ensure_kmod_symlinks(rootfs: &Path) {
519521
let kmod_candidates = ["bin/kmod", "usr/bin/kmod", "sbin/kmod", "usr/sbin/kmod"];
520-
let kmod_exists = kmod_candidates
521-
.iter()
522-
.any(|p| rootfs.join(p).exists());
522+
let kmod_exists = kmod_candidates.iter().any(|p| rootfs.join(p).exists());
523523

524524
if !kmod_exists {
525-
tracing::warn!("kmod not found in rootfs; modprobe will fail. \
526-
Ensure the sandbox image installs the 'kmod' package.");
525+
tracing::warn!(
526+
"kmod not found in rootfs; modprobe will fail. \
527+
Ensure the sandbox image installs the 'kmod' package."
528+
);
527529
return;
528530
}
529531

@@ -845,13 +847,16 @@ mod tests {
845847

846848
fs::create_dir_all(&modules_dir).expect("create modules dir");
847849
fs::create_dir_all(&rootfs).expect("create rootfs dir");
848-
fs::write(modules_dir.join("nvidia.ko"), b"\x7fELF-fake-module-1").expect("write nvidia.ko");
850+
fs::write(modules_dir.join("nvidia.ko"), b"\x7fELF-fake-module-1")
851+
.expect("write nvidia.ko");
849852
fs::write(modules_dir.join("nvidia-uvm.ko"), b"\x7fELF-fake-module-2")
850853
.expect("write nvidia-uvm.ko");
851854

852-
unsafe { std::env::set_var("OPENSHELL_GPU_MODULES_DIR", &modules_dir) };
853-
let result = inject_gpu_modules(&rootfs, Path::new("/dummy/state"));
854-
unsafe { std::env::remove_var("OPENSHELL_GPU_MODULES_DIR") };
855+
let result = temp_env::with_var(
856+
"OPENSHELL_GPU_MODULES_DIR",
857+
Some(modules_dir.as_os_str()),
858+
|| inject_gpu_modules(&rootfs, Path::new("/dummy/state")),
859+
);
855860

856861
result.expect("inject_gpu_modules should succeed");
857862

@@ -870,9 +875,11 @@ mod tests {
870875
fs::create_dir_all(&modules_dir).expect("create modules dir");
871876
fs::write(modules_dir.join("readme.txt"), b"not a kernel module").expect("write txt");
872877

873-
unsafe { std::env::set_var("OPENSHELL_GPU_MODULES_DIR", &modules_dir) };
874-
let result = inject_gpu_modules(Path::new("/dummy/rootfs"), Path::new("/dummy/state"));
875-
unsafe { std::env::remove_var("OPENSHELL_GPU_MODULES_DIR") };
878+
let result = temp_env::with_var(
879+
"OPENSHELL_GPU_MODULES_DIR",
880+
Some(modules_dir.as_os_str()),
881+
|| inject_gpu_modules(Path::new("/dummy/rootfs"), Path::new("/dummy/state")),
882+
);
876883

877884
let err = result.expect_err("should fail with no .ko files");
878885
assert!(
@@ -888,9 +895,11 @@ mod tests {
888895
let dir = unique_temp_dir();
889896
let missing = dir.join("does-not-exist");
890897

891-
unsafe { std::env::set_var("OPENSHELL_GPU_MODULES_DIR", &missing) };
892-
let result = inject_gpu_modules(Path::new("/dummy/rootfs"), Path::new("/dummy/state"));
893-
unsafe { std::env::remove_var("OPENSHELL_GPU_MODULES_DIR") };
898+
let result = temp_env::with_var(
899+
"OPENSHELL_GPU_MODULES_DIR",
900+
Some(missing.as_os_str()),
901+
|| inject_gpu_modules(Path::new("/dummy/rootfs"), Path::new("/dummy/state")),
902+
);
894903

895904
let err = result.expect_err("should fail with missing directory");
896905
assert!(
@@ -916,8 +925,7 @@ mod tests {
916925

917926
let content = fs::read(fw_dir.join("gsp.bin")).expect("read gsp.bin after injection");
918927
assert_eq!(
919-
content,
920-
b"original-firmware-content",
928+
content, b"original-firmware-content",
921929
"firmware should not be overwritten when rootfs already has .bin files"
922930
);
923931

sandboxes/nvidia-gpu/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ docker build \
7777
### Changing the NVIDIA driver version
7878

7979
Update all three locations:
80+
8081
1. `sandboxes/nvidia-gpu/versions.env`
8182
2. `sandboxes/nvidia-gpu/Dockerfile` ARG `NVIDIA_DRIVER_VERSION`
8283
3. Rebuild kernel modules: `NVIDIA_OPEN_VERSION=<version> mise run vm:nvidia-modules`

0 commit comments

Comments
 (0)