fix(installer): detect the BLS kernel layout in target verification
Some checks are pending
ci / test (push) Waiting to run

The installed-target check scanned only the top level of /boot and /efi for
vmlinuz/kernel. With layout=bls, kernel-install places the kernel as
<boot>/<machine-id>/<version>/linux, so a correct install would have been
rejected. Scan the tree (bounded, without following symlinks) for linux/vmlinuz/
kernel, and accept a BLS entry that names a kernel.
This commit is contained in:
Phoebus 2026-09-23 18:41:41 -07:00
parent a6ed363745
commit 8be9bb6444

View file

@ -807,21 +807,11 @@ impl Installer {
}
let root_p = std::path::Path::new(&self.exec.root);
let has_kernel = [root_p.join("boot"), root_p.join("efi")].iter().any(|dir| {
if let Ok(entries) = std::fs::read_dir(dir) {
entries.flatten().any(|e| {
let name = e.file_name().to_string_lossy().to_string();
name.starts_with("vmlinuz") || name.starts_with("kernel")
})
} else {
false
}
});
if !has_kernel {
if !target_has_kernel(root_p) {
return Err(phoebus_executor::ExecError::operation(
"finalize",
"verify target",
"no kernel image (vmlinuz/kernel) found in target /boot or /efi",
"no kernel image found in the target boot/efi tree",
));
}
@ -933,6 +923,65 @@ impl Installer {
}
}
/// Whether the target has an installed kernel image.
///
/// With `layout=bls`, systemd's `kernel-install` places the kernel as
/// `<boot>/<machine-id>/<version>/linux` (not a top-level `vmlinuz`), so a
/// shallow scan is not enough. A BLS entry that names a kernel is equally
/// authoritative. Symlinks are not followed, so the scan cannot escape the
/// target root.
fn target_has_kernel(root: &std::path::Path) -> bool {
for sub in ["boot", "efi"] {
let dir = root.join(sub);
if dir.is_dir() && dir_has_kernel(&dir, 0) {
return true;
}
let entries = dir.join("loader/entries");
if let Ok(read) = std::fs::read_dir(&entries) {
for entry in read.flatten() {
if let Ok(text) = std::fs::read_to_string(entry.path()) {
if text.lines().any(|line| {
let t = line.trim_start();
t.starts_with("linux ") || t.starts_with("kernel ")
}) {
return true;
}
}
}
}
}
false
}
fn dir_has_kernel(dir: &std::path::Path, depth: usize) -> bool {
if depth > 3 {
return false;
}
let Ok(entries) = std::fs::read_dir(dir) else {
return false;
};
for entry in entries.flatten() {
let path = entry.path();
let Ok(meta) = std::fs::symlink_metadata(&path) else {
continue;
};
if meta.file_type().is_symlink() {
continue;
}
if meta.is_dir() {
if dir_has_kernel(&path, depth + 1) {
return true;
}
} else if meta.is_file() {
let name = entry.file_name().to_string_lossy().to_string();
if name == "linux" || name.starts_with("vmlinuz") || name.starts_with("kernel") {
return true;
}
}
}
false
}
#[cfg(test)]
mod tests {
use super::*;
@ -1027,4 +1076,32 @@ mod tests {
let err = installer.revalidate_target_disk().unwrap_err().to_string();
assert!(err.contains("changed identity"), "{err}");
}
#[test]
fn target_kernel_detection_handles_the_bls_layout() {
let root = tempfile::tempdir().unwrap();
let root = root.path();
// A BLS/machine-id kernel is nested and named `linux`, not `vmlinuz`.
let nested = root.join("efi/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/6.18.48");
std::fs::create_dir_all(&nested).unwrap();
std::fs::write(nested.join("linux"), b"\x1f\x8b").unwrap();
assert!(target_has_kernel(root));
// A BLS entry that names a kernel is also sufficient.
let root2 = tempfile::tempdir().unwrap();
let root2 = root2.path();
let entries = root2.join("efi/loader/entries");
std::fs::create_dir_all(&entries).unwrap();
std::fs::write(
entries.join("deadbeef-6.18.48.conf"),
"title Gentoo\nlinux /aaaa/6.18.48/linux\n",
)
.unwrap();
assert!(target_has_kernel(root2));
// An empty tree has no kernel.
let root3 = tempfile::tempdir().unwrap();
std::fs::create_dir_all(root3.path().join("efi/loader")).unwrap();
assert!(!target_has_kernel(root3.path()));
}
}