runtime-rs: add the vm resize and get vminfo api for clh

Add API interfaces for get vminfo and resize. get vminfo can obtain the
memory size and number of vCPUs from the cloud hypervisor vmm in real
time. This interface provides information for the subsequent resize
memory and vCPU.

Signed-off-by: Fupan Li <fupan.lfp@antgroup.com>
This commit is contained in:
Fupan Li
2025-06-18 11:50:21 +08:00
committed by Fupan Li
parent 9a51ade4e2
commit 7df29605a4
2 changed files with 61 additions and 2 deletions

View File

@@ -2,8 +2,10 @@
//
// SPDX-License-Identifier: Apache-2.0
use crate::{DeviceConfig, DiskConfig, FsConfig, NetConfig, VmConfig, VsockConfig};
use anyhow::{anyhow, Result};
use crate::{
DeviceConfig, DiskConfig, FsConfig, NetConfig, VmConfig, VmInfo, VmResize, VsockConfig,
};
use anyhow::{anyhow, Context, Result};
use api_client::simple_api_full_command_and_response;
use serde::{Deserialize, Serialize};
@@ -190,3 +192,34 @@ pub async fn cloud_hypervisor_vm_vsock_add(
})
.await?
}
pub async fn cloud_hypervisor_vm_info(mut socket: UnixStream) -> Result<VmInfo> {
let vm_info = task::spawn_blocking(move || -> Result<Option<String>> {
let response = simple_api_full_command_and_response(&mut socket, "GET", "vm.info", None)
.map_err(|e| anyhow!(format!("failed to run get vminfo with err: {:?}", e)))?;
Ok(response)
})
.await??;
let vm_info = vm_info.ok_or(anyhow!("failed to get vminfo"))?;
serde_json::from_str(&vm_info).with_context(|| format!("failed to serde {}", vm_info))
}
pub async fn cloud_hypervisor_vm_resize(
mut socket: UnixStream,
vmresize: VmResize,
) -> Result<Option<String>> {
task::spawn_blocking(move || -> Result<Option<String>> {
let response = simple_api_full_command_and_response(
&mut socket,
"PUT",
"vm.resize",
Some(&serde_json::to_string(&vmresize)?),
)
.map_err(|e| anyhow!(e))?;
Ok(response)
})
.await?
}

View File

@@ -500,6 +500,32 @@ pub struct NamedHypervisorConfig {
pub guest_protection_to_use: GuestProtection,
}
#[derive(Clone, Debug, Deserialize, Serialize, Default)]
pub struct VmResize {
pub desired_vcpus: Option<u8>,
pub desired_ram: Option<u64>,
pub desired_balloon: Option<u64>,
}
/// VmInfo : Virtual Machine information
#[derive(Clone, Default, Debug, Serialize, Deserialize)]
pub struct VmInfo {
pub config: VmConfig,
pub state: State,
#[serde(skip_serializing_if = "Option::is_none")]
pub memory_actual_size: Option<u64>,
}
#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub enum State {
#[default]
Created,
Running,
Shutdown,
Paused,
}
// Returns true if the enabled guest protection is Intel TDX.
pub fn guest_protection_is_tdx(guest_protection_to_use: GuestProtection) -> bool {
matches!(guest_protection_to_use, GuestProtection::Tdx)