-
Notifications
You must be signed in to change notification settings - Fork 192
Expand file tree
/
Copy pathkernel.rs
More file actions
227 lines (204 loc) · 7.82 KB
/
kernel.rs
File metadata and controls
227 lines (204 loc) · 7.82 KB
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
//! Kernel detection for container images.
//!
//! This module provides functionality to detect kernel information in container
//! images, supporting both traditional kernels (with separate vmlinuz/initrd) and
//! Unified Kernel Images (UKI).
use std::path::Path;
use anyhow::Result;
use camino::Utf8PathBuf;
use cap_std_ext::cap_std::fs::Dir;
use cap_std_ext::dirext::CapStdExtDirExt;
use fn_error_context::context;
use serde::Serialize;
use crate::bootc_composefs::boot::EFI_LINUX;
/// Information about the kernel in a container image.
#[derive(Debug, Serialize)]
#[serde(rename_all = "kebab-case")]
pub(crate) struct Kernel {
/// The kernel version identifier. For traditional kernels, this is derived from the
/// `/usr/lib/modules/<version>` directory name. For UKI images, this is the UKI filename
/// (without the .efi extension).
pub(crate) version: String,
/// Whether the kernel is packaged as a UKI (Unified Kernel Image).
pub(crate) unified: bool,
}
/// Path to kernel component(s)
///
/// UKI kernels only have the single PE binary, whereas
/// traditional "vmlinuz" kernels have distinct kernel and
/// initramfs.
pub(crate) enum KernelPath {
Uki(Utf8PathBuf),
Vmlinuz {
path: Utf8PathBuf,
initramfs: Utf8PathBuf,
},
}
/// Internal-only kernel wrapper with extra path information that are
/// useful but we don't want to leak out via serialization to
/// inspection.
///
/// `Kernel` implements `From<KernelInternal>` so we can just `.into()`
/// to get the "public" form where needed.
pub(crate) struct KernelInternal {
pub(crate) kernel: Kernel,
pub(crate) path: KernelPath,
}
impl From<KernelInternal> for Kernel {
fn from(kernel_internal: KernelInternal) -> Self {
kernel_internal.kernel
}
}
/// Find the kernel in a container image root directory.
///
/// This function first attempts to find a UKI in `/boot/EFI/Linux/*.efi`.
/// If that doesn't exist, it falls back to looking for a traditional kernel
/// layout with `/usr/lib/modules/<version>/vmlinuz`.
///
/// Returns `None` if no kernel is found.
#[context("Finding kernel")]
pub(crate) fn find_kernel(root: &Dir) -> Result<Option<KernelInternal>> {
// First, try to find a UKI
if let Some(uki_path) = find_uki_path(root)? {
let version = uki_path.file_stem().unwrap_or(uki_path.as_str()).to_owned();
return Ok(Some(KernelInternal {
kernel: Kernel {
version,
unified: true,
},
path: KernelPath::Uki(uki_path),
}));
}
// Fall back to checking for a traditional kernel via ostree_ext
if let Some(modules_dir) = ostree_ext::bootabletree::find_kernel_dir_fs(root)? {
let version = modules_dir
.file_name()
.ok_or_else(|| anyhow::anyhow!("kernel dir should have a file name: {modules_dir}"))?
.to_owned();
let vmlinuz = modules_dir.join("vmlinuz");
let initramfs = modules_dir.join("initramfs.img");
return Ok(Some(KernelInternal {
kernel: Kernel {
version,
unified: false,
},
path: KernelPath::Vmlinuz {
path: vmlinuz,
initramfs,
},
}));
}
Ok(None)
}
/// Returns the path to the first UKI found in the container root, if any.
///
/// Looks in `/boot/EFI/Linux/*.efi`. If multiple UKIs are present, returns
/// the first one in sorted order for determinism.
fn find_uki_path(root: &Dir) -> Result<Option<Utf8PathBuf>> {
let Some(boot) = root.open_dir_optional(crate::install::BOOT)? else {
return Ok(None);
};
let Some(efi_linux) = boot.open_dir_optional(EFI_LINUX)? else {
return Ok(None);
};
let mut uki_files = Vec::new();
for entry in efi_linux.entries()? {
let entry = entry?;
let name = entry.file_name();
let name_path = Path::new(&name);
let extension = name_path.extension().and_then(|v| v.to_str());
if extension == Some("efi") {
if let Some(name_str) = name.to_str() {
uki_files.push(name_str.to_owned());
}
}
}
// Sort for deterministic behavior when multiple UKIs are present
uki_files.sort();
Ok(uki_files
.into_iter()
.next()
.map(|filename| Utf8PathBuf::from(format!("boot/{EFI_LINUX}/{filename}"))))
}
#[cfg(test)]
mod tests {
use super::*;
use cap_std_ext::{cap_std, cap_tempfile, dirext::CapStdExtDirExt};
#[test]
fn test_find_kernel_none() -> Result<()> {
let tempdir = cap_tempfile::tempdir(cap_std::ambient_authority())?;
assert!(find_kernel(&tempdir)?.is_none());
Ok(())
}
#[test]
fn test_find_kernel_traditional() -> Result<()> {
let tempdir = cap_tempfile::tempdir(cap_std::ambient_authority())?;
tempdir.create_dir_all("usr/lib/modules/6.12.0-100.fc41.x86_64")?;
tempdir.atomic_write(
"usr/lib/modules/6.12.0-100.fc41.x86_64/vmlinuz",
b"fake kernel",
)?;
let kernel_internal = find_kernel(&tempdir)?.expect("should find kernel");
assert_eq!(kernel_internal.kernel.version, "6.12.0-100.fc41.x86_64");
assert!(!kernel_internal.kernel.unified);
match &kernel_internal.path {
KernelPath::Vmlinuz { path, initramfs } => {
assert_eq!(
path.as_str(),
"usr/lib/modules/6.12.0-100.fc41.x86_64/vmlinuz"
);
assert_eq!(
initramfs.as_str(),
"usr/lib/modules/6.12.0-100.fc41.x86_64/initramfs.img"
);
}
KernelPath::Uki(_) => panic!("Expected Vmlinuz, got Uki"),
}
Ok(())
}
#[test]
fn test_find_kernel_uki() -> Result<()> {
let tempdir = cap_tempfile::tempdir(cap_std::ambient_authority())?;
tempdir.create_dir_all("boot/EFI/Linux")?;
tempdir.atomic_write("boot/EFI/Linux/fedora-6.12.0.efi", b"fake uki")?;
let kernel_internal = find_kernel(&tempdir)?.expect("should find kernel");
assert_eq!(kernel_internal.kernel.version, "fedora-6.12.0");
assert!(kernel_internal.kernel.unified);
match &kernel_internal.path {
KernelPath::Uki(path) => {
assert_eq!(path.as_str(), "boot/EFI/Linux/fedora-6.12.0.efi");
}
KernelPath::Vmlinuz { .. } => panic!("Expected Uki, got Vmlinuz"),
}
Ok(())
}
#[test]
fn test_find_kernel_uki_takes_precedence() -> Result<()> {
let tempdir = cap_tempfile::tempdir(cap_std::ambient_authority())?;
// Both traditional and UKI exist
tempdir.create_dir_all("usr/lib/modules/6.12.0-100.fc41.x86_64")?;
tempdir.atomic_write(
"usr/lib/modules/6.12.0-100.fc41.x86_64/vmlinuz",
b"fake kernel",
)?;
tempdir.create_dir_all("boot/EFI/Linux")?;
tempdir.atomic_write("boot/EFI/Linux/fedora-6.12.0.efi", b"fake uki")?;
let kernel_internal = find_kernel(&tempdir)?.expect("should find kernel");
// UKI should take precedence
assert_eq!(kernel_internal.kernel.version, "fedora-6.12.0");
assert!(kernel_internal.kernel.unified);
Ok(())
}
#[test]
fn test_find_uki_path_sorted() -> Result<()> {
let tempdir = cap_tempfile::tempdir(cap_std::ambient_authority())?;
tempdir.create_dir_all("boot/EFI/Linux")?;
tempdir.atomic_write("boot/EFI/Linux/zzz.efi", b"fake uki")?;
tempdir.atomic_write("boot/EFI/Linux/aaa.efi", b"fake uki")?;
tempdir.atomic_write("boot/EFI/Linux/mmm.efi", b"fake uki")?;
// Should return first in sorted order
let path = find_uki_path(&tempdir)?.expect("should find uki");
assert_eq!(path.as_str(), "boot/EFI/Linux/aaa.efi");
Ok(())
}
}