Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(vmm): add support for initramfs #18

Merged
merged 2 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: Option<PathBuf>,

/// Number of virtual CPUs assigned to the guest.
#[clap(short, long, env, default_value = "1")]
pub cpus: u8,
Expand Down
36 changes: 34 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,6 +126,7 @@ pub fn build_bootparams(
pub fn kernel_setup(
guest_memory: &GuestMemoryMmap,
kernel_path: PathBuf,
initramfs_path: Option<PathBuf>,
) -> Result<KernelLoaderResult> {
let mut kernel_image = File::open(kernel_path).map_err(Error::IO)?;
let zero_page_addr = GuestAddress(ZEROPG_START);
Expand Down Expand Up @@ -138,6 +158,18 @@ pub fn kernel_setup(
)
.map_err(Error::KernelLoad)?;

// 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>(
&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
17 changes: 14 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 @@ -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: &Option<PathBuf>,
) -> 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.clone(),
)?;
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