@@ -44,7 +44,7 @@ use ostree::gio;
4444use ostree_ext:: ostree;
4545use ostree_ext:: ostree_prepareroot:: { ComposefsState , Tristate } ;
4646use ostree_ext:: prelude:: Cast ;
47- use ostree_ext:: sysroot:: SysrootLock ;
47+ use ostree_ext:: sysroot:: { allocate_new_stateroot , list_stateroots , SysrootLock } ;
4848use ostree_ext:: { container as ostree_container, ostree_prepareroot} ;
4949#[ cfg( feature = "install-to-disk" ) ]
5050use rustix:: fs:: FileTypeExt ;
@@ -57,7 +57,10 @@ use self::baseline::InstallBlockDeviceOpts;
5757use crate :: bootc_composefs:: { boot:: setup_composefs_boot, repo:: initialize_composefs_repository} ;
5858use crate :: boundimage:: { BoundImage , ResolvedBoundImage } ;
5959use crate :: containerenv:: ContainerExecutionInfo ;
60- use crate :: deploy:: { prepare_for_pull, pull_from_prepared, PreparedImportMeta , PreparedPullResult } ;
60+ use crate :: deploy:: {
61+ prepare_for_pull, pull_from_prepared, MergeState , PreparedImportMeta , PreparedPullResult ,
62+ } ;
63+ use crate :: kernel_cmdline:: Cmdline ;
6164use crate :: lsm;
6265use crate :: progress_jsonl:: ProgressWriter ;
6366use crate :: spec:: { Bootloader , ImageReference } ;
@@ -405,6 +408,50 @@ pub(crate) struct InstallToExistingRootOpts {
405408 pub ( crate ) root_path : Utf8PathBuf ,
406409}
407410
411+ #[ derive( Debug , clap:: Parser , PartialEq , Eq ) ]
412+ pub ( crate ) struct InstallResetOpts {
413+ /// Acknowledge that this command is experimental.
414+ #[ clap( long) ]
415+ pub ( crate ) experimental : bool ,
416+
417+ #[ clap( flatten) ]
418+ pub ( crate ) source_opts : InstallSourceOpts ,
419+
420+ #[ clap( flatten) ]
421+ pub ( crate ) target_opts : InstallTargetOpts ,
422+
423+ /// Name of the target stateroot. If not provided, one will be automatically
424+ /// generated of the form s<year>-<serial> where <serial> starts at zero and
425+ /// increments automatically.
426+ #[ clap( long) ]
427+ pub ( crate ) stateroot : Option < String > ,
428+
429+ /// Don't display progress
430+ #[ clap( long) ]
431+ pub ( crate ) quiet : bool ,
432+
433+ #[ clap( flatten) ]
434+ pub ( crate ) progress : crate :: cli:: ProgressOptions ,
435+
436+ /// Restart or reboot into the new target image.
437+ ///
438+ /// Currently, this option always reboots. In the future this command
439+ /// will detect the case where no kernel changes are queued, and perform
440+ /// a userspace-only restart.
441+ #[ clap( long) ]
442+ pub ( crate ) apply : bool ,
443+
444+ /// Skip inheriting any automatically discovered root file system kernel arguments.
445+ #[ clap( long) ]
446+ no_root_kargs : bool ,
447+
448+ /// Add a kernel argument. This option can be provided multiple times.
449+ ///
450+ /// Example: --karg=nosmt --karg=console=ttyS0,114800n8
451+ #[ clap( long) ]
452+ karg : Option < Vec < String > > ,
453+ }
454+
408455/// Global state captured from the container.
409456#[ derive( Debug , Clone ) ]
410457pub ( crate ) struct SourceInfo {
@@ -443,6 +490,24 @@ pub(crate) struct State {
443490 pub ( crate ) composefs_options : Option < InstallComposefsOpts > ,
444491}
445492
493+ impl InstallTargetOpts {
494+ pub ( crate ) fn imageref ( & self ) -> Result < Option < ostree_container:: OstreeImageReference > > {
495+ let Some ( target_imgname) = self . target_imgref . as_deref ( ) else {
496+ return Ok ( None ) ;
497+ } ;
498+ let target_transport =
499+ ostree_container:: Transport :: try_from ( self . target_transport . as_str ( ) ) ?;
500+ let target_imgref = ostree_container:: OstreeImageReference {
501+ sigverify : ostree_container:: SignatureSource :: ContainerPolicyAllowInsecure ,
502+ imgref : ostree_container:: ImageReference {
503+ transport : target_transport,
504+ name : target_imgname. to_string ( ) ,
505+ } ,
506+ } ;
507+ Ok ( Some ( target_imgref) )
508+ }
509+ }
510+
446511impl State {
447512 #[ context( "Loading SELinux policy" ) ]
448513 pub ( crate ) fn load_policy ( & self ) -> Result < Option < ostree:: SePolicy > > {
@@ -2134,6 +2199,94 @@ pub(crate) async fn install_to_existing_root(opts: InstallToExistingRootOpts) ->
21342199 install_to_filesystem ( opts, true , cleanup) . await
21352200}
21362201
2202+ pub ( crate ) async fn install_reset ( opts : InstallResetOpts ) -> Result < ( ) > {
2203+ let rootfs = & Dir :: open_ambient_dir ( "/" , cap_std:: ambient_authority ( ) ) ?;
2204+ if !opts. experimental {
2205+ anyhow:: bail!( "This command requires --experimental" ) ;
2206+ }
2207+
2208+ let prog: ProgressWriter = opts. progress . try_into ( ) ?;
2209+
2210+ let sysroot = & crate :: cli:: get_storage ( ) . await ?;
2211+ let repo = & sysroot. repo ( ) ;
2212+ let ( booted_deployment, _deployments, host) =
2213+ crate :: status:: get_status_require_booted ( sysroot) ?;
2214+
2215+ let stateroots = list_stateroots ( sysroot) ?;
2216+ dbg ! ( & stateroots) ;
2217+ let target_stateroot = if let Some ( s) = opts. stateroot {
2218+ s
2219+ } else {
2220+ let now = chrono:: Utc :: now ( ) ;
2221+ let r = allocate_new_stateroot ( & sysroot, & stateroots, now) ?;
2222+ r. name
2223+ } ;
2224+
2225+ let booted_stateroot = booted_deployment. osname ( ) ;
2226+ assert ! ( booted_stateroot. as_str( ) != target_stateroot) ;
2227+ let ( fetched, spec) = if let Some ( target) = opts. target_opts . imageref ( ) ? {
2228+ let mut new_spec = host. spec ;
2229+ new_spec. image = Some ( target. into ( ) ) ;
2230+ let fetched = crate :: deploy:: pull (
2231+ repo,
2232+ & new_spec. image . as_ref ( ) . unwrap ( ) ,
2233+ None ,
2234+ opts. quiet ,
2235+ prog. clone ( ) ,
2236+ )
2237+ . await ?;
2238+ ( fetched, new_spec)
2239+ } else {
2240+ let imgstate = host
2241+ . status
2242+ . booted
2243+ . map ( |b| b. query_image ( repo) )
2244+ . transpose ( ) ?
2245+ . flatten ( )
2246+ . ok_or_else ( || anyhow:: anyhow!( "No image source specified" ) ) ?;
2247+ ( Box :: new ( ( * imgstate) . into ( ) ) , host. spec )
2248+ } ;
2249+ let spec = crate :: deploy:: RequiredHostSpec :: from_spec ( & spec) ?;
2250+
2251+ // Compute the kernel arguments to inherit. By default, that's only those involved
2252+ // in the root filesystem.
2253+ let root_kargs = if opts. no_root_kargs {
2254+ Vec :: new ( )
2255+ } else {
2256+ let bootcfg = booted_deployment
2257+ . bootconfig ( )
2258+ . ok_or_else ( || anyhow ! ( "Missing bootcfg for booted deployment" ) ) ?;
2259+ if let Some ( options) = bootcfg. get ( "options" ) {
2260+ let options = options. split_ascii_whitespace ( ) . collect :: < Vec < _ > > ( ) ;
2261+ crate :: kernel:: root_args_from_cmdline ( & options)
2262+ . into_iter ( )
2263+ . map ( ToOwned :: to_owned)
2264+ . collect :: < Vec < _ > > ( )
2265+ } else {
2266+ Vec :: new ( )
2267+ }
2268+ } ;
2269+
2270+ let kargs = crate :: kargs:: get_kargs_in_root ( rootfs, std:: env:: consts:: ARCH ) ?
2271+ . into_iter ( )
2272+ . chain ( root_kargs. into_iter ( ) )
2273+ . chain ( opts. karg . unwrap_or_default ( ) )
2274+ . collect :: < Vec < _ > > ( ) ;
2275+
2276+ let from = MergeState :: Reset {
2277+ stateroot : target_stateroot,
2278+ kargs,
2279+ } ;
2280+ crate :: deploy:: stage ( sysroot, from, & fetched, & spec, prog. clone ( ) ) . await ?;
2281+
2282+ sysroot. update_mtime ( ) ?;
2283+
2284+ if opts. apply {
2285+ crate :: reboot:: reboot ( ) ?;
2286+ }
2287+ Ok ( ( ) )
2288+ }
2289+
21372290/// Implementation of `bootc install finalize`.
21382291pub ( crate ) async fn install_finalize ( target : & Utf8Path ) -> Result < ( ) > {
21392292 // Log the installation finalization operation to systemd journal
0 commit comments