mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-04-30 04:34:27 +00:00
libs/sys-util: add wrappers for mount and fs
Add some wrappers for mount and fs syscall. Signed-off-by: Liu Jiang <gerry@linux.alibaba.com> Signed-off-by: Bin Liu <bin@hyper.sh> Signed-off-by: Fupan Li <lifupan@gmail.com> Signed-off-by: Huamin Tang <huamin.thm@alibaba-inc.com> Signed-off-by: Lei Wang <wllenyj@linux.alibaba.com> Signed-off-by: Quanwei Zhou <quanweiZhou@linux.alibaba.com>
This commit is contained in:
parent
45a00b4f02
commit
ccd03e2cae
100
src/libs/Cargo.lock
generated
100
src/libs/Cargo.lock
generated
@ -36,9 +36,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "autocfg"
|
name = "autocfg"
|
||||||
version = "1.0.1"
|
version = "1.1.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
|
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bitflags"
|
name = "bitflags"
|
||||||
@ -92,6 +92,12 @@ dependencies = [
|
|||||||
"winapi",
|
"winapi",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "common-path"
|
||||||
|
version = "1.0.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2382f75942f4b3be3690fe4f86365e9c853c1587d6ee58212cebf6e2a9ccd101"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "crossbeam-channel"
|
name = "crossbeam-channel"
|
||||||
version = "0.5.2"
|
version = "0.5.2"
|
||||||
@ -129,6 +135,17 @@ version = "1.6.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
|
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "fail"
|
||||||
|
version = "0.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ec3245a0ca564e7f3c797d20d833a6870f57a728ac967d5225b3ffdef4465011"
|
||||||
|
dependencies = [
|
||||||
|
"lazy_static",
|
||||||
|
"log",
|
||||||
|
"rand",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "fastrand"
|
name = "fastrand"
|
||||||
version = "1.6.0"
|
version = "1.6.0"
|
||||||
@ -233,6 +250,17 @@ dependencies = [
|
|||||||
"slab",
|
"slab",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "getrandom"
|
||||||
|
version = "0.2.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9be70c98951c83b8d2f8f60d7065fa6d5146873094452a1008da8c2f1e4205ad"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"libc",
|
||||||
|
"wasi 0.10.2+wasi-snapshot-preview1",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "glob"
|
name = "glob"
|
||||||
version = "0.3.0"
|
version = "0.3.0"
|
||||||
@ -309,6 +337,18 @@ checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35"
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "kata-sys-util"
|
name = "kata-sys-util"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"common-path",
|
||||||
|
"fail",
|
||||||
|
"kata-types",
|
||||||
|
"lazy_static",
|
||||||
|
"libc",
|
||||||
|
"nix 0.23.1",
|
||||||
|
"slog",
|
||||||
|
"slog-scope",
|
||||||
|
"tempfile",
|
||||||
|
"thiserror",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "kata-types"
|
name = "kata-types"
|
||||||
@ -385,7 +425,7 @@ dependencies = [
|
|||||||
"log",
|
"log",
|
||||||
"miow",
|
"miow",
|
||||||
"ntapi",
|
"ntapi",
|
||||||
"wasi",
|
"wasi 0.11.0+wasi-snapshot-preview1",
|
||||||
"winapi",
|
"winapi",
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -506,6 +546,12 @@ version = "0.1.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
|
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ppv-lite86"
|
||||||
|
version = "0.2.16"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "proc-macro2"
|
name = "proc-macro2"
|
||||||
version = "1.0.37"
|
version = "1.0.37"
|
||||||
@ -616,6 +662,36 @@ dependencies = [
|
|||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand"
|
||||||
|
version = "0.8.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
"rand_chacha",
|
||||||
|
"rand_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_chacha"
|
||||||
|
version = "0.3.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
|
||||||
|
dependencies = [
|
||||||
|
"ppv-lite86",
|
||||||
|
"rand_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_core"
|
||||||
|
version = "0.6.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
|
||||||
|
dependencies = [
|
||||||
|
"getrandom",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "redox_syscall"
|
name = "redox_syscall"
|
||||||
version = "0.2.10"
|
version = "0.2.10"
|
||||||
@ -667,18 +743,18 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde"
|
name = "serde"
|
||||||
version = "1.0.135"
|
version = "1.0.136"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "2cf9235533494ea2ddcdb794665461814781c53f19d87b76e571a1c35acbad2b"
|
checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"serde_derive",
|
"serde_derive",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde_derive"
|
name = "serde_derive"
|
||||||
version = "1.0.135"
|
version = "1.0.136"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "8dcde03d87d4c973c04be249e7d8f0b35db1c848c487bd43032808e59dd8328d"
|
checksum = "08597e7152fcd306f41838ed3e37be9eaeed2b61c42e2117266a554fab4662f9"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
@ -806,9 +882,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "thread_local"
|
name = "thread_local"
|
||||||
version = "1.1.3"
|
version = "1.1.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "8018d24e04c95ac8790716a5987d0fec4f8b27249ffa0f7d33f1369bdfb88cbd"
|
checksum = "5516c27b78311c50bf42c071425c560ac799b11c30b31f87e3081965fe5e0180"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"once_cell",
|
"once_cell",
|
||||||
]
|
]
|
||||||
@ -930,6 +1006,12 @@ dependencies = [
|
|||||||
"nix 0.23.1",
|
"nix 0.23.1",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "wasi"
|
||||||
|
version = "0.10.2+wasi-snapshot-preview1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "wasi"
|
name = "wasi"
|
||||||
version = "0.11.0+wasi-snapshot-preview1"
|
version = "0.11.0+wasi-snapshot-preview1"
|
||||||
|
@ -11,3 +11,16 @@ license = "Apache-2.0"
|
|||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
common-path = "=1.0.0"
|
||||||
|
fail = "0.5.0"
|
||||||
|
lazy_static = "1.4.0"
|
||||||
|
libc = "0.2.100"
|
||||||
|
nix = "0.23.0"
|
||||||
|
slog = "2.5.2"
|
||||||
|
slog-scope = "4.4.0"
|
||||||
|
thiserror = "1.0.30"
|
||||||
|
|
||||||
|
kata-types = { path = "../kata-types" }
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
tempfile = "3.2.0"
|
||||||
|
111
src/libs/kata-sys-util/src/fs.rs
Normal file
111
src/libs/kata-sys-util/src/fs.rs
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
// Copyright (c) 2019-2021 Alibaba Cloud
|
||||||
|
// Copyright (c) 2019-2021 Ant Group
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
//
|
||||||
|
|
||||||
|
use std::ffi::OsString;
|
||||||
|
use std::fs;
|
||||||
|
use std::io::Result;
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
|
use crate::eother;
|
||||||
|
|
||||||
|
// from linux.git/fs/fuse/inode.c: #define FUSE_SUPER_MAGIC 0x65735546
|
||||||
|
const FUSE_SUPER_MAGIC: u32 = 0x65735546;
|
||||||
|
|
||||||
|
/// Get bundle path (current working directory).
|
||||||
|
pub fn get_bundle_path() -> Result<PathBuf> {
|
||||||
|
std::env::current_dir()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the basename of the canonicalized path
|
||||||
|
pub fn get_base_name<P: AsRef<Path>>(src: P) -> Result<OsString> {
|
||||||
|
let s = src.as_ref().canonicalize()?;
|
||||||
|
s.file_name().map(|v| v.to_os_string()).ok_or_else(|| {
|
||||||
|
eother!(
|
||||||
|
"failed to get base name of path {}",
|
||||||
|
src.as_ref().to_string_lossy()
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Check whether `path` is on a fuse filesystem.
|
||||||
|
pub fn is_fuse_fs<P: AsRef<Path>>(path: P) -> bool {
|
||||||
|
if let Ok(st) = nix::sys::statfs::statfs(path.as_ref()) {
|
||||||
|
if st.filesystem_type().0 == FUSE_SUPER_MAGIC as i64 {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Check whether `path` is on a overlay filesystem.
|
||||||
|
pub fn is_overlay_fs<P: AsRef<Path>>(path: P) -> bool {
|
||||||
|
if let Ok(st) = nix::sys::statfs::statfs(path.as_ref()) {
|
||||||
|
if st.filesystem_type() == nix::sys::statfs::OVERLAYFS_SUPER_MAGIC {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Check whether the given path is a symlink.
|
||||||
|
pub fn is_symlink<P: AsRef<Path>>(path: P) -> std::io::Result<bool> {
|
||||||
|
let path = path.as_ref();
|
||||||
|
let meta = fs::symlink_metadata(path)?;
|
||||||
|
|
||||||
|
Ok(meta.file_type().is_symlink())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use crate::mount::umount_all;
|
||||||
|
use std::process::Command;
|
||||||
|
use thiserror::private::PathAsDisplay;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_get_base_name() {
|
||||||
|
assert_eq!(&get_base_name("/etc/hostname").unwrap(), "hostname");
|
||||||
|
assert_eq!(&get_base_name("/bin").unwrap(), "bin");
|
||||||
|
assert!(&get_base_name("/").is_err());
|
||||||
|
assert!(&get_base_name("").is_err());
|
||||||
|
assert!(get_base_name("/no/such/path________yeah").is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_is_symlink() {
|
||||||
|
let tmpdir = tempfile::tempdir().unwrap();
|
||||||
|
let path = tmpdir.path();
|
||||||
|
|
||||||
|
std::os::unix::fs::symlink(path, path.join("a")).unwrap();
|
||||||
|
assert!(is_symlink(path.join("a")).unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_is_overlayfs() {
|
||||||
|
let tmpdir1 = tempfile::tempdir().unwrap();
|
||||||
|
let tmpdir2 = tempfile::tempdir().unwrap();
|
||||||
|
let tmpdir3 = tempfile::tempdir().unwrap();
|
||||||
|
let tmpdir4 = tempfile::tempdir().unwrap();
|
||||||
|
|
||||||
|
let option = format!(
|
||||||
|
"-o lowerdir={},upperdir={},workdir={}",
|
||||||
|
tmpdir1.path().as_display(),
|
||||||
|
tmpdir2.path().display(),
|
||||||
|
tmpdir3.path().display()
|
||||||
|
);
|
||||||
|
let target = format!("{}", tmpdir4.path().display());
|
||||||
|
|
||||||
|
Command::new("/bin/mount")
|
||||||
|
.arg("-t overlay")
|
||||||
|
.arg(option)
|
||||||
|
.arg("overlay")
|
||||||
|
.arg(target)
|
||||||
|
.output()
|
||||||
|
.unwrap();
|
||||||
|
assert!(is_overlay_fs(tmpdir4.path()));
|
||||||
|
umount_all(tmpdir4.path(), false).unwrap();
|
||||||
|
}
|
||||||
|
}
|
@ -2,3 +2,25 @@
|
|||||||
//
|
//
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
//
|
//
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
extern crate slog;
|
||||||
|
|
||||||
|
pub mod fs;
|
||||||
|
pub mod mount;
|
||||||
|
|
||||||
|
// Convenience macro to obtain the scoped logger
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! sl {
|
||||||
|
() => {
|
||||||
|
slog_scope::logger()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! eother {
|
||||||
|
() => (std::io::Error::new(std::io::ErrorKind::Other, ""));
|
||||||
|
($fmt:expr, $($arg:tt)*) => ({
|
||||||
|
std::io::Error::new(std::io::ErrorKind::Other, format!($fmt, $($arg)*))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
1094
src/libs/kata-sys-util/src/mount.rs
Normal file
1094
src/libs/kata-sys-util/src/mount.rs
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user