vmm: api: ch-remote: Add balloon to VmResizeData

Signed-off-by: Hui Zhu <teawater@antfin.com>
This commit is contained in:
Hui Zhu 2020-04-03 17:27:20 +08:00 committed by Rob Bradford
parent f729b25a10
commit 8ffbc3d031
5 changed files with 44 additions and 2 deletions

View File

@ -23,6 +23,7 @@ enum Error {
ServerResponse(StatusCode, Option<String>),
InvalidCPUCount(std::num::ParseIntError),
InvalidMemorySize(std::num::ParseIntError),
InvalidBalloonSize(std::num::ParseIntError),
AddDeviceConfig(vmm::config::Error),
AddDiskConfig(vmm::config::Error),
AddFsConfig(vmm::config::Error),
@ -49,6 +50,7 @@ impl fmt::Display for Error {
}
InvalidCPUCount(e) => write!(f, "Error parsing CPU count: {}", e),
InvalidMemorySize(e) => write!(f, "Error parsing memory size: {}", e),
InvalidBalloonSize(e) => write!(f, "Error parsing balloon size: {}", e),
AddDeviceConfig(e) => write!(f, "Error parsing device syntax: {}", e),
AddDiskConfig(e) => write!(f, "Error parsing disk syntax: {}", e),
AddFsConfig(e) => write!(f, "Error parsing filesystem syntax: {}", e),
@ -204,6 +206,7 @@ fn resize_api_command(
socket: &mut UnixStream,
cpus: Option<&str>,
memory: Option<&str>,
balloon: Option<&str>,
) -> Result<(), Error> {
let desired_vcpus: Option<u8> = if let Some(cpus) = cpus {
Some(cpus.parse().map_err(Error::InvalidCPUCount)?)
@ -217,9 +220,16 @@ fn resize_api_command(
None
};
let desired_ram_w_balloon: Option<u64> = if let Some(balloon) = balloon {
Some(balloon.parse().map_err(Error::InvalidBalloonSize)?)
} else {
None
};
let resize = vmm::api::VmResizeData {
desired_vcpus,
desired_ram,
desired_ram_w_balloon,
};
simple_api_command(
@ -348,6 +358,10 @@ fn do_command(matches: &ArgMatches) -> Result<(), Error> {
.subcommand_matches("resize")
.unwrap()
.value_of("memory"),
matches
.subcommand_matches("resize")
.unwrap()
.value_of("balloon"),
),
Some("add-device") => add_device_api_command(
&mut socket,
@ -518,6 +532,13 @@ fn main() {
.help("New memory size (in MiB)")
.takes_value(true)
.number_of_values(1),
)
.arg(
Arg::with_name("balloon")
.long("balloon")
.help("New memory with balloon size")
.takes_value(true)
.number_of_values(1),
),
)
.subcommand(SubCommand::with_name("resume").about("Resume the VM"))

View File

@ -153,6 +153,7 @@ pub struct VmmPingResponse {
pub struct VmResizeData {
pub desired_vcpus: Option<u8>,
pub desired_ram: Option<u64>,
pub desired_ram_w_balloon: Option<u64>,
}
#[derive(Clone, Deserialize, Serialize, Default)]

View File

@ -680,6 +680,10 @@ components:
description: desired memory ram in bytes
type: integer
format: int64
desired_ram_w_balloon:
description: desired ballon size in bytes
type: integer
format: int64
VmAddDevice:
type: object

View File

@ -490,9 +490,10 @@ impl Vmm {
&mut self,
desired_vcpus: Option<u8>,
desired_ram: Option<u64>,
desired_ram_w_balloon: Option<u64>,
) -> result::Result<(), VmError> {
if let Some(ref mut vm) = self.vm {
if let Err(e) = vm.resize(desired_vcpus, desired_ram) {
if let Err(e) = vm.resize(desired_vcpus, desired_ram, desired_ram_w_balloon) {
error!("Error when resizing VM: {:?}", e);
Err(e)
} else {
@ -766,6 +767,7 @@ impl Vmm {
.vm_resize(
resize_data.desired_vcpus,
resize_data.desired_ram,
resize_data.desired_ram_w_balloon,
)
.map_err(ApiError::VmResize)
.map(|_| ApiResponsePayload::Empty);

View File

@ -680,7 +680,12 @@ impl Vm {
Ok(())
}
pub fn resize(&mut self, desired_vcpus: Option<u8>, desired_memory: Option<u64>) -> Result<()> {
pub fn resize(
&mut self,
desired_vcpus: Option<u8>,
desired_memory: Option<u64>,
desired_ram_w_balloon: Option<u64>,
) -> Result<()> {
if let Some(desired_vcpus) = desired_vcpus {
if self
.cpu_manager
@ -731,6 +736,15 @@ impl Vm {
// it will be running with the last configure memory size.
self.config.lock().unwrap().memory.size = desired_memory;
}
if let Some(desired_ram_w_balloon) = desired_ram_w_balloon {
self.memory_manager
.lock()
.unwrap()
.balloon_resize(desired_ram_w_balloon)
.map_err(Error::MemoryManager)?;
}
Ok(())
}