|
| 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_kernel_cmdline::utf8::Cmdline; |
| 11 | +use bootc_utils::CommandRunExt; |
| 12 | +use camino::Utf8Path; |
| 13 | +use cap_std_ext::cap_std::fs::Dir; |
| 14 | +use fn_error_context::context; |
| 15 | + |
| 16 | +use crate::bootc_composefs::digest::compute_composefs_digest; |
| 17 | +use crate::composefs_consts::COMPOSEFS_CMDLINE; |
| 18 | + |
| 19 | +/// Build a UKI from the given rootfs. |
| 20 | +/// |
| 21 | +/// This function: |
| 22 | +/// 1. Verifies that ukify is available |
| 23 | +/// 2. Finds the kernel in the rootfs |
| 24 | +/// 3. Computes the composefs digest |
| 25 | +/// 4. Reads kernel arguments from kargs.d |
| 26 | +/// 5. Appends any additional kargs provided via --karg |
| 27 | +/// 6. Invokes ukify with computed arguments plus any pass-through args |
| 28 | +#[context("Building UKI")] |
| 29 | +pub(crate) fn build_ukify( |
| 30 | + rootfs: &Utf8Path, |
| 31 | + extra_kargs: &[String], |
| 32 | + args: &[OsString], |
| 33 | +) -> Result<()> { |
| 34 | + // Warn if --karg is used (temporary workaround) |
| 35 | + if !extra_kargs.is_empty() { |
| 36 | + tracing::warn!( |
| 37 | + "The --karg flag is temporary and will be removed as soon as possible \ |
| 38 | + (https://github.com/bootc-dev/bootc/issues/1826)" |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + // Verify ukify is available |
| 43 | + if !crate::utils::have_executable("ukify")? { |
| 44 | + anyhow::bail!( |
| 45 | + "ukify executable not found in PATH. Please install systemd-ukify or equivalent." |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + // Open the rootfs directory |
| 50 | + let root = Dir::open_ambient_dir(rootfs, cap_std_ext::cap_std::ambient_authority()) |
| 51 | + .with_context(|| format!("Opening rootfs {rootfs}"))?; |
| 52 | + |
| 53 | + // Find the kernel |
| 54 | + let kernel = crate::kernel::find_kernel(&root)? |
| 55 | + .ok_or_else(|| anyhow::anyhow!("No kernel found in {rootfs}"))?; |
| 56 | + |
| 57 | + // We can only build a UKI from a traditional kernel, not from an existing UKI |
| 58 | + if kernel.unified { |
| 59 | + anyhow::bail!( |
| 60 | + "Cannot build UKI: rootfs already contains a UKI at boot/EFI/Linux/{}.efi", |
| 61 | + kernel.version |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + // Get paths from the kernel info |
| 66 | + let vmlinuz_path = kernel |
| 67 | + .vmlinuz_path() |
| 68 | + .ok_or_else(|| anyhow::anyhow!("Traditional kernel should have vmlinuz path"))?; |
| 69 | + let initramfs_path = kernel |
| 70 | + .initramfs_path() |
| 71 | + .ok_or_else(|| anyhow::anyhow!("Traditional kernel should have initramfs path"))?; |
| 72 | + |
| 73 | + // Verify kernel and initramfs exist |
| 74 | + if !root |
| 75 | + .try_exists(&vmlinuz_path) |
| 76 | + .context("Checking for vmlinuz")? |
| 77 | + { |
| 78 | + anyhow::bail!("Kernel not found at {vmlinuz_path}"); |
| 79 | + } |
| 80 | + if !root |
| 81 | + .try_exists(&initramfs_path) |
| 82 | + .context("Checking for initramfs")? |
| 83 | + { |
| 84 | + anyhow::bail!("Initramfs not found at {initramfs_path}"); |
| 85 | + } |
| 86 | + |
| 87 | + // Compute the composefs digest |
| 88 | + let composefs_digest = compute_composefs_digest(rootfs, None)?; |
| 89 | + |
| 90 | + // Get kernel arguments from kargs.d |
| 91 | + let mut cmdline = crate::bootc_kargs::get_kargs_in_root(&root, std::env::consts::ARCH)?; |
| 92 | + |
| 93 | + // Add the composefs digest |
| 94 | + let composefs_param = format!("{COMPOSEFS_CMDLINE}={composefs_digest}"); |
| 95 | + cmdline.extend(&Cmdline::from(composefs_param)); |
| 96 | + |
| 97 | + // Add any extra kargs provided via --karg |
| 98 | + for karg in extra_kargs { |
| 99 | + cmdline.extend(&Cmdline::from(karg)); |
| 100 | + } |
| 101 | + |
| 102 | + let cmdline_str = cmdline.to_string(); |
| 103 | + |
| 104 | + // Build the ukify command with cwd set to rootfs so paths can be relative |
| 105 | + let mut cmd = Command::new("ukify"); |
| 106 | + cmd.current_dir(rootfs); |
| 107 | + cmd.arg("build") |
| 108 | + .arg("--linux") |
| 109 | + .arg(&vmlinuz_path) |
| 110 | + .arg("--initrd") |
| 111 | + .arg(&initramfs_path) |
| 112 | + .arg("--uname") |
| 113 | + .arg(&kernel.version) |
| 114 | + .arg("--cmdline") |
| 115 | + .arg(&cmdline_str) |
| 116 | + .arg("--os-release") |
| 117 | + .arg("@usr/lib/os-release"); |
| 118 | + |
| 119 | + // Add pass-through arguments |
| 120 | + cmd.args(args); |
| 121 | + |
| 122 | + tracing::debug!("Executing ukify: {:?}", cmd); |
| 123 | + |
| 124 | + // Run ukify |
| 125 | + cmd.run_inherited().context("Running ukify")?; |
| 126 | + |
| 127 | + Ok(()) |
| 128 | +} |
| 129 | + |
| 130 | +#[cfg(test)] |
| 131 | +mod tests { |
| 132 | + use super::*; |
| 133 | + use std::fs; |
| 134 | + |
| 135 | + #[test] |
| 136 | + fn test_build_ukify_no_kernel() { |
| 137 | + let tempdir = tempfile::tempdir().unwrap(); |
| 138 | + let path = Utf8Path::from_path(tempdir.path()).unwrap(); |
| 139 | + |
| 140 | + let result = build_ukify(path, &[], &[]); |
| 141 | + assert!(result.is_err()); |
| 142 | + let err = format!("{:#}", result.unwrap_err()); |
| 143 | + assert!( |
| 144 | + err.contains("No kernel found") || err.contains("ukify executable not found"), |
| 145 | + "Unexpected error message: {err}" |
| 146 | + ); |
| 147 | + } |
| 148 | + |
| 149 | + #[test] |
| 150 | + fn test_build_ukify_already_uki() { |
| 151 | + let tempdir = tempfile::tempdir().unwrap(); |
| 152 | + let path = Utf8Path::from_path(tempdir.path()).unwrap(); |
| 153 | + |
| 154 | + // Create a UKI structure |
| 155 | + fs::create_dir_all(tempdir.path().join("boot/EFI/Linux")).unwrap(); |
| 156 | + fs::write(tempdir.path().join("boot/EFI/Linux/test.efi"), b"fake uki").unwrap(); |
| 157 | + |
| 158 | + let result = build_ukify(path, &[], &[]); |
| 159 | + assert!(result.is_err()); |
| 160 | + let err = format!("{:#}", result.unwrap_err()); |
| 161 | + assert!( |
| 162 | + err.contains("already contains a UKI") || err.contains("ukify executable not found"), |
| 163 | + "Unexpected error message: {err}" |
| 164 | + ); |
| 165 | + } |
| 166 | +} |
0 commit comments