Skip to content

Commit 54d5d30

Browse files
storage: Conditionally call prepare_for_write
For ostree systems, before making any changes to the repo/boot entries we call `prepare_for_write`. Before this we were calling it unconditionally even if the command was `bootc status`, which was causing test failures. We pass in a flag now to `BootedStorage::new` which will conditionally call `prepare_for_write`. We also need this to make sure we keep the API more or less similar to what we had before Signed-off-by: Pragyan Poudyal <[email protected]>
1 parent db55ae7 commit 54d5d30

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed

crates/lib/src/cli.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,15 @@ pub(crate) fn ensure_self_unshared_mount_namespace() -> Result<()> {
715715
/// Load global storage state, expecting that we're booted into a bootc system.
716716
#[context("Initializing storage")]
717717
pub(crate) async fn get_storage() -> Result<crate::store::BootedStorage> {
718-
BootedStorage::new().await
718+
BootedStorage::new(true).await
719+
}
720+
721+
/// Load global storage state, but do not internally call `prepare_for_write` for ostree booted
722+
/// systems
723+
/// We do this to keep the `bootc status` output unchanged
724+
#[context("Initializing locked storage")]
725+
pub(crate) async fn get_storage_unlocked() -> Result<crate::store::BootedStorage> {
726+
BootedStorage::new(false).await
719727
}
720728

721729
#[context("Querying root privilege")]

crates/lib/src/status.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ use ostree_ext::sysroot::SysrootLock;
1818

1919
use ostree_ext::ostree;
2020

21+
use crate::cli::get_storage_unlocked;
2122
use crate::cli::OutputFormat;
2223
use crate::spec::BootEntryComposefs;
2324
use crate::spec::ImageStatus;
2425
use crate::spec::{BootEntry, BootOrder, Host, HostSpec, HostStatus, HostType};
2526
use crate::spec::{ImageReference, ImageSignature};
27+
use crate::store::BootedStorageKind;
2628
use crate::store::CachedImageStatus;
2729

2830
impl From<ostree_container::SignatureSource> for ImageSignature {
@@ -367,15 +369,29 @@ pub(crate) fn get_status(
367369
}
368370

369371
async fn get_host() -> Result<Host> {
370-
let storage = super::cli::get_storage().await?;
371-
372-
let host = match storage.kind()? {
373-
crate::store::BootedStorageKind::Ostree(booted_ostree) => {
374-
let (_deployments, host) = get_status(&booted_ostree)?;
375-
host
372+
let storage = match get_storage_unlocked().await {
373+
Ok(storage) => storage,
374+
Err(_) => {
375+
// If storage initialization fails (e.g., in container environments),
376+
// return a default host indicating the system is not deployed via bootc
377+
return Ok(Host::default());
376378
}
377-
crate::store::BootedStorageKind::Composefs(booted_cfs) => {
378-
crate::bootc_composefs::status::get_composefs_status(&storage, &booted_cfs).await?
379+
};
380+
381+
let host = match storage.kind() {
382+
Ok(kind) => match kind {
383+
BootedStorageKind::Ostree(booted_ostree) => {
384+
let (_deployments, host) = get_status(&booted_ostree)?;
385+
host
386+
}
387+
BootedStorageKind::Composefs(booted_cfs) => {
388+
crate::bootc_composefs::status::get_composefs_status(&storage, &booted_cfs).await?
389+
}
390+
},
391+
Err(_) => {
392+
// If determining storage kind fails (e.g., no booted deployment),
393+
// return a default host indicating the system is not deployed via bootc
394+
Host::default()
379395
}
380396
};
381397

crates/lib/src/store/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl BootedStorage {
109109
///
110110
/// This detects whether the system is booted via composefs or ostree
111111
/// and initializes the appropriate storage backend.
112-
pub(crate) async fn new() -> Result<Self> {
112+
pub(crate) async fn new(prep_for_write: bool) -> Result<Self> {
113113
let physical_root = Dir::open_ambient_dir("/sysroot", cap_std::ambient_authority())
114114
.context("Opening /sysroot")?;
115115

@@ -134,7 +134,9 @@ impl BootedStorage {
134134
return Ok(Self { storage });
135135
}
136136

137-
prepare_for_write()?;
137+
if prep_for_write {
138+
prepare_for_write()?;
139+
}
138140

139141
let sysroot = ostree::Sysroot::new_default();
140142
sysroot.set_mount_namespace_in_use();

0 commit comments

Comments
 (0)