From d3bb25418875d2cbb329812b6aab28819ba7a1f3 Mon Sep 17 00:00:00 2001 From: Archana Shinde Date: Fri, 3 Feb 2023 15:41:59 -0800 Subject: [PATCH] utils: Add function to check vhost-vsock Add function to check if the host-system has the vhost-vsock kernel module. Signed-off-by: Archana Shinde --- src/tools/kata-ctl/src/utils.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/tools/kata-ctl/src/utils.rs b/src/tools/kata-ctl/src/utils.rs index b1564f4c47..9c92f82bf9 100644 --- a/src/tools/kata-ctl/src/utils.rs +++ b/src/tools/kata-ctl/src/utils.rs @@ -144,6 +144,12 @@ pub fn get_generic_cpu_details(cpu_info_file: &str) -> Result<(String, String)> Ok((vendor, model)) } +const VHOST_VSOCK_DEVICE: &str = "/dev/vhost-vsock"; +pub fn supports_vsocks(vsock_path: &str) -> Result { + let metadata = fs::metadata(vsock_path)?; + Ok(metadata.is_file()) +} + #[cfg(test)] mod tests { use super::*; @@ -283,4 +289,30 @@ mod tests { ); assert_eq!(actual, expected); } + + #[test] + fn check_supports_vsocks_valid() { + let dir = tempdir().unwrap(); + let file_path = dir.path().join("vhost-vsock"); + let path = file_path.clone(); + let _file = fs::File::create(file_path).unwrap(); + let res = supports_vsocks(path.to_str().unwrap()).unwrap(); + assert!(res); + } + + #[test] + fn check_supports_vsocks_dir() { + let dir = tempdir().unwrap(); + let file_path = dir.path().join("vhost-vsock"); + let path = file_path.clone(); + let _dir = fs::create_dir(file_path).unwrap(); + let res = supports_vsocks(path.to_str().unwrap()).unwrap(); + assert!(!res); + } + + #[test] + fn check_supports_vsocks_missing_file() { + let res = supports_vsocks("/xyz/vhost-vsock"); + assert!(res.is_err()); + } }