diff options
author | Zach Reizner <zachr@google.com> | 2019-07-31 17:07:27 -0700 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2019-08-06 19:23:06 +0000 |
commit | 267f2c80d1144e2eb7da1aca51c9c75eac186c77 (patch) | |
tree | 25e6584d8fe36a7e928892e1dc5f9796916a0714 /src/main.rs | |
parent | 1e26230f3a490a7955d2ea9fe7855af5498ced70 (diff) | |
download | crosvm-267f2c80d1144e2eb7da1aca51c9c75eac186c77.tar crosvm-267f2c80d1144e2eb7da1aca51c9c75eac186c77.tar.gz crosvm-267f2c80d1144e2eb7da1aca51c9c75eac186c77.tar.bz2 crosvm-267f2c80d1144e2eb7da1aca51c9c75eac186c77.tar.lz crosvm-267f2c80d1144e2eb7da1aca51c9c75eac186c77.tar.xz crosvm-267f2c80d1144e2eb7da1aca51c9c75eac186c77.tar.zst crosvm-267f2c80d1144e2eb7da1aca51c9c75eac186c77.zip |
split crosvm into a library and a main "crosvm" binary
This change has 3 parts: - Modify the Cargo.toml to point at the bin and the lib source. - Move modules and Config struct into the lib source - Fix the argument/plugins module's doc comments which had never been tested. The motivation for this change is to make testing crosvm's major functionality (booting guest kernels, emulating hardware, etc) easier to do from a cargo test. Being able to launce a crosvm config via the API instead of the binary's command line will be possible with this change. A side benefit is that this also enables doc tests in the lib side of crosvm. The doc tests in binaries are not run due to a limitation in how they get tested by cargo. TEST=cargo test ./build_test kokoro/kokoro_simulator.sh emerge crosvm BUG=None Change-Id: I9d4b3a24231b895e8dfaf9e7b0f2b33350772041 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1730333 Reviewed-by: Zach Reizner <zachr@chromium.org> Reviewed-by: Daniel Verkamp <dverkamp@chromium.org> Tested-by: Zach Reizner <zachr@chromium.org> Tested-by: kokoro <noreply+kokoro@google.com> Commit-Queue: Zach Reizner <zachr@chromium.org>
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 165 |
1 files changed, 15 insertions, 150 deletions
diff --git a/src/main.rs b/src/main.rs index 78dfbf2..1d18c3b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,16 +4,10 @@ //! Runs a virtual machine under KVM -pub mod argument; -pub mod linux; pub mod panic_hook; -#[cfg(feature = "plugin")] -pub mod plugin; -use std::collections::BTreeMap; use std::fmt; use std::fs::{File, OpenOptions}; -use std::net; use std::num::ParseIntError; use std::os::unix::io::{FromRawFd, RawFd}; use std::path::{Path, PathBuf}; @@ -21,6 +15,10 @@ use std::string::String; use std::thread::sleep; use std::time::Duration; +use crosvm::{ + argument::{self, print_help, set_arguments, Argument}, + linux, BindMount, Config, DiskOption, Executable, GidMap, TouchDeviceOption, +}; use devices::{SerialParameters, SerialType}; use msg_socket::{MsgReceiver, MsgSender, MsgSocket}; use qcow::QcowFile; @@ -33,55 +31,6 @@ use vm_control::{ VmControlRequestSocket, VmRequest, VmResponse, USB_CONTROL_MAX_PORTS, }; -use crate::argument::{print_help, set_arguments, Argument}; - -static SECCOMP_POLICY_DIR: &'static str = "/usr/share/policy/crosvm"; - -struct DiskOption { - path: PathBuf, - read_only: bool, -} - -#[allow(dead_code)] -struct BindMount { - src: PathBuf, - dst: PathBuf, - writable: bool, -} - -#[allow(dead_code)] -struct GidMap { - inner: libc::gid_t, - outer: libc::gid_t, - count: u32, -} - -const DEFAULT_TOUCH_DEVICE_WIDTH: u32 = 800; -const DEFAULT_TOUCH_DEVICE_HEIGHT: u32 = 1280; - -struct TouchDeviceOption { - path: PathBuf, - width: u32, - height: u32, -} - -impl TouchDeviceOption { - fn new(path: PathBuf) -> TouchDeviceOption { - TouchDeviceOption { - path, - width: DEFAULT_TOUCH_DEVICE_WIDTH, - height: DEFAULT_TOUCH_DEVICE_HEIGHT, - } - } -} - -#[derive(Debug)] -pub enum Executable { - Bios(PathBuf), - Kernel(PathBuf), - Plugin(PathBuf), -} - fn executable_is_plugin(executable: &Option<Executable>) -> bool { match executable { Some(Executable::Plugin(_)) => true, @@ -89,92 +38,6 @@ fn executable_is_plugin(executable: &Option<Executable>) -> bool { } } -pub struct Config { - vcpu_count: Option<u32>, - vcpu_affinity: Vec<usize>, - memory: Option<usize>, - executable_path: Option<Executable>, - android_fstab: Option<PathBuf>, - initrd_path: Option<PathBuf>, - params: Vec<String>, - socket_path: Option<PathBuf>, - plugin_root: Option<PathBuf>, - plugin_mounts: Vec<BindMount>, - plugin_gid_maps: Vec<GidMap>, - disks: Vec<DiskOption>, - pmem_devices: Vec<DiskOption>, - host_ip: Option<net::Ipv4Addr>, - netmask: Option<net::Ipv4Addr>, - mac_address: Option<net_util::MacAddress>, - vhost_net: bool, - tap_fd: Vec<RawFd>, - cid: Option<u64>, - wayland_socket_path: Option<PathBuf>, - wayland_dmabuf: bool, - shared_dirs: Vec<(PathBuf, String)>, - sandbox: bool, - seccomp_policy_dir: PathBuf, - seccomp_log_failures: bool, - gpu: bool, - software_tpm: bool, - cras_audio: bool, - cras_capture: bool, - null_audio: bool, - serial_parameters: BTreeMap<u8, SerialParameters>, - syslog_tag: Option<String>, - virtio_single_touch: Option<TouchDeviceOption>, - virtio_trackpad: Option<TouchDeviceOption>, - virtio_mouse: Option<PathBuf>, - virtio_keyboard: Option<PathBuf>, - virtio_input_evdevs: Vec<PathBuf>, - split_irqchip: bool, -} - -impl Default for Config { - fn default() -> Config { - Config { - vcpu_count: None, - vcpu_affinity: Vec::new(), - memory: None, - executable_path: None, - android_fstab: None, - initrd_path: None, - params: Vec::new(), - socket_path: None, - plugin_root: None, - plugin_mounts: Vec::new(), - plugin_gid_maps: Vec::new(), - disks: Vec::new(), - pmem_devices: Vec::new(), - host_ip: None, - netmask: None, - mac_address: None, - vhost_net: false, - tap_fd: Vec::new(), - cid: None, - gpu: false, - software_tpm: false, - wayland_socket_path: None, - wayland_dmabuf: false, - shared_dirs: Vec::new(), - sandbox: !cfg!(feature = "default-no-sandbox"), - seccomp_policy_dir: PathBuf::from(SECCOMP_POLICY_DIR), - seccomp_log_failures: false, - cras_audio: false, - cras_capture: false, - null_audio: false, - serial_parameters: BTreeMap::new(), - syslog_tag: None, - virtio_single_touch: None, - virtio_trackpad: None, - virtio_mouse: None, - virtio_keyboard: None, - virtio_input_evdevs: Vec::new(), - split_irqchip: false, - } - } -} - // Wait for all children to exit. Return true if they have all exited, false // otherwise. fn wait_all_children() -> bool { @@ -939,16 +802,18 @@ fn run_vm(args: std::env::Args) -> std::result::Result<(), ()> { match match_res { #[cfg(feature = "plugin")] - Ok(()) if executable_is_plugin(&cfg.executable_path) => match plugin::run_config(cfg) { - Ok(_) => { - info!("crosvm and plugin have exited normally"); - Ok(()) - } - Err(e) => { - error!("{}", e); - Err(()) + Ok(()) if executable_is_plugin(&cfg.executable_path) => { + match crosvm::plugin::run_config(cfg) { + Ok(_) => { + info!("crosvm and plugin have exited normally"); + Ok(()) + } + Err(e) => { + error!("{}", e); + Err(()) + } } - }, + } Ok(()) => match linux::run_config(cfg) { Ok(_) => { info!("crosvm has exited normally"); |