@@ -26,12 +26,12 @@ use cap_std_ext::cap_std::fs::{Dir, DirBuilder, DirBuilderExt as _};
2626use cap_std_ext:: dirext:: CapStdExtDirExt ;
2727use fn_error_context:: context;
2828
29+ use ostree_ext:: container_utils:: ostree_booted;
2930use ostree_ext:: sysroot:: SysrootLock ;
3031use ostree_ext:: { gio, ostree} ;
3132use rustix:: fs:: Mode ;
3233
3334use crate :: bootc_composefs:: status:: { composefs_booted, ComposefsCmdline } ;
34- use crate :: cli:: prepare_for_write;
3535use crate :: lsm;
3636use crate :: podstorage:: CStorage ;
3737use crate :: spec:: ImageStatus ;
@@ -109,14 +109,18 @@ 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 ( prep_for_write : bool ) -> Result < Self > {
112+ ///
113+ /// Note: For write operations, callers should call `prepare_for_write()`
114+ /// before calling this function to ensure the process is in the correct
115+ /// mount namespace.
116+ pub ( crate ) async fn new ( ) -> Result < Option < Self > > {
113117 let physical_root = Dir :: open_ambient_dir ( "/sysroot" , cap_std:: ambient_authority ( ) )
114118 . context ( "Opening /sysroot" ) ?;
115119
116120 let run =
117121 Dir :: open_ambient_dir ( "/run" , cap_std:: ambient_authority ( ) ) . context ( "Opening /run" ) ?;
118122
119- if let Some ( cmdline) = composefs_booted ( ) ? {
123+ let r = if let Some ( cmdline) = composefs_booted ( ) ? {
120124 let mut composefs = ComposefsRepository :: open_path ( & physical_root, COMPOSEFS ) ?;
121125 if cmdline. insecure {
122126 composefs. set_insecure ( true ) ;
@@ -131,27 +135,26 @@ impl BootedStorage {
131135 imgstore : Default :: default ( ) ,
132136 } ;
133137
134- return Ok ( Self { storage } ) ;
135- }
136-
137- if prep_for_write {
138- prepare_for_write ( ) ?;
139- }
138+ Some ( Self { storage } )
139+ } else if ostree_booted ( ) ? {
140+ let sysroot = ostree :: Sysroot :: new_default ( ) ;
141+ sysroot . set_mount_namespace_in_use ( ) ;
142+ let sysroot = ostree_ext :: sysroot :: SysrootLock :: new_from_sysroot ( & sysroot ) . await ?;
143+ sysroot . load ( gio :: Cancellable :: NONE ) ? ;
140144
141- let sysroot = ostree:: Sysroot :: new_default ( ) ;
142- sysroot. set_mount_namespace_in_use ( ) ;
143- let sysroot = ostree_ext:: sysroot:: SysrootLock :: new_from_sysroot ( & sysroot) . await ?;
144- sysroot. load ( gio:: Cancellable :: NONE ) ?;
145+ let storage = Storage {
146+ physical_root,
147+ run,
148+ ostree : OnceCell :: from ( sysroot) ,
149+ composefs : Default :: default ( ) ,
150+ imgstore : Default :: default ( ) ,
151+ } ;
145152
146- let storage = Storage {
147- physical_root,
148- run,
149- ostree : OnceCell :: from ( sysroot) ,
150- composefs : Default :: default ( ) ,
151- imgstore : Default :: default ( ) ,
153+ Some ( Self { storage } )
154+ } else {
155+ None
152156 } ;
153-
154- Ok ( Self { storage } )
157+ Ok ( r)
155158 }
156159
157160 /// Determine the boot storage backend kind.
0 commit comments