mirror of
				https://github.com/kata-containers/kata-containers.git
				synced 2025-11-04 11:50:15 +00:00 
			
		
		
		
	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:
		@@ -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)
 | 
			
		||||
    create_mount_destination(src_path, mount_path, "", &storage.fstype)
 | 
			
		||||
        .context("Could not create mountpoint")?;
 | 
			
		||||
    }
 | 
			
		||||
    let (flags, options) = parse_mount_flags_and_options(storage.options.iter());
 | 
			
		||||
 | 
			
		||||
    info!(logger, "mounting storage";
 | 
			
		||||
        "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
 | 
			
		||||
// any error ecountered.
 | 
			
		||||
// any error encountered.
 | 
			
		||||
#[instrument]
 | 
			
		||||
pub fn get_mount_fs_type_from_file(mount_file: &str, mount_point: &str) -> Result<String> {
 | 
			
		||||
    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<P: AsRef<str> + 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<String, String> {
 | 
			
		||||
    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)]
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user