From 56f88aad3f79db5ade19bed35e89580bf5c6b3d5 Mon Sep 17 00:00:00 2001 From: ghe0 Date: Sat, 25 Jan 2025 16:13:04 +0200 Subject: [PATCH] refactored the vm update process --- src/state.rs | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/state.rs b/src/state.rs index 9dbdcad..a3b214a 100644 --- a/src/state.rs +++ b/src/state.rs @@ -401,22 +401,14 @@ impl From for snp_proto::MeasurementArgs { impl From for snp_proto::NewVmResp { fn from(vm: VM) -> Self { let uuid = vm.uuid.clone(); - snp_proto::NewVmResp { - uuid, - args: Some(vm.into()), - error: "".to_string(), - } + snp_proto::NewVmResp { uuid, args: Some(vm.into()), error: "".to_string() } } } impl From for snp_proto::UpdateVmResp { fn from(vm: VM) -> Self { let uuid = vm.uuid.clone(); - snp_proto::UpdateVmResp { - uuid, - args: Some(vm.into()), - error: "".to_string(), - } + snp_proto::UpdateVmResp { uuid, args: Some(vm.into()), error: "".to_string() } } } @@ -628,20 +620,22 @@ impl VM { config: &Config, res: &mut Resources, ) -> Result<(), VMCreationErrors> { - if config.max_cores_per_vm < req.vcpus { + if req.vcpus > 0 && config.max_cores_per_vm < req.vcpus { return Err(VMCreationErrors::TooManyCores); } - if config.max_vcpu_reservation - < res.reserved_vcpus.saturating_sub(self.vcpus).saturating_add(req.vcpus) + if req.vcpus > 0 + && config.max_vcpu_reservation + < res.reserved_vcpus.saturating_sub(self.vcpus).saturating_add(req.vcpus) { return Err(VMCreationErrors::NotEnoughCPU); } - if config.max_mem_reservation_mb - < res.reserved_memory.saturating_sub(self.memory_mb).saturating_add(req.memory_mb) + if req.memory_mb > 0 + && config.max_mem_reservation_mb + < res.reserved_memory.saturating_sub(self.memory_mb).saturating_add(req.memory_mb) { return Err(VMCreationErrors::NotEnoughMemory); } - if req.disk_size_gb < self.disk_size_gb { + if req.disk_size_gb > 0 && req.disk_size_gb < self.disk_size_gb { return Err(VMCreationErrors::DiskTooSmall); } @@ -683,9 +677,15 @@ impl VM { }); let _ = res.save_to_disk(); - self.memory_mb = req.memory_mb; - self.vcpus = req.vcpus; - self.disk_size_gb = req.disk_size_gb; + if req.memory_mb != 0 { + self.memory_mb = req.memory_mb; + } + if req.vcpus != 0 { + self.vcpus = req.vcpus; + } + if req.disk_size_gb != 0 { + self.disk_size_gb = req.disk_size_gb; + } if let Err(e) = systemctl_stop_and_disable(&self.uuid) { return Err(VMCreationErrors::HypervizorError(e.to_string()));