From 46ba7487ec8808e771fd4f0947f398fe4fc2c79c Mon Sep 17 00:00:00 2001 From: Patrick Roy Date: Fri, 15 Nov 2024 10:33:18 +0000 Subject: [PATCH] refactor: Remove some getters and setters for boot_source The initialization code for the `boot_source` field in `build_boot_source` had me do a double take when I first saw, with all the setters and getters obscuring what is really happening. Clean this up by simply using a struct initializer, and also remove the getters and setters altogether, since all the fields are pub anyway. Signed-off-by: Patrick Roy --- src/vmm/src/builder.rs | 6 ++++-- src/vmm/src/persist.rs | 2 +- src/vmm/src/resources.rs | 37 ++++++++----------------------------- 3 files changed, 13 insertions(+), 32 deletions(-) diff --git a/src/vmm/src/builder.rs b/src/vmm/src/builder.rs index 1f52fdad063..3355633e3c8 100644 --- a/src/vmm/src/builder.rs +++ b/src/vmm/src/builder.rs @@ -259,7 +259,9 @@ pub fn build_microvm_for_boot( let request_ts = TimestampUs::default(); let boot_config = vm_resources - .boot_source_builder() + .boot_source + .builder + .as_ref() .ok_or(MissingKernelConfig)?; let guest_memory = vm_resources @@ -508,7 +510,7 @@ pub fn build_microvm_from_snapshot( vmm.vm.restore_state(µvm_state.vm_state)?; // Restore the boot source config paths. - vm_resources.set_boot_source_config(microvm_state.vm_info.boot_source); + vm_resources.boot_source.config = microvm_state.vm_info.boot_source; // Restore devices states. let mmio_ctor_args = MMIODevManagerConstructorArgs { diff --git a/src/vmm/src/persist.rs b/src/vmm/src/persist.rs index 5b01ed49c75..3479e0b6309 100644 --- a/src/vmm/src/persist.rs +++ b/src/vmm/src/persist.rs @@ -64,7 +64,7 @@ impl From<&VmResources> for VmInfo { mem_size_mib: value.vm_config.mem_size_mib as u64, smt: value.vm_config.smt, cpu_template: StaticCpuTemplate::from(&value.vm_config.cpu_template), - boot_source: value.boot_source_config().clone(), + boot_source: value.boot_source.config.clone(), huge_pages: value.vm_config.huge_pages, } } diff --git a/src/vmm/src/resources.rs b/src/vmm/src/resources.rs index a4d15641975..0ad4df8aa19 100644 --- a/src/vmm/src/resources.rs +++ b/src/vmm/src/resources.rs @@ -315,16 +315,6 @@ impl VmResources { mmds_config } - /// Gets a reference to the boot source configuration. - pub fn boot_source_config(&self) -> &BootSourceConfig { - &self.boot_source.config - } - - /// Gets a reference to the boot source builder. - pub fn boot_source_builder(&self) -> Option<&BootConfig> { - self.boot_source.builder.as_ref() - } - /// Sets a balloon device to be attached when the VM starts. pub fn set_balloon_device( &mut self, @@ -354,14 +344,12 @@ impl VmResources { return Err(BootSourceConfigError::HugePagesAndInitRd); } - self.set_boot_source_config(boot_source_cfg); - self.boot_source.builder = Some(BootConfig::new(self.boot_source_config())?); - Ok(()) - } + self.boot_source = BootSource { + builder: Some(BootConfig::new(&boot_source_cfg)?), + config: boot_source_cfg, + }; - /// Set the boot source configuration (contains raw kernel config details). - pub fn set_boot_source_config(&mut self, boot_source_cfg: BootSourceConfig) { - self.boot_source.config = boot_source_cfg; + Ok(()) } /// Inserts a block to be attached when the VM starts. @@ -512,7 +500,7 @@ impl From<&VmResources> for VmmConfig { VmmConfig { balloon_device: resources.balloon.get_config().ok(), block_devices: resources.block.configs(), - boot_source: resources.boot_source_config().clone(), + boot_source: resources.boot_source.config.clone(), cpu_config: None, logger: None, machine_config: Some(MachineConfig::from(&resources.vm_config)), @@ -1521,15 +1509,6 @@ mod tests { assert_eq!(actual_entropy_cfg, entropy_device_cfg); } - #[test] - fn test_boot_config() { - let vm_resources = default_vm_resources(); - let expected_boot_cfg = vm_resources.boot_source.builder.as_ref().unwrap(); - let actual_boot_cfg = vm_resources.boot_source_builder().unwrap(); - - assert!(actual_boot_cfg == expected_boot_cfg); - } - #[test] fn test_set_boot_source() { let tmp_file = TempFile::new().unwrap(); @@ -1541,7 +1520,7 @@ mod tests { }; let mut vm_resources = default_vm_resources(); - let boot_builder = vm_resources.boot_source_builder().unwrap(); + let boot_builder = vm_resources.boot_source.builder.as_ref().unwrap(); let tmp_ino = tmp_file.as_file().metadata().unwrap().st_ino(); assert_ne!( @@ -1568,7 +1547,7 @@ mod tests { ); vm_resources.build_boot_source(expected_boot_cfg).unwrap(); - let boot_source_builder = vm_resources.boot_source_builder().unwrap(); + let boot_source_builder = vm_resources.boot_source.builder.unwrap(); assert_eq!( boot_source_builder .cmdline