From 0cfb2d257065619ffd97c0fb1c218163eda61b91 Mon Sep 17 00:00:00 2001 From: Pavel Mores Date: Tue, 7 Nov 2023 15:18:28 +0100 Subject: [PATCH] runtime-rs: add simple Persist implementation for Qemu This is not necessarily meant to work, just to stub out unimplemented functionality while focusing on more fundamental things. Signed-off-by: Pavel Mores --- .../crates/hypervisor/src/qemu/inner.rs | 36 ++++++++++++++++++- .../crates/hypervisor/src/qemu/mod.rs | 28 +++++++++++++-- 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/src/runtime-rs/crates/hypervisor/src/qemu/inner.rs b/src/runtime-rs/crates/hypervisor/src/qemu/inner.rs index 4cbf2673f9..e7477b547a 100644 --- a/src/runtime-rs/crates/hypervisor/src/qemu/inner.rs +++ b/src/runtime-rs/crates/hypervisor/src/qemu/inner.rs @@ -5,8 +5,13 @@ use anyhow::Result; -use crate::{HypervisorConfig, MemoryConfig, VcpuThreadIds}; +use crate::{ + hypervisor_persist::HypervisorState, HypervisorConfig, MemoryConfig, + VcpuThreadIds, VsockDevice, HYPERVISOR_QEMU, +}; use kata_types::capabilities::{Capabilities, CapabilityBits}; +use async_trait::async_trait; +use persist::sandbox_persist::Persist; const VSOCK_SCHEME: &str = "vsock"; const VSOCK_AGENT_CID: u32 = 3; @@ -178,3 +183,32 @@ impl QemuInner { Ok(()) } } + +#[async_trait] +impl Persist for QemuInner { + type State = HypervisorState; + type ConstructorArgs = (); + + /// Save a state of hypervisor + async fn save(&self) -> Result { + Ok(HypervisorState { + hypervisor_type: HYPERVISOR_QEMU.to_string(), + id: self.id.clone(), + config: self.hypervisor_config(), + ..Default::default() + }) + } + + /// Restore hypervisor + async fn restore( + _hypervisor_args: Self::ConstructorArgs, + hypervisor_state: Self::State, + ) -> Result { + Ok(QemuInner { + id: hypervisor_state.id, + qemu_process: None, + config: hypervisor_state.config, + devices: Vec::new(), + }) + } +} diff --git a/src/runtime-rs/crates/hypervisor/src/qemu/mod.rs b/src/runtime-rs/crates/hypervisor/src/qemu/mod.rs index 3bede9dae2..7b712f140c 100644 --- a/src/runtime-rs/crates/hypervisor/src/qemu/mod.rs +++ b/src/runtime-rs/crates/hypervisor/src/qemu/mod.rs @@ -11,8 +11,9 @@ use crate::{Hypervisor, MemoryConfig}; use crate::{HypervisorConfig, VcpuThreadIds}; use inner::QemuInner; use kata_types::capabilities::{Capabilities, CapabilityBits}; +use persist::sandbox_persist::Persist; -use anyhow::Result; +use anyhow::{Context, Result}; use async_trait::async_trait; use std::sync::Arc; @@ -145,7 +146,7 @@ impl Hypervisor for Qemu { } async fn save_state(&self) -> Result { - todo!() + self.save().await } async fn capabilities(&self) -> Result { @@ -178,3 +179,26 @@ impl Hypervisor for Qemu { inner.resize_memory(new_mem_mb) } } + +#[async_trait] +impl Persist for Qemu { + type State = HypervisorState; + type ConstructorArgs = (); + + /// Save a state of the component. + async fn save(&self) -> Result { + let inner = self.inner.read().await; + inner.save().await.context("save qemu hypervisor state") + } + + /// Restore a component from a specified state. + async fn restore( + hypervisor_args: Self::ConstructorArgs, + hypervisor_state: Self::State, + ) -> Result { + let inner = QemuInner::restore(hypervisor_args, hypervisor_state).await?; + Ok(Self { + inner: Arc::new(RwLock::new(inner)), + }) + } +}