Skip to content

Commit

Permalink
refactor: Remove some getters and setters for boot_source
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
roypat committed Nov 25, 2024
1 parent 1320786 commit 46ba748
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 32 deletions.
6 changes: 4 additions & 2 deletions src/vmm/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -508,7 +510,7 @@ pub fn build_microvm_from_snapshot(
vmm.vm.restore_state(&microvm_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 {
Expand Down
2 changes: 1 addition & 1 deletion src/vmm/src/persist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down
37 changes: 8 additions & 29 deletions src/vmm/src/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)),
Expand Down Expand Up @@ -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();
Expand All @@ -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!(
Expand All @@ -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
Expand Down

0 comments on commit 46ba748

Please sign in to comment.