Skip to content

Commit

Permalink
lumper: Add serial console file option
Browse files Browse the repository at this point in the history
  * New cli option --console STRING
  * Lumper guest output now goes to a file if console file specified

Signed-off-by: AlexandreBrg <[email protected]>
  • Loading branch information
alexandrebrg authored and Samuel Ortiz committed Jan 28, 2022
1 parent 8e7e869 commit 47e294f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
7 changes: 6 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ struct VMMOpts {
/// A level of verbosity, and can be used multiple times
#[clap(short, long, parse(from_occurrences))]
verbose: i32,

/// Stdout console file path
#[clap(long)]
console: Option<String>
}

#[derive(Debug)]
Expand All @@ -42,7 +46,8 @@ fn main() -> Result<(), Error> {
// * Number of virtual CPUs
// * Memory size (in MB)
// * Path to a Linux kernel
vmm.configure(opts.cpus, opts.memory, &opts.kernel)
// * Optional path to console file
vmm.configure(opts.cpus, opts.memory, &opts.kernel, opts.console)
.map_err(Error::VmmConfigure)?;

// Run the VMM
Expand Down
8 changes: 4 additions & 4 deletions src/vmm/src/devices/serial.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0

use std::io::{stdout, Error, Result, Stdout};
use std::io::{Error, Result, Write};
use std::ops::Deref;

use vm_superio::serial::NoEvents;
Expand Down Expand Up @@ -41,16 +41,16 @@ pub(crate) struct LumperSerial {
eventfd: EventFdTrigger,

// serial is the actual serial device.
pub serial: Serial<EventFdTrigger, NoEvents, Stdout>,
pub serial: Serial<EventFdTrigger, NoEvents, Box<dyn Write + Send>>,
}

impl LumperSerial {
pub fn new() -> Result<Self> {
pub fn new(output: Box<dyn Write + Send>) -> Result<Self> {
let eventfd = EventFdTrigger::new(libc::EFD_NONBLOCK).unwrap();

Ok(LumperSerial {
eventfd: eventfd.try_clone()?,
serial: Serial::new(eventfd.try_clone()?, stdout()),
serial: Serial::new(eventfd.try_clone()?, Box::new(output)),
})
}

Expand Down
26 changes: 23 additions & 3 deletions src/vmm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,24 @@ extern crate linux_loader;
extern crate vm_memory;
extern crate vm_superio;

use std::io::stdout;
use std::os::unix::io::AsRawFd;
use std::os::unix::prelude::RawFd;
use std::sync::{Arc, Mutex};
use std::thread;
use std::{io, path::PathBuf};
use std::fs::File;

use kvm_bindings::{kvm_userspace_memory_region, KVM_MAX_CPUID_ENTRIES};
use kvm_ioctls::{Kvm, VmFd};
use linux_loader::loader::{self, KernelLoaderResult};
use vm_memory::{Address, GuestAddress, GuestMemory, GuestMemoryMmap, GuestMemoryRegion};
use vmm_sys_util::terminal::Terminal;

mod cpu;
use cpu::{cpuid, mptable, Vcpu};
mod devices;
use devices::serial::LumperSerial;

mod epoll_context;
use epoll_context::{EpollContext, EPOLL_EVENTS_LEN};
mod kernel;
Expand Down Expand Up @@ -62,6 +64,8 @@ pub enum Error {
StdinWrite(vm_superio::serial::Error<io::Error>),
/// Terminal configuration error
TerminalConfigure(kvm_ioctls::Error),
/// Console configuration error
ConsoleError(io::Error),
}

/// Dedicated [`Result`](https://doc.rust-lang.org/std/result/) type.
Expand Down Expand Up @@ -96,7 +100,7 @@ impl VMM {
guest_memory: GuestMemoryMmap::default(),
vcpus: vec![],
serial: Arc::new(Mutex::new(
LumperSerial::new().map_err(Error::SerialCreation)?,
LumperSerial::new(Box::new(stdout())).map_err(Error::SerialCreation)?,
)),
epoll,
};
Expand Down Expand Up @@ -160,6 +164,21 @@ impl VMM {
Ok(())
}

pub fn configure_console(
&mut self,
console_path: Option<String>
) -> Result<()> {
if let Some(console_path) = console_path {
// We create the file if it does not exist, else we open
let file = File::create(&console_path).map_err(Error::ConsoleError)?;

let mut serial = self.serial.lock().unwrap();
*serial = LumperSerial::new(Box::new(file)).map_err(Error::SerialCreation)?;
}

Ok(())
}

pub fn configure_vcpus(
&mut self,
num_vcpus: u8,
Expand Down Expand Up @@ -247,7 +266,8 @@ impl VMM {
}
}

pub fn configure(&mut self, num_vcpus: u8, mem_size_mb: u32, kernel_path: &str) -> Result<()> {
pub fn configure(&mut self, num_vcpus: u8, mem_size_mb: u32, kernel_path: &str, console: Option<String>) -> Result<()> {
self.configure_console(console)?;
self.configure_memory(mem_size_mb)?;
let kernel_load = kernel::kernel_setup(&self.guest_memory, PathBuf::from(kernel_path))?;
self.configure_io()?;
Expand Down

0 comments on commit 47e294f

Please sign in to comment.