Skip to content

WIP: composefs branch #1444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 19 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
5885686
WIP: composefs backend
Johan-Liebert1 May 7, 2025
d9b384d
Rework composefs_booted to use kernel cmdline
cgwalters Aug 7, 2025
93a6d75
composefs/status: Read UKI entries to check for queued rollback
Johan-Liebert1 Jul 27, 2025
dc862a2
parser/grub: Use String instead of &str
Johan-Liebert1 Jul 27, 2025
7a10221
composefs/rollback: Handle UKI rollback
Johan-Liebert1 Jul 27, 2025
d3e8d36
composefs/state: Use atomic writes for origin and staged deployment f…
Johan-Liebert1 Jul 28, 2025
8e410f1
composefs/boot/bls: Handle duplicate VMLinuz + Initrd
Johan-Liebert1 Jul 28, 2025
805a42d
parser/bls: `impl Display` for BLSConfig
Johan-Liebert1 Jul 28, 2025
009ee03
lib/composefs: Centralize constants
Johan-Liebert1 Jul 29, 2025
1b99733
composefs/state: Name state directory `default`
Johan-Liebert1 Jul 29, 2025
a53ca13
parser/bls: Add tests for bls parser
Johan-Liebert1 Jul 29, 2025
08b1cad
install/composefs/uki: Write only staged + booted menuentry on upgrade
Johan-Liebert1 Jul 31, 2025
d425878
rollback/composefs: Print whether we are reverting the queued rollback
Johan-Liebert1 Jul 31, 2025
521a2ed
refactor: Pass boot dir to boot entry readers
Johan-Liebert1 Aug 1, 2025
2018de3
test: Add tests for reading boot entries
Johan-Liebert1 Aug 1, 2025
95bbfe6
Drop duplicate bls_config
cgwalters Aug 12, 2025
b091e0f
Merge pull request #1502 from cgwalters/composefs-add-lost-commits
cgwalters Aug 12, 2025
06693d6
install: Use read_file from composefs-boot
cgwalters Aug 13, 2025
e2b19f9
Merge pull request #1507 from cgwalters/composefs-more-cleanups
cgwalters Aug 13, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions crates/lib/src/bootloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,25 @@ pub(crate) fn install_via_bootupd(
device: &PartitionTable,
rootfs: &Utf8Path,
configopts: &crate::install::InstallConfigOpts,
deployment_path: &str,
deployment_path: Option<&str>,
) -> Result<()> {
let verbose = std::env::var_os("BOOTC_BOOTLOADER_DEBUG").map(|_| "-vvvv");
// bootc defaults to only targeting the platform boot method.
let bootupd_opts = (!configopts.generic_image).then_some(["--update-firmware", "--auto"]);

let srcroot = rootfs.join(deployment_path);
let abs_deployment_path = deployment_path.map(|v| rootfs.join(v));
let src_root_arg = if let Some(p) = abs_deployment_path.as_deref() {
vec!["--src-root", p.as_str()]
} else {
vec![]
};
let devpath = device.path();
println!("Installing bootloader via bootupd");
Command::new("bootupctl")
.args(["backend", "install", "--write-uuid"])
.args(verbose)
.args(bootupd_opts.iter().copied().flatten())
.args(["--src-root", srcroot.as_str()])
.args(src_root_arg)
.args(["--device", devpath.as_str(), rootfs.as_str()])
.log_debug()
.run_inherited_with_cmd_context()
Expand Down
167 changes: 157 additions & 10 deletions crates/lib/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@ use ostree_ext::ostree;
use schemars::schema_for;
use serde::{Deserialize, Serialize};

use crate::deploy::RequiredHostSpec;
use crate::deploy::{composefs_rollback, RequiredHostSpec};
use crate::install::{
pull_composefs_repo, setup_composefs_bls_boot, setup_composefs_uki_boot, write_composefs_state,
BootSetupType, BootType,
};
use crate::lints;
use crate::progress_jsonl::{ProgressWriter, RawProgressFd};
use crate::spec::Host;
use crate::spec::ImageReference;
use crate::status::{composefs_booted, composefs_deployment_status};
use crate::utils::sigpolicy_from_opt;

/// Shared progress options
Expand Down Expand Up @@ -778,6 +783,69 @@ fn prepare_for_write() -> Result<()> {
Ok(())
}

#[context("Upgrading composefs")]
async fn upgrade_composefs(_opts: UpgradeOpts) -> Result<()> {
// TODO: IMPORTANT Have all the checks here that `bootc upgrade` has for an ostree booted system

let host = composefs_deployment_status()
.await
.context("Getting composefs deployment status")?;

// TODO: IMPORTANT We need to check if any deployment is staged and get the image from that
let imgref = host
.spec
.image
.as_ref()
.ok_or_else(|| anyhow::anyhow!("No image source specified"))?;

// let booted_image = host
// .status
// .booted
// .ok_or(anyhow::anyhow!("Could not find booted image"))?
// .image
// .ok_or(anyhow::anyhow!("Could not find booted image"))?;

// tracing::debug!("booted_image: {booted_image:#?}");
// tracing::debug!("imgref: {imgref:#?}");

// let digest = booted_image
// .digest()
// .context("Getting digest for booted image")?;

let (repo, entries, id) = pull_composefs_repo(&imgref.transport, &imgref.image).await?;

let Some(entry) = entries.into_iter().next() else {
anyhow::bail!("No boot entries!");
};

let boot_type = BootType::from(&entry);
let mut boot_digest = None;

match boot_type {
BootType::Bls => {
boot_digest = Some(setup_composefs_bls_boot(
BootSetupType::Upgrade,
repo,
&id,
entry,
)?)
}

BootType::Uki => setup_composefs_uki_boot(BootSetupType::Upgrade, repo, &id, entry)?,
};

write_composefs_state(
&Utf8PathBuf::from("/sysroot"),
id,
imgref,
true,
boot_type,
boot_digest,
)?;

Ok(())
}

/// Implementation of the `bootc upgrade` CLI command.
#[context("Upgrading")]
async fn upgrade(opts: UpgradeOpts) -> Result<()> {
Expand Down Expand Up @@ -891,9 +959,7 @@ async fn upgrade(opts: UpgradeOpts) -> Result<()> {
Ok(())
}

/// Implementation of the `bootc switch` CLI command.
#[context("Switching")]
async fn switch(opts: SwitchOpts) -> Result<()> {
fn imgref_for_switch(opts: &SwitchOpts) -> Result<ImageReference> {
let transport = ostree_container::Transport::try_from(opts.transport.as_str())?;
let imgref = ostree_container::ImageReference {
transport,
Expand All @@ -902,6 +968,72 @@ async fn switch(opts: SwitchOpts) -> Result<()> {
let sigverify = sigpolicy_from_opt(opts.enforce_container_sigpolicy);
let target = ostree_container::OstreeImageReference { sigverify, imgref };
let target = ImageReference::from(target);

return Ok(target);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

In Rust, it's more idiomatic to omit the return keyword for the final expression in a function.

    Ok(target)

}

#[context("Composefs Switching")]
async fn switch_composefs(opts: SwitchOpts) -> Result<()> {
let target = imgref_for_switch(&opts)?;
// TODO: Handle in-place

let host = composefs_deployment_status()
.await
.context("Getting composefs deployment status")?;

let new_spec = {
let mut new_spec = host.spec.clone();
new_spec.image = Some(target.clone());
new_spec
};

if new_spec == host.spec {
println!("Image specification is unchanged.");
return Ok(());
}

let Some(target_imgref) = new_spec.image else {
anyhow::bail!("Target image is undefined")
};

let (repo, entries, id) = pull_composefs_repo(&"docker".into(), &target_imgref.image).await?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The transport is hardcoded to "docker". The target_imgref variable already contains the correct transport from the user's options. Using a hardcoded value will likely cause switch to fail for other transports.

    let (repo, entries, id) = pull_composefs_repo(&target_imgref.transport, &target_imgref.image).await?;


let Some(entry) = entries.into_iter().next() else {
anyhow::bail!("No boot entries!");
};

let boot_type = BootType::from(&entry);
let mut boot_digest = None;

match boot_type {
BootType::Bls => {
boot_digest = Some(setup_composefs_bls_boot(
BootSetupType::Upgrade,
repo,
&id,
entry,
)?)
}
BootType::Uki => setup_composefs_uki_boot(BootSetupType::Upgrade, repo, &id, entry)?,
};

write_composefs_state(
&Utf8PathBuf::from("/sysroot"),
id,
&target_imgref,
true,
boot_type,
boot_digest,
)?;

Ok(())
}

/// Implementation of the `bootc switch` CLI command.
#[context("Switching")]
async fn switch(opts: SwitchOpts) -> Result<()> {
let target = imgref_for_switch(&opts)?;

let prog: ProgressWriter = opts.progress.try_into()?;

// If we're doing an in-place mutation, we shortcut most of the rest of the work here
Expand Down Expand Up @@ -966,8 +1098,12 @@ async fn switch(opts: SwitchOpts) -> Result<()> {
/// Implementation of the `bootc rollback` CLI command.
#[context("Rollback")]
async fn rollback(opts: RollbackOpts) -> Result<()> {
let sysroot = &get_storage().await?;
crate::deploy::rollback(sysroot).await?;
if composefs_booted()?.is_some() {
composefs_rollback().await?
} else {
let sysroot = &get_storage().await?;
crate::deploy::rollback(sysroot).await?;
};

if opts.apply {
crate::reboot::reboot()?;
Expand Down Expand Up @@ -1117,8 +1253,20 @@ impl Opt {
async fn run_from_opt(opt: Opt) -> Result<()> {
let root = &Dir::open_ambient_dir("/", cap_std::ambient_authority())?;
match opt {
Opt::Upgrade(opts) => upgrade(opts).await,
Opt::Switch(opts) => switch(opts).await,
Opt::Upgrade(opts) => {
if composefs_booted()?.is_some() {
upgrade_composefs(opts).await
} else {
upgrade(opts).await
}
}
Opt::Switch(opts) => {
if composefs_booted()?.is_some() {
switch_composefs(opts).await
} else {
switch(opts).await
}
}
Opt::Rollback(opts) => rollback(opts).await,
Opt::Edit(opts) => edit(opts).await,
Opt::UsrOverlay => usroverlay().await,
Expand Down Expand Up @@ -1259,8 +1407,7 @@ async fn run_from_opt(opt: Opt) -> Result<()> {
FsverityOpts::Enable { path } => {
let fd =
std::fs::File::open(&path).with_context(|| format!("Reading {path}"))?;
// Note this is not robust to forks, we're not using the _maybe_copy variant
fsverity::enable_verity_with_retry::<fsverity::Sha256HashValue>(&fd)?;
fsverity::enable_verity_raw::<fsverity::Sha256HashValue>(&fd)?;
Ok(())
}
},
Expand Down
35 changes: 35 additions & 0 deletions crates/lib/src/composefs_consts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/// composefs= paramter in kernel cmdline
pub const COMPOSEFS_CMDLINE: &str = "composefs";

/// Directory to store transient state, such as staged deployemnts etc
pub(crate) const COMPOSEFS_TRANSIENT_STATE_DIR: &str = "/run/composefs";
/// File created in /run/composefs to record a staged-deployment
pub(crate) const COMPOSEFS_STAGED_DEPLOYMENT_FNAME: &str = "staged-deployment";

/// Absolute path to composefs-native state directory
pub(crate) const STATE_DIR_ABS: &str = "/sysroot/state/deploy";
/// Relative path to composefs-native state directory. Relative to /sysroot
pub(crate) const STATE_DIR_RELATIVE: &str = "state/deploy";
/// Relative path to the shared 'var' directory. Relative to /sysroot
pub(crate) const SHARED_VAR_PATH: &str = "state/os/default/var";

/// Section in .origin file to store boot related metadata
pub(crate) const ORIGIN_KEY_BOOT: &str = "boot";
/// Whether the deployment was booted with BLS or UKI
pub(crate) const ORIGIN_KEY_BOOT_TYPE: &str = "boot_type";
/// Key to store the SHA256 sum of vmlinuz + initrd for a deployment
pub(crate) const ORIGIN_KEY_BOOT_DIGEST: &str = "digest";

/// Filename for `loader/entries`
pub(crate) const BOOT_LOADER_ENTRIES: &str = "entries";
/// Filename for staged boot loader entries
pub(crate) const STAGED_BOOT_LOADER_ENTRIES: &str = "entries.staged";
/// Filename for rollback boot loader entries
pub(crate) const ROLLBACK_BOOT_LOADER_ENTRIES: &str = STAGED_BOOT_LOADER_ENTRIES;

/// Filename for grub user config
pub(crate) const USER_CFG: &str = "user.cfg";
/// Filename for staged grub user config
pub(crate) const USER_CFG_STAGED: &str = "user.cfg.staged";
/// Filename for rollback grub user config
pub(crate) const USER_CFG_ROLLBACK: &str = USER_CFG_STAGED;
Loading
Loading