Skip to content

Commit f6c9f4e

Browse files
committed
install: Reduce usage of absolute path for rootfs
In the install flow we have both `rootfs` and `rootfs_fd` which hold the physical root. Using fd-relative accesses where we can provides a lot of advantages, so switch most uses over to the file descriptor. Signed-off-by: Colin Walters <[email protected]>
1 parent 3604dbb commit f6c9f4e

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

lib/src/install.rs

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ mod osbuild;
1212
pub(crate) mod osconfig;
1313

1414
use std::io::Write;
15-
use std::os::fd::AsFd;
15+
use std::os::fd::{AsFd, AsRawFd};
1616
use std::os::unix::process::CommandExt;
1717
use std::path::Path;
1818
use std::process::Command;
@@ -581,18 +581,16 @@ async fn initialize_ostree_root(state: &State, root_setup: &RootSetup) -> Result
581581
let sepolicy = sepolicy.as_ref();
582582
// Load a fd for the mounted target physical root
583583
let rootfs_dir = &root_setup.rootfs_fd;
584-
let rootfs = root_setup.rootfs.as_path();
585584
let cancellable = gio::Cancellable::NONE;
586585

587586
let stateroot = state.stateroot();
588587

589588
let has_ostree = rootfs_dir.try_exists("ostree/repo")?;
590589
if !has_ostree {
591-
Task::new_and_run(
592-
"Initializing ostree layout",
593-
"ostree",
594-
["admin", "init-fs", "--modern", rootfs.as_str()],
595-
)?;
590+
Task::new("Initializing ostree layout", "ostree")
591+
.args(["admin", "init-fs", "--modern", "."])
592+
.cwd(rootfs_dir)?
593+
.run()?;
596594
} else {
597595
println!("Reusing extant ostree layout");
598596

@@ -607,8 +605,7 @@ async fn initialize_ostree_root(state: &State, root_setup: &RootSetup) -> Result
607605
crate::lsm::ensure_dir_labeled(rootfs_dir, "", Some("/".into()), 0o755.into(), sepolicy)?;
608606

609607
// And also label /boot AKA xbootldr, if it exists
610-
let bootdir = rootfs.join("boot");
611-
if bootdir.try_exists()? {
608+
if rootfs_dir.try_exists("boot")? {
612609
crate::lsm::ensure_dir_labeled(rootfs_dir, "boot", None, 0o755.into(), sepolicy)?;
613610
}
614611

@@ -626,7 +623,10 @@ async fn initialize_ostree_root(state: &State, root_setup: &RootSetup) -> Result
626623
.run()?;
627624
}
628625

629-
let sysroot = ostree::Sysroot::new(Some(&gio::File::for_path(rootfs)));
626+
let sysroot = {
627+
let path = format!("/proc/self/fd/{}", rootfs_dir.as_fd().as_raw_fd());
628+
ostree::Sysroot::new(Some(&gio::File::for_path(path)))
629+
};
630630
sysroot.load(cancellable)?;
631631

632632
let stateroot_exists = rootfs_dir.try_exists(format!("ostree/deploy/{stateroot}"))?;
@@ -661,7 +661,6 @@ async fn initialize_ostree_root(state: &State, root_setup: &RootSetup) -> Result
661661
)?;
662662
}
663663

664-
let sysroot = ostree::Sysroot::new(Some(&gio::File::for_path(rootfs)));
665664
sysroot.load(cancellable)?;
666665
let sysroot = SysrootLock::new_from_sysroot(&sysroot).await?;
667666
Ok((Storage::new(sysroot, &temp_run)?, has_ostree))
@@ -999,24 +998,29 @@ pub(crate) fn reexecute_self_for_selinux_if_needed(
999998

1000999
/// Trim, flush outstanding writes, and freeze/thaw the target mounted filesystem;
10011000
/// these steps prepare the filesystem for its first booted use.
1002-
pub(crate) fn finalize_filesystem(fs: &Utf8Path) -> Result<()> {
1003-
let fsname = fs.file_name().unwrap();
1001+
pub(crate) fn finalize_filesystem(
1002+
fsname: &str,
1003+
root: &Dir,
1004+
path: impl AsRef<Utf8Path>,
1005+
) -> Result<()> {
1006+
let path = path.as_ref();
10041007
// fstrim ensures the underlying block device knows about unused space
1005-
Task::new_and_run(
1006-
format!("Trimming {fsname}"),
1007-
"fstrim",
1008-
["--quiet-unsupported", "-v", fs.as_str()],
1009-
)?;
1008+
Task::new(format!("Trimming {fsname}"), "fstrim")
1009+
.args(["--quiet-unsupported", "-v", path.as_str()])
1010+
.cwd(root)?
1011+
.run()?;
10101012
// Remounting readonly will flush outstanding writes and ensure we error out if there were background
10111013
// writeback problems.
10121014
Task::new(format!("Finalizing filesystem {fsname}"), "mount")
1013-
.args(["-o", "remount,ro", fs.as_str()])
1015+
.cwd(root)?
1016+
.args(["-o", "remount,ro", path.as_str()])
10141017
.run()?;
10151018
// Finally, freezing (and thawing) the filesystem will flush the journal, which means the next boot is clean.
10161019
for a in ["-f", "-u"] {
10171020
Task::new("Flushing filesystem journal", "fsfreeze")
10181021
.quiet()
1019-
.args([a, fs.as_str()])
1022+
.cwd(root)?
1023+
.args([a, path.as_str()])
10201024
.run()?;
10211025
}
10221026
Ok(())
@@ -1419,10 +1423,9 @@ async fn install_to_filesystem_impl(state: &State, rootfs: &mut RootSetup) -> Re
14191423

14201424
// Finalize mounted filesystems
14211425
if !rootfs.skip_finalize {
1422-
let bootfs = rootfs.boot.as_ref().map(|_| rootfs.rootfs.join("boot"));
1423-
let bootfs = bootfs.as_ref().map(|p| p.as_path());
1424-
for fs in std::iter::once(rootfs.rootfs.as_path()).chain(bootfs) {
1425-
finalize_filesystem(fs)?;
1426+
let bootfs = rootfs.boot.as_ref().map(|_| ("boot", "boot"));
1427+
for (fsname, fs) in std::iter::once(("root", ".")).chain(bootfs) {
1428+
finalize_filesystem(fsname, &rootfs.rootfs_fd, fs)?;
14261429
}
14271430
}
14281431

0 commit comments

Comments
 (0)