@@ -20,8 +20,8 @@ use std::str::FromStr;
2020use std:: sync:: Arc ;
2121use std:: time:: Duration ;
2222
23- use anyhow:: Ok ;
2423use anyhow:: { anyhow, Context , Result } ;
24+ use anyhow:: { ensure, Ok } ;
2525use bootc_utils:: CommandRunExt ;
2626use camino:: Utf8Path ;
2727use camino:: Utf8PathBuf ;
@@ -576,26 +576,38 @@ pub(crate) fn print_configuration() -> Result<()> {
576576}
577577
578578#[ context( "Creating ostree deployment" ) ]
579- async fn initialize_ostree_root ( state : & State , root_setup : & RootSetup ) -> Result < Storage > {
579+ async fn initialize_ostree_root ( state : & State , root_setup : & RootSetup ) -> Result < ( Storage , bool ) > {
580580 let sepolicy = state. load_policy ( ) ?;
581581 let sepolicy = sepolicy. as_ref ( ) ;
582582 // Load a fd for the mounted target physical root
583583 let rootfs_dir = & root_setup. rootfs_fd ;
584584 let rootfs = root_setup. rootfs . as_path ( ) ;
585585 let cancellable = gio:: Cancellable :: NONE ;
586586
587+ let stateroot = state. stateroot ( ) ;
588+
589+ let has_ostree = rootfs_dir. try_exists ( "ostree/repo" ) ?;
590+ if !has_ostree {
591+ Task :: new_and_run (
592+ "Initializing ostree layout" ,
593+ "ostree" ,
594+ [ "admin" , "init-fs" , "--modern" , rootfs. as_str ( ) ] ,
595+ ) ?;
596+ } else {
597+ println ! ( "Reusing extant ostree layout" ) ;
598+ let path = "." . into ( ) ;
599+ let _ = crate :: utils:: open_dir_remount_rw ( rootfs_dir, path)
600+ . context ( "remounting target as read-write" ) ?;
601+ let path = "sysroot" . into ( ) ;
602+ let _ = crate :: utils:: open_dir_remount_rw ( rootfs_dir, path)
603+ . context ( "remounting target sysroot as read-write" ) ?;
604+ crate :: utils:: remove_immutability ( rootfs_dir, path) ?;
605+ }
606+
587607 // Ensure that the physical root is labeled.
588608 // Another implementation: https://github.com/coreos/coreos-assembler/blob/3cd3307904593b3a131b81567b13a4d0b6fe7c90/src/create_disk.sh#L295
589609 crate :: lsm:: ensure_dir_labeled ( rootfs_dir, "" , Some ( "/" . into ( ) ) , 0o755 . into ( ) , sepolicy) ?;
590610
591- let stateroot = state. stateroot ( ) ;
592-
593- Task :: new_and_run (
594- "Initializing ostree layout" ,
595- "ostree" ,
596- [ "admin" , "init-fs" , "--modern" , rootfs. as_str ( ) ] ,
597- ) ?;
598-
599611 // And also label /boot AKA xbootldr, if it exists
600612 let bootdir = rootfs. join ( "boot" ) ;
601613 if bootdir. try_exists ( ) ? {
@@ -619,6 +631,11 @@ async fn initialize_ostree_root(state: &State, root_setup: &RootSetup) -> Result
619631 let sysroot = ostree:: Sysroot :: new ( Some ( & gio:: File :: for_path ( rootfs) ) ) ;
620632 sysroot. load ( cancellable) ?;
621633
634+ let stateroot_exists = rootfs_dir. try_exists ( format ! ( "ostree/deploy/{stateroot}" ) ) ?;
635+ ensure ! (
636+ !stateroot_exists,
637+ "Cannot redeploy over extant stateroot {stateroot}"
638+ ) ;
622639 sysroot
623640 . init_osname ( stateroot, cancellable)
624641 . context ( "initializing stateroot" ) ?;
@@ -649,14 +666,15 @@ async fn initialize_ostree_root(state: &State, root_setup: &RootSetup) -> Result
649666 let sysroot = ostree:: Sysroot :: new ( Some ( & gio:: File :: for_path ( rootfs) ) ) ;
650667 sysroot. load ( cancellable) ?;
651668 let sysroot = SysrootLock :: new_from_sysroot ( & sysroot) . await ?;
652- Storage :: new ( sysroot, & temp_run)
669+ Ok ( ( Storage :: new ( sysroot, & temp_run) ? , has_ostree ) )
653670}
654671
655672#[ context( "Creating ostree deployment" ) ]
656673async fn install_container (
657674 state : & State ,
658675 root_setup : & RootSetup ,
659676 sysroot : & ostree:: Sysroot ,
677+ has_ostree : bool ,
660678) -> Result < ( ostree:: Deployment , InstallAleph ) > {
661679 let sepolicy = state. load_policy ( ) ?;
662680 let sepolicy = sepolicy. as_ref ( ) ;
@@ -749,6 +767,7 @@ async fn install_container(
749767 options. kargs = Some ( kargs. as_slice ( ) ) ;
750768 options. target_imgref = Some ( & state. target_imgref ) ;
751769 options. proxy_cfg = proxy_cfg;
770+ options. no_clean = has_ostree;
752771 let imgstate = crate :: utils:: async_task_with_spinner (
753772 "Deploying container image" ,
754773 ostree_container:: deploy:: deploy ( & sysroot, stateroot, & src_imageref, Some ( options) ) ,
@@ -1295,10 +1314,11 @@ async fn install_with_sysroot(
12951314 sysroot : & Storage ,
12961315 boot_uuid : & str ,
12971316 bound_images : & [ crate :: boundimage:: ResolvedBoundImage ] ,
1317+ has_ostree : bool ,
12981318) -> Result < ( ) > {
12991319 // And actually set up the container in that root, returning a deployment and
13001320 // the aleph state (see below).
1301- let ( _deployment, aleph) = install_container ( state, rootfs, & sysroot) . await ?;
1321+ let ( _deployment, aleph) = install_container ( state, rootfs, & sysroot, has_ostree ) . await ?;
13021322 // Write the aleph data that captures the system state at the time of provisioning for aid in future debugging.
13031323 rootfs
13041324 . rootfs_fd
@@ -1359,6 +1379,12 @@ async fn install_to_filesystem_impl(state: &State, rootfs: &mut RootSetup) -> Re
13591379 . ok_or_else ( || anyhow ! ( "No uuid for boot/root" ) ) ?;
13601380 tracing:: debug!( "boot uuid={boot_uuid}" ) ;
13611381
1382+ // If we're doing an alongside install, then the /dev bootupd sees needs to be the host's.
1383+ ensure ! (
1384+ crate :: mount:: is_same_as_host( Utf8Path :: new( "/dev" ) ) ?,
1385+ "Missing /dev mount to host /dev"
1386+ ) ;
1387+
13621388 let bound_images = if state. config_opts . skip_bound_images {
13631389 Vec :: new ( )
13641390 } else {
@@ -1379,8 +1405,16 @@ async fn install_to_filesystem_impl(state: &State, rootfs: &mut RootSetup) -> Re
13791405
13801406 // Initialize the ostree sysroot (repo, stateroot, etc.)
13811407 {
1382- let sysroot = initialize_ostree_root ( state, rootfs) . await ?;
1383- install_with_sysroot ( state, rootfs, & sysroot, & boot_uuid, & bound_images) . await ?;
1408+ let ( sysroot, has_ostree) = initialize_ostree_root ( state, rootfs) . await ?;
1409+ install_with_sysroot (
1410+ state,
1411+ rootfs,
1412+ & sysroot,
1413+ & boot_uuid,
1414+ & bound_images,
1415+ has_ostree,
1416+ )
1417+ . await ?;
13841418 // We must drop the sysroot here in order to close any open file
13851419 // descriptors.
13861420 }
@@ -1521,7 +1555,8 @@ fn remove_all_in_dir_no_xdev(d: &Dir) -> Result<()> {
15211555
15221556#[ context( "Removing boot directory content" ) ]
15231557fn clean_boot_directories ( rootfs : & Dir ) -> Result < ( ) > {
1524- let bootdir = rootfs. open_dir ( BOOT ) . context ( "Opening /boot" ) ?;
1558+ let bootdir =
1559+ crate :: utils:: open_dir_remount_rw ( rootfs, BOOT . into ( ) ) . context ( "Opening /boot" ) ?;
15251560 // This should not remove /boot/efi note.
15261561 remove_all_in_dir_no_xdev ( & bootdir) ?;
15271562 if ARCH_USES_EFI {
@@ -1612,12 +1647,35 @@ pub(crate) async fn install_to_filesystem(
16121647 if !st. is_dir ( ) {
16131648 anyhow:: bail!( "Not a directory: {root_path}" ) ;
16141649 }
1650+
1651+ let inspect = crate :: mount:: inspect_filesystem ( & fsopts. root_path ) ?;
1652+
1653+ let alternative_root = fsopts. root_path . join ( "sysroot" ) ;
1654+ let root_path = match inspect. fstype . as_str ( ) {
1655+ // Our target filesystem is an overlay, the true root is in `/sysroot`
1656+ "overlay" => {
1657+ tracing:: debug!(
1658+ "Overlay filesystem detected, using {alternative_root} instead of {root_path} as target root"
1659+ ) ;
1660+ & alternative_root
1661+ }
1662+ _ => root_path,
1663+ } ;
16151664 let rootfs_fd = Dir :: open_ambient_dir ( root_path, cap_std:: ambient_authority ( ) )
16161665 . with_context ( || format ! ( "Opening target root directory {root_path}" ) ) ?;
1666+
1667+ tracing:: debug!( "Root filesystem: {root_path}" ) ;
1668+
16171669 if let Some ( false ) = ostree_ext:: mountutil:: is_mountpoint ( & rootfs_fd, "." ) ? {
16181670 anyhow:: bail!( "Not a mountpoint: {root_path}" ) ;
16191671 }
16201672
1673+ let fsopts = {
1674+ let mut fsopts = fsopts. clone ( ) ;
1675+ fsopts. root_path = root_path. clone ( ) ;
1676+ fsopts
1677+ } ;
1678+
16211679 // Gather global state, destructuring the provided options.
16221680 // IMPORTANT: We might re-execute the current process in this function (for SELinux among other things)
16231681 // IMPORTANT: and hence anything that is done before MUST BE IDEMPOTENT.
0 commit comments