Skip to content

Commit cccc2f8

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 711f896 commit cccc2f8

File tree

9 files changed

+253
-12
lines changed

9 files changed

+253
-12
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: 18 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,23 @@ 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(efi_linux) = root.open_dir_optional(EFI_LINUX)? else {
132+
return Ok(false);
133+
};
134+
for entry in efi_linux.entries()? {
135+
let entry = entry?;
136+
let name = entry.file_name();
137+
let name = Path::new(&name);
138+
let extension = name.extension().and_then(|v| v.to_str());
139+
if extension == Some("efi") {
140+
return Ok(true);
141+
}
142+
}
143+
Ok(false)
144+
}
145+
129146
pub fn get_esp_partition(device: &str) -> Result<(String, Option<String>)> {
130147
let device_info = bootc_blockdev::partitions_of(Utf8Path::new(device))?;
131148
let esp = device_info

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

crates/lib/src/install.rs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ pub(crate) struct InstallConfigOpts {
247247
pub(crate) stateroot: Option<String>,
248248
}
249249

250-
#[derive(Debug, Clone, clap::Parser, Serialize, Deserialize, PartialEq, Eq)]
250+
#[derive(Debug, Default, Clone, clap::Parser, Serialize, Deserialize, PartialEq, Eq)]
251251
pub(crate) struct InstallComposefsOpts {
252252
#[clap(long, default_value_t)]
253253
#[serde(default)]
@@ -438,6 +438,10 @@ pub(crate) struct State {
438438
pub(crate) container_root: Dir,
439439
pub(crate) tempdir: TempDir,
440440

441+
/// Set if we have determined that composefs is required
442+
#[allow(dead_code)]
443+
pub(crate) composefs_required: bool,
444+
441445
// If Some, then --composefs_native is passed
442446
#[cfg(feature = "composefs-backend")]
443447
pub(crate) composefs_options: Option<InstallComposefsOpts>,
@@ -793,6 +797,7 @@ async fn install_container(
793797
let sepolicy = sepolicy.as_ref();
794798
let stateroot = state.stateroot();
795799

800+
// TODO factor out this
796801
let (src_imageref, proxy_cfg) = if !state.source.in_host_mountns {
797802
(state.source.imageref.clone(), None)
798803
} else {
@@ -1221,20 +1226,28 @@ async fn verify_target_fetch(
12211226
Ok(())
12221227
}
12231228

1229+
fn root_has_uki(root: &Dir) -> Result<bool> {
1230+
#[cfg(feature = "composefs-backend")]
1231+
return crate::bootc_composefs::boot::container_root_has_uki(root);
1232+
1233+
#[cfg(not(feature = "composefs-backend"))]
1234+
Ok(false)
1235+
}
1236+
12241237
/// Preparation for an install; validates and prepares some (thereafter immutable) global state.
12251238
async fn prepare_install(
12261239
config_opts: InstallConfigOpts,
12271240
source_opts: InstallSourceOpts,
12281241
target_opts: InstallTargetOpts,
1229-
_composefs_opts: Option<InstallComposefsOpts>,
1242+
composefs_options: Option<InstallComposefsOpts>,
12301243
) -> Result<Arc<State>> {
12311244
tracing::trace!("Preparing install");
12321245
let rootfs = cap_std::fs::Dir::open_ambient_dir("/", cap_std::ambient_authority())
12331246
.context("Opening /")?;
12341247

12351248
let host_is_container = crate::containerenv::is_container(&rootfs);
12361249
let external_source = source_opts.source_imgref.is_some();
1237-
let source = match source_opts.source_imgref {
1250+
let (source, target_rootfs) = match source_opts.source_imgref {
12381251
None => {
12391252
ensure!(host_is_container, "Either --source-imgref must be defined or this command must be executed inside a podman container.");
12401253

@@ -1259,11 +1272,13 @@ async fn prepare_install(
12591272
};
12601273
tracing::trace!("Read container engine info {:?}", container_info);
12611274

1262-
SourceInfo::from_container(&rootfs, &container_info)?
1275+
let source = SourceInfo::from_container(&rootfs, &container_info)?;
1276+
(source, Some(rootfs.try_clone()?))
12631277
}
12641278
Some(source) => {
12651279
crate::cli::require_root(false)?;
1266-
SourceInfo::from_imageref(&source, &rootfs)?
1280+
let source = SourceInfo::from_imageref(&source, &rootfs)?;
1281+
(source, None)
12671282
}
12681283
};
12691284

@@ -1291,6 +1306,15 @@ async fn prepare_install(
12911306
};
12921307
tracing::debug!("Target image reference: {target_imgref}");
12931308

1309+
let composefs_required = if let Some(root) = target_rootfs.as_ref() {
1310+
root_has_uki(root)?
1311+
} else {
1312+
false
1313+
};
1314+
tracing::debug!("Composefs required: {composefs_required}");
1315+
let composefs_options =
1316+
composefs_options.or_else(|| composefs_required.then_some(InstallComposefsOpts::default()));
1317+
12941318
// We need to access devices that are set up by the host udev
12951319
bootc_mount::ensure_mirrored_host_mount("/dev")?;
12961320
// We need to read our own container image (and any logically bound images)
@@ -1371,8 +1395,9 @@ async fn prepare_install(
13711395
container_root: rootfs,
13721396
tempdir,
13731397
host_is_container,
1398+
composefs_required,
13741399
#[cfg(feature = "composefs-backend")]
1375-
composefs_options: _composefs_opts,
1400+
composefs_options,
13761401
});
13771402

13781403
Ok(state)

crates/lib/src/podstorage.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,17 @@ fn new_podman_cmd_in(storage_root: &Dir, run_root: &Dir) -> Result<Command> {
127127
Ok(cmd)
128128
}
129129

130+
/// Adjust the provided command (skopeo or podman e.g.) to reference
131+
/// the provided path as an additional image store.
132+
pub fn set_additional_image_store<'c>(
133+
cmd: &'c mut Command,
134+
ais: impl AsRef<Utf8Path>,
135+
) -> &'c mut Command {
136+
let ais = ais.as_ref();
137+
let storage_opt = format!("additionalimagestore={ais}");
138+
cmd.env("STORAGE_OPTS", storage_opt)
139+
}
140+
130141
/// Ensure that "podman" is the first thing to touch the global storage
131142
/// instance. This is a workaround for https://github.com/bootc-dev/bootc/pull/1101#issuecomment-2653862974
132143
/// Basically podman has special upgrade logic for when it is the first thing

0 commit comments

Comments
 (0)