Skip to content

Commit 5bc902b

Browse files
committed
refactor: Have create_vmm_and_vcpus return VmmError
Eliminate a lot of .map_err(Internal) to make the code more readable. Signed-off-by: Patrick Roy <[email protected]>
1 parent c52f1a7 commit 5bc902b

File tree

1 file changed

+14
-30
lines changed

1 file changed

+14
-30
lines changed

src/vmm/src/builder.rs

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use vmm_sys_util::eventfd::EventFd;
3232
#[cfg(target_arch = "x86_64")]
3333
use crate::acpi;
3434
use crate::arch::{BootProtocol, EntryPoint, InitrdConfig};
35+
use crate::builder::StartMicrovmError::Internal;
3536
#[cfg(target_arch = "aarch64")]
3637
use crate::construct_kvm_mpidrs;
3738
use crate::cpu_config::templates::{
@@ -157,53 +158,34 @@ fn create_vmm_and_vcpus(
157158
uffd: Option<Uffd>,
158159
vcpu_count: u8,
159160
kvm_capabilities: Vec<KvmCapability>,
160-
) -> Result<(Vmm, Vec<Vcpu>), StartMicrovmError> {
161-
use self::StartMicrovmError::*;
162-
163-
let kvm = Kvm::new(kvm_capabilities)
164-
.map_err(VmmError::Kvm)
165-
.map_err(StartMicrovmError::Internal)?;
161+
) -> Result<(Vmm, Vec<Vcpu>), VmmError> {
162+
let kvm = Kvm::new(kvm_capabilities).map_err(VmmError::Kvm)?;
166163
// Set up Kvm Vm and register memory regions.
167164
// Build custom CPU config if a custom template is provided.
168-
let mut vm = Vm::new(&kvm)
169-
.map_err(VmmError::Vm)
170-
.map_err(StartMicrovmError::Internal)?;
171-
kvm.check_memory(&guest_memory)
172-
.map_err(VmmError::Kvm)
173-
.map_err(StartMicrovmError::Internal)?;
174-
vm.memory_init(&guest_memory)
175-
.map_err(VmmError::Vm)
176-
.map_err(StartMicrovmError::Internal)?;
165+
let mut vm = Vm::new(&kvm).map_err(VmmError::Vm)?;
166+
kvm.check_memory(&guest_memory).map_err(VmmError::Kvm)?;
167+
vm.memory_init(&guest_memory).map_err(VmmError::Vm)?;
177168

178-
let resource_allocator = ResourceAllocator::new()
179-
.map_err(VmmError::AllocateResources)
180-
.map_err(StartMicrovmError::Internal)?;
169+
let resource_allocator = ResourceAllocator::new().map_err(VmmError::AllocateResources)?;
181170

182171
// Instantiate the MMIO device manager.
183172
let mmio_device_manager = MMIODeviceManager::new();
184173

185174
// Instantiate ACPI device manager.
186175
let acpi_device_manager = ACPIDeviceManager::new();
187176

188-
let (vcpus, vcpus_exit_evt) = vm
189-
.create_vcpus(vcpu_count)
190-
.map_err(VmmError::Vm)
191-
.map_err(Internal)?;
177+
let (vcpus, vcpus_exit_evt) = vm.create_vcpus(vcpu_count).map_err(VmmError::Vm)?;
192178

193179
#[cfg(target_arch = "x86_64")]
194180
let pio_device_manager = {
195181
// Make stdout non blocking.
196182
set_stdout_nonblocking();
197183

198184
// Serial device setup.
199-
let serial_device =
200-
setup_serial_device(event_manager, std::io::stdin(), io::stdout()).map_err(Internal)?;
185+
let serial_device = setup_serial_device(event_manager, std::io::stdin(), io::stdout())?;
201186

202187
// x86_64 uses the i8042 reset event as the Vmm exit event.
203-
let reset_evt = vcpus_exit_evt
204-
.try_clone()
205-
.map_err(VmmError::EventFd)
206-
.map_err(Internal)?;
188+
let reset_evt = vcpus_exit_evt.try_clone().map_err(VmmError::EventFd)?;
207189

208190
// create pio dev manager with legacy devices
209191
// TODO Remove these unwraps.
@@ -276,7 +258,8 @@ pub fn build_microvm_for_boot(
276258
None,
277259
vm_resources.machine_config.vcpu_count,
278260
cpu_template.kvm_capabilities.clone(),
279-
)?;
261+
)
262+
.map_err(Internal)?;
280263

281264
#[cfg(feature = "gdb")]
282265
let (gdb_tx, gdb_rx) = mpsc::channel();
@@ -471,7 +454,8 @@ pub fn build_microvm_from_snapshot(
471454
uffd,
472455
vm_resources.machine_config.vcpu_count,
473456
microvm_state.kvm_state.kvm_cap_modifiers.clone(),
474-
)?;
457+
)
458+
.map_err(Internal)?;
475459

476460
#[cfg(target_arch = "x86_64")]
477461
{

0 commit comments

Comments
 (0)