Skip to content

Commit 46ba748

Browse files
committed
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 <[email protected]>
1 parent 1320786 commit 46ba748

File tree

3 files changed

+13
-32
lines changed

3 files changed

+13
-32
lines changed

src/vmm/src/builder.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,9 @@ pub fn build_microvm_for_boot(
259259
let request_ts = TimestampUs::default();
260260

261261
let boot_config = vm_resources
262-
.boot_source_builder()
262+
.boot_source
263+
.builder
264+
.as_ref()
263265
.ok_or(MissingKernelConfig)?;
264266

265267
let guest_memory = vm_resources
@@ -508,7 +510,7 @@ pub fn build_microvm_from_snapshot(
508510
vmm.vm.restore_state(&microvm_state.vm_state)?;
509511

510512
// Restore the boot source config paths.
511-
vm_resources.set_boot_source_config(microvm_state.vm_info.boot_source);
513+
vm_resources.boot_source.config = microvm_state.vm_info.boot_source;
512514

513515
// Restore devices states.
514516
let mmio_ctor_args = MMIODevManagerConstructorArgs {

src/vmm/src/persist.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl From<&VmResources> for VmInfo {
6464
mem_size_mib: value.vm_config.mem_size_mib as u64,
6565
smt: value.vm_config.smt,
6666
cpu_template: StaticCpuTemplate::from(&value.vm_config.cpu_template),
67-
boot_source: value.boot_source_config().clone(),
67+
boot_source: value.boot_source.config.clone(),
6868
huge_pages: value.vm_config.huge_pages,
6969
}
7070
}

src/vmm/src/resources.rs

+8-29
Original file line numberDiff line numberDiff line change
@@ -315,16 +315,6 @@ impl VmResources {
315315
mmds_config
316316
}
317317

318-
/// Gets a reference to the boot source configuration.
319-
pub fn boot_source_config(&self) -> &BootSourceConfig {
320-
&self.boot_source.config
321-
}
322-
323-
/// Gets a reference to the boot source builder.
324-
pub fn boot_source_builder(&self) -> Option<&BootConfig> {
325-
self.boot_source.builder.as_ref()
326-
}
327-
328318
/// Sets a balloon device to be attached when the VM starts.
329319
pub fn set_balloon_device(
330320
&mut self,
@@ -354,14 +344,12 @@ impl VmResources {
354344
return Err(BootSourceConfigError::HugePagesAndInitRd);
355345
}
356346

357-
self.set_boot_source_config(boot_source_cfg);
358-
self.boot_source.builder = Some(BootConfig::new(self.boot_source_config())?);
359-
Ok(())
360-
}
347+
self.boot_source = BootSource {
348+
builder: Some(BootConfig::new(&boot_source_cfg)?),
349+
config: boot_source_cfg,
350+
};
361351

362-
/// Set the boot source configuration (contains raw kernel config details).
363-
pub fn set_boot_source_config(&mut self, boot_source_cfg: BootSourceConfig) {
364-
self.boot_source.config = boot_source_cfg;
352+
Ok(())
365353
}
366354

367355
/// Inserts a block to be attached when the VM starts.
@@ -512,7 +500,7 @@ impl From<&VmResources> for VmmConfig {
512500
VmmConfig {
513501
balloon_device: resources.balloon.get_config().ok(),
514502
block_devices: resources.block.configs(),
515-
boot_source: resources.boot_source_config().clone(),
503+
boot_source: resources.boot_source.config.clone(),
516504
cpu_config: None,
517505
logger: None,
518506
machine_config: Some(MachineConfig::from(&resources.vm_config)),
@@ -1521,15 +1509,6 @@ mod tests {
15211509
assert_eq!(actual_entropy_cfg, entropy_device_cfg);
15221510
}
15231511

1524-
#[test]
1525-
fn test_boot_config() {
1526-
let vm_resources = default_vm_resources();
1527-
let expected_boot_cfg = vm_resources.boot_source.builder.as_ref().unwrap();
1528-
let actual_boot_cfg = vm_resources.boot_source_builder().unwrap();
1529-
1530-
assert!(actual_boot_cfg == expected_boot_cfg);
1531-
}
1532-
15331512
#[test]
15341513
fn test_set_boot_source() {
15351514
let tmp_file = TempFile::new().unwrap();
@@ -1541,7 +1520,7 @@ mod tests {
15411520
};
15421521

15431522
let mut vm_resources = default_vm_resources();
1544-
let boot_builder = vm_resources.boot_source_builder().unwrap();
1523+
let boot_builder = vm_resources.boot_source.builder.as_ref().unwrap();
15451524
let tmp_ino = tmp_file.as_file().metadata().unwrap().st_ino();
15461525

15471526
assert_ne!(
@@ -1568,7 +1547,7 @@ mod tests {
15681547
);
15691548

15701549
vm_resources.build_boot_source(expected_boot_cfg).unwrap();
1571-
let boot_source_builder = vm_resources.boot_source_builder().unwrap();
1550+
let boot_source_builder = vm_resources.boot_source.builder.unwrap();
15721551
assert_eq!(
15731552
boot_source_builder
15741553
.cmdline

0 commit comments

Comments
 (0)