mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-04-30 20:54:26 +00:00
agent: use function from kata-sys-utils to reduce code
Use function get_linux_mount_info() from kata-sys-util crate to share common code. Signed-off-by: Jiang Liu <gerry@linux.alibaba.com>
This commit is contained in:
parent
a6921dd837
commit
880e6c9a76
@ -8,12 +8,14 @@ use std::fmt::Debug;
|
|||||||
use std::fs::{self, File, OpenOptions};
|
use std::fs::{self, File, OpenOptions};
|
||||||
use std::io::{BufRead, BufReader, Write};
|
use std::io::{BufRead, BufReader, Write};
|
||||||
use std::iter;
|
use std::iter;
|
||||||
|
use std::ops::Deref;
|
||||||
use std::os::unix::fs::{MetadataExt, PermissionsExt};
|
use std::os::unix::fs::{MetadataExt, PermissionsExt};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::str::FromStr;
|
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 nix::mount::MsFlags;
|
use nix::mount::MsFlags;
|
||||||
use nix::unistd::{Gid, Uid};
|
use nix::unistd::{Gid, Uid};
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
@ -144,11 +146,6 @@ pub const STORAGE_HANDLER_LIST: &[&str] = &[
|
|||||||
DRIVER_WATCHABLE_BIND_TYPE,
|
DRIVER_WATCHABLE_BIND_TYPE,
|
||||||
];
|
];
|
||||||
|
|
||||||
#[instrument]
|
|
||||||
pub fn get_mounts() -> Result<String, std::io::Error> {
|
|
||||||
fs::read_to_string("/proc/mounts")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[instrument]
|
#[instrument]
|
||||||
pub fn baremount(
|
pub fn baremount(
|
||||||
source: &Path,
|
source: &Path,
|
||||||
@ -173,27 +170,14 @@ pub fn baremount(
|
|||||||
}
|
}
|
||||||
|
|
||||||
let destination_str = destination.to_string_lossy();
|
let destination_str = destination.to_string_lossy();
|
||||||
let mounts = get_mounts().unwrap_or_else(|_| String::new());
|
let mut already_mounted = false;
|
||||||
let already_mounted = mounts
|
if let Ok(m) = get_linux_mount_info(destination_str.deref()) {
|
||||||
.lines()
|
if m.fs_type == fs_type {
|
||||||
.map(|line| line.split_whitespace().collect::<Vec<&str>>())
|
already_mounted = true;
|
||||||
.filter(|parts| parts.len() >= 3) // ensure we have at least [source}, destination, and fs_type
|
}
|
||||||
.any(|parts| {
|
}
|
||||||
// Check if source, destination and fs_type match any entry in /proc/mounts
|
|
||||||
// minimal check is for destination an fstype since source can have different names like:
|
|
||||||
// udev /dev devtmpfs
|
|
||||||
// dev /dev devtmpfs
|
|
||||||
// depending on which entity is mounting the dev/fs/pseudo-fs
|
|
||||||
parts[1] == destination_str && parts[2] == fs_type
|
|
||||||
});
|
|
||||||
|
|
||||||
if already_mounted {
|
if already_mounted {
|
||||||
slog_info!(
|
slog_info!(logger, "{source:?} is already mounted at {destination:?}");
|
||||||
logger,
|
|
||||||
"{:?} is already mounted at {:?}",
|
|
||||||
source,
|
|
||||||
destination
|
|
||||||
);
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -782,18 +766,7 @@ pub fn recursive_ownership_change(
|
|||||||
#[instrument]
|
#[instrument]
|
||||||
pub fn is_mounted(mount_point: &str) -> Result<bool> {
|
pub fn is_mounted(mount_point: &str) -> Result<bool> {
|
||||||
let mount_point = mount_point.trim_end_matches('/');
|
let mount_point = mount_point.trim_end_matches('/');
|
||||||
let found = fs::metadata(mount_point).is_ok()
|
let found = fs::metadata(mount_point).is_ok() && get_linux_mount_info(mount_point).is_ok();
|
||||||
// Looks through /proc/mounts and check if the mount exists
|
|
||||||
&& fs::read_to_string("/proc/mounts")?
|
|
||||||
.lines()
|
|
||||||
.any(|line| {
|
|
||||||
// The 2nd column reveals the mount point.
|
|
||||||
line.split_whitespace()
|
|
||||||
.nth(1)
|
|
||||||
.map(|target| mount_point.eq(target))
|
|
||||||
.unwrap_or(false)
|
|
||||||
});
|
|
||||||
|
|
||||||
Ok(found)
|
Ok(found)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user