@@ -20,8 +20,8 @@ use std::str::FromStr;
20
20
use std:: sync:: Arc ;
21
21
use std:: time:: Duration ;
22
22
23
- use anyhow:: Ok ;
24
23
use anyhow:: { anyhow, Context , Result } ;
24
+ use anyhow:: { ensure, Ok } ;
25
25
use bootc_utils:: CommandRunExt ;
26
26
use camino:: Utf8Path ;
27
27
use camino:: Utf8PathBuf ;
@@ -576,26 +576,35 @@ pub(crate) fn print_configuration() -> Result<()> {
576
576
}
577
577
578
578
#[ 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 ) > {
580
580
let sepolicy = state. load_policy ( ) ?;
581
581
let sepolicy = sepolicy. as_ref ( ) ;
582
582
// Load a fd for the mounted target physical root
583
583
let rootfs_dir = & root_setup. rootfs_fd ;
584
584
let rootfs = root_setup. rootfs . as_path ( ) ;
585
585
let cancellable = gio:: Cancellable :: NONE ;
586
586
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 sysroot as read-write" ) ?;
601
+ crate :: utils:: remove_immutability ( rootfs_dir, path) ?;
602
+ }
603
+
587
604
// Ensure that the physical root is labeled.
588
605
// Another implementation: https://github.com/coreos/coreos-assembler/blob/3cd3307904593b3a131b81567b13a4d0b6fe7c90/src/create_disk.sh#L295
589
606
crate :: lsm:: ensure_dir_labeled ( rootfs_dir, "" , Some ( "/" . into ( ) ) , 0o755 . into ( ) , sepolicy) ?;
590
607
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
-
599
608
// And also label /boot AKA xbootldr, if it exists
600
609
let bootdir = rootfs. join ( "boot" ) ;
601
610
if bootdir. try_exists ( ) ? {
@@ -619,6 +628,11 @@ async fn initialize_ostree_root(state: &State, root_setup: &RootSetup) -> Result
619
628
let sysroot = ostree:: Sysroot :: new ( Some ( & gio:: File :: for_path ( rootfs) ) ) ;
620
629
sysroot. load ( cancellable) ?;
621
630
631
+ let stateroot_exists = rootfs_dir. try_exists ( format ! ( "ostree/deploy/{stateroot}" ) ) ?;
632
+ ensure ! (
633
+ !stateroot_exists,
634
+ "Cannot redeploy over extant stateroot {stateroot}"
635
+ ) ;
622
636
sysroot
623
637
. init_osname ( stateroot, cancellable)
624
638
. context ( "initializing stateroot" ) ?;
@@ -649,14 +663,15 @@ async fn initialize_ostree_root(state: &State, root_setup: &RootSetup) -> Result
649
663
let sysroot = ostree:: Sysroot :: new ( Some ( & gio:: File :: for_path ( rootfs) ) ) ;
650
664
sysroot. load ( cancellable) ?;
651
665
let sysroot = SysrootLock :: new_from_sysroot ( & sysroot) . await ?;
652
- Storage :: new ( sysroot, & temp_run)
666
+ Ok ( ( Storage :: new ( sysroot, & temp_run) ? , has_ostree ) )
653
667
}
654
668
655
669
#[ context( "Creating ostree deployment" ) ]
656
670
async fn install_container (
657
671
state : & State ,
658
672
root_setup : & RootSetup ,
659
673
sysroot : & ostree:: Sysroot ,
674
+ has_ostree : bool ,
660
675
) -> Result < ( ostree:: Deployment , InstallAleph ) > {
661
676
let sepolicy = state. load_policy ( ) ?;
662
677
let sepolicy = sepolicy. as_ref ( ) ;
@@ -749,6 +764,7 @@ async fn install_container(
749
764
options. kargs = Some ( kargs. as_slice ( ) ) ;
750
765
options. target_imgref = Some ( & state. target_imgref ) ;
751
766
options. proxy_cfg = proxy_cfg;
767
+ options. no_clean = has_ostree;
752
768
let imgstate = crate :: utils:: async_task_with_spinner (
753
769
"Deploying container image" ,
754
770
ostree_container:: deploy:: deploy ( & sysroot, stateroot, & src_imageref, Some ( options) ) ,
@@ -1295,10 +1311,11 @@ async fn install_with_sysroot(
1295
1311
sysroot : & Storage ,
1296
1312
boot_uuid : & str ,
1297
1313
bound_images : & [ crate :: boundimage:: ResolvedBoundImage ] ,
1314
+ has_ostree : bool ,
1298
1315
) -> Result < ( ) > {
1299
1316
// And actually set up the container in that root, returning a deployment and
1300
1317
// the aleph state (see below).
1301
- let ( _deployment, aleph) = install_container ( state, rootfs, & sysroot) . await ?;
1318
+ let ( _deployment, aleph) = install_container ( state, rootfs, & sysroot, has_ostree ) . await ?;
1302
1319
// Write the aleph data that captures the system state at the time of provisioning for aid in future debugging.
1303
1320
rootfs
1304
1321
. rootfs_fd
@@ -1359,6 +1376,12 @@ async fn install_to_filesystem_impl(state: &State, rootfs: &mut RootSetup) -> Re
1359
1376
. ok_or_else ( || anyhow ! ( "No uuid for boot/root" ) ) ?;
1360
1377
tracing:: debug!( "boot uuid={boot_uuid}" ) ;
1361
1378
1379
+ // If we're doing an alongside install, then the /dev bootupd sees needs to be the host's.
1380
+ ensure ! (
1381
+ crate :: mount:: is_same_as_host( Utf8Path :: new( "/dev" ) ) ?,
1382
+ "Missing /dev mount to host /dev"
1383
+ ) ;
1384
+
1362
1385
let bound_images = if state. config_opts . skip_bound_images {
1363
1386
Vec :: new ( )
1364
1387
} else {
@@ -1379,8 +1402,16 @@ async fn install_to_filesystem_impl(state: &State, rootfs: &mut RootSetup) -> Re
1379
1402
1380
1403
// Initialize the ostree sysroot (repo, stateroot, etc.)
1381
1404
{
1382
- let sysroot = initialize_ostree_root ( state, rootfs) . await ?;
1383
- install_with_sysroot ( state, rootfs, & sysroot, & boot_uuid, & bound_images) . await ?;
1405
+ let ( sysroot, has_ostree) = initialize_ostree_root ( state, rootfs) . await ?;
1406
+ install_with_sysroot (
1407
+ state,
1408
+ rootfs,
1409
+ & sysroot,
1410
+ & boot_uuid,
1411
+ & bound_images,
1412
+ has_ostree,
1413
+ )
1414
+ . await ?;
1384
1415
// We must drop the sysroot here in order to close any open file
1385
1416
// descriptors.
1386
1417
}
@@ -1521,7 +1552,8 @@ fn remove_all_in_dir_no_xdev(d: &Dir) -> Result<()> {
1521
1552
1522
1553
#[ context( "Removing boot directory content" ) ]
1523
1554
fn clean_boot_directories ( rootfs : & Dir ) -> Result < ( ) > {
1524
- let bootdir = rootfs. open_dir ( BOOT ) . context ( "Opening /boot" ) ?;
1555
+ let bootdir =
1556
+ crate :: utils:: open_dir_remount_rw ( rootfs, BOOT . into ( ) ) . context ( "Opening /boot" ) ?;
1525
1557
// This should not remove /boot/efi note.
1526
1558
remove_all_in_dir_no_xdev ( & bootdir) ?;
1527
1559
if ARCH_USES_EFI {
@@ -1612,12 +1644,35 @@ pub(crate) async fn install_to_filesystem(
1612
1644
if !st. is_dir ( ) {
1613
1645
anyhow:: bail!( "Not a directory: {root_path}" ) ;
1614
1646
}
1647
+
1648
+ let inspect = crate :: mount:: inspect_filesystem ( & fsopts. root_path ) ?;
1649
+
1650
+ let alternative_root = fsopts. root_path . join ( "sysroot" ) ;
1651
+ let root_path = match inspect. fstype . as_str ( ) {
1652
+ // Our target filesystem is an overlay, the true root is in `/sysroot`
1653
+ "overlay" => {
1654
+ tracing:: debug!(
1655
+ "Overlay filesystem detected, using {alternative_root} instead of {root_path} as target root"
1656
+ ) ;
1657
+ & alternative_root
1658
+ }
1659
+ _ => root_path,
1660
+ } ;
1615
1661
let rootfs_fd = Dir :: open_ambient_dir ( root_path, cap_std:: ambient_authority ( ) )
1616
1662
. with_context ( || format ! ( "Opening target root directory {root_path}" ) ) ?;
1663
+
1664
+ tracing:: debug!( "Root filesystem: {root_path}" ) ;
1665
+
1617
1666
if let Some ( false ) = ostree_ext:: mountutil:: is_mountpoint ( & rootfs_fd, "." ) ? {
1618
1667
anyhow:: bail!( "Not a mountpoint: {root_path}" ) ;
1619
1668
}
1620
1669
1670
+ let fsopts = {
1671
+ let mut fsopts = fsopts. clone ( ) ;
1672
+ fsopts. root_path = root_path. clone ( ) ;
1673
+ fsopts
1674
+ } ;
1675
+
1621
1676
// Gather global state, destructuring the provided options.
1622
1677
// IMPORTANT: We might re-execute the current process in this function (for SELinux among other things)
1623
1678
// IMPORTANT: and hence anything that is done before MUST BE IDEMPOTENT.
0 commit comments