agent: use create_mount_destination() from kata-sys-util

Use create_mount_destination() from kata-sys-util crate to reduce
redundant code.

Signed-off-by: Jiang Liu <gerry@linux.alibaba.com>
This commit is contained in:
Jiang Liu 2023-08-07 16:41:07 +08:00
parent 5e867f0538
commit c00d8f3d48

View File

@ -15,7 +15,7 @@ use std::str::FromStr;
use std::sync::Arc; use std::sync::Arc;
use anyhow::{anyhow, Context, Result}; 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 kata_types::mount::{KATA_MOUNT_OPTION_FS_GID, KATA_SHAREDFS_GUEST_PREMOUNT_TAG};
use nix::mount::MsFlags; use nix::mount::MsFlags;
use nix::unistd::{Gid, Uid}; use nix::unistd::{Gid, Uid};
@ -646,16 +646,11 @@ fn mount_storage(logger: &Logger, storage: &Storage) -> Result<()> {
return Ok(()); return Ok(());
} }
let (flags, options) = parse_mount_flags_and_options(&storage.options);
let mount_path = Path::new(&storage.mount_point); let mount_path = Path::new(&storage.mount_point);
let src_path = Path::new(&storage.source); let src_path = Path::new(&storage.source);
if storage.fstype == "bind" && !src_path.is_dir() { create_mount_destination(src_path, mount_path, "", &storage.fstype)
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")?; .context("Could not create mountpoint")?;
}
let (flags, options) = parse_mount_flags_and_options(storage.options.iter());
info!(logger, "mounting storage"; info!(logger, "mounting storage";
"mount-source" => src_path.display(), "mount-source" => src_path.display(),
@ -923,7 +918,7 @@ pub fn get_mount_fs_type(mount_point: &str) -> Result<String> {
} }
// get_mount_fs_type_from_file returns the FS type corresponding to the passed mount point and // get_mount_fs_type_from_file returns the FS type corresponding to the passed mount point and
// any error ecountered. // any error encountered.
#[instrument] #[instrument]
pub fn get_mount_fs_type_from_file(mount_file: &str, mount_point: &str) -> Result<String> { pub fn get_mount_fs_type_from_file(mount_file: &str, mount_point: &str) -> Result<String> {
if mount_point.is_empty() { if mount_point.is_empty() {
@ -1058,37 +1053,17 @@ pub fn cgroups_mount(logger: &Logger, unified_cgroup_hierarchy: bool) -> Result<
// Enable memory hierarchical account. // Enable memory hierarchical account.
// For more information see https://www.kernel.org/doc/Documentation/cgroup-v1/memory.txt // For more information see https://www.kernel.org/doc/Documentation/cgroup-v1/memory.txt
online_device("/sys/fs/cgroup/memory/memory.use_hierarchy")?; online_device("/sys/fs/cgroup/memory/memory.use_hierarchy")
Ok(())
} }
#[instrument] #[instrument]
pub fn remove_mounts(mounts: &[String]) -> Result<()> { pub fn remove_mounts<P: AsRef<str> + std::fmt::Debug>(mounts: &[P]) -> Result<()> {
for m in mounts.iter() { 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(()) 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] #[instrument]
fn parse_options(option_list: &[String]) -> HashMap<String, String> { fn parse_options(option_list: &[String]) -> HashMap<String, String> {
let mut options = HashMap::new(); 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] #[test]
fn test_mount_storage() { fn test_mount_storage() {
#[derive(Debug)] #[derive(Debug)]