Skip to content

Commit d9b384d

Browse files
committed
Rework composefs_booted to use kernel cmdline
Signed-off-by: Colin Walters <[email protected]>
1 parent 5885686 commit d9b384d

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

crates/lib/src/cli.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use ostree_ext::composefs::fsverity;
2121
use ostree_ext::composefs::fsverity::FsVerityHashValue;
2222
use ostree_ext::composefs::splitstream::SplitStreamWriter;
2323
use ostree_ext::container as ostree_container;
24-
use ostree_ext::container_utils::{composefs_booted, ostree_booted};
24+
use ostree_ext::container_utils::ostree_booted;
2525
use ostree_ext::keyfileext::KeyFileExt;
2626
use ostree_ext::ostree;
2727
use schemars::schema_for;
@@ -36,7 +36,7 @@ use crate::lints;
3636
use crate::progress_jsonl::{ProgressWriter, RawProgressFd};
3737
use crate::spec::Host;
3838
use crate::spec::ImageReference;
39-
use crate::status::composefs_deployment_status;
39+
use crate::status::{composefs_booted, composefs_deployment_status};
4040
use crate::utils::sigpolicy_from_opt;
4141

4242
/// Shared progress options
@@ -1073,7 +1073,7 @@ async fn switch(opts: SwitchOpts) -> Result<()> {
10731073
/// Implementation of the `bootc rollback` CLI command.
10741074
#[context("Rollback")]
10751075
async fn rollback(opts: RollbackOpts) -> Result<()> {
1076-
if composefs_booted()? {
1076+
if composefs_booted()?.is_some() {
10771077
composefs_rollback().await?
10781078
} else {
10791079
let sysroot = &get_storage().await?;
@@ -1229,14 +1229,14 @@ async fn run_from_opt(opt: Opt) -> Result<()> {
12291229
let root = &Dir::open_ambient_dir("/", cap_std::ambient_authority())?;
12301230
match opt {
12311231
Opt::Upgrade(opts) => {
1232-
if composefs_booted()? {
1232+
if composefs_booted()?.is_some() {
12331233
upgrade_composefs(opts).await
12341234
} else {
12351235
upgrade(opts).await
12361236
}
12371237
}
12381238
Opt::Switch(opts) => {
1239-
if composefs_booted()? {
1239+
if composefs_booted()?.is_some() {
12401240
switch_composefs(opts).await
12411241
} else {
12421242
switch(opts).await

crates/lib/src/status.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::io::IsTerminal;
44
use std::io::Read;
55
use std::io::Write;
66
use std::str::FromStr;
7+
use std::sync::OnceLock;
78

89
use anyhow::{Context, Result};
910
use bootc_utils::try_deserialize_timestamp;
@@ -14,7 +15,6 @@ use ostree::glib;
1415
use ostree_container::OstreeImageReference;
1516
use ostree_ext::container as ostree_container;
1617
use ostree_ext::container::deploy::ORIGIN_CONTAINER;
17-
use ostree_ext::container_utils::composefs_booted;
1818
use ostree_ext::container_utils::ostree_booted;
1919
use ostree_ext::containers_image_proxy;
2020
use ostree_ext::keyfileext::KeyFileExt;
@@ -56,6 +56,21 @@ impl From<ImageSignature> for ostree_container::SignatureSource {
5656
}
5757
}
5858

59+
/// Detect if we have composefs=<digest> in /proc/cmdline
60+
pub(crate) fn composefs_booted() -> Result<Option<&'static str>> {
61+
static CACHED_DIGEST_VALUE: OnceLock<Option<String>> = OnceLock::new();
62+
if let Some(v) = CACHED_DIGEST_VALUE.get() {
63+
return Ok(v.as_deref());
64+
}
65+
let cmdline = crate::kernel_cmdline::Cmdline::from_proc()?;
66+
let Some(kv) = cmdline.find_str("composefs") else {
67+
return Ok(None);
68+
};
69+
let Some(v) = kv.value else { return Ok(None) };
70+
let r = CACHED_DIGEST_VALUE.get_or_init(|| Some(v.to_owned()));
71+
Ok(r.as_deref())
72+
}
73+
5974
/// Fixme lower serializability into ostree-ext
6075
fn transport_to_string(transport: ostree_container::Transport) -> String {
6176
match transport {
@@ -484,7 +499,7 @@ pub(crate) async fn status(opts: super::cli::StatusOpts) -> Result<()> {
484499
let booted_deployment = sysroot.booted_deployment();
485500
let (_deployments, host) = get_status(&sysroot, booted_deployment.as_ref())?;
486501
host
487-
} else if composefs_booted()? {
502+
} else if composefs_booted()?.is_some() {
488503
composefs_deployment_status().await?
489504
} else {
490505
Default::default()

crates/ostree-ext/src/container_utils.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,6 @@ pub fn ostree_booted() -> io::Result<bool> {
7777
Path::new(&format!("/{OSTREE_BOOTED}")).try_exists()
7878
}
7979

80-
/// Returns true if the system appears to have been booted with composefs.
81-
pub fn composefs_booted() -> io::Result<bool> {
82-
let cmdline = std::fs::read_to_string("/proc/cmdline")?;
83-
Ok(cmdline.contains("composefs="))
84-
}
85-
8680
/// Returns true if the target root appears to have been booted via ostree.
8781
pub fn is_ostree_booted_in(rootfs: &Dir) -> io::Result<bool> {
8882
rootfs.try_exists(OSTREE_BOOTED)

0 commit comments

Comments
 (0)