Skip to content

Commit 2067e47

Browse files
committed
Various composefs enhancements
- Add build infrastructure to toplevel to seal - Change the install logic to detect UKIs and automatically enable composefs Signed-off-by: Colin Walters <[email protected]>
1 parent 6e19512 commit 2067e47

File tree

9 files changed

+288
-13
lines changed

9 files changed

+288
-13
lines changed

Cargo.lock

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

Dockerfile.cfsuki

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Override via --build-arg=base=<image> to use a different base
2+
ARG base=localhost/bootc
3+
# This is where we get the tools to build the UKI
4+
ARG buildroot=quay.io/fedora/fedora:42
5+
FROM $base AS base
6+
7+
FROM $buildroot as buildroot-base
8+
RUN <<EORUN
9+
set -xeuo pipefail
10+
dnf install -y systemd-ukify sbsigntools systemd-boot-unsigned
11+
dnf clean all
12+
EORUN
13+
14+
# This must be provided and computed via cfs oci compute-id
15+
ARG COMPOSEFS_FSVERITY
16+
17+
FROM buildroot-base as kernel
18+
RUN --mount=type=secret,id=key \
19+
--mount=type=secret,id=cert \
20+
--mount=type=bind,from=base,target=/target \
21+
<<EOF
22+
set -eux
23+
24+
# Inject the composefs kernel argument and specify a root with the x86_64 DPS UUID.
25+
# TODO: Discoverable partition fleshed out, or drop root UUID as systemd-stub extension
26+
# TODO: https://github.com/containers/composefs-rs/issues/183
27+
cmdline="composefs=${COMPOSEFS_FSVERITY} root=UUID=4f68bce3-e8cd-4db1-96e7-fbcaf984b709 console=ttyS0,114800n8 enforcing=0 rw"
28+
29+
kver=$(cd /target/usr/lib/modules && echo *)
30+
ukify build \
31+
--linux "/target/usr/lib/modules/$kver/vmlinuz" \
32+
--initrd "/target/usr/lib/modules/$kver/initramfs.img" \
33+
--uname="${kver}" \
34+
--cmdline "${cmdline}" \
35+
--os-release "@/target/usr/lib/os-release" \
36+
--signtool sbsign \
37+
--secureboot-private-key "/run/secrets/key" \
38+
--secureboot-certificate "/run/secrets/cert" \
39+
--measure \
40+
--json pretty \
41+
--output "/boot/$kver.efi"
42+
sbsign \
43+
--key "/run/secrets/key" \
44+
--cert "/run/secrets/cert" \
45+
"/usr/lib/systemd/boot/efi/systemd-bootx64.efi" \
46+
--output "/boot/systemd-bootx64.efi"
47+
EOF
48+
49+
FROM base as final
50+
51+
RUN --mount=type=bind,from=kernel,target=/run/kernel <<EOF
52+
kver=$(cd /usr/lib/modules && echo *)
53+
mkdir -p /boot/EFI/Linux
54+
# We put the UKI in /boot for now due to composefs verity not being the
55+
# same due to mtime of /usr/lib/modules being changed
56+
cp /run/kernel/boot/$kver.efi /boot/EFI/Linux/$kver.efi
57+
EOF
58+
59+
FROM base as final-final
60+
COPY --from=final /boot /boot
61+
# Override the default
62+
LABEL containers.bootc=sealed

Justfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,22 @@
1313
build *ARGS:
1414
podman build --jobs=4 -t localhost/bootc {{ARGS}} .
1515

16+
build-sealed *ARGS:
17+
podman build --jobs=4 -t localhost/bootc-unsealed {{ARGS}} .
18+
cargo xtask build-sealed localhost/bootc-unsealed localhost/bootc
19+
1620
# This container image has additional testing content and utilities
1721
build-integration-test-image *ARGS:
1822
cd hack && podman build --jobs=4 -t localhost/bootc-integration -f Containerfile {{ARGS}} .
1923
# Keep these in sync with what's used in hack/lbi
2024
podman pull -q --retry 5 --retry-delay 5s quay.io/curl/curl:latest quay.io/curl/curl-base:latest registry.access.redhat.com/ubi9/podman:latest
2125

26+
# Like build-integration-test-image but sealed
27+
build-sealed-integration-test-image *ARGS:
28+
just build {{ARGS}}
29+
just build-integration-test-image
30+
cargo xtask build-sealed localhost/bootc-integration localhost/bootc-integration-sealed
31+
2232
# Only used by ci.yml right now
2333
build-install-test-image: build-integration-test-image
2434
cd hack && podman build -t localhost/bootc-integration-install -f Containerfile.drop-lbis

crates/lib/src/bootc_composefs/boot.rs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use std::ffi::OsStr;
21
use std::fs::create_dir_all;
32
use std::io::Write;
43
use std::path::Path;
4+
use std::ffi::OsStr;
55

66
use anyhow::{anyhow, Context, Result};
77
use bootc_blockdev::find_parent_devices;
@@ -126,6 +126,26 @@ fi
126126
)
127127
}
128128

129+
/// Returns `true` if detect the target rootfs carries a UKI.
130+
pub(crate) fn container_root_has_uki(root: &Dir) -> Result<bool> {
131+
let Some(boot) = root.open_dir_optional(crate::install::BOOT)? else {
132+
return Ok(false);
133+
};
134+
let Some(efi_linux) = boot.open_dir_optional(EFI_LINUX)? else {
135+
return Ok(false);
136+
};
137+
for entry in efi_linux.entries()? {
138+
let entry = entry?;
139+
let name = entry.file_name();
140+
let name = Path::new(&name);
141+
let extension = name.extension().and_then(|v| v.to_str());
142+
if extension == Some("efi") {
143+
return Ok(true);
144+
}
145+
}
146+
Ok(false)
147+
}
148+
129149
pub fn get_esp_partition(device: &str) -> Result<(String, Option<String>)> {
130150
let device_info = bootc_blockdev::partitions_of(Utf8Path::new(device))?;
131151
let esp = device_info
@@ -1001,3 +1021,34 @@ pub(crate) fn setup_composefs_boot(
10011021

10021022
Ok(())
10031023
}
1024+
1025+
#[cfg(test)]
1026+
mod tests {
1027+
use cap_std_ext::cap_std;
1028+
use super::*;
1029+
1030+
#[test]
1031+
fn test_root_has_uki() -> Result<()> {
1032+
// Test case 1: No boot directory
1033+
let tempdir = cap_std_ext::cap_tempfile::tempdir(cap_std::ambient_authority())?;
1034+
assert_eq!(container_root_has_uki(&tempdir)?, false);
1035+
1036+
// Test case 2: boot directory exists but no EFI/Linux
1037+
tempdir.create_dir(crate::install::BOOT)?;
1038+
assert_eq!(container_root_has_uki(&tempdir)?, false);
1039+
1040+
// Test case 3: boot/EFI/Linux exists but no .efi files
1041+
tempdir.create_dir_all("boot/EFI/Linux")?;
1042+
assert_eq!(container_root_has_uki(&tempdir)?, false);
1043+
1044+
// Test case 4: boot/EFI/Linux exists with non-.efi file
1045+
tempdir.atomic_write("boot/EFI/Linux/readme.txt", b"some file")?;
1046+
assert_eq!(container_root_has_uki(&tempdir)?, false);
1047+
1048+
// Test case 5: boot/EFI/Linux exists with .efi file
1049+
tempdir.atomic_write("boot/EFI/Linux/bootx64.efi", b"fake efi binary")?;
1050+
assert_eq!(container_root_has_uki(&tempdir)?, true);
1051+
1052+
Ok(())
1053+
}
1054+
}

crates/lib/src/cli.rs

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ use std::ffi::{CString, OsStr, OsString};
66
use std::io::Seek;
77
use std::os::unix::process::CommandExt;
88
use std::process::Command;
9+
use std::sync::Arc;
910

1011
use anyhow::{ensure, Context, Result};
11-
use camino::Utf8PathBuf;
12+
use camino::{Utf8Path, Utf8PathBuf};
1213
use cap_std_ext::cap_std;
1314
use cap_std_ext::cap_std::fs::Dir;
1415
use clap::Parser;
1516
use clap::ValueEnum;
17+
use composefs_boot::BootOps as _;
1618
use etc_merge::{compute_diff, print_diff};
1719
use fn_error_context::context;
1820
use indoc::indoc;
@@ -23,11 +25,13 @@ use ostree_ext::composefs::fsverity::FsVerityHashValue;
2325
use ostree_ext::composefs::splitstream::SplitStreamWriter;
2426
use ostree_ext::container as ostree_container;
2527
use ostree_ext::container_utils::ostree_booted;
28+
use ostree_ext::containers_image_proxy::ImageProxyConfig;
2629
use ostree_ext::keyfileext::KeyFileExt;
2730
use ostree_ext::ostree;
2831
use ostree_ext::sysroot::SysrootLock;
2932
use schemars::schema_for;
3033
use serde::{Deserialize, Serialize};
34+
use tempfile::tempdir_in;
3135

3236
#[cfg(feature = "composefs-backend")]
3337
use crate::bootc_composefs::{
@@ -40,9 +44,11 @@ use crate::bootc_composefs::{
4044
};
4145
use crate::deploy::RequiredHostSpec;
4246
use crate::lints;
47+
use crate::podstorage::set_additional_image_store;
4348
use crate::progress_jsonl::{ProgressWriter, RawProgressFd};
4449
use crate::spec::Host;
4550
use crate::spec::ImageReference;
51+
use crate::store::ComposefsRepository;
4652
use crate::utils::sigpolicy_from_opt;
4753

4854
/// Shared progress options
@@ -315,6 +321,12 @@ pub(crate) enum ContainerOpts {
315321
#[clap(long)]
316322
no_truncate: bool,
317323
},
324+
/// Output the bootable composefs digest.
325+
#[clap(hide = true)]
326+
ComputeComposefsDigest {
327+
/// Identifier for image; if not provided, the running image will be used.
328+
image: Option<String>,
329+
},
318330
}
319331

320332
/// Subcommands which operate on images.
@@ -1335,6 +1347,55 @@ async fn run_from_opt(opt: Opt) -> Result<()> {
13351347
)?;
13361348
Ok(())
13371349
}
1350+
ContainerOpts::ComputeComposefsDigest { image } => {
1351+
// Allocate a tempdir
1352+
let td = tempdir_in("/var/tmp")?;
1353+
let td = td.path();
1354+
let td = &Dir::open_ambient_dir(td, cap_std::ambient_authority())?;
1355+
1356+
td.create_dir("repo")?;
1357+
let repo = td.open_dir("repo")?;
1358+
let mut repo =
1359+
ComposefsRepository::open_path(&repo, ".").context("Init cfs repo")?;
1360+
// We don't need to hard require verity on the *host* system, we're just computing a checksum here
1361+
repo.set_insecure(true);
1362+
let repo = &Arc::new(repo);
1363+
1364+
let mut proxycfg = ImageProxyConfig::default();
1365+
1366+
let image = if let Some(image) = image {
1367+
image
1368+
} else {
1369+
let host_container_store = Utf8Path::new("/run/host-container-storage");
1370+
// If no image is provided, assume that we're running in a container in privileged mode
1371+
// with access to the container storage.
1372+
let container_info = crate::containerenv::get_container_execution_info(&root)?;
1373+
let iid = container_info.imageid;
1374+
tracing::debug!("Computing digest of {iid}");
1375+
1376+
if !host_container_store.try_exists()? {
1377+
anyhow::bail!("Must be readonly mount of host container store: {host_container_store}");
1378+
}
1379+
// And ensure we're finding the image in the host storage
1380+
let mut cmd = Command::new("skopeo");
1381+
set_additional_image_store(&mut cmd, "/run/host-container-storage");
1382+
proxycfg.skopeo_cmd = Some(cmd);
1383+
iid
1384+
};
1385+
1386+
let imgref = format!("containers-storage:{image}");
1387+
let (imgid, verity) = composefs_oci::pull(repo, &imgref, None, Some(proxycfg))
1388+
.await
1389+
.context("Pulling image")?;
1390+
let imgid = hex::encode(imgid);
1391+
let mut fs = composefs_oci::image::create_filesystem(repo, &imgid, Some(&verity))
1392+
.context("Populating fs")?;
1393+
fs.transform_for_boot(&repo).context("Preparing for boot")?;
1394+
let id = fs.compute_image_id();
1395+
println!("{}", id.to_hex());
1396+
1397+
Ok(())
1398+
}
13381399
},
13391400
Opt::Image(opts) => match opts {
13401401
ImageOpts::List {

crates/lib/src/generator.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ ExecStart=bootc internals fixup-etc-fstab\n\
139139
#[cfg(test)]
140140
mod tests {
141141
use camino::Utf8Path;
142-
use cap_std_ext::cmdext::CapStdExtCommandExt as _;
143142

144143
use super::*;
145144

0 commit comments

Comments
 (0)