diff --git a/src/agent/src/mount.rs b/src/agent/src/mount.rs index b941fe57be..f2dd9de3ab 100644 --- a/src/agent/src/mount.rs +++ b/src/agent/src/mount.rs @@ -15,7 +15,7 @@ use std::str::FromStr; use std::sync::Arc; use anyhow::{anyhow, Context, Result}; -use kata_sys_util::mount::get_linux_mount_info; +use kata_sys_util::mount::{create_mount_destination, get_linux_mount_info}; use kata_types::mount::{KATA_MOUNT_OPTION_FS_GID, KATA_SHAREDFS_GUEST_PREMOUNT_TAG}; use nix::mount::MsFlags; use nix::unistd::{Gid, Uid}; @@ -646,16 +646,11 @@ fn mount_storage(logger: &Logger, storage: &Storage) -> Result<()> { return Ok(()); } + let (flags, options) = parse_mount_flags_and_options(&storage.options); let mount_path = Path::new(&storage.mount_point); let src_path = Path::new(&storage.source); - if storage.fstype == "bind" && !src_path.is_dir() { - ensure_destination_file_exists(mount_path).context("Could not create mountpoint file")?; - } else { - fs::create_dir_all(mount_path) - .map_err(anyhow::Error::from) - .context("Could not create mountpoint")?; - } - let (flags, options) = parse_mount_flags_and_options(storage.options.iter()); + create_mount_destination(src_path, mount_path, "", &storage.fstype) + .context("Could not create mountpoint")?; info!(logger, "mounting storage"; "mount-source" => src_path.display(), @@ -923,7 +918,7 @@ pub fn get_mount_fs_type(mount_point: &str) -> Result { } // get_mount_fs_type_from_file returns the FS type corresponding to the passed mount point and -// any error ecountered. +// any error encountered. #[instrument] pub fn get_mount_fs_type_from_file(mount_file: &str, mount_point: &str) -> Result { if mount_point.is_empty() { @@ -1058,37 +1053,17 @@ pub fn cgroups_mount(logger: &Logger, unified_cgroup_hierarchy: bool) -> Result< // Enable memory hierarchical account. // For more information see https://www.kernel.org/doc/Documentation/cgroup-v1/memory.txt - online_device("/sys/fs/cgroup/memory/memory.use_hierarchy")?; - Ok(()) + online_device("/sys/fs/cgroup/memory/memory.use_hierarchy") } #[instrument] -pub fn remove_mounts(mounts: &[String]) -> Result<()> { +pub fn remove_mounts + std::fmt::Debug>(mounts: &[P]) -> Result<()> { for m in mounts.iter() { - nix::mount::umount(m.as_str()).context(format!("failed to umount {:?}", m))?; + nix::mount::umount(m.as_ref()).context(format!("failed to umount {:?}", m.as_ref()))?; } Ok(()) } -#[instrument] -fn ensure_destination_file_exists(path: &Path) -> Result<()> { - if path.is_file() { - return Ok(()); - } else if path.exists() { - return Err(anyhow!("{:?} exists but is not a regular file", path)); - } - - let dir = path - .parent() - .ok_or_else(|| anyhow!("failed to find parent path for {:?}", path))?; - - fs::create_dir_all(dir).context(format!("create_dir_all {:?}", dir))?; - - fs::File::create(path).context(format!("create empty file {:?}", path))?; - - Ok(()) -} - #[instrument] fn parse_options(option_list: &[String]) -> HashMap { let mut options = HashMap::new(); @@ -1678,24 +1653,6 @@ mod tests { } } - #[test] - fn test_ensure_destination_file_exists() { - let dir = tempdir().expect("failed to create tmpdir"); - - let mut testfile = dir.into_path(); - testfile.push("testfile"); - - let result = ensure_destination_file_exists(&testfile); - - assert!(result.is_ok()); - assert!(testfile.exists()); - - let result = ensure_destination_file_exists(&testfile); - assert!(result.is_ok()); - - assert!(testfile.is_file()); - } - #[test] fn test_mount_storage() { #[derive(Debug)]