Skip to content

Commit 5c63e1e

Browse files
committed
install: Clean up filesystem finalization
- Don't trim *all* mounted filesystems, just the ones we mount - Move code into a helper function - Add some comments/docs Signed-off-by: Colin Walters <[email protected]>
1 parent 839be56 commit 5c63e1e

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

lib/src/install.rs

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,27 @@ fn gather_source_data() -> Result<SourceData> {
659659
Ok(SourceData { commit, selinux })
660660
}
661661

662+
/// Trim, flush outstanding writes, and freeze/thaw the target mounted filesystem;
663+
/// these steps prepare the filesystem for its first booted use.
664+
pub(crate) fn finalize_filesystem(fs: &Utf8Path) -> Result<()> {
665+
let fsname = fs.file_name().unwrap();
666+
// fstrim ensures the underlying block device knows about unused space
667+
Task::new_and_run(format!("Trimming {fsname}"), "fstrim", ["-v", fs.as_str()])?;
668+
// Remounting readonly will flush outstanding writes and ensure we error out if there were background
669+
// writeback problems.
670+
Task::new(&format!("Finalizing filesystem {fsname}"), "mount")
671+
.args(["-o", "remount,ro", fs.as_str()])
672+
.run()?;
673+
// Finally, freezing (and thawing) the filesystem will flush the journal, which means the next boot is clean.
674+
for a in ["-f", "-u"] {
675+
Task::new("Flushing filesystem journal", "xfs_freeze")
676+
.quiet()
677+
.args([a, fs.as_str()])
678+
.run()?;
679+
}
680+
Ok(())
681+
}
682+
662683
/// Implementation of the `bootc install` CLI command.
663684
pub(crate) async fn install(opts: InstallOpts) -> Result<()> {
664685
// This command currently *must* be run inside a privileged container.
@@ -780,25 +801,17 @@ pub(crate) async fn install(opts: InstallOpts) -> Result<()> {
780801
println!("Installed Ignition config from {ignition_file}");
781802
}
782803

804+
// ostree likes to have the immutable bit on the physical sysroot to ensure
805+
// that it doesn't accumulate junk; all system state should be in deployments.
783806
Task::new("Setting root immutable bit", "chattr")
784807
.root(&rootfs.rootfs_fd)?
785808
.args(["+i", "."])
786809
.run()?;
787810

788-
Task::new_and_run("Trimming filesystems", "fstrim", ["-a", "-v"])?;
789-
811+
// Finalize mounted filesystems
790812
let bootfs = rootfs.rootfs.join("boot");
791813
for fs in [bootfs.as_path(), rootfs.rootfs.as_path()] {
792-
let fsname = fs.file_name().unwrap();
793-
Task::new(&format!("Finalizing filesystem {fsname}"), "mount")
794-
.args(["-o", "remount,ro", fs.as_str()])
795-
.run()?;
796-
for a in ["-f", "-u"] {
797-
Task::new("Flushing filesystem journal", "xfs_freeze")
798-
.quiet()
799-
.args([a, fs.as_str()])
800-
.run()?;
801-
}
814+
finalize_filesystem(fs)?;
802815
}
803816

804817
// Drop all data about the root except the path to ensure any file descriptors etc. are closed.

0 commit comments

Comments
 (0)