Skip to content

Commit

Permalink
feat(vmm): add support for initramfs
Browse files Browse the repository at this point in the history
This adds a new command line argument, `--initramfs`, which takes the
path of an initramfs archive to load into the memory of the VM.

The initramfs is loaded after the kernel in memory, and the boot
parameters are updated to tell the kernel the address and size of the
loaded initramfs.

Signed-off-by: Kuruyia <github@kuruyia.net>
  • Loading branch information
Kuruyia committed Apr 20, 2024
1 parent b98a1c6 commit 931a321
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/vmm/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ pub struct CliArguments {
#[arg(short, long, env)]
pub kernel: PathBuf,

/// Path to the cpio archive to use as the initramfs.
#[arg(short, long, env)]
pub initramfs: PathBuf,

/// Number of virtual CPUs assigned to the guest.
#[clap(short, long, env, default_value = "1")]
pub cpus: u8,
Expand Down
33 changes: 31 additions & 2 deletions src/vmm/src/core/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use linux_loader::bootparam::boot_params;
use linux_loader::cmdline::Cmdline;
use linux_loader::configurator::{linux::LinuxBootConfigurator, BootConfigurator, BootParams};
use linux_loader::loader::{elf::Elf, load_cmdline, KernelLoader, KernelLoaderResult};
use std::fs::File;
use std::fs::{self, File};
use std::path::PathBuf;
use std::result;
use vm_memory::{Address, GuestAddress, GuestMemory, GuestMemoryMmap};
use vm_memory::{Address, Bytes, GuestAddress, GuestMemory, GuestMemoryMmap};

// x86_64 boot constants. See https://www.kernel.org/doc/Documentation/x86/boot.txt for the full
// documentation.
Expand Down Expand Up @@ -98,6 +98,25 @@ pub fn build_bootparams(
Ok(params)
}

/// Load the initramfs into guest memory. Returns a tuple containing the address
/// where the initramfs was loaded, and its size.
///
/// # Arguments
///
/// * `guest_memory` - guest memory
/// * `start_addr` - the address where to start looking for a place to store the initramfs
/// * `data` - the initramfs data
fn load_initramfs(mem: &GuestMemoryMmap, start_addr: u64, data: Vec<u8>) -> Result<(u32, u32)> {
let addr = GuestAddress(start_addr);

mem.checked_offset(addr, data.len())
.ok_or(Error::InitramfsLoad)?;
mem.write_slice(data.as_slice(), addr)
.map_err(|_| Error::InitramfsLoad)?;

Ok((addr.raw_value() as u32, data.len() as u32))
}

/// Set guest kernel up.
///
/// # Arguments
Expand All @@ -107,8 +126,10 @@ pub fn build_bootparams(
pub fn kernel_setup(
guest_memory: &GuestMemoryMmap,
kernel_path: PathBuf,
initramfs_path: 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 @@ -120,6 +141,10 @@ 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 @@ -138,6 +163,10 @@ 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;

// Write the boot parameters in the zeropage.
LinuxBootConfigurator::write_bootparams::<GuestMemoryMmap>(
&BootParams::new::<boot_params>(&bootparams, zero_page_addr),
Expand Down
2 changes: 2 additions & 0 deletions src/vmm/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ pub enum Error {
Cmdline(linux_loader::cmdline::Error),
/// Failed to load kernel.
KernelLoad(loader::Error),
/// Failed to load the initramfs.
InitramfsLoad,
/// Invalid E820 configuration.
E820Configuration,
/// Highmem start address is past the guest memory end.
Expand Down
15 changes: 13 additions & 2 deletions src/vmm/src/core/vmm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,20 @@ impl VMM {
/// * `num_vcpus` Number of virtual CPUs
/// * `mem_size_mb` Memory size (in MB)
/// * `kernel_path` Path to a Linux kernel
pub fn configure(&mut self, num_vcpus: u8, mem_size_mb: u32, kernel_path: &Path) -> Result<()> {
/// * `initramfs_path` Path to an initramfs
pub fn configure(
&mut self,
num_vcpus: u8,
mem_size_mb: u32,
kernel_path: &Path,
initramfs_path: &Path,
) -> Result<()> {
self.configure_memory(mem_size_mb)?;
let kernel_load = kernel::kernel_setup(&self.guest_memory, kernel_path.to_path_buf())?;
let kernel_load = kernel::kernel_setup(
&self.guest_memory,
kernel_path.to_path_buf(),
initramfs_path.to_path_buf(),
)?;
self.configure_io()?;
self.configure_vcpus(num_vcpus, kernel_load)?;

Expand Down
2 changes: 1 addition & 1 deletion src/vmm/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn main() -> Result<(), Error> {
let mut vmm =
VMM::new(args.network_host_ip, args.network_host_netmask).map_err(Error::VmmNew)?;

vmm.configure(args.cpus, args.memory, &args.kernel)
vmm.configure(args.cpus, args.memory, &args.kernel, &args.initramfs)
.map_err(Error::VmmConfigure)?;

// Run the VMM
Expand Down

0 comments on commit 931a321

Please sign in to comment.