@@ -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 } ;
@@ -399,6 +402,50 @@ pub(crate) struct InstallToExistingRootOpts {
399402 pub ( crate ) root_path : Utf8PathBuf ,
400403}
401404
405+ #[ derive( Debug , clap:: Parser , PartialEq , Eq ) ]
406+ pub ( crate ) struct InstallResetOpts {
407+ /// Acknowledge that this command is experimental.
408+ #[ clap( long) ]
409+ pub ( crate ) experimental : bool ,
410+
411+ #[ clap( flatten) ]
412+ pub ( crate ) source_opts : InstallSourceOpts ,
413+
414+ #[ clap( flatten) ]
415+ pub ( crate ) target_opts : InstallTargetOpts ,
416+
417+ /// Name of the target stateroot. If not provided, one will be automatically
418+ /// generated of the form s<year>-<serial> where <serial> starts at zero and
419+ /// increments automatically.
420+ #[ clap( long) ]
421+ pub ( crate ) stateroot : Option < String > ,
422+
423+ /// Don't display progress
424+ #[ clap( long) ]
425+ pub ( crate ) quiet : bool ,
426+
427+ #[ clap( flatten) ]
428+ pub ( crate ) progress : crate :: cli:: ProgressOptions ,
429+
430+ /// Restart or reboot into the new target image.
431+ ///
432+ /// Currently, this option always reboots. In the future this command
433+ /// will detect the case where no kernel changes are queued, and perform
434+ /// a userspace-only restart.
435+ #[ clap( long) ]
436+ pub ( crate ) apply : bool ,
437+
438+ /// Skip inheriting any automatically discovered root file system kernel arguments.
439+ #[ clap( long) ]
440+ no_root_kargs : bool ,
441+
442+ /// Add a kernel argument. This option can be provided multiple times.
443+ ///
444+ /// Example: --karg=nosmt --karg=console=ttyS0,114800n8
445+ #[ clap( long) ]
446+ karg : Option < Vec < String > > ,
447+ }
448+
402449/// Global state captured from the container.
403450#[ derive( Debug , Clone ) ]
404451pub ( crate ) struct SourceInfo {
@@ -437,6 +484,24 @@ pub(crate) struct State {
437484 pub ( crate ) composefs_options : Option < InstallComposefsOpts > ,
438485}
439486
487+ impl InstallTargetOpts {
488+ pub ( crate ) fn imageref ( & self ) -> Result < Option < ostree_container:: OstreeImageReference > > {
489+ let Some ( target_imgname) = self . target_imgref . as_deref ( ) else {
490+ return Ok ( None ) ;
491+ } ;
492+ let target_transport =
493+ ostree_container:: Transport :: try_from ( self . target_transport . as_str ( ) ) ?;
494+ let target_imgref = ostree_container:: OstreeImageReference {
495+ sigverify : ostree_container:: SignatureSource :: ContainerPolicyAllowInsecure ,
496+ imgref : ostree_container:: ImageReference {
497+ transport : target_transport,
498+ name : target_imgname. to_string ( ) ,
499+ } ,
500+ } ;
501+ Ok ( Some ( target_imgref) )
502+ }
503+ }
504+
440505impl State {
441506 #[ context( "Loading SELinux policy" ) ]
442507 pub ( crate ) fn load_policy ( & self ) -> Result < Option < ostree:: SePolicy > > {
@@ -2128,6 +2193,94 @@ pub(crate) async fn install_to_existing_root(opts: InstallToExistingRootOpts) ->
21282193 install_to_filesystem ( opts, true , cleanup) . await
21292194}
21302195
2196+ pub ( crate ) async fn install_reset ( opts : InstallResetOpts ) -> Result < ( ) > {
2197+ let rootfs = & Dir :: open_ambient_dir ( "/" , cap_std:: ambient_authority ( ) ) ?;
2198+ if !opts. experimental {
2199+ anyhow:: bail!( "This command requires --experimental" ) ;
2200+ }
2201+
2202+ let prog: ProgressWriter = opts. progress . try_into ( ) ?;
2203+
2204+ let sysroot = & crate :: cli:: get_storage ( ) . await ?;
2205+ let repo = & sysroot. repo ( ) ;
2206+ let ( booted_deployment, _deployments, host) =
2207+ crate :: status:: get_status_require_booted ( sysroot) ?;
2208+
2209+ let stateroots = list_stateroots ( sysroot) ?;
2210+ dbg ! ( & stateroots) ;
2211+ let target_stateroot = if let Some ( s) = opts. stateroot {
2212+ s
2213+ } else {
2214+ let now = chrono:: Utc :: now ( ) ;
2215+ let r = allocate_new_stateroot ( & sysroot, & stateroots, now) ?;
2216+ r. name
2217+ } ;
2218+
2219+ let booted_stateroot = booted_deployment. osname ( ) ;
2220+ assert ! ( booted_stateroot. as_str( ) != target_stateroot) ;
2221+ let ( fetched, spec) = if let Some ( target) = opts. target_opts . imageref ( ) ? {
2222+ let mut new_spec = host. spec ;
2223+ new_spec. image = Some ( target. into ( ) ) ;
2224+ let fetched = crate :: deploy:: pull (
2225+ repo,
2226+ & new_spec. image . as_ref ( ) . unwrap ( ) ,
2227+ None ,
2228+ opts. quiet ,
2229+ prog. clone ( ) ,
2230+ )
2231+ . await ?;
2232+ ( fetched, new_spec)
2233+ } else {
2234+ let imgstate = host
2235+ . status
2236+ . booted
2237+ . map ( |b| b. query_image ( repo) )
2238+ . transpose ( ) ?
2239+ . flatten ( )
2240+ . ok_or_else ( || anyhow:: anyhow!( "No image source specified" ) ) ?;
2241+ ( Box :: new ( ( * imgstate) . into ( ) ) , host. spec )
2242+ } ;
2243+ let spec = crate :: deploy:: RequiredHostSpec :: from_spec ( & spec) ?;
2244+
2245+ // Compute the kernel arguments to inherit. By default, that's only those involved
2246+ // in the root filesystem.
2247+ let root_kargs = if opts. no_root_kargs {
2248+ Vec :: new ( )
2249+ } else {
2250+ let bootcfg = booted_deployment
2251+ . bootconfig ( )
2252+ . ok_or_else ( || anyhow ! ( "Missing bootcfg for booted deployment" ) ) ?;
2253+ if let Some ( options) = bootcfg. get ( "options" ) {
2254+ let options = options. split_ascii_whitespace ( ) . collect :: < Vec < _ > > ( ) ;
2255+ crate :: kernel:: root_args_from_cmdline ( & options)
2256+ . into_iter ( )
2257+ . map ( ToOwned :: to_owned)
2258+ . collect :: < Vec < _ > > ( )
2259+ } else {
2260+ Vec :: new ( )
2261+ }
2262+ } ;
2263+
2264+ let kargs = crate :: kargs:: get_kargs_in_root ( rootfs, std:: env:: consts:: ARCH ) ?
2265+ . into_iter ( )
2266+ . chain ( root_kargs. into_iter ( ) )
2267+ . chain ( opts. karg . unwrap_or_default ( ) )
2268+ . collect :: < Vec < _ > > ( ) ;
2269+
2270+ let from = MergeState :: Reset {
2271+ stateroot : target_stateroot,
2272+ kargs,
2273+ } ;
2274+ crate :: deploy:: stage ( sysroot, from, & fetched, & spec, prog. clone ( ) ) . await ?;
2275+
2276+ sysroot. update_mtime ( ) ?;
2277+
2278+ if opts. apply {
2279+ crate :: reboot:: reboot ( ) ?;
2280+ }
2281+ Ok ( ( ) )
2282+ }
2283+
21312284/// Implementation of `bootc install finalize`.
21322285pub ( crate ) async fn install_finalize ( target : & Utf8Path ) -> Result < ( ) > {
21332286 // Log the installation finalization operation to systemd journal
0 commit comments