Skip to content

Commit 828895c

Browse files
mount: clean up mount APIs
Change the Repository::mount() API to return the mounted filesystem as an fd rather than taking the mountpoint as an argument. Create a new mount_at() API to replace the old one, replacing the canicalize() and mount_at() calls that used to be in mount_composefs_at(), which we remove. Update the various users. Making this change lets us simplify the logic in composefs-setup-root: it no longer has to manually open the image in order to perform the fsmount operation: it can use the new API on the repository. This allows us to make Repository::open_image() private, so do that too. Co-authored-by: Sanne Raymaekers <[email protected]> Signed-off-by: Allison Karlitskaya <[email protected]>
1 parent 9b6b2a0 commit 828895c

File tree

5 files changed

+16
-27
lines changed

5 files changed

+16
-27
lines changed

crates/cfsctl/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ async fn main() -> Result<()> {
339339
fs.print_dumpfile()?;
340340
}
341341
Command::Mount { name, mountpoint } => {
342-
repo.mount(&name, &mountpoint)?;
342+
repo.mount_at(&name, &mountpoint)?;
343343
}
344344
Command::ImageObjects { name } => {
345345
let objects = repo.objects_for_image(&name)?;

crates/composefs-oci/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ pub fn mount<ObjectID: FsVerityHashValue>(
152152
let Some(id) = config.get_config_annotation("containers.composefs.fsverity") else {
153153
bail!("Can only mount sealed containers");
154154
};
155-
repo.mount(id, mountpoint)
155+
repo.mount_at(id, mountpoint)
156156
}
157157

158158
#[cfg(test)]

crates/composefs-setup-root/src/main.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use serde::Deserialize;
1919

2020
use composefs::{
2121
fsverity::{FsVerityHashValue, Sha256HashValue},
22-
mount::{composefs_fsmount, mount_at, FsHandle},
22+
mount::{mount_at, FsHandle},
2323
mountcompat::{overlayfs_set_fd, overlayfs_set_lower_and_data_fds, prepare_mount},
2424
repository::Repository,
2525
};
@@ -166,8 +166,7 @@ fn open_root_fs(path: &Path) -> Result<OwnedFd> {
166166

167167
fn mount_composefs_image(sysroot: &OwnedFd, name: &str) -> Result<OwnedFd> {
168168
let repo = Repository::<Sha256HashValue>::open_path(sysroot, "composefs")?;
169-
let image = repo.open_image(name)?;
170-
composefs_fsmount(image, name, repo.objects_dir()?).context("Failed to mount composefs image")
169+
repo.mount(name)
171170
}
172171

173172
fn mount_subdir(

crates/composefs/src/mount.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
use std::{
2-
fs::canonicalize,
32
io::Result,
43
os::fd::{AsFd, BorrowedFd, OwnedFd},
5-
path::Path,
64
};
75

86
use rustix::{
9-
fs::CWD,
107
mount::{
118
fsconfig_create, fsconfig_set_flag, fsconfig_set_string, fsmount, fsopen, move_mount,
129
FsMountFlags, FsOpenFlags, MountAttrFlags, MoveMountFlags,
@@ -95,13 +92,3 @@ pub fn composefs_fsmount(image: OwnedFd, name: &str, basedir: impl AsFd) -> Resu
9592
MountAttrFlags::empty(),
9693
)?)
9794
}
98-
99-
pub fn mount_composefs_at(
100-
image: OwnedFd,
101-
name: &str,
102-
basedir: impl AsFd,
103-
mountpoint: impl AsRef<Path>,
104-
) -> Result<()> {
105-
let mnt = composefs_fsmount(image, name, basedir)?;
106-
Ok(mount_at(mnt, CWD, &canonicalize(mountpoint)?)?)
107-
}

crates/composefs/src/repository.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{
22
collections::HashSet,
33
ffi::CStr,
4-
fs::File,
4+
fs::{canonicalize, File},
55
io::{Read, Write},
66
os::fd::{AsFd, OwnedFd},
77
path::{Path, PathBuf},
@@ -23,7 +23,7 @@ use crate::{
2323
fsverity::{
2424
compute_verity, enable_verity, ensure_verity_equal, measure_verity, FsVerityHashValue,
2525
},
26-
mount::mount_composefs_at,
26+
mount::{composefs_fsmount, mount_at},
2727
splitstream::{DigestMap, SplitStreamReader, SplitStreamWriter},
2828
util::{proc_self_fd, Sha256Digest},
2929
};
@@ -383,7 +383,7 @@ impl<ObjectID: FsVerityHashValue> Repository<ObjectID> {
383383
self.write_image(Some(name), &data)
384384
}
385385

386-
pub fn open_image(&self, name: &str) -> Result<OwnedFd> {
386+
fn open_image(&self, name: &str) -> Result<OwnedFd> {
387387
let image = self.openat(&format!("images/{name}"), OFlags::RDONLY)?;
388388

389389
if !name.contains("/") {
@@ -394,13 +394,16 @@ impl<ObjectID: FsVerityHashValue> Repository<ObjectID> {
394394
Ok(image)
395395
}
396396

397-
pub fn mount(&self, name: &str, mountpoint: &str) -> Result<()> {
397+
pub fn mount(&self, name: &str) -> Result<OwnedFd> {
398398
let image = self.open_image(name)?;
399-
Ok(mount_composefs_at(
400-
image,
401-
name,
402-
self.objects_dir()?,
403-
mountpoint,
399+
Ok(composefs_fsmount(image, name, self.objects_dir()?)?)
400+
}
401+
402+
pub fn mount_at(&self, name: &str, mountpoint: impl AsRef<Path>) -> Result<()> {
403+
Ok(mount_at(
404+
self.mount(name)?,
405+
CWD,
406+
&canonicalize(mountpoint)?,
404407
)?)
405408
}
406409

0 commit comments

Comments
 (0)