@@ -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 } ;
@@ -413,6 +416,50 @@ pub(crate) struct InstallToExistingRootOpts {
413416 pub ( crate ) composefs_opts : InstallComposefsOpts ,
414417}
415418
419+ #[ derive( Debug , clap:: Parser , PartialEq , Eq ) ]
420+ pub ( crate ) struct InstallResetOpts {
421+ /// Acknowledge that this command is experimental.
422+ #[ clap( long) ]
423+ pub ( crate ) experimental : bool ,
424+
425+ #[ clap( flatten) ]
426+ pub ( crate ) source_opts : InstallSourceOpts ,
427+
428+ #[ clap( flatten) ]
429+ pub ( crate ) target_opts : InstallTargetOpts ,
430+
431+ /// Name of the target stateroot. If not provided, one will be automatically
432+ /// generated of the form s<year>-<serial> where <serial> starts at zero and
433+ /// increments automatically.
434+ #[ clap( long) ]
435+ pub ( crate ) stateroot : Option < String > ,
436+
437+ /// Don't display progress
438+ #[ clap( long) ]
439+ pub ( crate ) quiet : bool ,
440+
441+ #[ clap( flatten) ]
442+ pub ( crate ) progress : crate :: cli:: ProgressOptions ,
443+
444+ /// Restart or reboot into the new target image.
445+ ///
446+ /// Currently, this option always reboots. In the future this command
447+ /// will detect the case where no kernel changes are queued, and perform
448+ /// a userspace-only restart.
449+ #[ clap( long) ]
450+ pub ( crate ) apply : bool ,
451+
452+ /// Skip inheriting any automatically discovered root file system kernel arguments.
453+ #[ clap( long) ]
454+ no_root_kargs : bool ,
455+
456+ /// Add a kernel argument. This option can be provided multiple times.
457+ ///
458+ /// Example: --karg=nosmt --karg=console=ttyS0,114800n8
459+ #[ clap( long) ]
460+ karg : Option < Vec < String > > ,
461+ }
462+
416463/// Global state captured from the container.
417464#[ derive( Debug , Clone ) ]
418465pub ( crate ) struct SourceInfo {
@@ -458,6 +505,24 @@ pub(crate) struct State {
458505 pub ( crate ) detected_bootloader : crate :: spec:: Bootloader ,
459506}
460507
508+ impl InstallTargetOpts {
509+ pub ( crate ) fn imageref ( & self ) -> Result < Option < ostree_container:: OstreeImageReference > > {
510+ let Some ( target_imgname) = self . target_imgref . as_deref ( ) else {
511+ return Ok ( None ) ;
512+ } ;
513+ let target_transport =
514+ ostree_container:: Transport :: try_from ( self . target_transport . as_str ( ) ) ?;
515+ let target_imgref = ostree_container:: OstreeImageReference {
516+ sigverify : ostree_container:: SignatureSource :: ContainerPolicyAllowInsecure ,
517+ imgref : ostree_container:: ImageReference {
518+ transport : target_transport,
519+ name : target_imgname. to_string ( ) ,
520+ } ,
521+ } ;
522+ Ok ( Some ( target_imgref) )
523+ }
524+ }
525+
461526impl State {
462527 #[ context( "Loading SELinux policy" ) ]
463528 pub ( crate ) fn load_policy ( & self ) -> Result < Option < ostree:: SePolicy > > {
@@ -2201,6 +2266,94 @@ pub(crate) async fn install_to_existing_root(opts: InstallToExistingRootOpts) ->
22012266 install_to_filesystem ( opts, true , cleanup) . await
22022267}
22032268
2269+ pub ( crate ) async fn install_reset ( opts : InstallResetOpts ) -> Result < ( ) > {
2270+ let rootfs = & Dir :: open_ambient_dir ( "/" , cap_std:: ambient_authority ( ) ) ?;
2271+ if !opts. experimental {
2272+ anyhow:: bail!( "This command requires --experimental" ) ;
2273+ }
2274+
2275+ let prog: ProgressWriter = opts. progress . try_into ( ) ?;
2276+
2277+ let sysroot = & crate :: cli:: get_storage ( ) . await ?;
2278+ let repo = & sysroot. repo ( ) ;
2279+ let ( booted_deployment, _deployments, host) =
2280+ crate :: status:: get_status_require_booted ( sysroot) ?;
2281+
2282+ let stateroots = list_stateroots ( sysroot) ?;
2283+ dbg ! ( & stateroots) ;
2284+ let target_stateroot = if let Some ( s) = opts. stateroot {
2285+ s
2286+ } else {
2287+ let now = chrono:: Utc :: now ( ) ;
2288+ let r = allocate_new_stateroot ( & sysroot, & stateroots, now) ?;
2289+ r. name
2290+ } ;
2291+
2292+ let booted_stateroot = booted_deployment. osname ( ) ;
2293+ assert ! ( booted_stateroot. as_str( ) != target_stateroot) ;
2294+ let ( fetched, spec) = if let Some ( target) = opts. target_opts . imageref ( ) ? {
2295+ let mut new_spec = host. spec ;
2296+ new_spec. image = Some ( target. into ( ) ) ;
2297+ let fetched = crate :: deploy:: pull (
2298+ repo,
2299+ & new_spec. image . as_ref ( ) . unwrap ( ) ,
2300+ None ,
2301+ opts. quiet ,
2302+ prog. clone ( ) ,
2303+ )
2304+ . await ?;
2305+ ( fetched, new_spec)
2306+ } else {
2307+ let imgstate = host
2308+ . status
2309+ . booted
2310+ . map ( |b| b. query_image ( repo) )
2311+ . transpose ( ) ?
2312+ . flatten ( )
2313+ . ok_or_else ( || anyhow:: anyhow!( "No image source specified" ) ) ?;
2314+ ( Box :: new ( ( * imgstate) . into ( ) ) , host. spec )
2315+ } ;
2316+ let spec = crate :: deploy:: RequiredHostSpec :: from_spec ( & spec) ?;
2317+
2318+ // Compute the kernel arguments to inherit. By default, that's only those involved
2319+ // in the root filesystem.
2320+ let root_kargs = if opts. no_root_kargs {
2321+ Vec :: new ( )
2322+ } else {
2323+ let bootcfg = booted_deployment
2324+ . bootconfig ( )
2325+ . ok_or_else ( || anyhow ! ( "Missing bootcfg for booted deployment" ) ) ?;
2326+ if let Some ( options) = bootcfg. get ( "options" ) {
2327+ let options = options. split_ascii_whitespace ( ) . collect :: < Vec < _ > > ( ) ;
2328+ crate :: kernel:: root_args_from_cmdline ( & options)
2329+ . into_iter ( )
2330+ . map ( ToOwned :: to_owned)
2331+ . collect :: < Vec < _ > > ( )
2332+ } else {
2333+ Vec :: new ( )
2334+ }
2335+ } ;
2336+
2337+ let kargs = crate :: kargs:: get_kargs_in_root ( rootfs, std:: env:: consts:: ARCH ) ?
2338+ . into_iter ( )
2339+ . chain ( root_kargs. into_iter ( ) )
2340+ . chain ( opts. karg . unwrap_or_default ( ) )
2341+ . collect :: < Vec < _ > > ( ) ;
2342+
2343+ let from = MergeState :: Reset {
2344+ stateroot : target_stateroot,
2345+ kargs,
2346+ } ;
2347+ crate :: deploy:: stage ( sysroot, from, & fetched, & spec, prog. clone ( ) ) . await ?;
2348+
2349+ sysroot. update_mtime ( ) ?;
2350+
2351+ if opts. apply {
2352+ crate :: reboot:: reboot ( ) ?;
2353+ }
2354+ Ok ( ( ) )
2355+ }
2356+
22042357/// Implementation of `bootc install finalize`.
22052358pub ( crate ) async fn install_finalize ( target : & Utf8Path ) -> Result < ( ) > {
22062359 // Log the installation finalization operation to systemd journal
0 commit comments