3
3
//! Create a merged filesystem tree with the image and mounted configmaps.
4
4
5
5
use std:: collections:: HashSet ;
6
+ use std:: fmt:: Write as _;
6
7
use std:: fs:: create_dir_all;
7
- use std:: io:: { BufRead , Read as _ , Write } ;
8
+ use std:: io:: { BufRead , Read , Write } ;
8
9
use std:: path:: PathBuf ;
9
10
10
11
use anyhow:: Ok ;
@@ -25,12 +26,11 @@ use ostree_ext::tokio_util::spawn_blocking_cancellable_flatten;
25
26
use rustix:: fs:: { fsync, renameat_with, AtFlags , RenameFlags } ;
26
27
27
28
use crate :: bls_config:: { parse_bls_config, BLSConfig } ;
28
- #[ allow( unused_imports) ]
29
- use crate :: install:: { get_efi_uuid_source, get_user_config, BootType } ;
29
+ use crate :: install:: { get_efi_uuid_source, BootType } ;
30
30
use crate :: parsers:: grub_menuconfig:: { parse_grub_menuentry_file, MenuEntry } ;
31
31
use crate :: progress_jsonl:: { Event , ProgressWriter , SubTaskBytes , SubTaskStep } ;
32
32
use crate :: spec:: ImageReference ;
33
- use crate :: spec:: { BootEntry , BootOrder , HostSpec } ;
33
+ use crate :: spec:: { BootOrder , HostSpec } ;
34
34
use crate :: status:: { composefs_deployment_status, labels_of_config} ;
35
35
use crate :: store:: Storage ;
36
36
use crate :: utils:: async_task_with_spinner;
@@ -744,42 +744,61 @@ pub(crate) async fn stage(
744
744
Ok ( ( ) )
745
745
}
746
746
747
+ /// Filename for `loader/entries`
748
+ pub ( crate ) const USER_CFG : & str = "user.cfg" ;
749
+ pub ( crate ) const USER_CFG_STAGED : & str = "user.cfg.staged" ;
750
+ pub ( crate ) const USER_CFG_ROLLBACK : & str = USER_CFG_STAGED ;
751
+
747
752
#[ context( "Rolling back UKI" ) ]
748
- pub ( crate ) fn rollback_composefs_uki ( _current : & BootEntry , _rollback : & BootEntry ) -> Result < ( ) > {
749
- unimplemented ! ( )
750
- // let user_cfg_name = "grub2/user.cfg.staged";
751
- // let user_cfg_path = PathBuf::from("boot").join(user_cfg_name);
752
- // let sysroot = &Dir::open_ambient_dir("/sysroot", cap_std::ambient_authority())?;
753
-
754
- // let efi_uuid_source = get_efi_uuid_source();
755
-
756
- // let rollback_verity = if let Some(composefs) = &rollback.composefs {
757
- // composefs.verity.clone()
758
- // } else {
759
- // // Shouldn't really happen
760
- // anyhow::bail!("Verity not found for rollback deployment")
761
- // };
762
- // let rollback_config = get_user_config(todo!(), &rollback_verity).as_bytes();
763
-
764
- // let current_verity = if let Some(composefs) = ¤t.composefs {
765
- // composefs.verity.clone()
766
- // } else {
767
- // // Shouldn't really happen
768
- // anyhow::bail!("Verity not found for booted deployment")
769
- // };
770
- // let current_config = get_user_config(todo!(), ¤t_verity).as_bytes();
771
-
772
- // // TODO: Need to check if user.cfg.staged exists
773
- // sysroot
774
- // .atomic_replace_with(user_cfg_path, |w| {
775
- // write!(w, "{efi_uuid_source}")?;
776
- // w.write_all(rollback_config)?;
777
- // w.write_all(current_config)?;
778
- // Ok(())
779
- // })
780
- // .with_context(|| format!("Writing {user_cfg_name}"))?;
781
-
782
- // Ok(())
753
+ pub ( crate ) fn rollback_composefs_uki ( ) -> Result < ( ) > {
754
+ let user_cfg_path = PathBuf :: from ( "/sysroot/boot/grub2" ) ;
755
+
756
+ let mut str = String :: new ( ) ;
757
+ let mut menuentries =
758
+ get_sorted_uki_boot_entries ( & mut str) . context ( "Getting UKI boot entries" ) ?;
759
+
760
+ // TODO(Johan-Liebert): Currently assuming there are only two deployments
761
+ assert ! ( menuentries. len( ) == 2 ) ;
762
+
763
+ let ( first, second) = menuentries. split_at_mut ( 1 ) ;
764
+ std:: mem:: swap ( & mut first[ 0 ] , & mut second[ 0 ] ) ;
765
+
766
+ let mut buffer = get_efi_uuid_source ( ) ;
767
+
768
+ for entry in menuentries {
769
+ write ! ( buffer, "{entry}" ) ?;
770
+ }
771
+
772
+ let entries_dir =
773
+ cap_std:: fs:: Dir :: open_ambient_dir ( & user_cfg_path, cap_std:: ambient_authority ( ) )
774
+ . with_context ( || format ! ( "Opening {user_cfg_path:?}" ) ) ?;
775
+
776
+ entries_dir
777
+ . atomic_write ( USER_CFG_ROLLBACK , buffer)
778
+ . with_context ( || format ! ( "Writing to {USER_CFG_ROLLBACK}" ) ) ?;
779
+
780
+ tracing:: debug!( "Atomically exchanging for {USER_CFG_ROLLBACK} and {USER_CFG}" ) ;
781
+ renameat_with (
782
+ & entries_dir,
783
+ USER_CFG_ROLLBACK ,
784
+ & entries_dir,
785
+ USER_CFG ,
786
+ RenameFlags :: EXCHANGE ,
787
+ )
788
+ . context ( "renameat" ) ?;
789
+
790
+ tracing:: debug!( "Removing {USER_CFG_ROLLBACK}" ) ;
791
+ rustix:: fs:: unlinkat ( & entries_dir, USER_CFG_ROLLBACK , AtFlags :: empty ( ) ) . context ( "unlinkat" ) ?;
792
+
793
+ tracing:: debug!( "Syncing to disk" ) ;
794
+ fsync (
795
+ entries_dir
796
+ . reopen_as_ownedfd ( )
797
+ . with_context ( || format ! ( "Reopening {user_cfg_path:?} as owned fd" ) ) ?,
798
+ )
799
+ . with_context ( || format ! ( "fsync {user_cfg_path:?}" ) ) ?;
800
+
801
+ Ok ( ( ) )
783
802
}
784
803
785
804
/// Filename for `loader/entries`
@@ -837,6 +856,7 @@ pub(crate) fn rollback_composefs_bls() -> Result<()> {
837
856
cfg. version = idx as u32 ;
838
857
}
839
858
859
+ // TODO(Johan-Liebert): Currently assuming there are only two deployments
840
860
assert ! ( all_configs. len( ) == 2 ) ;
841
861
842
862
// Write these
@@ -852,7 +872,7 @@ pub(crate) fn rollback_composefs_bls() -> Result<()> {
852
872
let file_name = format ! ( "bootc-composefs-{}.conf" , cfg. version) ;
853
873
854
874
rollback_entries_dir
855
- . atomic_write ( & file_name, cfg. to_string ( ) . as_bytes ( ) )
875
+ . atomic_write ( & file_name, cfg. to_string ( ) )
856
876
. with_context ( || format ! ( "Writing to {file_name}" ) ) ?;
857
877
}
858
878
@@ -879,7 +899,7 @@ pub(crate) fn rollback_composefs_bls() -> Result<()> {
879
899
. context ( "renameat" ) ?;
880
900
881
901
tracing:: debug!( "Removing {ROLLBACK_ENTRIES}" ) ;
882
- rustix:: fs:: unlinkat ( & dir, ROLLBACK_ENTRIES , AtFlags :: REMOVEDIR ) . context ( "unlinkat" ) ?;
902
+ rustix:: fs:: unlinkat ( & dir, ROLLBACK_ENTRIES , AtFlags :: empty ( ) ) . context ( "unlinkat" ) ?;
883
903
884
904
tracing:: debug!( "Syncing to disk" ) ;
885
905
fsync (
@@ -923,7 +943,7 @@ pub(crate) async fn composefs_rollback() -> Result<()> {
923
943
924
944
match rollback_composefs_entry. boot_type {
925
945
BootType :: Bls => rollback_composefs_bls ( ) ,
926
- BootType :: Uki => rollback_composefs_uki ( & host . status . booted . unwrap ( ) , & rollback_status ) ,
946
+ BootType :: Uki => rollback_composefs_uki ( ) ,
927
947
} ?;
928
948
929
949
Ok ( ( ) )
0 commit comments