Skip to content

Commit b710875

Browse files
dianpopaalxiord
authored andcommitted
separate boot source config from runtime details
Signed-off-by: Diana Popa <dpopa@amazon.com>
1 parent 9b16583 commit b710875

File tree

4 files changed

+42
-33
lines changed

4 files changed

+42
-33
lines changed

src/vmm/src/builder.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub enum StartMicrovmError {
7878
KernelLoader(linux_loader::loader::Error),
7979
/// Cannot load command line string.
8080
LoadCommandline(linux_loader::loader::Error),
81-
/// Cannot start the VM because the kernel was not configured.
81+
/// Cannot start the VM because the kernel builder was not configured.
8282
MissingKernelConfig,
8383
/// Cannot start the VM because the size of the guest memory was not specified.
8484
MissingMemSizeConfig,
@@ -326,7 +326,9 @@ pub fn build_microvm_for_boot(
326326
// Timestamp for measuring microVM boot duration.
327327
let request_ts = TimestampUs::default();
328328

329-
let boot_config = vm_resources.boot_source().ok_or(MissingKernelConfig)?;
329+
let boot_config = vm_resources
330+
.boot_source_builder()
331+
.ok_or(MissingKernelConfig)?;
330332

331333
let track_dirty_pages = vm_resources.track_dirty_pages();
332334
let guest_memory =

src/vmm/src/resources.rs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ use utils::net::ipv4addr::is_link_local_valid;
1212

1313
use crate::device_manager::persist::SharedDeviceType;
1414
use crate::vmm_config::balloon::*;
15-
use crate::vmm_config::boot_source::{BootConfig, BootSourceConfig, BootSourceConfigError};
15+
use crate::vmm_config::boot_source::{
16+
BootConfig, BootSource, BootSourceConfig, BootSourceConfigError,
17+
};
1618
use crate::vmm_config::drive::*;
1719
use crate::vmm_config::instance_info::InstanceInfo;
1820
use crate::vmm_config::logger::{init_logger, LoggerConfig, LoggerConfigError};
@@ -99,8 +101,8 @@ pub struct VmmConfig {
99101
pub struct VmResources {
100102
/// The vCpu and memory configuration for this microVM.
101103
vm_config: VmConfig,
102-
/// The boot configuration for this microVM.
103-
boot_config: Option<BootConfig>,
104+
/// The boot source spec (contains both config and builder) for this microVM.
105+
boot_source: BootSource,
104106
/// The block devices.
105107
pub block: BlockBuilder,
106108
/// The vsock device.
@@ -146,7 +148,7 @@ impl VmResources {
146148
resources.update_vm_config(&machine_config)?;
147149
}
148150

149-
resources.set_boot_source(vmm_config.boot_source)?;
151+
resources.build_boot_source(vmm_config.boot_source)?;
150152

151153
for drive_config in vmm_config.block_devices.into_iter() {
152154
resources.set_block_device(drive_config)?;
@@ -340,8 +342,13 @@ impl VmResources {
340342
}
341343

342344
/// Gets a reference to the boot source configuration.
343-
pub fn boot_source(&self) -> Option<&BootConfig> {
344-
self.boot_config.as_ref()
345+
pub fn boot_source_config(&self) -> &BootSourceConfig {
346+
&self.boot_source.config
347+
}
348+
349+
/// Gets a reference to the boot source builder.
350+
pub fn boot_source_builder(&self) -> Option<&BootConfig> {
351+
self.boot_source.builder.as_ref()
345352
}
346353

347354
/// Sets a balloon device to be attached when the VM starts.
@@ -358,15 +365,21 @@ impl VmResources {
358365
self.balloon.set(config)
359366
}
360367

361-
/// Set the guest boot source configuration.
362-
pub fn set_boot_source(
368+
/// Obtains the boot source hooks (kernel fd, commandline creation and validation).
369+
pub fn build_boot_source(
363370
&mut self,
364371
boot_source_cfg: BootSourceConfig,
365372
) -> Result<BootSourceConfigError> {
366-
self.boot_config = Some(BootConfig::new(boot_source_cfg)?);
373+
self.set_boot_source_config(boot_source_cfg);
374+
self.boot_source.builder = Some(BootConfig::new(&self.boot_source_config())?);
367375
Ok(())
368376
}
369377

378+
/// Set the boot source configuration (contains raw kernel config details).
379+
pub fn set_boot_source_config(&mut self, boot_source_cfg: BootSourceConfig) {
380+
self.boot_source.config = boot_source_cfg;
381+
}
382+
370383
/// Inserts a block to be attached when the VM starts.
371384
// Only call this function as part of user configuration.
372385
// If the drive_id does not exist, a new Block Device Config is added to the list.
@@ -465,16 +478,10 @@ impl VmResources {
465478

466479
impl From<&VmResources> for VmmConfig {
467480
fn from(resources: &VmResources) -> Self {
468-
let boot_source = resources
469-
.boot_config
470-
.as_ref()
471-
.map(BootSourceConfig::from)
472-
.unwrap_or_default();
473-
474481
VmmConfig {
475482
balloon_device: resources.balloon.get_config().ok(),
476483
block_devices: resources.block.configs(),
477-
boot_source,
484+
boot_source: resources.boot_source_config().clone(),
478485
logger: None,
479486
machine_config: Some(resources.vm_config.clone()),
480487
metrics: None,

src/vmm/src/rpc_interface.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ impl<'a> PrebootApiController<'a> {
483483
fn set_boot_source(&mut self, cfg: BootSourceConfig) -> ActionResult {
484484
self.boot_path = true;
485485
self.vm_resources
486-
.set_boot_source(cfg)
486+
.build_boot_source(cfg)
487487
.map(|()| VmmData::Empty)
488488
.map_err(VmmActionError::BootSource)
489489
}

src/vmm/src/vmm_config/boot_source.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,6 @@ pub struct BootSourceConfig {
3535
pub boot_args: Option<String>,
3636
}
3737

38-
impl From<&BootConfig> for BootSourceConfig {
39-
fn from(cfg: &BootConfig) -> Self {
40-
cfg.description.clone()
41-
}
42-
}
43-
4438
/// Errors associated with actions on `BootSourceConfig`.
4539
#[derive(Debug)]
4640
pub enum BootSourceConfigError {
@@ -69,21 +63,29 @@ impl Display for BootSourceConfigError {
6963
}
7064
}
7165

72-
/// Holds the kernel configuration.
66+
/// Holds the kernel specification (both configuration as well as runtime details).
67+
#[derive(Default)]
68+
pub struct BootSource {
69+
/// The boot source configuration.
70+
pub config: BootSourceConfig,
71+
/// The boot source builder (a boot source allocated and validated).
72+
/// It is an option cause a resumed microVM does not need it.
73+
pub builder: Option<BootConfig>,
74+
}
75+
76+
/// Holds the kernel builder (created and validates based on BootSourceConfig).
7377
pub struct BootConfig {
7478
/// The commandline validated against correctness.
7579
pub cmdline: linux_loader::cmdline::Cmdline,
7680
/// The descriptor to the kernel file.
77-
pub kernel_file: std::fs::File,
81+
pub kernel_file: File,
7882
/// The descriptor to the initrd file, if there is one.
79-
pub initrd_file: Option<std::fs::File>,
80-
/// The configuration above fields are based on.
81-
pub description: BootSourceConfig,
83+
pub initrd_file: Option<File>,
8284
}
8385

8486
impl BootConfig {
8587
/// Creates the BootConfig based on a given configuration.
86-
pub fn new(cfg: BootSourceConfig) -> std::result::Result<Self, BootSourceConfigError> {
88+
pub fn new(cfg: &BootSourceConfig) -> std::result::Result<Self, BootSourceConfigError> {
8789
use self::BootSourceConfigError::{
8890
InvalidInitrdPath, InvalidKernelCommandLine, InvalidKernelPath,
8991
};
@@ -107,8 +109,6 @@ impl BootConfig {
107109
cmdline,
108110
kernel_file,
109111
initrd_file,
110-
// We can simply store original config since it doesn't support updates.
111-
description: cfg,
112112
})
113113
}
114114
}

0 commit comments

Comments
 (0)