diff --git a/src/libs/kata-types/src/config/hypervisor/mod.rs b/src/libs/kata-types/src/config/hypervisor/mod.rs index 0ab024c6c9..0fa0023afd 100644 --- a/src/libs/kata-types/src/config/hypervisor/mod.rs +++ b/src/libs/kata-types/src/config/hypervisor/mod.rs @@ -50,6 +50,8 @@ const VIRTIO_FS: &str = "virtio-fs"; const VIRTIO_FS_INLINE: &str = "inline-virtio-fs"; const MAX_BRIDGE_SIZE: u32 = 5; +const KERNEL_PARAM_DELIMITER: &str = " "; + lazy_static! { static ref HYPERVISOR_PLUGINS: Mutex>> = Mutex::new(HashMap::new()); @@ -237,6 +239,14 @@ impl BootInfo { Ok(()) } + /// Add kernel parameters to bootinfo. It is always added before the original + /// to let the original one takes priority + pub fn add_kparams(&mut self, params: Vec) { + let mut p = params; + p.push(self.kernel_params.clone()); // [new_params0, new_params1, ..., original_params] + self.kernel_params = p.join(KERNEL_PARAM_DELIMITER); + } + /// Validate guest kernel image annotaion pub fn validate_boot_path(&self, path: &str) -> Result<()> { validate_path!(path, "path {} is invalid{}")?; diff --git a/src/libs/kata-types/src/config/mod.rs b/src/libs/kata-types/src/config/mod.rs index e837e59c69..56ea0c9175 100644 --- a/src/libs/kata-types/src/config/mod.rs +++ b/src/libs/kata-types/src/config/mod.rs @@ -151,7 +151,29 @@ impl TomlConfig { Ok(()) } - /// Probe configuration file according to the default configuration file list. + /// Get agent-specfic kernel parameters for further Hypervisor config revision + pub fn get_agent_kparams(&self) -> Result> { + let mut kv = HashMap::new(); + if let Some(cfg) = self.agent.get(&self.runtime.agent_name) { + if cfg.debug { + kv.insert("agent.log".to_string(), "debug".to_string()); + } + if cfg.enable_tracing { + kv.insert("agent.trace".to_string(), "true".to_string()); + } + if cfg.container_pipe_size > 0 { + let container_pipe_size = cfg.container_pipe_size.to_string(); + kv.insert("agent.container_pipe_size".to_string(), container_pipe_size); + } + if cfg.debug_console_enabled { + kv.insert("agent.debug_console".to_string(), "".to_string()); + kv.insert("agent.debug_console_vport".to_string(), "1026".to_string()); + } + } + Ok(kv) + } + + /// Probe configuration file according to the default configuration file list. fn get_default_config_file() -> Result { for f in default::DEFAULT_RUNTIME_CONFIGURATIONS.iter() { if let Ok(path) = fs::canonicalize(f) { diff --git a/src/runtime-rs/crates/hypervisor/src/dragonball/inner.rs b/src/runtime-rs/crates/hypervisor/src/dragonball/inner.rs index 786088633b..851f2b66ae 100644 --- a/src/runtime-rs/crates/hypervisor/src/dragonball/inner.rs +++ b/src/runtime-rs/crates/hypervisor/src/dragonball/inner.rs @@ -91,6 +91,7 @@ impl DragonballInner { kernel_params.append(&mut KernelParams::from_string( &self.config.boot_info.kernel_params, )); + info!(sl!(), "prepared kernel_params={:?}", kernel_params); // set boot source let kernel_path = self.config.boot_info.kernel.clone(); diff --git a/src/runtime-rs/crates/hypervisor/src/kernel_param.rs b/src/runtime-rs/crates/hypervisor/src/kernel_param.rs index d8b20b5972..25d4cb8c78 100644 --- a/src/runtime-rs/crates/hypervisor/src/kernel_param.rs +++ b/src/runtime-rs/crates/hypervisor/src/kernel_param.rs @@ -28,6 +28,18 @@ impl Param { value: value.to_owned(), } } + + pub fn to_string(&self) -> Result { + if self.key.is_empty() && self.value.is_empty() { + Err(anyhow!("Empty key and value")) + } else if self.key.is_empty() { + Err(anyhow!("Empty key")) + } else if self.value.is_empty() { + Ok(self.key.to_string()) + } else { + Ok(format!("{}{}{}", self.key, KERNEL_KV_DELIMITER, self.value)) + } + } } #[derive(Debug, PartialEq)] @@ -129,18 +141,7 @@ impl KernelParams { let mut parameters: Vec = Vec::new(); for param in &self.params { - if param.key.is_empty() && param.value.is_empty() { - return Err(anyhow!("Empty key and value")); - } else if param.key.is_empty() { - return Err(anyhow!("Empty key")); - } else if param.value.is_empty() { - parameters.push(param.key.to_string()); - } else { - parameters.push(format!( - "{}{}{}", - param.key, KERNEL_KV_DELIMITER, param.value - )); - } + parameters.push(param.to_string()?); } Ok(parameters.join(KERNEL_PARAM_DELIMITER)) @@ -153,6 +154,20 @@ mod tests { use super::*; + #[test] + fn test_params() { + let param1 = Param::new("", ""); + let param2 = Param::new("", "foo"); + let param3 = Param::new("foo", ""); + + assert!(param1.to_string().is_err()); + assert!(param2.to_string().is_err()); + assert_eq!(param3.to_string().unwrap(), String::from("foo")); + + let param4 = Param::new("foo", "bar"); + assert_eq!(param4.to_string().unwrap(), String::from("foo=bar")); + } + #[test] fn test_kernel_params() -> Result<()> { let expect_params_string = "k1=v1 k2=v2 k3=v3".to_string(); diff --git a/src/runtime-rs/crates/runtimes/Cargo.toml b/src/runtime-rs/crates/runtimes/Cargo.toml index e9306d16be..910c192d1b 100644 --- a/src/runtime-rs/crates/runtimes/Cargo.toml +++ b/src/runtime-rs/crates/runtimes/Cargo.toml @@ -19,6 +19,7 @@ kata-types = { path = "../../../libs/kata-types" } logging = { path = "../../../libs/logging"} oci = { path = "../../../libs/oci" } persist = { path = "../persist" } +hypervisor = { path = "../hypervisor" } # runtime handler linux_container = { path = "./linux_container", optional = true } virt_container = { path = "./virt_container", optional = true } diff --git a/src/runtime-rs/crates/runtimes/src/manager.rs b/src/runtime-rs/crates/runtimes/src/manager.rs index 3bdd16a9f4..7b049d86ac 100644 --- a/src/runtime-rs/crates/runtimes/src/manager.rs +++ b/src/runtime-rs/crates/runtimes/src/manager.rs @@ -14,6 +14,7 @@ use common::{ types::{Request, Response}, RuntimeHandler, RuntimeInstance, Sandbox, }; +use hypervisor::Param; use kata_types::{annotations::Annotation, config::TomlConfig}; #[cfg(feature = "linux")] use linux_container::LinuxContainer; @@ -323,6 +324,7 @@ fn load_config(spec: &oci::Spec) -> Result { let (mut toml_config, _) = TomlConfig::load_from_file(&config_path).context("load toml config")?; annotation.update_config_by_annotation(&mut toml_config)?; + update_agent_kparams(&mut toml_config)?; // validate configuration and return the error toml_config.validate()?; @@ -346,3 +348,20 @@ fn load_config(spec: &oci::Spec) -> Result { info!(sl!(), "get config content {:?}", &toml_config); Ok(toml_config) } + +// this update the agent-specfic kernel parameters into hypervisor's bootinfo +// the agent inside the VM will read from file cmdline to get the params and function +fn update_agent_kparams(config: &mut TomlConfig) -> Result<()> { + let mut params = vec![]; + if let Ok(kv) = config.get_agent_kparams() { + for (k, v) in kv.into_iter() { + if let Ok(s) = Param::new(k.as_str(), v.as_str()).to_string() { + params.push(s); + } + } + if let Some(h) = config.hypervisor.get_mut(&config.runtime.hypervisor_name) { + h.boot_info.add_kparams(params); + } + } + Ok(()) +}