mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2024-10-02 11:35:46 +00:00
cloud-hypervisor: Add --disk option to provide VM rootfs
Based on the new virtio-blk support, this commit allows any user to specify a --disk option in order to select the rootfs it wants to use for the VM. For now it assumes the partition 3 /dev/vd3 is the one where we can find the rootfs. Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
parent
b67e0b3dad
commit
1270d09301
15
src/main.rs
15
src/main.rs
@ -25,6 +25,12 @@ fn main() {
|
|||||||
.help("Path to kernel image (vmlinux)")
|
.help("Path to kernel image (vmlinux)")
|
||||||
.takes_value(true),
|
.takes_value(true),
|
||||||
)
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name("disk")
|
||||||
|
.long("disk")
|
||||||
|
.help("Path to VM disk image")
|
||||||
|
.takes_value(true),
|
||||||
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::with_name("cpus")
|
Arg::with_name("cpus")
|
||||||
.long("cpus")
|
.long("cpus")
|
||||||
@ -43,9 +49,14 @@ fn main() {
|
|||||||
.value_of("kernel")
|
.value_of("kernel")
|
||||||
.map(PathBuf::from)
|
.map(PathBuf::from)
|
||||||
.expect("Missing argument: kernel");
|
.expect("Missing argument: kernel");
|
||||||
|
|
||||||
let kernel_path = kernel_arg.as_path();
|
let kernel_path = kernel_arg.as_path();
|
||||||
|
|
||||||
|
let disk_arg = cmd_arguments
|
||||||
|
.value_of("disk")
|
||||||
|
.map(PathBuf::from)
|
||||||
|
.expect("Missing argument: disk");
|
||||||
|
let disk_path = disk_arg.as_path();
|
||||||
|
|
||||||
let mut vcpus = DEFAULT_VCPUS;
|
let mut vcpus = DEFAULT_VCPUS;
|
||||||
if let Some(cpus) = cmd_arguments.value_of("cpus") {
|
if let Some(cpus) = cmd_arguments.value_of("cpus") {
|
||||||
vcpus = cpus.parse::<u8>().unwrap();
|
vcpus = cpus.parse::<u8>().unwrap();
|
||||||
@ -59,7 +70,7 @@ fn main() {
|
|||||||
println!("VM [{} vCPUS {} MB of memory]", vcpus, memory);
|
println!("VM [{} vCPUS {} MB of memory]", vcpus, memory);
|
||||||
println!("Booting {:?}...", kernel_path);
|
println!("Booting {:?}...", kernel_path);
|
||||||
|
|
||||||
let vm_config = VmConfig::new(kernel_path, vcpus, memory).unwrap();
|
let vm_config = VmConfig::new(kernel_path, disk_path, vcpus, memory).unwrap();
|
||||||
|
|
||||||
vmm::boot_kernel(vm_config).unwrap();
|
vmm::boot_kernel(vm_config).unwrap();
|
||||||
}
|
}
|
||||||
|
@ -206,6 +206,7 @@ impl Vcpu {
|
|||||||
|
|
||||||
pub struct VmConfig<'a> {
|
pub struct VmConfig<'a> {
|
||||||
kernel_path: &'a Path,
|
kernel_path: &'a Path,
|
||||||
|
disk_path: &'a Path,
|
||||||
cmdline: Option<cmdline::Cmdline>,
|
cmdline: Option<cmdline::Cmdline>,
|
||||||
cmdline_addr: GuestAddress,
|
cmdline_addr: GuestAddress,
|
||||||
|
|
||||||
@ -214,9 +215,15 @@ pub struct VmConfig<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> VmConfig<'a> {
|
impl<'a> VmConfig<'a> {
|
||||||
pub fn new(kernel_path: &'a Path, vcpus: u8, memory_size: GuestUsize) -> Result<Self> {
|
pub fn new(
|
||||||
|
kernel_path: &'a Path,
|
||||||
|
disk_path: &'a Path,
|
||||||
|
vcpus: u8,
|
||||||
|
memory_size: GuestUsize,
|
||||||
|
) -> Result<Self> {
|
||||||
Ok(VmConfig {
|
Ok(VmConfig {
|
||||||
kernel_path,
|
kernel_path,
|
||||||
|
disk_path,
|
||||||
memory_size,
|
memory_size,
|
||||||
vcpu_count: vcpus,
|
vcpu_count: vcpus,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
@ -232,6 +239,7 @@ impl<'a> Default for VmConfig<'a> {
|
|||||||
|
|
||||||
VmConfig {
|
VmConfig {
|
||||||
kernel_path: Path::new(""),
|
kernel_path: Path::new(""),
|
||||||
|
disk_path: Path::new(""),
|
||||||
cmdline: Some(cmdline),
|
cmdline: Some(cmdline),
|
||||||
cmdline_addr: CMDLINE_OFFSET,
|
cmdline_addr: CMDLINE_OFFSET,
|
||||||
memory_size: DEFAULT_MEMORY,
|
memory_size: DEFAULT_MEMORY,
|
||||||
@ -257,7 +265,12 @@ struct DeviceManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl DeviceManager {
|
impl DeviceManager {
|
||||||
fn new(memory: GuestMemoryMmap, allocator: &mut SystemAllocator, vm_fd: &VmFd) -> Result<Self> {
|
fn new(
|
||||||
|
memory: GuestMemoryMmap,
|
||||||
|
allocator: &mut SystemAllocator,
|
||||||
|
vm_fd: &VmFd,
|
||||||
|
vm_cfg: &VmConfig,
|
||||||
|
) -> Result<Self> {
|
||||||
let io_bus = devices::Bus::new();
|
let io_bus = devices::Bus::new();
|
||||||
let mut mmio_bus = devices::Bus::new();
|
let mut mmio_bus = devices::Bus::new();
|
||||||
let serial_evt = EventFd::new(EFD_NONBLOCK).map_err(Error::EventFd)?;
|
let serial_evt = EventFd::new(EFD_NONBLOCK).map_err(Error::EventFd)?;
|
||||||
@ -277,7 +290,7 @@ impl DeviceManager {
|
|||||||
let raw_img: File = OpenOptions::new()
|
let raw_img: File = OpenOptions::new()
|
||||||
.read(true)
|
.read(true)
|
||||||
.write(true)
|
.write(true)
|
||||||
.open("/foo/bar/rootfs.img")
|
.open(&vm_cfg.disk_path)
|
||||||
.map_err(Error::Disk)?;
|
.map_err(Error::Disk)?;
|
||||||
|
|
||||||
let virtio_block_device =
|
let virtio_block_device =
|
||||||
@ -478,7 +491,7 @@ impl<'a> Vm<'a> {
|
|||||||
)
|
)
|
||||||
.ok_or(Error::CreateSystemAllocator)?;
|
.ok_or(Error::CreateSystemAllocator)?;
|
||||||
|
|
||||||
let device_manager = DeviceManager::new(guest_memory.clone(), &mut allocator, &fd)
|
let device_manager = DeviceManager::new(guest_memory.clone(), &mut allocator, &fd, &config)
|
||||||
.map_err(|_| Error::DeviceManager)?;
|
.map_err(|_| Error::DeviceManager)?;
|
||||||
fd.register_irqfd(device_manager.serial_evt.as_raw_fd(), 4)
|
fd.register_irqfd(device_manager.serial_evt.as_raw_fd(), 4)
|
||||||
.map_err(Error::Irq)?;
|
.map_err(Error::Irq)?;
|
||||||
|
Loading…
Reference in New Issue
Block a user