Skip to content

Commit

Permalink
feat(vmm): make the initramfs optional
Browse files Browse the repository at this point in the history
Signed-off-by: Kuruyia <github@kuruyia.net>
  • Loading branch information
Kuruyia committed Apr 20, 2024
1 parent 931a321 commit 419fee9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/vmm/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>,

/// Number of virtual CPUs assigned to the guest.
#[clap(short, long, env, default_value = "1")]
Expand Down
21 changes: 12 additions & 9 deletions src/vmm/src/core/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,9 @@ fn load_initramfs(mem: &GuestMemoryMmap, start_addr: u64, data: Vec<u8>) -> Resu
pub fn kernel_setup(
guest_memory: &GuestMemoryMmap,
kernel_path: PathBuf,
initramfs_path: PathBuf,
initramfs_path: Option<PathBuf>,
) -> Result<KernelLoaderResult> {
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.
Expand All @@ -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))?;

Expand All @@ -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::<GuestMemoryMmap>(
Expand Down
6 changes: 3 additions & 3 deletions src/vmm/src/core/vmm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -220,13 +220,13 @@ impl VMM {
num_vcpus: u8,
mem_size_mb: u32,
kernel_path: &Path,
initramfs_path: &Path,
initramfs_path: &Option<PathBuf>,
) -> 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)?;
Expand Down

0 comments on commit 419fee9

Please sign in to comment.