|
| 1 | +//! Build Unified Kernel Images (UKI) using ukify. |
| 2 | +//! |
| 3 | +//! This module provides functionality to build UKIs by computing the necessary |
| 4 | +//! arguments from a container image and invoking the ukify tool. |
| 5 | +
|
| 6 | +use std::ffi::OsString; |
| 7 | +use std::process::Command; |
| 8 | + |
| 9 | +use anyhow::{Context, Result}; |
| 10 | +use bootc_utils::CommandRunExt; |
| 11 | +use camino::Utf8Path; |
| 12 | +use cap_std_ext::cap_std::fs::Dir; |
| 13 | +use fn_error_context::context; |
| 14 | + |
| 15 | +use crate::bootc_composefs::digest::compute_composefs_digest; |
| 16 | + |
| 17 | +/// Build a UKI from the given rootfs. |
| 18 | +/// |
| 19 | +/// This function: |
| 20 | +/// 1. Verifies that ukify is available |
| 21 | +/// 2. Finds the kernel in the rootfs |
| 22 | +/// 3. Computes the composefs digest |
| 23 | +/// 4. Reads kernel arguments from kargs.d |
| 24 | +/// 5. Invokes ukify with computed arguments plus any pass-through args |
| 25 | +#[context("Building UKI")] |
| 26 | +pub(crate) fn build_ukify(rootfs: &Utf8Path, args: Vec<OsString>) -> Result<()> { |
| 27 | + // Verify ukify is available |
| 28 | + if !crate::utils::have_executable("ukify")? { |
| 29 | + anyhow::bail!( |
| 30 | + "ukify executable not found in PATH. Please install systemd-ukify or equivalent." |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + // Open the rootfs directory |
| 35 | + let root = Dir::open_ambient_dir(rootfs, cap_std_ext::cap_std::ambient_authority()) |
| 36 | + .with_context(|| format!("Opening rootfs {rootfs}"))?; |
| 37 | + |
| 38 | + // Find the kernel |
| 39 | + let kernel = crate::kernel::find_kernel(&root)? |
| 40 | + .ok_or_else(|| anyhow::anyhow!("No kernel found in {rootfs}"))?; |
| 41 | + |
| 42 | + // We can only build a UKI from a traditional kernel, not from an existing UKI |
| 43 | + if kernel.unified { |
| 44 | + anyhow::bail!( |
| 45 | + "Cannot build UKI: rootfs already contains a UKI at boot/EFI/Linux/{}.efi", |
| 46 | + kernel.version |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + let kver = &kernel.version; |
| 51 | + |
| 52 | + // Compute paths for kernel and initrd |
| 53 | + let modules_dir = format!("usr/lib/modules/{kver}"); |
| 54 | + let vmlinuz_path = rootfs.join(&modules_dir).join("vmlinuz"); |
| 55 | + let initramfs_path = rootfs.join(&modules_dir).join("initramfs.img"); |
| 56 | + |
| 57 | + // Verify kernel and initramfs exist |
| 58 | + if !root |
| 59 | + .try_exists(format!("{modules_dir}/vmlinuz")) |
| 60 | + .context("Checking for vmlinuz")? |
| 61 | + { |
| 62 | + anyhow::bail!("Kernel not found at {vmlinuz_path}"); |
| 63 | + } |
| 64 | + if !root |
| 65 | + .try_exists(format!("{modules_dir}/initramfs.img")) |
| 66 | + .context("Checking for initramfs")? |
| 67 | + { |
| 68 | + anyhow::bail!("Initramfs not found at {initramfs_path}"); |
| 69 | + } |
| 70 | + |
| 71 | + // Compute the composefs digest |
| 72 | + let composefs_digest = compute_composefs_digest(rootfs, None)?; |
| 73 | + |
| 74 | + // Get kernel arguments from kargs.d |
| 75 | + let kargs = crate::bootc_kargs::get_kargs_in_root(&root, std::env::consts::ARCH)?; |
| 76 | + let kargs_str: Vec<&str> = kargs.iter_str().collect(); |
| 77 | + |
| 78 | + // Build the cmdline: composefs digest + kargs from kargs.d |
| 79 | + let cmdline = if kargs_str.is_empty() { |
| 80 | + format!("composefs={composefs_digest}") |
| 81 | + } else { |
| 82 | + format!("composefs={composefs_digest} {}", kargs_str.join(" ")) |
| 83 | + }; |
| 84 | + |
| 85 | + // Path to os-release (ukify expects @path format for reading from file) |
| 86 | + let os_release_path = rootfs.join("usr/lib/os-release"); |
| 87 | + let os_release_arg = format!("@{os_release_path}"); |
| 88 | + |
| 89 | + // Build the ukify command |
| 90 | + let mut cmd = Command::new("ukify"); |
| 91 | + cmd.arg("build") |
| 92 | + .arg("--linux") |
| 93 | + .arg(&vmlinuz_path) |
| 94 | + .arg("--initrd") |
| 95 | + .arg(&initramfs_path) |
| 96 | + .arg("--uname") |
| 97 | + .arg(kver) |
| 98 | + .arg("--cmdline") |
| 99 | + .arg(&cmdline) |
| 100 | + .arg("--os-release") |
| 101 | + .arg(&os_release_arg); |
| 102 | + |
| 103 | + // Add pass-through arguments |
| 104 | + cmd.args(&args); |
| 105 | + |
| 106 | + tracing::debug!("Executing ukify: {:?}", cmd); |
| 107 | + |
| 108 | + // Run ukify |
| 109 | + cmd.run_inherited().context("Running ukify")?; |
| 110 | + |
| 111 | + Ok(()) |
| 112 | +} |
| 113 | + |
| 114 | +#[cfg(test)] |
| 115 | +mod tests { |
| 116 | + use super::*; |
| 117 | + use std::fs; |
| 118 | + |
| 119 | + #[test] |
| 120 | + fn test_build_ukify_no_kernel() { |
| 121 | + let tempdir = tempfile::tempdir().unwrap(); |
| 122 | + let path = Utf8Path::from_path(tempdir.path()).unwrap(); |
| 123 | + |
| 124 | + let result = build_ukify(path, vec![]); |
| 125 | + assert!(result.is_err()); |
| 126 | + let err = format!("{:#}", result.unwrap_err()); |
| 127 | + assert!( |
| 128 | + err.contains("No kernel found") || err.contains("ukify executable not found"), |
| 129 | + "Unexpected error message: {err}" |
| 130 | + ); |
| 131 | + } |
| 132 | + |
| 133 | + #[test] |
| 134 | + fn test_build_ukify_already_uki() { |
| 135 | + let tempdir = tempfile::tempdir().unwrap(); |
| 136 | + let path = Utf8Path::from_path(tempdir.path()).unwrap(); |
| 137 | + |
| 138 | + // Create a UKI structure |
| 139 | + fs::create_dir_all(tempdir.path().join("boot/EFI/Linux")).unwrap(); |
| 140 | + fs::write(tempdir.path().join("boot/EFI/Linux/test.efi"), b"fake uki").unwrap(); |
| 141 | + |
| 142 | + let result = build_ukify(path, vec![]); |
| 143 | + assert!(result.is_err()); |
| 144 | + let err = format!("{:#}", result.unwrap_err()); |
| 145 | + assert!( |
| 146 | + err.contains("already contains a UKI") || err.contains("ukify executable not found"), |
| 147 | + "Unexpected error message: {err}" |
| 148 | + ); |
| 149 | + } |
| 150 | +} |
0 commit comments