Zach Reizner | 639d967 | 2017-05-01 17:57:18 -0700 | [diff] [blame] | 1 | // Copyright 2017 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | //! Runs a virtual machine under KVM |
| 6 | |
Sonny Rao | 2ffa0cb | 2018-02-26 17:27:40 -0800 | [diff] [blame] | 7 | #[cfg(any(target_arch = "arm", target_arch = "aarch64"))] |
| 8 | extern crate aarch64; |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 9 | extern crate arch; |
Dylan Reid | 3082e8e | 2019-01-07 10:33:48 -0800 | [diff] [blame] | 10 | extern crate audio_streams; |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 11 | extern crate byteorder; |
Dylan Reid | d169a8d | 2017-10-06 15:26:46 -0700 | [diff] [blame] | 12 | extern crate devices; |
Dylan Reid | 61edbbf | 2017-05-12 16:15:53 -0700 | [diff] [blame] | 13 | extern crate io_jail; |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 14 | extern crate kernel_cmdline; |
| 15 | extern crate kernel_loader; |
Zach Reizner | 639d967 | 2017-05-01 17:57:18 -0700 | [diff] [blame] | 16 | extern crate kvm; |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 17 | extern crate kvm_sys; |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 18 | extern crate libc; |
paulhsia | f052cfe | 2019-01-22 15:22:25 +0800 | [diff] [blame] | 19 | extern crate libcras; |
Jason D. Clinton | 865323d | 2017-09-27 22:04:03 -0600 | [diff] [blame] | 20 | extern crate net_util; |
Dylan Reid | 88624f8 | 2018-01-11 09:20:16 -0800 | [diff] [blame] | 21 | extern crate qcow; |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 22 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 23 | extern crate x86_64; |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 24 | #[macro_use] |
| 25 | extern crate sys_util; |
Zach Reizner | 29ad3c7 | 2017-08-04 15:12:58 -0700 | [diff] [blame] | 26 | extern crate data_model; |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 27 | #[cfg(feature = "wl-dmabuf")] |
| 28 | extern crate gpu_buffer; |
Jingkui Wang | e13b180 | 2018-10-03 13:04:47 -0700 | [diff] [blame] | 29 | extern crate msg_socket; |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 30 | #[cfg(feature = "plugin")] |
| 31 | extern crate plugin_proto; |
| 32 | #[cfg(feature = "plugin")] |
| 33 | extern crate protobuf; |
Daniel Prilik | 2200604 | 2019-01-14 14:19:04 -0800 | [diff] [blame] | 34 | extern crate rand_ish; |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 35 | extern crate resources; |
David Tolnay | 1d4d44a | 2018-12-03 23:37:46 -0800 | [diff] [blame] | 36 | extern crate sync; |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 37 | extern crate vhost; |
| 38 | extern crate vm_control; |
Zach Reizner | 639d967 | 2017-05-01 17:57:18 -0700 | [diff] [blame] | 39 | |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 40 | pub mod argument; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 41 | pub mod linux; |
Zach Reizner | b3fa5c9 | 2019-01-28 14:05:23 -0800 | [diff] [blame] | 42 | pub mod panic_hook; |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 43 | #[cfg(feature = "plugin")] |
| 44 | pub mod plugin; |
Zach Reizner | 29ad3c7 | 2017-08-04 15:12:58 -0700 | [diff] [blame] | 45 | |
Dylan Reid | 2dcb632 | 2018-07-13 10:42:48 -0700 | [diff] [blame] | 46 | use std::fs::OpenOptions; |
Stephen Barber | 2cfc205 | 2017-06-21 15:16:11 -0700 | [diff] [blame] | 47 | use std::net; |
Chirantan Ekbote | 5f78721 | 2018-05-31 15:31:31 -0700 | [diff] [blame] | 48 | use std::os::unix::io::RawFd; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 49 | use std::path::PathBuf; |
Zach Reizner | 639d967 | 2017-05-01 17:57:18 -0700 | [diff] [blame] | 50 | use std::string::String; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 51 | use std::thread::sleep; |
Stephen Barber | 56fbf09 | 2017-06-29 16:12:14 -0700 | [diff] [blame] | 52 | use std::time::Duration; |
Zach Reizner | 639d967 | 2017-05-01 17:57:18 -0700 | [diff] [blame] | 53 | |
Dylan Reid | 2dcb632 | 2018-07-13 10:42:48 -0700 | [diff] [blame] | 54 | use qcow::QcowFile; |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 55 | use sys_util::{getpid, kill_process_group, net::UnixSeqpacket, reap_child, syslog}; |
Zach Reizner | 639d967 | 2017-05-01 17:57:18 -0700 | [diff] [blame] | 56 | |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 57 | use argument::{print_help, set_arguments, Argument}; |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame^] | 58 | use msg_socket::{MsgReceiver, MsgSender, MsgSocket}; |
| 59 | use vm_control::{VmRequest, VmResponse}; |
Zach Reizner | 639d967 | 2017-05-01 17:57:18 -0700 | [diff] [blame] | 60 | |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 61 | static SECCOMP_POLICY_DIR: &'static str = "/usr/share/policy/crosvm"; |
Zach Reizner | ee73bf3 | 2017-08-29 16:33:28 -0700 | [diff] [blame] | 62 | |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 63 | struct DiskOption { |
| 64 | path: PathBuf, |
Daniel Verkamp | de9ae03 | 2018-08-09 16:26:59 -0700 | [diff] [blame] | 65 | read_only: bool, |
Dylan Reid | f463bc1 | 2017-07-12 19:24:57 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Daniel Verkamp | bd1a084 | 2019-01-08 15:50:34 -0800 | [diff] [blame] | 68 | #[allow(dead_code)] |
Chirantan Ekbote | d41d726 | 2018-11-16 16:37:45 -0800 | [diff] [blame] | 69 | struct BindMount { |
| 70 | src: PathBuf, |
| 71 | dst: PathBuf, |
| 72 | writable: bool, |
| 73 | } |
| 74 | |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 75 | const DEFAULT_TRACKPAD_WIDTH: u32 = 800; |
| 76 | const DEFAULT_TRACKPAD_HEIGHT: u32 = 1280; |
| 77 | |
| 78 | struct TrackpadOption { |
| 79 | path: PathBuf, |
| 80 | width: u32, |
| 81 | height: u32, |
| 82 | } |
| 83 | |
| 84 | impl TrackpadOption { |
| 85 | fn new(path: PathBuf) -> TrackpadOption { |
| 86 | TrackpadOption { |
| 87 | path, |
| 88 | width: DEFAULT_TRACKPAD_WIDTH, |
| 89 | height: DEFAULT_TRACKPAD_HEIGHT, |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 94 | pub struct Config { |
| 95 | vcpu_count: Option<u32>, |
| 96 | memory: Option<usize>, |
| 97 | kernel_path: PathBuf, |
Tristan Muntsinger | 4133b01 | 2018-12-21 16:01:56 -0800 | [diff] [blame] | 98 | android_fstab: Option<PathBuf>, |
Daniel Verkamp | e403f5c | 2018-12-11 16:29:26 -0800 | [diff] [blame] | 99 | initrd_path: Option<PathBuf>, |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 100 | params: Vec<String>, |
| 101 | socket_path: Option<PathBuf>, |
| 102 | plugin: Option<PathBuf>, |
| 103 | plugin_root: Option<PathBuf>, |
Chirantan Ekbote | d41d726 | 2018-11-16 16:37:45 -0800 | [diff] [blame] | 104 | plugin_mounts: Vec<BindMount>, |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 105 | disks: Vec<DiskOption>, |
Stephen Barber | 2cfc205 | 2017-06-21 15:16:11 -0700 | [diff] [blame] | 106 | host_ip: Option<net::Ipv4Addr>, |
| 107 | netmask: Option<net::Ipv4Addr>, |
Stephen Barber | 308ff60 | 2018-02-13 22:47:07 -0800 | [diff] [blame] | 108 | mac_address: Option<net_util::MacAddress>, |
Stephen Barber | 5e77e88 | 2017-08-07 17:13:38 -0700 | [diff] [blame] | 109 | vhost_net: bool, |
Jorge E. Moreira | b795280 | 2019-02-12 16:43:05 -0800 | [diff] [blame] | 110 | tap_fd: Vec<RawFd>, |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 111 | cid: Option<u64>, |
Stephen Barber | 28a5a61 | 2017-10-20 17:15:30 -0700 | [diff] [blame] | 112 | wayland_socket_path: Option<PathBuf>, |
David Reveman | 52ba4e5 | 2018-04-22 21:42:09 -0400 | [diff] [blame] | 113 | wayland_dmabuf: bool, |
Chirantan Ekbote | ebd5681 | 2018-04-16 19:32:04 -0700 | [diff] [blame] | 114 | shared_dirs: Vec<(PathBuf, String)>, |
Zach Reizner | 639d967 | 2017-05-01 17:57:18 -0700 | [diff] [blame] | 115 | multiprocess: bool, |
Dylan Reid | d0c9adc | 2017-10-02 19:04:50 -0700 | [diff] [blame] | 116 | seccomp_policy_dir: PathBuf, |
Zach Reizner | 3a8100a | 2017-09-13 19:15:43 -0700 | [diff] [blame] | 117 | gpu: bool, |
David Tolnay | 43f8e21 | 2019-02-13 17:28:16 -0800 | [diff] [blame] | 118 | software_tpm: bool, |
paulhsia | f052cfe | 2019-01-22 15:22:25 +0800 | [diff] [blame] | 119 | cras_audio: bool, |
Dylan Reid | 3082e8e | 2019-01-07 10:33:48 -0800 | [diff] [blame] | 120 | null_audio: bool, |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 121 | virtio_trackpad: Option<TrackpadOption>, |
| 122 | virtio_mouse: Option<PathBuf>, |
| 123 | virtio_keyboard: Option<PathBuf>, |
| 124 | virtio_input_evdevs: Vec<PathBuf>, |
Miriam Zimmerman | 26ac928 | 2019-01-29 21:21:48 -0800 | [diff] [blame] | 125 | split_irqchip: bool, |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Dylan Reid | d0c9adc | 2017-10-02 19:04:50 -0700 | [diff] [blame] | 128 | impl Default for Config { |
| 129 | fn default() -> Config { |
| 130 | Config { |
Dylan Reid | d0c9adc | 2017-10-02 19:04:50 -0700 | [diff] [blame] | 131 | vcpu_count: None, |
| 132 | memory: None, |
| 133 | kernel_path: PathBuf::default(), |
Tristan Muntsinger | 4133b01 | 2018-12-21 16:01:56 -0800 | [diff] [blame] | 134 | android_fstab: None, |
Daniel Verkamp | e403f5c | 2018-12-11 16:29:26 -0800 | [diff] [blame] | 135 | initrd_path: None, |
Zach Reizner | bb67871 | 2018-01-30 18:13:04 -0800 | [diff] [blame] | 136 | params: Vec::new(), |
Dylan Reid | d0c9adc | 2017-10-02 19:04:50 -0700 | [diff] [blame] | 137 | socket_path: None, |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 138 | plugin: None, |
Dmitry Torokhov | 0f1770d | 2018-05-11 10:57:16 -0700 | [diff] [blame] | 139 | plugin_root: None, |
Chirantan Ekbote | d41d726 | 2018-11-16 16:37:45 -0800 | [diff] [blame] | 140 | plugin_mounts: Vec::new(), |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 141 | disks: Vec::new(), |
| 142 | host_ip: None, |
| 143 | netmask: None, |
| 144 | mac_address: None, |
| 145 | vhost_net: false, |
Jorge E. Moreira | b795280 | 2019-02-12 16:43:05 -0800 | [diff] [blame] | 146 | tap_fd: Vec::new(), |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 147 | cid: None, |
| 148 | gpu: false, |
David Tolnay | 43f8e21 | 2019-02-13 17:28:16 -0800 | [diff] [blame] | 149 | software_tpm: false, |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 150 | wayland_socket_path: None, |
| 151 | wayland_dmabuf: false, |
| 152 | shared_dirs: Vec::new(), |
| 153 | multiprocess: !cfg!(feature = "default-no-sandbox"), |
| 154 | seccomp_policy_dir: PathBuf::from(SECCOMP_POLICY_DIR), |
paulhsia | f052cfe | 2019-01-22 15:22:25 +0800 | [diff] [blame] | 155 | cras_audio: false, |
Dylan Reid | 3082e8e | 2019-01-07 10:33:48 -0800 | [diff] [blame] | 156 | null_audio: false, |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 157 | virtio_trackpad: None, |
| 158 | virtio_mouse: None, |
| 159 | virtio_keyboard: None, |
| 160 | virtio_input_evdevs: Vec::new(), |
Miriam Zimmerman | 26ac928 | 2019-01-29 21:21:48 -0800 | [diff] [blame] | 161 | split_irqchip: false, |
Dylan Reid | d0c9adc | 2017-10-02 19:04:50 -0700 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
Stephen Barber | a00753b | 2017-07-18 13:57:26 -0700 | [diff] [blame] | 166 | // Wait for all children to exit. Return true if they have all exited, false |
| 167 | // otherwise. |
| 168 | fn wait_all_children() -> bool { |
Stephen Barber | 49dd2e2 | 2018-10-29 18:29:58 -0700 | [diff] [blame] | 169 | const CHILD_WAIT_MAX_ITER: isize = 100; |
Stephen Barber | a00753b | 2017-07-18 13:57:26 -0700 | [diff] [blame] | 170 | const CHILD_WAIT_MS: u64 = 10; |
| 171 | for _ in 0..CHILD_WAIT_MAX_ITER { |
Stephen Barber | a00753b | 2017-07-18 13:57:26 -0700 | [diff] [blame] | 172 | loop { |
Zach Reizner | 56158c8 | 2017-08-24 13:50:14 -0700 | [diff] [blame] | 173 | match reap_child() { |
| 174 | Ok(0) => break, |
| 175 | // We expect ECHILD which indicates that there were no children left. |
| 176 | Err(e) if e.errno() == libc::ECHILD => return true, |
| 177 | Err(e) => { |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 178 | warn!("error while waiting for children: {}", e); |
Zach Reizner | 56158c8 | 2017-08-24 13:50:14 -0700 | [diff] [blame] | 179 | return false; |
Stephen Barber | a00753b | 2017-07-18 13:57:26 -0700 | [diff] [blame] | 180 | } |
Zach Reizner | 56158c8 | 2017-08-24 13:50:14 -0700 | [diff] [blame] | 181 | // We reaped one child, so continue reaping. |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 182 | _ => {} |
Stephen Barber | a00753b | 2017-07-18 13:57:26 -0700 | [diff] [blame] | 183 | } |
| 184 | } |
Zach Reizner | 56158c8 | 2017-08-24 13:50:14 -0700 | [diff] [blame] | 185 | // There's no timeout option for waitpid which reap_child calls internally, so our only |
| 186 | // recourse is to sleep while waiting for the children to exit. |
Stephen Barber | a00753b | 2017-07-18 13:57:26 -0700 | [diff] [blame] | 187 | sleep(Duration::from_millis(CHILD_WAIT_MS)); |
| 188 | } |
| 189 | |
| 190 | // If we've made it to this point, not all of the children have exited. |
David Tolnay | 5bbbf61 | 2018-12-01 17:49:30 -0800 | [diff] [blame] | 191 | false |
Stephen Barber | a00753b | 2017-07-18 13:57:26 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 194 | fn set_argument(cfg: &mut Config, name: &str, value: Option<&str>) -> argument::Result<()> { |
| 195 | match name { |
| 196 | "" => { |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 197 | if cfg.plugin.is_some() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 198 | return Err(argument::Error::TooManyArguments( |
| 199 | "`plugin` can not be used with kernel".to_owned(), |
| 200 | )); |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 201 | } else if !cfg.kernel_path.as_os_str().is_empty() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 202 | return Err(argument::Error::TooManyArguments( |
| 203 | "expected exactly one kernel path".to_owned(), |
| 204 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 205 | } else { |
| 206 | let kernel_path = PathBuf::from(value.unwrap()); |
| 207 | if !kernel_path.exists() { |
| 208 | return Err(argument::Error::InvalidValue { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 209 | value: value.unwrap().to_owned(), |
| 210 | expected: "this kernel path does not exist", |
| 211 | }); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 212 | } |
| 213 | cfg.kernel_path = kernel_path; |
| 214 | } |
| 215 | } |
Tristan Muntsinger | 4133b01 | 2018-12-21 16:01:56 -0800 | [diff] [blame] | 216 | "android-fstab" => { |
| 217 | if cfg.android_fstab.is_some() |
| 218 | && !cfg.android_fstab.as_ref().unwrap().as_os_str().is_empty() |
| 219 | { |
| 220 | return Err(argument::Error::TooManyArguments( |
| 221 | "expected exactly one android fstab path".to_owned(), |
| 222 | )); |
| 223 | } else { |
| 224 | let android_fstab = PathBuf::from(value.unwrap()); |
| 225 | if !android_fstab.exists() { |
| 226 | return Err(argument::Error::InvalidValue { |
| 227 | value: value.unwrap().to_owned(), |
| 228 | expected: "this android fstab path does not exist", |
| 229 | }); |
| 230 | } |
| 231 | cfg.android_fstab = Some(android_fstab); |
| 232 | } |
| 233 | } |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 234 | "params" => { |
Zach Reizner | bb67871 | 2018-01-30 18:13:04 -0800 | [diff] [blame] | 235 | cfg.params.push(value.unwrap().to_owned()); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 236 | } |
| 237 | "cpus" => { |
| 238 | if cfg.vcpu_count.is_some() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 239 | return Err(argument::Error::TooManyArguments( |
| 240 | "`cpus` already given".to_owned(), |
| 241 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 242 | } |
| 243 | cfg.vcpu_count = |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 244 | Some( |
| 245 | value |
| 246 | .unwrap() |
| 247 | .parse() |
| 248 | .map_err(|_| argument::Error::InvalidValue { |
| 249 | value: value.unwrap().to_owned(), |
| 250 | expected: "this value for `cpus` needs to be integer", |
| 251 | })?, |
| 252 | ) |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 253 | } |
| 254 | "mem" => { |
| 255 | if cfg.memory.is_some() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 256 | return Err(argument::Error::TooManyArguments( |
| 257 | "`mem` already given".to_owned(), |
| 258 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 259 | } |
| 260 | cfg.memory = |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 261 | Some( |
| 262 | value |
| 263 | .unwrap() |
| 264 | .parse() |
| 265 | .map_err(|_| argument::Error::InvalidValue { |
| 266 | value: value.unwrap().to_owned(), |
| 267 | expected: "this value for `mem` needs to be integer", |
| 268 | })?, |
| 269 | ) |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 270 | } |
paulhsia | f052cfe | 2019-01-22 15:22:25 +0800 | [diff] [blame] | 271 | "cras-audio" => { |
| 272 | cfg.cras_audio = true; |
| 273 | } |
Dylan Reid | 3082e8e | 2019-01-07 10:33:48 -0800 | [diff] [blame] | 274 | "null-audio" => { |
| 275 | cfg.null_audio = true; |
| 276 | } |
Dylan Reid | 88624f8 | 2018-01-11 09:20:16 -0800 | [diff] [blame] | 277 | "root" | "disk" | "rwdisk" | "qcow" | "rwqcow" => { |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 278 | let disk_path = PathBuf::from(value.unwrap()); |
| 279 | if !disk_path.exists() { |
| 280 | return Err(argument::Error::InvalidValue { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 281 | value: value.unwrap().to_owned(), |
| 282 | expected: "this disk path does not exist", |
| 283 | }); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 284 | } |
| 285 | if name == "root" { |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 286 | if cfg.disks.len() >= 26 { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 287 | return Err(argument::Error::TooManyArguments( |
| 288 | "ran out of letters for to assign to root disk".to_owned(), |
| 289 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 290 | } |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 291 | cfg.params.push(format!( |
| 292 | "root=/dev/vd{} ro", |
David Tolnay | 5bbbf61 | 2018-12-01 17:49:30 -0800 | [diff] [blame] | 293 | char::from(b'a' + cfg.disks.len() as u8) |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 294 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 295 | } |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 296 | cfg.disks.push(DiskOption { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 297 | path: disk_path, |
| 298 | read_only: !name.starts_with("rw"), |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 299 | }); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 300 | } |
| 301 | "host_ip" => { |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 302 | if cfg.host_ip.is_some() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 303 | return Err(argument::Error::TooManyArguments( |
| 304 | "`host_ip` already given".to_owned(), |
| 305 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 306 | } |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 307 | cfg.host_ip = |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 308 | Some( |
| 309 | value |
| 310 | .unwrap() |
| 311 | .parse() |
| 312 | .map_err(|_| argument::Error::InvalidValue { |
| 313 | value: value.unwrap().to_owned(), |
| 314 | expected: "`host_ip` needs to be in the form \"x.x.x.x\"", |
| 315 | })?, |
| 316 | ) |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 317 | } |
| 318 | "netmask" => { |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 319 | if cfg.netmask.is_some() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 320 | return Err(argument::Error::TooManyArguments( |
| 321 | "`netmask` already given".to_owned(), |
| 322 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 323 | } |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 324 | cfg.netmask = |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 325 | Some( |
| 326 | value |
| 327 | .unwrap() |
| 328 | .parse() |
| 329 | .map_err(|_| argument::Error::InvalidValue { |
| 330 | value: value.unwrap().to_owned(), |
| 331 | expected: "`netmask` needs to be in the form \"x.x.x.x\"", |
| 332 | })?, |
| 333 | ) |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 334 | } |
| 335 | "mac" => { |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 336 | if cfg.mac_address.is_some() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 337 | return Err(argument::Error::TooManyArguments( |
| 338 | "`mac` already given".to_owned(), |
| 339 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 340 | } |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 341 | cfg.mac_address = |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 342 | Some( |
| 343 | value |
| 344 | .unwrap() |
| 345 | .parse() |
| 346 | .map_err(|_| argument::Error::InvalidValue { |
| 347 | value: value.unwrap().to_owned(), |
| 348 | expected: "`mac` needs to be in the form \"XX:XX:XX:XX:XX:XX\"", |
| 349 | })?, |
| 350 | ) |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 351 | } |
Stephen Barber | 28a5a61 | 2017-10-20 17:15:30 -0700 | [diff] [blame] | 352 | "wayland-sock" => { |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 353 | if cfg.wayland_socket_path.is_some() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 354 | return Err(argument::Error::TooManyArguments( |
| 355 | "`wayland-sock` already given".to_owned(), |
| 356 | )); |
Stephen Barber | 28a5a61 | 2017-10-20 17:15:30 -0700 | [diff] [blame] | 357 | } |
| 358 | let wayland_socket_path = PathBuf::from(value.unwrap()); |
| 359 | if !wayland_socket_path.exists() { |
| 360 | return Err(argument::Error::InvalidValue { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 361 | value: value.unwrap().to_string(), |
| 362 | expected: "Wayland socket does not exist", |
| 363 | }); |
Stephen Barber | 28a5a61 | 2017-10-20 17:15:30 -0700 | [diff] [blame] | 364 | } |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 365 | cfg.wayland_socket_path = Some(wayland_socket_path); |
Stephen Barber | 28a5a61 | 2017-10-20 17:15:30 -0700 | [diff] [blame] | 366 | } |
David Reveman | 52ba4e5 | 2018-04-22 21:42:09 -0400 | [diff] [blame] | 367 | #[cfg(feature = "wl-dmabuf")] |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 368 | "wayland-dmabuf" => cfg.wayland_dmabuf = true, |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 369 | "socket" => { |
| 370 | if cfg.socket_path.is_some() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 371 | return Err(argument::Error::TooManyArguments( |
| 372 | "`socket` already given".to_owned(), |
| 373 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 374 | } |
| 375 | let mut socket_path = PathBuf::from(value.unwrap()); |
| 376 | if socket_path.is_dir() { |
| 377 | socket_path.push(format!("crosvm-{}.sock", getpid())); |
| 378 | } |
| 379 | if socket_path.exists() { |
| 380 | return Err(argument::Error::InvalidValue { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 381 | value: socket_path.to_string_lossy().into_owned(), |
| 382 | expected: "this socket path already exists", |
| 383 | }); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 384 | } |
| 385 | cfg.socket_path = Some(socket_path); |
| 386 | } |
| 387 | "multiprocess" => { |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 388 | cfg.multiprocess = true; |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 389 | } |
Dylan Reid | d0c9adc | 2017-10-02 19:04:50 -0700 | [diff] [blame] | 390 | "disable-sandbox" => { |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 391 | cfg.multiprocess = false; |
Dylan Reid | d0c9adc | 2017-10-02 19:04:50 -0700 | [diff] [blame] | 392 | } |
Chirantan Ekbote | 88f9cba | 2017-08-28 09:51:18 -0700 | [diff] [blame] | 393 | "cid" => { |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 394 | if cfg.cid.is_some() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 395 | return Err(argument::Error::TooManyArguments( |
| 396 | "`cid` alread given".to_owned(), |
| 397 | )); |
Chirantan Ekbote | 88f9cba | 2017-08-28 09:51:18 -0700 | [diff] [blame] | 398 | } |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 399 | cfg.cid = Some( |
| 400 | value |
| 401 | .unwrap() |
| 402 | .parse() |
| 403 | .map_err(|_| argument::Error::InvalidValue { |
| 404 | value: value.unwrap().to_owned(), |
| 405 | expected: "this value for `cid` must be an unsigned integer", |
| 406 | })?, |
| 407 | ); |
Chirantan Ekbote | 88f9cba | 2017-08-28 09:51:18 -0700 | [diff] [blame] | 408 | } |
Chirantan Ekbote | ebd5681 | 2018-04-16 19:32:04 -0700 | [diff] [blame] | 409 | "shared-dir" => { |
| 410 | // Formatted as <src:tag>. |
| 411 | let param = value.unwrap(); |
| 412 | let mut components = param.splitn(2, ':'); |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 413 | let src = |
| 414 | PathBuf::from( |
| 415 | components |
| 416 | .next() |
| 417 | .ok_or_else(|| argument::Error::InvalidValue { |
| 418 | value: param.to_owned(), |
| 419 | expected: "missing source path for `shared-dir`", |
| 420 | })?, |
| 421 | ); |
| 422 | let tag = components |
| 423 | .next() |
| 424 | .ok_or_else(|| argument::Error::InvalidValue { |
Chirantan Ekbote | ebd5681 | 2018-04-16 19:32:04 -0700 | [diff] [blame] | 425 | value: param.to_owned(), |
| 426 | expected: "missing tag for `shared-dir`", |
David Tolnay | 2bac1e7 | 2018-12-12 14:33:42 -0800 | [diff] [blame] | 427 | })? |
| 428 | .to_owned(); |
Chirantan Ekbote | ebd5681 | 2018-04-16 19:32:04 -0700 | [diff] [blame] | 429 | |
| 430 | if !src.is_dir() { |
| 431 | return Err(argument::Error::InvalidValue { |
| 432 | value: param.to_owned(), |
| 433 | expected: "source path for `shared-dir` must be a directory", |
| 434 | }); |
| 435 | } |
| 436 | |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 437 | cfg.shared_dirs.push((src, tag)); |
Chirantan Ekbote | ebd5681 | 2018-04-16 19:32:04 -0700 | [diff] [blame] | 438 | } |
Dylan Reid | e026ef0 | 2017-10-02 19:03:52 -0700 | [diff] [blame] | 439 | "seccomp-policy-dir" => { |
Dylan Reid | d0c9adc | 2017-10-02 19:04:50 -0700 | [diff] [blame] | 440 | // `value` is Some because we are in this match so it's safe to unwrap. |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 441 | cfg.seccomp_policy_dir = PathBuf::from(value.unwrap()); |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 442 | } |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 443 | "plugin" => { |
| 444 | if !cfg.kernel_path.as_os_str().is_empty() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 445 | return Err(argument::Error::TooManyArguments( |
| 446 | "`plugin` can not be used with kernel".to_owned(), |
| 447 | )); |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 448 | } else if cfg.plugin.is_some() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 449 | return Err(argument::Error::TooManyArguments( |
| 450 | "`plugin` already given".to_owned(), |
| 451 | )); |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 452 | } |
Zach Reizner | cc30d58 | 2018-01-23 21:16:42 -0800 | [diff] [blame] | 453 | let plugin = PathBuf::from(value.unwrap().to_owned()); |
| 454 | if plugin.is_relative() { |
| 455 | return Err(argument::Error::InvalidValue { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 456 | value: plugin.to_string_lossy().into_owned(), |
| 457 | expected: "the plugin path must be an absolute path", |
| 458 | }); |
Zach Reizner | cc30d58 | 2018-01-23 21:16:42 -0800 | [diff] [blame] | 459 | } |
| 460 | cfg.plugin = Some(plugin); |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 461 | } |
Dmitry Torokhov | 0f1770d | 2018-05-11 10:57:16 -0700 | [diff] [blame] | 462 | "plugin-root" => { |
| 463 | cfg.plugin_root = Some(PathBuf::from(value.unwrap().to_owned())); |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 464 | } |
Chirantan Ekbote | d41d726 | 2018-11-16 16:37:45 -0800 | [diff] [blame] | 465 | "plugin-mount" => { |
| 466 | let components: Vec<&str> = value.unwrap().split(":").collect(); |
| 467 | if components.len() != 3 { |
| 468 | return Err(argument::Error::InvalidValue { |
| 469 | value: value.unwrap().to_owned(), |
| 470 | expected: |
| 471 | "`plugin-mount` must have exactly 3 components: <src>:<dst>:<writable>", |
| 472 | }); |
| 473 | } |
| 474 | |
| 475 | let src = PathBuf::from(components[0]); |
| 476 | if src.is_relative() { |
| 477 | return Err(argument::Error::InvalidValue { |
| 478 | value: components[0].to_owned(), |
| 479 | expected: "the source path for `plugin-mount` must be absolute", |
| 480 | }); |
| 481 | } |
| 482 | if !src.exists() { |
| 483 | return Err(argument::Error::InvalidValue { |
| 484 | value: components[0].to_owned(), |
| 485 | expected: "the source path for `plugin-mount` does not exist", |
| 486 | }); |
| 487 | } |
| 488 | |
| 489 | let dst = PathBuf::from(components[1]); |
| 490 | if dst.is_relative() { |
| 491 | return Err(argument::Error::InvalidValue { |
| 492 | value: components[1].to_owned(), |
| 493 | expected: "the destination path for `plugin-mount` must be absolute", |
| 494 | }); |
| 495 | } |
| 496 | |
| 497 | let writable: bool = |
| 498 | components[2] |
| 499 | .parse() |
| 500 | .map_err(|_| argument::Error::InvalidValue { |
| 501 | value: components[2].to_owned(), |
| 502 | expected: "the <writable> component for `plugin-mount` is not valid bool", |
| 503 | })?; |
| 504 | |
| 505 | cfg.plugin_mounts.push(BindMount { src, dst, writable }); |
| 506 | } |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 507 | "vhost-net" => cfg.vhost_net = true, |
Chirantan Ekbote | 5f78721 | 2018-05-31 15:31:31 -0700 | [diff] [blame] | 508 | "tap-fd" => { |
Jorge E. Moreira | b795280 | 2019-02-12 16:43:05 -0800 | [diff] [blame] | 509 | cfg.tap_fd.push( |
| 510 | value |
| 511 | .unwrap() |
| 512 | .parse() |
| 513 | .map_err(|_| argument::Error::InvalidValue { |
| 514 | value: value.unwrap().to_owned(), |
| 515 | expected: "this value for `tap-fd` must be an unsigned integer", |
| 516 | })?, |
| 517 | ); |
Chirantan Ekbote | 5f78721 | 2018-05-31 15:31:31 -0700 | [diff] [blame] | 518 | } |
Zach Reizner | 3a8100a | 2017-09-13 19:15:43 -0700 | [diff] [blame] | 519 | "gpu" => { |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 520 | cfg.gpu = true; |
Zach Reizner | 3a8100a | 2017-09-13 19:15:43 -0700 | [diff] [blame] | 521 | } |
David Tolnay | 43f8e21 | 2019-02-13 17:28:16 -0800 | [diff] [blame] | 522 | "software-tpm" => { |
| 523 | cfg.software_tpm = true; |
| 524 | } |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 525 | "trackpad" => { |
| 526 | if cfg.virtio_trackpad.is_some() { |
| 527 | return Err(argument::Error::TooManyArguments( |
| 528 | "`trackpad` already given".to_owned(), |
| 529 | )); |
| 530 | } |
| 531 | let mut it = value.unwrap().split(":"); |
| 532 | |
| 533 | let mut trackpad_spec = |
| 534 | TrackpadOption::new(PathBuf::from(it.next().unwrap().to_owned())); |
| 535 | if let Some(width) = it.next() { |
| 536 | trackpad_spec.width = width.trim().parse().unwrap(); |
| 537 | } |
| 538 | if let Some(height) = it.next() { |
| 539 | trackpad_spec.height = height.trim().parse().unwrap(); |
| 540 | } |
| 541 | |
| 542 | cfg.virtio_trackpad = Some(trackpad_spec); |
| 543 | } |
| 544 | "mouse" => { |
| 545 | if cfg.virtio_mouse.is_some() { |
| 546 | return Err(argument::Error::TooManyArguments( |
| 547 | "`mouse` already given".to_owned(), |
| 548 | )); |
| 549 | } |
| 550 | cfg.virtio_mouse = Some(PathBuf::from(value.unwrap().to_owned())); |
| 551 | } |
| 552 | "keyboard" => { |
| 553 | if cfg.virtio_keyboard.is_some() { |
| 554 | return Err(argument::Error::TooManyArguments( |
| 555 | "`keyboard` already given".to_owned(), |
| 556 | )); |
| 557 | } |
| 558 | cfg.virtio_keyboard = Some(PathBuf::from(value.unwrap().to_owned())); |
| 559 | } |
| 560 | "evdev" => { |
| 561 | let dev_path = PathBuf::from(value.unwrap()); |
| 562 | if !dev_path.exists() { |
| 563 | return Err(argument::Error::InvalidValue { |
| 564 | value: value.unwrap().to_owned(), |
| 565 | expected: "this input device path does not exist", |
| 566 | }); |
| 567 | } |
| 568 | cfg.virtio_input_evdevs.push(dev_path); |
| 569 | } |
Miriam Zimmerman | 26ac928 | 2019-01-29 21:21:48 -0800 | [diff] [blame] | 570 | "split-irqchip" => { |
| 571 | cfg.split_irqchip = true; |
| 572 | } |
Daniel Verkamp | e403f5c | 2018-12-11 16:29:26 -0800 | [diff] [blame] | 573 | "initrd" => { |
| 574 | cfg.initrd_path = Some(PathBuf::from(value.unwrap().to_owned())); |
| 575 | } |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 576 | "help" => return Err(argument::Error::PrintHelp), |
| 577 | _ => unreachable!(), |
| 578 | } |
| 579 | Ok(()) |
| 580 | } |
| 581 | |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 582 | fn run_vm(args: std::env::Args) -> std::result::Result<(), ()> { |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 583 | let arguments = |
| 584 | &[Argument::positional("KERNEL", "bzImage of kernel to run"), |
Tristan Muntsinger | 4133b01 | 2018-12-21 16:01:56 -0800 | [diff] [blame] | 585 | Argument::value("android-fstab", "PATH", "Path to Android fstab"), |
Daniel Verkamp | e403f5c | 2018-12-11 16:29:26 -0800 | [diff] [blame] | 586 | Argument::short_value('i', "initrd", "PATH", "Initial ramdisk to load."), |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 587 | Argument::short_value('p', |
| 588 | "params", |
| 589 | "PARAMS", |
Zach Reizner | bb67871 | 2018-01-30 18:13:04 -0800 | [diff] [blame] | 590 | "Extra kernel or plugin command line arguments. Can be given more than once."), |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 591 | Argument::short_value('c', "cpus", "N", "Number of VCPUs. (default: 1)"), |
| 592 | Argument::short_value('m', |
| 593 | "mem", |
| 594 | "N", |
| 595 | "Amount of guest memory in MiB. (default: 256)"), |
| 596 | Argument::short_value('r', |
| 597 | "root", |
| 598 | "PATH", |
| 599 | "Path to a root disk image. Like `--disk` but adds appropriate kernel command line option."), |
| 600 | Argument::short_value('d', "disk", "PATH", "Path to a disk image."), |
Daniel Verkamp | f02fdd1 | 2018-10-10 17:25:14 -0700 | [diff] [blame] | 601 | Argument::value("qcow", "PATH", "Path to a qcow2 disk image. (Deprecated; use --disk instead.)"), |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 602 | Argument::value("rwdisk", "PATH", "Path to a writable disk image."), |
Daniel Verkamp | f02fdd1 | 2018-10-10 17:25:14 -0700 | [diff] [blame] | 603 | Argument::value("rwqcow", "PATH", "Path to a writable qcow2 disk image. (Deprecated; use --rwdisk instead.)"), |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 604 | Argument::value("host_ip", |
| 605 | "IP", |
| 606 | "IP address to assign to host tap interface."), |
| 607 | Argument::value("netmask", "NETMASK", "Netmask for VM subnet."), |
| 608 | Argument::value("mac", "MAC", "MAC address for VM."), |
paulhsia | f052cfe | 2019-01-22 15:22:25 +0800 | [diff] [blame] | 609 | Argument::flag("cras-audio", "Add an audio device to the VM that plays samples through CRAS server"), |
Dylan Reid | 3082e8e | 2019-01-07 10:33:48 -0800 | [diff] [blame] | 610 | Argument::flag("null-audio", "Add an audio device to the VM that plays samples to /dev/null"), |
Stephen Barber | 28a5a61 | 2017-10-20 17:15:30 -0700 | [diff] [blame] | 611 | Argument::value("wayland-sock", "PATH", "Path to the Wayland socket to use."), |
David Reveman | 52ba4e5 | 2018-04-22 21:42:09 -0400 | [diff] [blame] | 612 | #[cfg(feature = "wl-dmabuf")] |
| 613 | Argument::flag("wayland-dmabuf", "Enable support for DMABufs in Wayland device."), |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 614 | Argument::short_value('s', |
| 615 | "socket", |
| 616 | "PATH", |
| 617 | "Path to put the control socket. If PATH is a directory, a name will be generated."), |
Dylan Reid | d0c9adc | 2017-10-02 19:04:50 -0700 | [diff] [blame] | 618 | Argument::short_flag('u', "multiprocess", "Run each device in a child process(default)."), |
| 619 | Argument::flag("disable-sandbox", "Run all devices in one, non-sandboxed process."), |
Chirantan Ekbote | ebd5681 | 2018-04-16 19:32:04 -0700 | [diff] [blame] | 620 | Argument::value("cid", "CID", "Context ID for virtual sockets."), |
| 621 | Argument::value("shared-dir", "PATH:TAG", |
| 622 | "Directory to be shared with a VM as a source:tag pair. Can be given more than once."), |
Dylan Reid | e026ef0 | 2017-10-02 19:03:52 -0700 | [diff] [blame] | 623 | Argument::value("seccomp-policy-dir", "PATH", "Path to seccomp .policy files."), |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 624 | #[cfg(feature = "plugin")] |
Zach Reizner | cc30d58 | 2018-01-23 21:16:42 -0800 | [diff] [blame] | 625 | Argument::value("plugin", "PATH", "Absolute path to plugin process to run under crosvm."), |
Daniel Verkamp | bd1a084 | 2019-01-08 15:50:34 -0800 | [diff] [blame] | 626 | #[cfg(feature = "plugin")] |
Dmitry Torokhov | 0f1770d | 2018-05-11 10:57:16 -0700 | [diff] [blame] | 627 | Argument::value("plugin-root", "PATH", "Absolute path to a directory that will become root filesystem for the plugin process."), |
Daniel Verkamp | bd1a084 | 2019-01-08 15:50:34 -0800 | [diff] [blame] | 628 | #[cfg(feature = "plugin")] |
Chirantan Ekbote | d41d726 | 2018-11-16 16:37:45 -0800 | [diff] [blame] | 629 | Argument::value("plugin-mount", "PATH:PATH:BOOL", "Path to be mounted into the plugin's root filesystem. Can be given more than once."), |
Rob Bradford | 8f002f5 | 2018-02-19 16:31:11 +0000 | [diff] [blame] | 630 | Argument::flag("vhost-net", "Use vhost for networking."), |
Chirantan Ekbote | 5f78721 | 2018-05-31 15:31:31 -0700 | [diff] [blame] | 631 | Argument::value("tap-fd", |
| 632 | "fd", |
Jorge E. Moreira | b795280 | 2019-02-12 16:43:05 -0800 | [diff] [blame] | 633 | "File descriptor for configured tap device. A different virtual network card will be added each time this argument is given."), |
Zach Reizner | 3a8100a | 2017-09-13 19:15:43 -0700 | [diff] [blame] | 634 | #[cfg(feature = "gpu")] |
| 635 | Argument::flag("gpu", "(EXPERIMENTAL) enable virtio-gpu device"), |
David Tolnay | 43f8e21 | 2019-02-13 17:28:16 -0800 | [diff] [blame] | 636 | #[cfg(feature = "tpm")] |
| 637 | Argument::flag("software-tpm", "enable a software emulated trusted platform module device"), |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 638 | Argument::value("evdev", "PATH", "Path to an event device node. The device will be grabbed (unusable from the host) and made available to the guest with the same configuration it shows on the host"), |
| 639 | Argument::value("trackpad", "PATH:WIDTH:HEIGHT", "Path to a socket from where to read trackpad input events and write status updates to, optionally followed by screen width and height (defaults to 800x1280)."), |
| 640 | Argument::value("mouse", "PATH", "Path to a socket from where to read mouse input events and write status updates to."), |
| 641 | Argument::value("keyboard", "PATH", "Path to a socket from where to read keyboard input events and write status updates to."), |
Miriam Zimmerman | 26ac928 | 2019-01-29 21:21:48 -0800 | [diff] [blame] | 642 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 643 | Argument::flag("split-irqchip", "(EXPERIMENTAL) enable split-irqchip support"), |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 644 | Argument::short_flag('h', "help", "Print help message.")]; |
| 645 | |
| 646 | let mut cfg = Config::default(); |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 647 | let match_res = set_arguments(args, &arguments[..], |name, value| { |
| 648 | set_argument(&mut cfg, name, value) |
David Tolnay | 2bac1e7 | 2018-12-12 14:33:42 -0800 | [diff] [blame] | 649 | }) |
| 650 | .and_then(|_| { |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 651 | if cfg.kernel_path.as_os_str().is_empty() && cfg.plugin.is_none() { |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 652 | return Err(argument::Error::ExpectedArgument("`KERNEL`".to_owned())); |
| 653 | } |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 654 | if cfg.host_ip.is_some() || cfg.netmask.is_some() || cfg.mac_address.is_some() { |
| 655 | if cfg.host_ip.is_none() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 656 | return Err(argument::Error::ExpectedArgument( |
| 657 | "`host_ip` missing from network config".to_owned(), |
| 658 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 659 | } |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 660 | if cfg.netmask.is_none() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 661 | return Err(argument::Error::ExpectedArgument( |
| 662 | "`netmask` missing from network config".to_owned(), |
| 663 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 664 | } |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 665 | if cfg.mac_address.is_none() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 666 | return Err(argument::Error::ExpectedArgument( |
| 667 | "`mac` missing from network config".to_owned(), |
| 668 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 669 | } |
| 670 | } |
Dmitry Torokhov | 0f1770d | 2018-05-11 10:57:16 -0700 | [diff] [blame] | 671 | if cfg.plugin_root.is_some() && cfg.plugin.is_none() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 672 | return Err(argument::Error::ExpectedArgument( |
| 673 | "`plugin-root` requires `plugin`".to_owned(), |
| 674 | )); |
Dmitry Torokhov | 0f1770d | 2018-05-11 10:57:16 -0700 | [diff] [blame] | 675 | } |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 676 | Ok(()) |
| 677 | }); |
| 678 | |
| 679 | match match_res { |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 680 | #[cfg(feature = "plugin")] |
David Tolnay | 2bac1e7 | 2018-12-12 14:33:42 -0800 | [diff] [blame] | 681 | Ok(()) if cfg.plugin.is_some() => match plugin::run_config(cfg) { |
| 682 | Ok(_) => { |
| 683 | info!("crosvm and plugin have exited normally"); |
| 684 | Ok(()) |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 685 | } |
David Tolnay | 2bac1e7 | 2018-12-12 14:33:42 -0800 | [diff] [blame] | 686 | Err(e) => { |
| 687 | error!("{}", e); |
| 688 | Err(()) |
| 689 | } |
| 690 | }, |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 691 | Ok(()) => match linux::run_config(cfg) { |
| 692 | Ok(_) => { |
| 693 | info!("crosvm has exited normally"); |
| 694 | Ok(()) |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 695 | } |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 696 | Err(e) => { |
| 697 | error!("{}", e); |
| 698 | Err(()) |
| 699 | } |
| 700 | }, |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 701 | Err(argument::Error::PrintHelp) => { |
| 702 | print_help("crosvm run", "KERNEL", &arguments[..]); |
| 703 | Ok(()) |
| 704 | } |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 705 | Err(e) => { |
| 706 | println!("{}", e); |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 707 | Err(()) |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 708 | } |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 709 | } |
| 710 | } |
| 711 | |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame^] | 712 | fn vms_request(request: &VmRequest, args: std::env::Args) -> std::result::Result<(), ()> { |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 713 | let mut return_result = Ok(()); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 714 | for socket_path in args { |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 715 | match UnixSeqpacket::connect(&socket_path) { |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 716 | Ok(s) => { |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame^] | 717 | let socket = MsgSocket::<VmRequest, VmResponse>::new(s); |
| 718 | if let Err(e) = socket.send(request) { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 719 | error!( |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 720 | "failed to send request to socket at '{}': {}", |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 721 | socket_path, e |
| 722 | ); |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame^] | 723 | return_result = Err(()); |
| 724 | continue; |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 725 | } |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame^] | 726 | match socket.recv() { |
| 727 | Ok(response) => info!("request response was {}", response), |
| 728 | Err(e) => { |
| 729 | error!( |
| 730 | "failed to send request to socket at2 '{}': {}", |
| 731 | socket_path, e |
| 732 | ); |
| 733 | return_result = Err(()); |
| 734 | continue; |
| 735 | } |
Dylan Reid | d443204 | 2017-12-06 18:20:09 -0800 | [diff] [blame] | 736 | } |
| 737 | } |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 738 | Err(e) => { |
| 739 | error!("failed to connect to socket at '{}': {}", socket_path, e); |
| 740 | return_result = Err(()); |
| 741 | } |
Dylan Reid | d443204 | 2017-12-06 18:20:09 -0800 | [diff] [blame] | 742 | } |
| 743 | } |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 744 | |
| 745 | return_result |
Dylan Reid | d443204 | 2017-12-06 18:20:09 -0800 | [diff] [blame] | 746 | } |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 747 | |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame^] | 748 | fn stop_vms(args: std::env::Args) -> std::result::Result<(), ()> { |
| 749 | if args.len() == 0 { |
| 750 | print_help("crosvm stop", "VM_SOCKET...", &[]); |
| 751 | println!("Stops the crosvm instance listening on each `VM_SOCKET` given."); |
| 752 | return Ok(()); |
| 753 | } |
| 754 | vms_request(&VmRequest::Exit, args) |
| 755 | } |
| 756 | |
| 757 | fn suspend_vms(args: std::env::Args) -> std::result::Result<(), ()> { |
| 758 | if args.len() == 0 { |
| 759 | print_help("crosvm suspend", "VM_SOCKET...", &[]); |
| 760 | println!("Suspends the crosvm instance listening on each `VM_SOCKET` given."); |
| 761 | return Ok(()); |
| 762 | } |
| 763 | vms_request(&VmRequest::Suspend, args) |
| 764 | } |
| 765 | |
| 766 | fn resume_vms(args: std::env::Args) -> std::result::Result<(), ()> { |
| 767 | if args.len() == 0 { |
| 768 | print_help("crosvm resume", "VM_SOCKET...", &[]); |
| 769 | println!("Resumes the crosvm instance listening on each `VM_SOCKET` given."); |
| 770 | return Ok(()); |
| 771 | } |
| 772 | vms_request(&VmRequest::Resume, args) |
| 773 | } |
| 774 | |
| 775 | fn balloon_vms(mut args: std::env::Args) -> std::result::Result<(), ()> { |
| 776 | if args.len() < 2 { |
| 777 | print_help("crosvm balloon", "SIZE VM_SOCKET...", &[]); |
| 778 | println!("Set the ballon size of the crosvm instance to `SIZE` bytes."); |
| 779 | return Ok(()); |
| 780 | } |
| 781 | let num_bytes = match args.nth(0).unwrap().parse::<u64>() { |
| 782 | Ok(n) => n, |
| 783 | Err(_) => { |
| 784 | error!("Failed to parse number of bytes"); |
| 785 | return Err(()); |
| 786 | } |
| 787 | }; |
| 788 | |
| 789 | vms_request(&VmRequest::BalloonAdjust(num_bytes), args) |
| 790 | } |
| 791 | |
Dylan Reid | 2dcb632 | 2018-07-13 10:42:48 -0700 | [diff] [blame] | 792 | fn create_qcow2(mut args: std::env::Args) -> std::result::Result<(), ()> { |
| 793 | if args.len() != 2 { |
| 794 | print_help("crosvm create_qcow2", "PATH SIZE", &[]); |
Dylan Reid | 940259c | 2018-07-20 14:22:33 -0700 | [diff] [blame] | 795 | println!("Create a new QCOW2 image at `PATH` of the specified `SIZE` in bytes."); |
Dylan Reid | 2dcb632 | 2018-07-13 10:42:48 -0700 | [diff] [blame] | 796 | } |
| 797 | let file_path = args.nth(0).unwrap(); |
| 798 | let size: u64 = match args.nth(0).unwrap().parse::<u64>() { |
| 799 | Ok(n) => n, |
| 800 | Err(_) => { |
| 801 | error!("Failed to parse size of the disk."); |
| 802 | return Err(()); |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 803 | } |
Dylan Reid | 2dcb632 | 2018-07-13 10:42:48 -0700 | [diff] [blame] | 804 | }; |
| 805 | |
| 806 | let file = OpenOptions::new() |
| 807 | .create(true) |
| 808 | .read(true) |
| 809 | .write(true) |
| 810 | .open(&file_path) |
| 811 | .map_err(|e| { |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 812 | error!("Failed opening qcow file at '{}': {}", file_path, e); |
Dylan Reid | 2dcb632 | 2018-07-13 10:42:48 -0700 | [diff] [blame] | 813 | })?; |
| 814 | |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 815 | QcowFile::new(file, size).map_err(|e| { |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 816 | error!("Failed to create qcow file at '{}': {}", file_path, e); |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 817 | })?; |
Dylan Reid | 2dcb632 | 2018-07-13 10:42:48 -0700 | [diff] [blame] | 818 | |
| 819 | Ok(()) |
| 820 | } |
| 821 | |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 822 | fn disk_cmd(mut args: std::env::Args) -> std::result::Result<(), ()> { |
| 823 | if args.len() < 2 { |
| 824 | print_help("crosvm disk", "SUBCOMMAND VM_SOCKET...", &[]); |
| 825 | println!("Manage attached virtual disk devices."); |
| 826 | println!("Subcommands:"); |
| 827 | println!(" resize DISK_INDEX NEW_SIZE VM_SOCKET"); |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame^] | 828 | return Ok(()); |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 829 | } |
| 830 | let subcommand: &str = &args.nth(0).unwrap(); |
| 831 | |
| 832 | let request = match subcommand { |
| 833 | "resize" => { |
| 834 | let disk_index = match args.nth(0).unwrap().parse::<usize>() { |
| 835 | Ok(n) => n, |
| 836 | Err(_) => { |
| 837 | error!("Failed to parse disk index"); |
| 838 | return Err(()); |
| 839 | } |
| 840 | }; |
| 841 | |
| 842 | let new_size = match args.nth(0).unwrap().parse::<u64>() { |
| 843 | Ok(n) => n, |
| 844 | Err(_) => { |
| 845 | error!("Failed to parse disk size"); |
| 846 | return Err(()); |
| 847 | } |
| 848 | }; |
| 849 | |
| 850 | VmRequest::DiskResize { |
| 851 | disk_index, |
| 852 | new_size, |
| 853 | } |
| 854 | } |
| 855 | _ => { |
| 856 | error!("Unknown disk subcommand '{}'", subcommand); |
| 857 | return Err(()); |
| 858 | } |
| 859 | }; |
| 860 | |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame^] | 861 | vms_request(&request, args) |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 862 | } |
| 863 | |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 864 | fn print_usage() { |
| 865 | print_help("crosvm", "[stop|run]", &[]); |
| 866 | println!("Commands:"); |
| 867 | println!(" stop - Stops crosvm instances via their control sockets."); |
| 868 | println!(" run - Start a new crosvm instance."); |
Dylan Reid | 2dcb632 | 2018-07-13 10:42:48 -0700 | [diff] [blame] | 869 | println!(" create_qcow2 - Create a new qcow2 disk image file."); |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 870 | println!(" disk - Manage attached virtual disk devices.") |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 871 | } |
| 872 | |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 873 | fn crosvm_main() -> std::result::Result<(), ()> { |
Stephen Barber | 56fbf09 | 2017-06-29 16:12:14 -0700 | [diff] [blame] | 874 | if let Err(e) = syslog::init() { |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 875 | println!("failed to initialize syslog: {}", e); |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 876 | return Err(()); |
Stephen Barber | 56fbf09 | 2017-06-29 16:12:14 -0700 | [diff] [blame] | 877 | } |
Zach Reizner | 639d967 | 2017-05-01 17:57:18 -0700 | [diff] [blame] | 878 | |
Zach Reizner | b3fa5c9 | 2019-01-28 14:05:23 -0800 | [diff] [blame] | 879 | panic_hook::set_panic_hook(); |
| 880 | |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 881 | let mut args = std::env::args(); |
| 882 | if args.next().is_none() { |
| 883 | error!("expected executable name"); |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 884 | return Err(()); |
Zach Reizner | 639d967 | 2017-05-01 17:57:18 -0700 | [diff] [blame] | 885 | } |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 886 | |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 887 | // Past this point, usage of exit is in danger of leaking zombie processes. |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 888 | let ret = match args.next().as_ref().map(|a| a.as_ref()) { |
| 889 | None => { |
| 890 | print_usage(); |
| 891 | Ok(()) |
| 892 | } |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 893 | Some("stop") => stop_vms(args), |
Zach Reizner | 6a8fdd9 | 2019-01-16 14:38:41 -0800 | [diff] [blame] | 894 | Some("suspend") => suspend_vms(args), |
| 895 | Some("resume") => resume_vms(args), |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 896 | Some("run") => run_vm(args), |
| 897 | Some("balloon") => balloon_vms(args), |
| 898 | Some("create_qcow2") => create_qcow2(args), |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 899 | Some("disk") => disk_cmd(args), |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 900 | Some(c) => { |
| 901 | println!("invalid subcommand: {:?}", c); |
| 902 | print_usage(); |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 903 | Err(()) |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 904 | } |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 905 | }; |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 906 | |
| 907 | // Reap exit status from any child device processes. At this point, all devices should have been |
| 908 | // dropped in the main process and told to shutdown. Try over a period of 100ms, since it may |
| 909 | // take some time for the processes to shut down. |
| 910 | if !wait_all_children() { |
| 911 | // We gave them a chance, and it's too late. |
| 912 | warn!("not all child processes have exited; sending SIGKILL"); |
| 913 | if let Err(e) = kill_process_group() { |
| 914 | // We're now at the mercy of the OS to clean up after us. |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 915 | warn!("unable to kill all child processes: {}", e); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 916 | } |
| 917 | } |
| 918 | |
| 919 | // WARNING: Any code added after this point is not guaranteed to run |
| 920 | // since we may forcibly kill this process (and its children) above. |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 921 | ret |
| 922 | } |
| 923 | |
| 924 | fn main() { |
| 925 | std::process::exit(if crosvm_main().is_ok() { 0 } else { 1 }); |
Zach Reizner | 639d967 | 2017-05-01 17:57:18 -0700 | [diff] [blame] | 926 | } |