diff options
author | Zach Reizner <zachr@google.com> | 2018-10-03 10:22:32 -0700 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2018-10-09 21:14:05 -0700 |
commit | 55a9e504beef368bd97e51ffd5a7fa6c034eb8ad (patch) | |
tree | 894d8685e2fdfa105ea35d1cb6cfceee06502c7a /src/main.rs | |
parent | 046df60760f3b0691f23c27a7f24a96c9afe8c05 (diff) | |
download | crosvm-55a9e504beef368bd97e51ffd5a7fa6c034eb8ad.tar crosvm-55a9e504beef368bd97e51ffd5a7fa6c034eb8ad.tar.gz crosvm-55a9e504beef368bd97e51ffd5a7fa6c034eb8ad.tar.bz2 crosvm-55a9e504beef368bd97e51ffd5a7fa6c034eb8ad.tar.lz crosvm-55a9e504beef368bd97e51ffd5a7fa6c034eb8ad.tar.xz crosvm-55a9e504beef368bd97e51ffd5a7fa6c034eb8ad.tar.zst crosvm-55a9e504beef368bd97e51ffd5a7fa6c034eb8ad.zip |
cargo fmt all source code
Now that cargo fmt has landed, run it over everything at once to bring rust source to the standard formatting. TEST=cargo test BUG=None Change-Id: Ic95a48725e5a40dcbd33ba6d5aef2bd01e91865b Reviewed-on: https://chromium-review.googlesource.com/1259287 Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com> Tested-by: Zach Reizner <zachr@chromium.org> Reviewed-by: Zach Reizner <zachr@chromium.org>
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 423 |
1 files changed, 231 insertions, 192 deletions
diff --git a/src/main.rs b/src/main.rs index 8e065ed..28bc09e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,34 +4,34 @@ //! Runs a virtual machine under KVM -extern crate arch; #[cfg(any(target_arch = "arm", target_arch = "aarch64"))] extern crate aarch64; +extern crate arch; +extern crate byteorder; extern crate devices; -extern crate libc; extern crate io_jail; +extern crate kernel_cmdline; +extern crate kernel_loader; extern crate kvm; extern crate kvm_sys; -#[cfg(any(target_arch = "x86", target_arch = "x86_64"))] -extern crate x86_64; -extern crate kernel_loader; -extern crate kernel_cmdline; -extern crate byteorder; +extern crate libc; extern crate net_util; extern crate qcow; +#[cfg(any(target_arch = "x86", target_arch = "x86_64"))] +extern crate x86_64; #[macro_use] extern crate sys_util; -extern crate resources; -extern crate vhost; -extern crate vm_control; extern crate data_model; +#[cfg(feature = "wl-dmabuf")] +extern crate gpu_buffer; #[cfg(feature = "plugin")] extern crate plugin_proto; #[cfg(feature = "plugin")] extern crate protobuf; -#[cfg(feature = "wl-dmabuf")] -extern crate gpu_buffer; extern crate rand; +extern crate resources; +extern crate vhost; +extern crate vm_control; pub mod argument; pub mod linux; @@ -47,10 +47,10 @@ use std::string::String; use std::thread::sleep; use std::time::Duration; -use sys_util::{getpid, kill_process_group, reap_child, syslog}; use qcow::QcowFile; +use sys_util::{getpid, kill_process_group, reap_child, syslog}; -use argument::{Argument, set_arguments, print_help}; +use argument::{print_help, set_arguments, Argument}; use vm_control::VmRequest; static SECCOMP_POLICY_DIR: &'static str = "/usr/share/policy/crosvm"; @@ -140,7 +140,7 @@ fn wait_all_children() -> bool { return false; } // We reaped one child, so continue reaping. - _ => {}, + _ => {} } } // There's no timeout option for waitpid which reap_child calls internally, so our only @@ -156,18 +156,20 @@ fn set_argument(cfg: &mut Config, name: &str, value: Option<&str>) -> argument:: match name { "" => { if cfg.plugin.is_some() { - return Err(argument::Error::TooManyArguments("`plugin` can not be used with kernel" - .to_owned())); + return Err(argument::Error::TooManyArguments( + "`plugin` can not be used with kernel".to_owned(), + )); } else if !cfg.kernel_path.as_os_str().is_empty() { - return Err(argument::Error::TooManyArguments("expected exactly one kernel path" - .to_owned())); + return Err(argument::Error::TooManyArguments( + "expected exactly one kernel path".to_owned(), + )); } else { let kernel_path = PathBuf::from(value.unwrap()); if !kernel_path.exists() { return Err(argument::Error::InvalidValue { - value: value.unwrap().to_owned(), - expected: "this kernel path does not exist", - }); + value: value.unwrap().to_owned(), + expected: "this kernel path does not exist", + }); } cfg.kernel_path = kernel_path; } @@ -177,127 +179,140 @@ fn set_argument(cfg: &mut Config, name: &str, value: Option<&str>) -> argument:: } "cpus" => { if cfg.vcpu_count.is_some() { - return Err(argument::Error::TooManyArguments("`cpus` already given".to_owned())); + return Err(argument::Error::TooManyArguments( + "`cpus` already given".to_owned(), + )); } cfg.vcpu_count = - Some(value - .unwrap() - .parse() - .map_err(|_| { - argument::Error::InvalidValue { - value: value.unwrap().to_owned(), - expected: "this value for `cpus` needs to be integer", - } - })?) + Some( + value + .unwrap() + .parse() + .map_err(|_| argument::Error::InvalidValue { + value: value.unwrap().to_owned(), + expected: "this value for `cpus` needs to be integer", + })?, + ) } "mem" => { if cfg.memory.is_some() { - return Err(argument::Error::TooManyArguments("`mem` already given".to_owned())); + return Err(argument::Error::TooManyArguments( + "`mem` already given".to_owned(), + )); } cfg.memory = - Some(value - .unwrap() - .parse() - .map_err(|_| { - argument::Error::InvalidValue { - value: value.unwrap().to_owned(), - expected: "this value for `mem` needs to be integer", - } - })?) + Some( + value + .unwrap() + .parse() + .map_err(|_| argument::Error::InvalidValue { + value: value.unwrap().to_owned(), + expected: "this value for `mem` needs to be integer", + })?, + ) } "root" | "disk" | "rwdisk" | "qcow" | "rwqcow" => { let disk_path = PathBuf::from(value.unwrap()); if !disk_path.exists() { return Err(argument::Error::InvalidValue { - value: value.unwrap().to_owned(), - expected: "this disk path does not exist", - }); + value: value.unwrap().to_owned(), + expected: "this disk path does not exist", + }); } if name == "root" { if cfg.virtio_dev_info.disks.len() >= 26 { - return Err(argument::Error::TooManyArguments("ran out of letters for to assign to root disk".to_owned())); + return Err(argument::Error::TooManyArguments( + "ran out of letters for to assign to root disk".to_owned(), + )); } - cfg.params - .push(format!("root=/dev/vd{} ro", - char::from('a' as u8 + cfg.virtio_dev_info.disks.len() as u8))); + cfg.params.push(format!( + "root=/dev/vd{} ro", + char::from('a' as u8 + cfg.virtio_dev_info.disks.len() as u8) + )); } - cfg.virtio_dev_info.disks - .push(DiskOption { - path: disk_path, - read_only: !name.starts_with("rw"), - disk_type: if name.ends_with("qcow") { - DiskType::Qcow - } else { - DiskType::FlatFile - }, - }); + cfg.virtio_dev_info.disks.push(DiskOption { + path: disk_path, + read_only: !name.starts_with("rw"), + disk_type: if name.ends_with("qcow") { + DiskType::Qcow + } else { + DiskType::FlatFile + }, + }); } "host_ip" => { if cfg.virtio_dev_info.host_ip.is_some() { - return Err(argument::Error::TooManyArguments("`host_ip` already given".to_owned())); + return Err(argument::Error::TooManyArguments( + "`host_ip` already given".to_owned(), + )); } cfg.virtio_dev_info.host_ip = - Some(value - .unwrap() - .parse() - .map_err(|_| { - argument::Error::InvalidValue { - value: value.unwrap().to_owned(), - expected: "`host_ip` needs to be in the form \"x.x.x.x\"", - } - })?) + Some( + value + .unwrap() + .parse() + .map_err(|_| argument::Error::InvalidValue { + value: value.unwrap().to_owned(), + expected: "`host_ip` needs to be in the form \"x.x.x.x\"", + })?, + ) } "netmask" => { if cfg.virtio_dev_info.netmask.is_some() { - return Err(argument::Error::TooManyArguments("`netmask` already given".to_owned())); + return Err(argument::Error::TooManyArguments( + "`netmask` already given".to_owned(), + )); } cfg.virtio_dev_info.netmask = - Some(value - .unwrap() - .parse() - .map_err(|_| { - argument::Error::InvalidValue { - value: value.unwrap().to_owned(), - expected: "`netmask` needs to be in the form \"x.x.x.x\"", - } - })?) + Some( + value + .unwrap() + .parse() + .map_err(|_| argument::Error::InvalidValue { + value: value.unwrap().to_owned(), + expected: "`netmask` needs to be in the form \"x.x.x.x\"", + })?, + ) } "mac" => { if cfg.virtio_dev_info.mac_address.is_some() { - return Err(argument::Error::TooManyArguments("`mac` already given".to_owned())); + return Err(argument::Error::TooManyArguments( + "`mac` already given".to_owned(), + )); } cfg.virtio_dev_info.mac_address = - Some(value - .unwrap() - .parse() - .map_err(|_| { - argument::Error::InvalidValue { - value: value.unwrap().to_owned(), - expected: "`mac` needs to be in the form \"XX:XX:XX:XX:XX:XX\"", - } - })?) + Some( + value + .unwrap() + .parse() + .map_err(|_| argument::Error::InvalidValue { + value: value.unwrap().to_owned(), + expected: "`mac` needs to be in the form \"XX:XX:XX:XX:XX:XX\"", + })?, + ) } "wayland-sock" => { if cfg.virtio_dev_info.wayland_socket_path.is_some() { - return Err(argument::Error::TooManyArguments("`wayland-sock` already given" - .to_owned())); + return Err(argument::Error::TooManyArguments( + "`wayland-sock` already given".to_owned(), + )); } let wayland_socket_path = PathBuf::from(value.unwrap()); if !wayland_socket_path.exists() { return Err(argument::Error::InvalidValue { - value: value.unwrap().to_string(), - expected: "Wayland socket does not exist", - }); + value: value.unwrap().to_string(), + expected: "Wayland socket does not exist", + }); } cfg.virtio_dev_info.wayland_socket_path = Some(wayland_socket_path); } #[cfg(feature = "wl-dmabuf")] - "wayland-dmabuf" => { - cfg.virtio_dev_info.wayland_dmabuf = true - } + "wayland-dmabuf" => cfg.virtio_dev_info.wayland_dmabuf = true, "socket" => { if cfg.socket_path.is_some() { - return Err(argument::Error::TooManyArguments("`socket` already given".to_owned())); + return Err(argument::Error::TooManyArguments( + "`socket` already given".to_owned(), + )); } let mut socket_path = PathBuf::from(value.unwrap()); if socket_path.is_dir() { @@ -305,9 +320,9 @@ fn set_argument(cfg: &mut Config, name: &str, value: Option<&str>) -> argument:: } if socket_path.exists() { return Err(argument::Error::InvalidValue { - value: socket_path.to_string_lossy().into_owned(), - expected: "this socket path already exists", - }); + value: socket_path.to_string_lossy().into_owned(), + expected: "this socket path already exists", + }); } cfg.socket_path = Some(socket_path); } @@ -319,31 +334,40 @@ fn set_argument(cfg: &mut Config, name: &str, value: Option<&str>) -> argument:: } "cid" => { if cfg.virtio_dev_info.cid.is_some() { - return Err(argument::Error::TooManyArguments("`cid` alread given".to_owned())); + return Err(argument::Error::TooManyArguments( + "`cid` alread given".to_owned(), + )); } - cfg.virtio_dev_info.cid = Some(value.unwrap().parse().map_err(|_| { - argument::Error::InvalidValue { - value: value.unwrap().to_owned(), - expected: "this value for `cid` must be an unsigned integer", - } - })?); + cfg.virtio_dev_info.cid = + Some( + value + .unwrap() + .parse() + .map_err(|_| argument::Error::InvalidValue { + value: value.unwrap().to_owned(), + expected: "this value for `cid` must be an unsigned integer", + })?, + ); } "shared-dir" => { // Formatted as <src:tag>. let param = value.unwrap(); let mut components = param.splitn(2, ':'); - let src = PathBuf::from(components.next().ok_or_else(|| { - argument::Error::InvalidValue { - value: param.to_owned(), - expected: "missing source path for `shared-dir`", - } - })?); - let tag = components.next().ok_or_else(|| { - argument::Error::InvalidValue { + let src = + PathBuf::from( + components + .next() + .ok_or_else(|| argument::Error::InvalidValue { + value: param.to_owned(), + expected: "missing source path for `shared-dir`", + })?, + ); + let tag = components + .next() + .ok_or_else(|| argument::Error::InvalidValue { value: param.to_owned(), expected: "missing tag for `shared-dir`", - } - })?.to_owned(); + })?.to_owned(); if !src.is_dir() { return Err(argument::Error::InvalidValue { @@ -357,38 +381,46 @@ fn set_argument(cfg: &mut Config, name: &str, value: Option<&str>) -> argument:: "seccomp-policy-dir" => { // `value` is Some because we are in this match so it's safe to unwrap. cfg.virtio_dev_info.seccomp_policy_dir = PathBuf::from(value.unwrap()); - }, + } "plugin" => { if !cfg.kernel_path.as_os_str().is_empty() { - return Err(argument::Error::TooManyArguments("`plugin` can not be used with kernel".to_owned())); + return Err(argument::Error::TooManyArguments( + "`plugin` can not be used with kernel".to_owned(), + )); } else if cfg.plugin.is_some() { - return Err(argument::Error::TooManyArguments("`plugin` already given".to_owned())); + return Err(argument::Error::TooManyArguments( + "`plugin` already given".to_owned(), + )); } let plugin = PathBuf::from(value.unwrap().to_owned()); if plugin.is_relative() { return Err(argument::Error::InvalidValue { - value: plugin.to_string_lossy().into_owned(), - expected: "the plugin path must be an absolute path", - }) + value: plugin.to_string_lossy().into_owned(), + expected: "the plugin path must be an absolute path", + }); } cfg.plugin = Some(plugin); - }, + } "plugin-root" => { cfg.plugin_root = Some(PathBuf::from(value.unwrap().to_owned())); - }, - "vhost-net" => { - cfg.virtio_dev_info.vhost_net = true - }, + } + "vhost-net" => cfg.virtio_dev_info.vhost_net = true, "tap-fd" => { if cfg.virtio_dev_info.tap_fd.is_some() { - return Err(argument::Error::TooManyArguments("`tap-fd` alread given".to_owned())); + return Err(argument::Error::TooManyArguments( + "`tap-fd` alread given".to_owned(), + )); } - cfg.virtio_dev_info.tap_fd = Some(value.unwrap().parse().map_err(|_| { - argument::Error::InvalidValue { - value: value.unwrap().to_owned(), - expected: "this value for `tap-fd` must be an unsigned integer", - } - })?); + cfg.virtio_dev_info.tap_fd = + Some( + value + .unwrap() + .parse() + .map_err(|_| argument::Error::InvalidValue { + value: value.unwrap().to_owned(), + expected: "this value for `tap-fd` must be an unsigned integer", + })?, + ); } "gpu" => { cfg.virtio_dev_info.gpu = true; @@ -399,7 +431,6 @@ fn set_argument(cfg: &mut Config, name: &str, value: Option<&str>) -> argument:: Ok(()) } - fn run_vm(args: std::env::Args) -> std::result::Result<(), ()> { let arguments = &[Argument::positional("KERNEL", "bzImage of kernel to run"), @@ -453,37 +484,54 @@ fn run_vm(args: std::env::Args) -> std::result::Result<(), ()> { Argument::short_flag('h', "help", "Print help message.")]; let mut cfg = Config::default(); - let match_res = set_arguments(args, &arguments[..], |name, value| set_argument(&mut cfg, name, value)).and_then(|_| { + let match_res = set_arguments(args, &arguments[..], |name, value| { + set_argument(&mut cfg, name, value) + }).and_then(|_| { if cfg.kernel_path.as_os_str().is_empty() && cfg.plugin.is_none() { return Err(argument::Error::ExpectedArgument("`KERNEL`".to_owned())); } - if cfg.virtio_dev_info.host_ip.is_some() || cfg.virtio_dev_info.netmask.is_some() - || cfg.virtio_dev_info.mac_address.is_some() { + if cfg.virtio_dev_info.host_ip.is_some() + || cfg.virtio_dev_info.netmask.is_some() + || cfg.virtio_dev_info.mac_address.is_some() + { if cfg.virtio_dev_info.host_ip.is_none() { - return Err(argument::Error::ExpectedArgument("`host_ip` missing from network config".to_owned())); + return Err(argument::Error::ExpectedArgument( + "`host_ip` missing from network config".to_owned(), + )); } if cfg.virtio_dev_info.netmask.is_none() { - return Err(argument::Error::ExpectedArgument("`netmask` missing from network config".to_owned())); + return Err(argument::Error::ExpectedArgument( + "`netmask` missing from network config".to_owned(), + )); } if cfg.virtio_dev_info.mac_address.is_none() { - return Err(argument::Error::ExpectedArgument("`mac` missing from network config".to_owned())); + return Err(argument::Error::ExpectedArgument( + "`mac` missing from network config".to_owned(), + )); } } if cfg.plugin_root.is_some() && cfg.plugin.is_none() { - return Err(argument::Error::ExpectedArgument("`plugin-root` requires `plugin`".to_owned())); - } - if cfg.virtio_dev_info.tap_fd.is_some() && (cfg.virtio_dev_info.host_ip.is_some() || - cfg.virtio_dev_info.netmask.is_some() || - cfg.virtio_dev_info.mac_address.is_some()) { + return Err(argument::Error::ExpectedArgument( + "`plugin-root` requires `plugin`".to_owned(), + )); + } + if cfg.virtio_dev_info.tap_fd.is_some() + && (cfg.virtio_dev_info.host_ip.is_some() + || cfg.virtio_dev_info.netmask.is_some() + || cfg.virtio_dev_info.mac_address.is_some()) + { return Err(argument::Error::TooManyArguments( - "`tap_fd` and any of `host_ip`, `netmask`, or `mac` are mutually exclusive".to_owned())); + "`tap_fd` and any of `host_ip`, `netmask`, or `mac` are mutually exclusive" + .to_owned(), + )); } Ok(()) }); match match_res { #[cfg(feature = "plugin")] - Ok(()) if cfg.plugin.is_some() => { + Ok(()) if cfg.plugin.is_some() => + { match plugin::run_config(cfg) { Ok(_) => { info!("crosvm and plugin have exited normally"); @@ -495,18 +543,16 @@ fn run_vm(args: std::env::Args) -> std::result::Result<(), ()> { } } } - Ok(()) => { - match linux::run_config(cfg) { - Ok(_) => { - info!("crosvm has exited normally"); - Ok(()) - } - Err(e) => { - error!("{}", e); - Err(()) - } + Ok(()) => match linux::run_config(cfg) { + Ok(_) => { + info!("crosvm has exited normally"); + Ok(()) } - } + Err(e) => { + error!("{}", e); + Err(()) + } + }, Err(argument::Error::PrintHelp) => { print_help("crosvm run", "KERNEL", &arguments[..]); Ok(()) @@ -527,14 +573,15 @@ fn stop_vms(args: std::env::Args) -> std::result::Result<(), ()> { let mut return_result = Ok(()); for socket_path in args { match UnixDatagram::unbound().and_then(|s| { - s.connect(&socket_path)?; - Ok(s) - }) { + s.connect(&socket_path)?; + Ok(s) + }) { Ok(s) => { if let Err(e) = VmRequest::Exit.send(&s) { - error!("failed to send stop request to socket at '{}': {:?}", - socket_path, - e); + error!( + "failed to send stop request to socket at '{}': {:?}", + socket_path, e + ); } } Err(e) => { @@ -557,20 +604,21 @@ fn balloon_vms(mut args: std::env::Args) -> std::result::Result<(), ()> { Err(_) => { error!("Failed to parse number of pages"); return Err(()); - }, + } }; let mut return_result = Ok(()); for socket_path in args { match UnixDatagram::unbound().and_then(|s| { - s.connect(&socket_path)?; - Ok(s) - }) { + s.connect(&socket_path)?; + Ok(s) + }) { Ok(s) => { if let Err(e) = VmRequest::BalloonAdjust(num_pages).send(&s) { - error!("failed to send balloon request to socket at '{}': {:?}", - socket_path, - e); + error!( + "failed to send balloon request to socket at '{}': {:?}", + socket_path, e + ); } } Err(e) => { @@ -594,7 +642,7 @@ fn create_qcow2(mut args: std::env::Args) -> std::result::Result<(), ()> { Err(_) => { error!("Failed to parse size of the disk."); return Err(()); - }, + } }; let file = OpenOptions::new() @@ -607,11 +655,10 @@ fn create_qcow2(mut args: std::env::Args) -> std::result::Result<(), ()> { () })?; - QcowFile::new(file, size) - .map_err(|e| { - error!("Failed to create qcow file at '{}': {:?}", file_path, e); - () - })?; + QcowFile::new(file, size).map_err(|e| { + error!("Failed to create qcow file at '{}': {:?}", file_path, e); + () + })?; Ok(()) } @@ -642,18 +689,10 @@ fn crosvm_main() -> std::result::Result<(), ()> { print_usage(); Ok(()) } - Some("stop") => { - stop_vms(args) - } - Some("run") => { - run_vm(args) - } - Some("balloon") => { - balloon_vms(args) - } - Some("create_qcow2") => { - create_qcow2(args) - } + Some("stop") => stop_vms(args), + Some("run") => run_vm(args), + Some("balloon") => balloon_vms(args), + Some("create_qcow2") => create_qcow2(args), Some(c) => { println!("invalid subcommand: {:?}", c); print_usage(); |