summary refs log tree commit diff
path: root/src/crosvm.rs
diff options
context:
space:
mode:
authorZach Reizner <zachr@google.com>2019-07-31 17:07:27 -0700
committerCommit Bot <commit-bot@chromium.org>2019-08-06 19:23:06 +0000
commit267f2c80d1144e2eb7da1aca51c9c75eac186c77 (patch)
tree25e6584d8fe36a7e928892e1dc5f9796916a0714 /src/crosvm.rs
parent1e26230f3a490a7955d2ea9fe7855af5498ced70 (diff)
downloadcrosvm-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/crosvm.rs')
-rw-r--r--src/crosvm.rs156
1 files changed, 156 insertions, 0 deletions
diff --git a/src/crosvm.rs b/src/crosvm.rs
new file mode 100644
index 0000000..4e820c9
--- /dev/null
+++ b/src/crosvm.rs
@@ -0,0 +1,156 @@
+// Copyright 2019 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+//! The root level module that includes the config and aggregate of the submodules for running said
+//! configs.
+
+pub mod argument;
+pub mod linux;
+#[cfg(feature = "plugin")]
+pub mod plugin;
+
+use std::collections::BTreeMap;
+use std::net;
+use std::os::unix::io::RawFd;
+use std::path::PathBuf;
+
+use devices::SerialParameters;
+
+static SECCOMP_POLICY_DIR: &'static str = "/usr/share/policy/crosvm";
+
+/// Indicates the location and kind of executable kernel for a VM.
+#[derive(Debug)]
+pub enum Executable {
+    /// An executable intended to be run as a BIOS directly.
+    Bios(PathBuf),
+    /// A elf linux kernel, loaded and executed by crosvm.
+    Kernel(PathBuf),
+    /// Path to a plugin executable that is forked by crosvm.
+    Plugin(PathBuf),
+}
+
+pub struct DiskOption {
+    pub path: PathBuf,
+    pub read_only: bool,
+}
+
+/// A bind mount for directories in the plugin process.
+pub struct BindMount {
+    pub src: PathBuf,
+    pub dst: PathBuf,
+    pub writable: bool,
+}
+
+/// A mapping of linux group IDs for the plugin process.
+pub struct GidMap {
+    pub inner: libc::gid_t,
+    pub outer: libc::gid_t,
+    pub count: u32,
+}
+
+const DEFAULT_TOUCH_DEVICE_WIDTH: u32 = 800;
+const DEFAULT_TOUCH_DEVICE_HEIGHT: u32 = 1280;
+
+pub struct TouchDeviceOption {
+    pub path: PathBuf,
+    pub width: u32,
+    pub height: u32,
+}
+
+impl TouchDeviceOption {
+    pub fn new(path: PathBuf) -> TouchDeviceOption {
+        TouchDeviceOption {
+            path,
+            width: DEFAULT_TOUCH_DEVICE_WIDTH,
+            height: DEFAULT_TOUCH_DEVICE_HEIGHT,
+        }
+    }
+}
+
+/// Aggregate of all configurable options for a running VM.
+pub struct Config {
+    pub vcpu_count: Option<u32>,
+    pub vcpu_affinity: Vec<usize>,
+    pub memory: Option<usize>,
+    pub executable_path: Option<Executable>,
+    pub android_fstab: Option<PathBuf>,
+    pub initrd_path: Option<PathBuf>,
+    pub params: Vec<String>,
+    pub socket_path: Option<PathBuf>,
+    pub plugin_root: Option<PathBuf>,
+    pub plugin_mounts: Vec<BindMount>,
+    pub plugin_gid_maps: Vec<GidMap>,
+    pub disks: Vec<DiskOption>,
+    pub pmem_devices: Vec<DiskOption>,
+    pub host_ip: Option<net::Ipv4Addr>,
+    pub netmask: Option<net::Ipv4Addr>,
+    pub mac_address: Option<net_util::MacAddress>,
+    pub vhost_net: bool,
+    pub tap_fd: Vec<RawFd>,
+    pub cid: Option<u64>,
+    pub wayland_socket_path: Option<PathBuf>,
+    pub wayland_dmabuf: bool,
+    pub shared_dirs: Vec<(PathBuf, String)>,
+    pub sandbox: bool,
+    pub seccomp_policy_dir: PathBuf,
+    pub seccomp_log_failures: bool,
+    pub gpu: bool,
+    pub software_tpm: bool,
+    pub cras_audio: bool,
+    pub cras_capture: bool,
+    pub null_audio: bool,
+    pub serial_parameters: BTreeMap<u8, SerialParameters>,
+    pub syslog_tag: Option<String>,
+    pub virtio_single_touch: Option<TouchDeviceOption>,
+    pub virtio_trackpad: Option<TouchDeviceOption>,
+    pub virtio_mouse: Option<PathBuf>,
+    pub virtio_keyboard: Option<PathBuf>,
+    pub virtio_input_evdevs: Vec<PathBuf>,
+    pub 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,
+        }
+    }
+}