From 2d2e65bf54faa39c7b53ffd85aed7642bb950d02 Mon Sep 17 00:00:00 2001 From: Egor Lazarchuk Date: Tue, 7 Oct 2025 16:24:01 +0000 Subject: [PATCH 1/5] refactor: remove remaining usage of anyhow The only place where anyhow was used is in the device manager code that runs a specified function with a certain device. No instead of converting the possible error of that function to anyhow::error, just return the error and lest the call site do deal with it. Signed-off-by: Egor Lazarchuk --- Cargo.lock | 7 ------- src/vmm/Cargo.toml | 1 - src/vmm/src/device_manager/mod.rs | 11 ++++------- src/vmm/src/lib.rs | 30 ++++++++++++++++++++++-------- 4 files changed, 26 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index acb1572ccbf..89dadff8e06 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -112,12 +112,6 @@ dependencies = [ "windows-sys 0.60.2", ] -[[package]] -name = "anyhow" -version = "1.0.100" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a23eb6b1614318a8071c9b2521f36b424b2c83db5eb3a0fead4a6c0809af6e61" - [[package]] name = "arrayvec" version = "0.7.6" @@ -1687,7 +1681,6 @@ version = "0.1.0" dependencies = [ "acpi_tables", "aes-gcm", - "anyhow", "arrayvec", "aws-lc-rs", "base64", diff --git a/src/vmm/Cargo.toml b/src/vmm/Cargo.toml index 69cf2f54e4e..4187875b3ce 100644 --- a/src/vmm/Cargo.toml +++ b/src/vmm/Cargo.toml @@ -17,7 +17,6 @@ gdb = ["arrayvec", "gdbstub", "gdbstub_arch"] acpi_tables = { path = "../acpi-tables" } aes-gcm = { version = "0.10.1", default-features = false, features = ["aes"] } -anyhow = "1.0.100" arrayvec = { version = "0.7.6", optional = true } aws-lc-rs = { version = "1.14.1", features = ["bindgen"] } base64 = "0.22.1" diff --git a/src/vmm/src/device_manager/mod.rs b/src/vmm/src/device_manager/mod.rs index 8e894d0fc06..d591351172a 100644 --- a/src/vmm/src/device_manager/mod.rs +++ b/src/vmm/src/device_manager/mod.rs @@ -91,8 +91,6 @@ pub enum FindDeviceError { InvalidDeviceType, /// Device not found DeviceNotFound, - /// Internal Device error: {0} - InternalDeviceError(anyhow::Error), } #[derive(Debug)] @@ -376,7 +374,7 @@ impl DeviceManager { &self, id: &str, f: F, - ) -> Result + ) -> Result, FindDeviceError> where T: VirtioDevice + 'static + Debug, E: std::error::Error + 'static + Send + Sync, @@ -384,11 +382,10 @@ impl DeviceManager { { if let Some(device) = self.get_virtio_device(T::const_device_type(), id) { let mut dev = device.lock().expect("Poisoned lock"); - f(dev + Ok(f(dev .as_mut_any() .downcast_mut::() - .ok_or(FindDeviceError::InvalidDeviceType)?) - .map_err(|e| FindDeviceError::InternalDeviceError(e.into())) + .ok_or(FindDeviceError::InvalidDeviceType)?)) } else { Err(FindDeviceError::DeviceNotFound) } @@ -400,7 +397,7 @@ impl DeviceManager { T: VirtioDevice + 'static + Debug, F: FnOnce(&mut T) -> R, { - self.try_with_virtio_device_with_id(id, |dev: &mut T| Ok::(f(dev))) + self.try_with_virtio_device_with_id(id, |dev: &mut T| Ok::(f(dev)))? } } diff --git a/src/vmm/src/lib.rs b/src/vmm/src/lib.rs index ace273bb94c..00c7dca0999 100644 --- a/src/vmm/src/lib.rs +++ b/src/vmm/src/lib.rs @@ -136,7 +136,10 @@ use vstate::kvm::Kvm; use vstate::vcpu::{self, StartThreadedError, VcpuSendEventError}; use crate::cpu_config::templates::CpuConfiguration; -use crate::devices::virtio::balloon::{BALLOON_DEV_ID, Balloon, BalloonConfig, BalloonStats}; +use crate::devices::virtio::balloon::{ + BALLOON_DEV_ID, Balloon, BalloonConfig, BalloonError, BalloonStats, +}; +use crate::devices::virtio::block::BlockError; use crate::devices::virtio::block::device::Block; use crate::devices::virtio::net::Net; use crate::logger::{METRICS, MetricsError, error, info, warn}; @@ -248,6 +251,10 @@ pub enum VmmError { VMGenID(#[from] VmGenIdError), /// Failed perform action on device: {0} FindDeviceError(#[from] device_manager::FindDeviceError), + /// Block: {0} + Block(#[from] BlockError), + /// Balloon: {0} + Balloon(#[from] BalloonError), } /// Shorthand type for KVM dirty page bitmap. @@ -522,7 +529,8 @@ impl Vmm { .try_with_virtio_device_with_id(drive_id, |block: &mut Block| { block.update_disk_image(path_on_host) }) - .map_err(VmmError::FindDeviceError) + .map_err(VmmError::FindDeviceError)??; + Ok(()) } /// Updates the rate limiter parameters for block device with `drive_id` id. @@ -536,14 +544,16 @@ impl Vmm { .try_with_virtio_device_with_id(drive_id, |block: &mut Block| { block.update_rate_limiter(rl_bytes, rl_ops) }) - .map_err(VmmError::FindDeviceError) + .map_err(VmmError::FindDeviceError)??; + Ok(()) } /// Updates the rate limiter parameters for block device with `drive_id` id. pub fn update_vhost_user_block_config(&mut self, drive_id: &str) -> Result<(), VmmError> { self.device_manager .try_with_virtio_device_with_id(drive_id, |block: &mut Block| block.update_config()) - .map_err(VmmError::FindDeviceError) + .map_err(VmmError::FindDeviceError)??; + Ok(()) } /// Updates the rate limiter parameters for net device with `net_id` id. @@ -571,9 +581,11 @@ impl Vmm { /// Returns the latest balloon statistics if they are enabled. pub fn latest_balloon_stats(&self) -> Result { - self.device_manager + let stats = self + .device_manager .try_with_virtio_device_with_id(BALLOON_DEV_ID, |dev: &mut Balloon| dev.latest_stats()) - .map_err(VmmError::FindDeviceError) + .map_err(VmmError::FindDeviceError)??; + Ok(stats) } /// Updates configuration for the balloon device target size. @@ -582,7 +594,8 @@ impl Vmm { .try_with_virtio_device_with_id(BALLOON_DEV_ID, |dev: &mut Balloon| { dev.update_size(amount_mib) }) - .map_err(VmmError::FindDeviceError) + .map_err(VmmError::FindDeviceError)??; + Ok(()) } /// Updates configuration for the balloon device as described in `balloon_stats_update`. @@ -594,7 +607,8 @@ impl Vmm { .try_with_virtio_device_with_id(BALLOON_DEV_ID, |dev: &mut Balloon| { dev.update_stats_polling_interval(stats_polling_interval_s) }) - .map_err(VmmError::FindDeviceError) + .map_err(VmmError::FindDeviceError)??; + Ok(()) } /// Signals Vmm to stop and exit. From b2cbd238b9b6039ef886678779bbb02498bc944a Mon Sep 17 00:00:00 2001 From: Egor Lazarchuk Date: Tue, 7 Oct 2025 16:27:28 +0000 Subject: [PATCH 2/5] refactor: assert device downcasting in device manager When we get a type erased device from a underlying device storage we expect that it will always be of the type we requested with T::const_device_type(). If it is not true, we have an internal bug. Signed-off-by: Egor Lazarchuk --- src/vmm/src/device_manager/mod.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/vmm/src/device_manager/mod.rs b/src/vmm/src/device_manager/mod.rs index d591351172a..05cca130570 100644 --- a/src/vmm/src/device_manager/mod.rs +++ b/src/vmm/src/device_manager/mod.rs @@ -87,8 +87,6 @@ pub enum AttachDeviceError { #[derive(Debug, thiserror::Error, displaydoc::Display)] /// Error while searching for a VirtIO device pub enum FindDeviceError { - /// Device type is invalid - InvalidDeviceType, /// Device not found DeviceNotFound, } @@ -385,7 +383,7 @@ impl DeviceManager { Ok(f(dev .as_mut_any() .downcast_mut::() - .ok_or(FindDeviceError::InvalidDeviceType)?)) + .expect("Invalid device for a given device type"))) } else { Err(FindDeviceError::DeviceNotFound) } From d93db3ea273d70ac28e597cbe8a7fa8901d75ab3 Mon Sep 17 00:00:00 2001 From: Egor Lazarchuk Date: Tue, 7 Oct 2025 16:33:02 +0000 Subject: [PATCH 3/5] refactor: delete unused code with_virtio_device_with_id was never used in the mmio device manager. Signed-off-by: Egor Lazarchuk --- src/vmm/src/device_manager/mmio.rs | 32 ------------------------------ 1 file changed, 32 deletions(-) diff --git a/src/vmm/src/device_manager/mmio.rs b/src/vmm/src/device_manager/mmio.rs index c3a209a4b26..eea024d9692 100644 --- a/src/vmm/src/device_manager/mmio.rs +++ b/src/vmm/src/device_manager/mmio.rs @@ -25,7 +25,6 @@ use crate::arch::{RTC_MEM_START, SERIAL_MEM_START}; #[cfg(target_arch = "aarch64")] use crate::devices::legacy::{RTCDevice, SerialDevice}; use crate::devices::pseudo::BootTimer; -use crate::devices::virtio::device::VirtioDevice; use crate::devices::virtio::transport::mmio::MmioTransport; use crate::vstate::bus::{Bus, BusError}; #[cfg(target_arch = "x86_64")] @@ -41,12 +40,6 @@ pub enum MmioError { BusInsert(#[from] BusError), /// Failed to allocate requested resourc: {0} Cmdline(#[from] linux_loader::cmdline::Error), - /// Failed to find the device on the bus. - DeviceNotFound, - /// Invalid device type found on the MMIO bus. - InvalidDeviceType, - /// {0} - InternalDeviceError(String), /// Could not create IRQ for MMIO device: {0} CreateIrq(#[from] std::io::Error), /// Invalid MMIO IRQ configuration. @@ -407,31 +400,6 @@ impl MMIODeviceManager { Ok(()) } - /// Run fn `f()` for the virtio device matching `virtio_type` and `id`. - pub fn with_virtio_device_with_id( - &self, - virtio_type: u32, - id: &str, - f: F, - ) -> Result<(), MmioError> - where - T: VirtioDevice + 'static + Debug, - F: FnOnce(&mut T) -> Result<(), String>, - { - if let Some(device) = self.get_virtio_device(virtio_type, id) { - let virtio_device = device.inner.lock().expect("Poisoned lock").device(); - let mut dev = virtio_device.lock().expect("Poisoned lock"); - f(dev - .as_mut_any() - .downcast_mut::() - .ok_or(MmioError::InvalidDeviceType)?) - .map_err(MmioError::InternalDeviceError)?; - } else { - return Err(MmioError::DeviceNotFound); - } - Ok(()) - } - #[cfg(target_arch = "aarch64")] pub fn virtio_device_info(&self) -> Vec<&MMIODeviceInfo> { let mut device_info = Vec::new(); From 74562055749e71babc41ff4da21174ba434ff63e Mon Sep 17 00:00:00 2001 From: Egor Lazarchuk Date: Wed, 8 Oct 2025 16:39:39 +0000 Subject: [PATCH 4/5] refactor: further simplify `with_virtio_device` functions Replace 2 functions: `try_with_virtio_device_with_id` and `with_virtio_device_with_id`, with single `with_virtio_device` that handles any return type of the passed function. Signed-off-by: Egor Lazarchuk --- src/vmm/src/device_manager/mod.rs | 18 ++------------ src/vmm/src/lib.rs | 39 ++++++++++++++----------------- 2 files changed, 19 insertions(+), 38 deletions(-) diff --git a/src/vmm/src/device_manager/mod.rs b/src/vmm/src/device_manager/mod.rs index 05cca130570..c045e17f0bf 100644 --- a/src/vmm/src/device_manager/mod.rs +++ b/src/vmm/src/device_manager/mod.rs @@ -368,15 +368,10 @@ impl DeviceManager { } /// Run fn `f()` for the virtio device matching `virtio_type` and `id`. - pub fn try_with_virtio_device_with_id( - &self, - id: &str, - f: F, - ) -> Result, FindDeviceError> + pub fn with_virtio_device(&self, id: &str, f: F) -> Result where T: VirtioDevice + 'static + Debug, - E: std::error::Error + 'static + Send + Sync, - F: FnOnce(&mut T) -> Result, + F: FnOnce(&mut T) -> R, { if let Some(device) = self.get_virtio_device(T::const_device_type(), id) { let mut dev = device.lock().expect("Poisoned lock"); @@ -388,15 +383,6 @@ impl DeviceManager { Err(FindDeviceError::DeviceNotFound) } } - - /// Run fn `f()` for the virtio device matching `virtio_type` and `id`. - pub fn with_virtio_device_with_id(&self, id: &str, f: F) -> Result - where - T: VirtioDevice + 'static + Debug, - F: FnOnce(&mut T) -> R, - { - self.try_with_virtio_device_with_id(id, |dev: &mut T| Ok::(f(dev)))? - } } #[derive(Debug, Default, Clone, Serialize, Deserialize)] diff --git a/src/vmm/src/lib.rs b/src/vmm/src/lib.rs index 00c7dca0999..7186b904b8a 100644 --- a/src/vmm/src/lib.rs +++ b/src/vmm/src/lib.rs @@ -526,10 +526,9 @@ impl Vmm { path_on_host: String, ) -> Result<(), VmmError> { self.device_manager - .try_with_virtio_device_with_id(drive_id, |block: &mut Block| { + .with_virtio_device(drive_id, |block: &mut Block| { block.update_disk_image(path_on_host) - }) - .map_err(VmmError::FindDeviceError)??; + })??; Ok(()) } @@ -541,18 +540,16 @@ impl Vmm { rl_ops: BucketUpdate, ) -> Result<(), VmmError> { self.device_manager - .try_with_virtio_device_with_id(drive_id, |block: &mut Block| { + .with_virtio_device(drive_id, |block: &mut Block| { block.update_rate_limiter(rl_bytes, rl_ops) - }) - .map_err(VmmError::FindDeviceError)??; + })??; Ok(()) } /// Updates the rate limiter parameters for block device with `drive_id` id. pub fn update_vhost_user_block_config(&mut self, drive_id: &str) -> Result<(), VmmError> { self.device_manager - .try_with_virtio_device_with_id(drive_id, |block: &mut Block| block.update_config()) - .map_err(VmmError::FindDeviceError)??; + .with_virtio_device(drive_id, |block: &mut Block| block.update_config())??; Ok(()) } @@ -566,35 +563,34 @@ impl Vmm { tx_ops: BucketUpdate, ) -> Result<(), VmmError> { self.device_manager - .with_virtio_device_with_id(net_id, |net: &mut Net| { + .with_virtio_device(net_id, |net: &mut Net| { net.patch_rate_limiters(rx_bytes, rx_ops, tx_bytes, tx_ops) - }) - .map_err(VmmError::FindDeviceError) + })?; + Ok(()) } /// Returns a reference to the balloon device if present. pub fn balloon_config(&self) -> Result { - self.device_manager - .with_virtio_device_with_id(BALLOON_DEV_ID, |dev: &mut Balloon| dev.config()) - .map_err(VmmError::FindDeviceError) + let config = self + .device_manager + .with_virtio_device(BALLOON_DEV_ID, |dev: &mut Balloon| dev.config())?; + Ok(config) } /// Returns the latest balloon statistics if they are enabled. pub fn latest_balloon_stats(&self) -> Result { let stats = self .device_manager - .try_with_virtio_device_with_id(BALLOON_DEV_ID, |dev: &mut Balloon| dev.latest_stats()) - .map_err(VmmError::FindDeviceError)??; + .with_virtio_device(BALLOON_DEV_ID, |dev: &mut Balloon| dev.latest_stats())??; Ok(stats) } /// Updates configuration for the balloon device target size. pub fn update_balloon_config(&mut self, amount_mib: u32) -> Result<(), VmmError> { self.device_manager - .try_with_virtio_device_with_id(BALLOON_DEV_ID, |dev: &mut Balloon| { + .with_virtio_device(BALLOON_DEV_ID, |dev: &mut Balloon| { dev.update_size(amount_mib) - }) - .map_err(VmmError::FindDeviceError)??; + })??; Ok(()) } @@ -604,10 +600,9 @@ impl Vmm { stats_polling_interval_s: u16, ) -> Result<(), VmmError> { self.device_manager - .try_with_virtio_device_with_id(BALLOON_DEV_ID, |dev: &mut Balloon| { + .with_virtio_device(BALLOON_DEV_ID, |dev: &mut Balloon| { dev.update_stats_polling_interval(stats_polling_interval_s) - }) - .map_err(VmmError::FindDeviceError)??; + })??; Ok(()) } From 02f7219dc2a37c16ea9d4ef12dcfada10c027b64 Mon Sep 17 00:00:00 2001 From: Egor Lazarchuk Date: Fri, 10 Oct 2025 16:28:08 +0000 Subject: [PATCH 5/5] refactor: remove SharedDeviceType This wrapper was only used in the `update_from_restored_device` function which was instantly unwrapping the value and just adding device to the VmResources. Instead of this round trip just add devices directly since fields are public. This also removes the check for the addition of balloon device, but this check is not needed anyway, since snapshot should not contain conflicting configs. Signed-off-by: Egor Lazarchuk --- src/vmm/src/device_manager/pci_mngr.rs | 26 +++++------ src/vmm/src/device_manager/persist.rs | 34 ++++++-------- src/vmm/src/resources.rs | 61 -------------------------- 3 files changed, 26 insertions(+), 95 deletions(-) diff --git a/src/vmm/src/device_manager/pci_mngr.rs b/src/vmm/src/device_manager/pci_mngr.rs index bf5252d2d17..6c89540381a 100644 --- a/src/vmm/src/device_manager/pci_mngr.rs +++ b/src/vmm/src/device_manager/pci_mngr.rs @@ -10,7 +10,7 @@ use event_manager::{MutEventSubscriber, SubscriberOps}; use log::{debug, error, warn}; use serde::{Deserialize, Serialize}; -use super::persist::{MmdsState, SharedDeviceType}; +use super::persist::MmdsState; use crate::devices::pci::PciSegment; use crate::devices::virtio::balloon::Balloon; use crate::devices::virtio::balloon::persist::{BalloonConstructorArgs, BalloonState}; @@ -431,8 +431,8 @@ impl<'a> Persist<'a> for PciDevices { constructor_args .vm_resources - .update_from_restored_device(SharedDeviceType::Balloon(device.clone())) - .unwrap(); + .balloon + .set_device(device.clone()); pci_devices .restore_pci_device( @@ -456,8 +456,8 @@ impl<'a> Persist<'a> for PciDevices { constructor_args .vm_resources - .update_from_restored_device(SharedDeviceType::VirtioBlock(device.clone())) - .unwrap(); + .block + .add_virtio_device(device.clone()); pci_devices .restore_pci_device( @@ -506,8 +506,8 @@ impl<'a> Persist<'a> for PciDevices { constructor_args .vm_resources - .update_from_restored_device(SharedDeviceType::Network(device.clone())) - .unwrap(); + .net_builder + .add_device(device.clone()); pci_devices .restore_pci_device( @@ -539,8 +539,8 @@ impl<'a> Persist<'a> for PciDevices { constructor_args .vm_resources - .update_from_restored_device(SharedDeviceType::Vsock(device.clone())) - .unwrap(); + .vsock + .set_device(device.clone()); pci_devices .restore_pci_device( @@ -562,8 +562,8 @@ impl<'a> Persist<'a> for PciDevices { constructor_args .vm_resources - .update_from_restored_device(SharedDeviceType::Entropy(device.clone())) - .unwrap(); + .entropy + .set_device(device.clone()); pci_devices .restore_pci_device( @@ -590,8 +590,8 @@ impl<'a> Persist<'a> for PciDevices { constructor_args .vm_resources - .update_from_restored_device(SharedDeviceType::Pmem(device.clone())) - .unwrap(); + .pmem + .add_device(device.clone()); pci_devices .restore_pci_device( diff --git a/src/vmm/src/device_manager/persist.rs b/src/vmm/src/device_manager/persist.rs index 7616f252658..49b4115f2cc 100644 --- a/src/vmm/src/device_manager/persist.rs +++ b/src/vmm/src/device_manager/persist.rs @@ -44,7 +44,7 @@ use crate::devices::virtio::vsock::persist::{ }; use crate::devices::virtio::vsock::{Vsock, VsockError, VsockUnixBackend, VsockUnixBackendError}; use crate::mmds::data_store::MmdsVersion; -use crate::resources::{ResourcesError, VmResources}; +use crate::resources::VmResources; use crate::snapshot::Persist; use crate::vmm_config::mmds::MmdsConfigError; use crate::vstate::bus::BusError; @@ -79,8 +79,6 @@ pub enum DevicePersistError { Entropy(#[from] EntropyError), /// Pmem: {0} Pmem(#[from] PmemError), - /// Resource misconfiguration: {0}. Is the snapshot file corrupted? - ResourcesError(#[from] ResourcesError), /// Could not activate device: {0} DeviceActivation(#[from] ActivateError), } @@ -136,18 +134,6 @@ pub struct DeviceStates { pub pmem_devices: Vec>, } -/// A type used to extract the concrete `Arc>` for each of the device -/// types when restoring from a snapshot. -#[derive(Debug)] -pub enum SharedDeviceType { - VirtioBlock(Arc>), - Network(Arc>), - Balloon(Arc>), - Vsock(Arc>>), - Entropy(Arc>), - Pmem(Arc>), -} - pub struct MMIODevManagerConstructorArgs<'a> { pub mem: &'a GuestMemoryMmap, pub vm: &'a Arc, @@ -437,7 +423,8 @@ impl<'a> Persist<'a> for MMIODeviceManager { constructor_args .vm_resources - .update_from_restored_device(SharedDeviceType::Balloon(device.clone()))?; + .balloon + .set_device(device.clone()); restore_helper( device.clone(), @@ -459,7 +446,8 @@ impl<'a> Persist<'a> for MMIODeviceManager { constructor_args .vm_resources - .update_from_restored_device(SharedDeviceType::VirtioBlock(device.clone()))?; + .block + .add_virtio_device(device.clone()); restore_helper( device.clone(), @@ -498,7 +486,8 @@ impl<'a> Persist<'a> for MMIODeviceManager { constructor_args .vm_resources - .update_from_restored_device(SharedDeviceType::Network(device.clone()))?; + .net_builder + .add_device(device.clone()); restore_helper( device.clone(), @@ -527,7 +516,8 @@ impl<'a> Persist<'a> for MMIODeviceManager { constructor_args .vm_resources - .update_from_restored_device(SharedDeviceType::Vsock(device.clone()))?; + .vsock + .set_device(device.clone()); restore_helper( device.clone(), @@ -551,7 +541,8 @@ impl<'a> Persist<'a> for MMIODeviceManager { constructor_args .vm_resources - .update_from_restored_device(SharedDeviceType::Entropy(device.clone()))?; + .entropy + .set_device(device.clone()); restore_helper( device.clone(), @@ -576,7 +567,8 @@ impl<'a> Persist<'a> for MMIODeviceManager { constructor_args .vm_resources - .update_from_restored_device(SharedDeviceType::Pmem(device.clone()))?; + .pmem + .add_device(device.clone()); restore_helper( device.clone(), diff --git a/src/vmm/src/resources.rs b/src/vmm/src/resources.rs index 53f1185115a..066fe3524be 100644 --- a/src/vmm/src/resources.rs +++ b/src/vmm/src/resources.rs @@ -9,7 +9,6 @@ use serde::{Deserialize, Serialize}; use vm_memory::GuestAddress; use crate::cpu_config::templates::CustomCpuTemplate; -use crate::device_manager::persist::SharedDeviceType; use crate::logger::info; use crate::mmds; use crate::mmds::data_store::{Mmds, MmdsVersion}; @@ -230,40 +229,6 @@ impl VmResources { Ok(mmds.lock().expect("Poisoned lock")) } - /// Updates the resources from a restored device (used for configuring resources when - /// restoring from a snapshot). - pub fn update_from_restored_device( - &mut self, - device: SharedDeviceType, - ) -> Result<(), ResourcesError> { - match device { - SharedDeviceType::VirtioBlock(block) => { - self.block.add_virtio_device(block); - } - SharedDeviceType::Network(network) => { - self.net_builder.add_device(network); - } - SharedDeviceType::Balloon(balloon) => { - self.balloon.set_device(balloon); - - if self.machine_config.huge_pages != HugePageConfig::None { - return Err(ResourcesError::BalloonDevice(BalloonConfigError::HugePages)); - } - } - SharedDeviceType::Vsock(vsock) => { - self.vsock.set_device(vsock); - } - SharedDeviceType::Entropy(entropy) => { - self.entropy.set_device(entropy); - } - SharedDeviceType::Pmem(pmem) => { - self.pmem.add_device(pmem); - } - } - - Ok(()) - } - /// Add a custom CPU template to the VM resources /// to configure vCPUs. pub fn set_custom_cpu_template(&mut self, cpu_template: CustomCpuTemplate) { @@ -563,7 +528,6 @@ mod tests { use crate::HTTP_MAX_PAYLOAD_SIZE; use crate::cpu_config::templates::test_utils::TEST_TEMPLATE_JSON; use crate::cpu_config::templates::{CpuTemplateType, StaticCpuTemplate}; - use crate::devices::virtio::balloon::Balloon; use crate::devices::virtio::block::virtio::VirtioBlockError; use crate::devices::virtio::block::{BlockError, CacheType}; use crate::devices::virtio::vsock::VSOCK_DEV_ID; @@ -1539,31 +1503,6 @@ mod tests { .unwrap_err(); } - #[test] - fn test_negative_restore_balloon_device_with_huge_pages() { - let mut vm_resources = default_vm_resources(); - vm_resources.balloon = BalloonBuilder::new(); - vm_resources - .update_machine_config(&MachineConfigUpdate { - huge_pages: Some(HugePageConfig::Hugetlbfs2M), - ..Default::default() - }) - .unwrap(); - let err = vm_resources - .update_from_restored_device(SharedDeviceType::Balloon(Arc::new(Mutex::new( - Balloon::new(128, false, 0).unwrap(), - )))) - .unwrap_err(); - assert!( - matches!( - err, - ResourcesError::BalloonDevice(BalloonConfigError::HugePages) - ), - "{:?}", - err - ); - } - #[test] fn test_set_entropy_device() { let mut vm_resources = default_vm_resources();