diff --git a/src/vmm/src/args.rs b/src/vmm/src/args.rs index 606d7bc..fcfca2d 100644 --- a/src/vmm/src/args.rs +++ b/src/vmm/src/args.rs @@ -15,7 +15,7 @@ pub struct CliArguments { /// Path to the cpio archive to use as the initramfs. #[arg(short, long, env)] - pub initramfs: PathBuf, + pub initramfs: Option, /// Number of virtual CPUs assigned to the guest. #[clap(short, long, env, default_value = "1")] diff --git a/src/vmm/src/core/kernel.rs b/src/vmm/src/core/kernel.rs index c3777e5..c9b82ea 100644 --- a/src/vmm/src/core/kernel.rs +++ b/src/vmm/src/core/kernel.rs @@ -126,10 +126,9 @@ fn load_initramfs(mem: &GuestMemoryMmap, start_addr: u64, data: Vec) -> Resu pub fn kernel_setup( guest_memory: &GuestMemoryMmap, kernel_path: PathBuf, - initramfs_path: PathBuf, + initramfs_path: Option, ) -> Result { let mut kernel_image = File::open(kernel_path).map_err(Error::IO)?; - let initramfs = fs::read(initramfs_path).map_err(Error::IO)?; let zero_page_addr = GuestAddress(ZEROPG_START); // Load the kernel into guest memory. @@ -141,10 +140,6 @@ pub fn kernel_setup( ) .map_err(Error::KernelLoad)?; - // Load the initramfs into guest memory. - let (initramfs_addr, initramfs_size) = - load_initramfs(guest_memory, kernel_load.kernel_end, initramfs)?; - // Generate boot parameters. let mut bootparams = build_bootparams(guest_memory, GuestAddress(HIMEM_START))?; @@ -163,9 +158,17 @@ pub fn kernel_setup( ) .map_err(Error::KernelLoad)?; - // Add the initramfs to the boot parameters. - bootparams.hdr.ramdisk_image = initramfs_addr; - bootparams.hdr.ramdisk_size = initramfs_size; + // Handle the initramfs. + if let Some(initramfs_path) = initramfs_path { + // Load the initramfs into guest memory. + let initramfs = fs::read(initramfs_path).map_err(Error::IO)?; + let (initramfs_addr, initramfs_size) = + load_initramfs(guest_memory, kernel_load.kernel_end, initramfs)?; + + // Add the initramfs to the boot parameters. + bootparams.hdr.ramdisk_image = initramfs_addr; + bootparams.hdr.ramdisk_size = initramfs_size; + } // Write the boot parameters in the zeropage. LinuxBootConfigurator::write_bootparams::( diff --git a/src/vmm/src/core/vmm.rs b/src/vmm/src/core/vmm.rs index 806b46d..023dbfb 100644 --- a/src/vmm/src/core/vmm.rs +++ b/src/vmm/src/core/vmm.rs @@ -12,7 +12,7 @@ use std::io; use std::net::Ipv4Addr; use std::os::unix::io::AsRawFd; use std::os::unix::prelude::RawFd; -use std::path::Path; +use std::path::{Path, PathBuf}; use std::sync::{Arc, Mutex}; use std::thread; use tracing::info; @@ -220,13 +220,13 @@ impl VMM { num_vcpus: u8, mem_size_mb: u32, kernel_path: &Path, - initramfs_path: &Path, + initramfs_path: &Option, ) -> Result<()> { self.configure_memory(mem_size_mb)?; let kernel_load = kernel::kernel_setup( &self.guest_memory, kernel_path.to_path_buf(), - initramfs_path.to_path_buf(), + initramfs_path.clone(), )?; self.configure_io()?; self.configure_vcpus(num_vcpus, kernel_load)?;