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 | |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 7 | pub mod argument; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 8 | pub mod linux; |
Zach Reizner | b3fa5c9 | 2019-01-28 14:05:23 -0800 | [diff] [blame] | 9 | pub mod panic_hook; |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 10 | #[cfg(feature = "plugin")] |
| 11 | pub mod plugin; |
Zach Reizner | 29ad3c7 | 2017-08-04 15:12:58 -0700 | [diff] [blame] | 12 | |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 13 | use std::collections::BTreeMap; |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 14 | use std::fmt; |
| 15 | use std::fs::{File, OpenOptions}; |
Stephen Barber | 2cfc205 | 2017-06-21 15:16:11 -0700 | [diff] [blame] | 16 | use std::net; |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 17 | use std::num::ParseIntError; |
| 18 | use std::os::unix::io::{FromRawFd, RawFd}; |
| 19 | use std::path::{Path, PathBuf}; |
Zach Reizner | 639d967 | 2017-05-01 17:57:18 -0700 | [diff] [blame] | 20 | use std::string::String; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 21 | use std::thread::sleep; |
Stephen Barber | 56fbf09 | 2017-06-29 16:12:14 -0700 | [diff] [blame] | 22 | use std::time::Duration; |
Zach Reizner | 639d967 | 2017-05-01 17:57:18 -0700 | [diff] [blame] | 23 | |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 24 | use devices::{SerialParameters, SerialType}; |
David Tolnay | fe3ef7d | 2019-03-08 15:57:49 -0800 | [diff] [blame] | 25 | use msg_socket::{MsgReceiver, MsgSender, MsgSocket}; |
Dylan Reid | 2dcb632 | 2018-07-13 10:42:48 -0700 | [diff] [blame] | 26 | use qcow::QcowFile; |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 27 | use sys_util::{ |
David Tolnay | 633426a | 2019-04-12 12:18:35 -0700 | [diff] [blame] | 28 | debug, error, getpid, info, kill_process_group, net::UnixSeqpacket, reap_child, syslog, |
| 29 | validate_raw_fd, warn, |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 30 | }; |
Jakub Staron | e7c5905 | 2019-04-09 12:31:14 -0700 | [diff] [blame] | 31 | use vm_control::{ |
Jakub Staron | 1f828d7 | 2019-04-11 12:49:29 -0700 | [diff] [blame] | 32 | BalloonControlCommand, DiskControlCommand, MaybeOwnedFd, UsbControlCommand, UsbControlResult, |
Zach Reizner | aff94ca | 2019-03-18 20:58:31 -0700 | [diff] [blame] | 33 | VmControlRequestSocket, VmRequest, VmResponse, USB_CONTROL_MAX_PORTS, |
Jakub Staron | e7c5905 | 2019-04-09 12:31:14 -0700 | [diff] [blame] | 34 | }; |
Zach Reizner | 639d967 | 2017-05-01 17:57:18 -0700 | [diff] [blame] | 35 | |
David Tolnay | fe3ef7d | 2019-03-08 15:57:49 -0800 | [diff] [blame] | 36 | use crate::argument::{print_help, set_arguments, Argument}; |
| 37 | |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 38 | static SECCOMP_POLICY_DIR: &'static str = "/usr/share/policy/crosvm"; |
Zach Reizner | ee73bf3 | 2017-08-29 16:33:28 -0700 | [diff] [blame] | 39 | |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 40 | struct DiskOption { |
| 41 | path: PathBuf, |
Daniel Verkamp | de9ae03 | 2018-08-09 16:26:59 -0700 | [diff] [blame] | 42 | read_only: bool, |
Dylan Reid | f463bc1 | 2017-07-12 19:24:57 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Daniel Verkamp | bd1a084 | 2019-01-08 15:50:34 -0800 | [diff] [blame] | 45 | #[allow(dead_code)] |
Chirantan Ekbote | d41d726 | 2018-11-16 16:37:45 -0800 | [diff] [blame] | 46 | struct BindMount { |
| 47 | src: PathBuf, |
| 48 | dst: PathBuf, |
| 49 | writable: bool, |
| 50 | } |
| 51 | |
Dmitry Torokhov | 1a6262b | 2019-03-01 00:34:03 -0800 | [diff] [blame] | 52 | #[allow(dead_code)] |
| 53 | struct GidMap { |
| 54 | inner: libc::gid_t, |
| 55 | outer: libc::gid_t, |
| 56 | count: u32, |
| 57 | } |
| 58 | |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 59 | const DEFAULT_TOUCH_DEVICE_WIDTH: u32 = 800; |
| 60 | const DEFAULT_TOUCH_DEVICE_HEIGHT: u32 = 1280; |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 61 | |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 62 | struct TouchDeviceOption { |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 63 | path: PathBuf, |
| 64 | width: u32, |
| 65 | height: u32, |
| 66 | } |
| 67 | |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 68 | impl TouchDeviceOption { |
| 69 | fn new(path: PathBuf) -> TouchDeviceOption { |
| 70 | TouchDeviceOption { |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 71 | path, |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 72 | width: DEFAULT_TOUCH_DEVICE_WIDTH, |
| 73 | height: DEFAULT_TOUCH_DEVICE_HEIGHT, |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 78 | pub struct Config { |
| 79 | vcpu_count: Option<u32>, |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 80 | vcpu_affinity: Vec<usize>, |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 81 | memory: Option<usize>, |
| 82 | kernel_path: PathBuf, |
Tristan Muntsinger | 4133b01 | 2018-12-21 16:01:56 -0800 | [diff] [blame] | 83 | android_fstab: Option<PathBuf>, |
Daniel Verkamp | e403f5c | 2018-12-11 16:29:26 -0800 | [diff] [blame] | 84 | initrd_path: Option<PathBuf>, |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 85 | params: Vec<String>, |
| 86 | socket_path: Option<PathBuf>, |
| 87 | plugin: Option<PathBuf>, |
| 88 | plugin_root: Option<PathBuf>, |
Chirantan Ekbote | d41d726 | 2018-11-16 16:37:45 -0800 | [diff] [blame] | 89 | plugin_mounts: Vec<BindMount>, |
Dmitry Torokhov | 1a6262b | 2019-03-01 00:34:03 -0800 | [diff] [blame] | 90 | plugin_gid_maps: Vec<GidMap>, |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 91 | disks: Vec<DiskOption>, |
Stephen Barber | 2cfc205 | 2017-06-21 15:16:11 -0700 | [diff] [blame] | 92 | host_ip: Option<net::Ipv4Addr>, |
| 93 | netmask: Option<net::Ipv4Addr>, |
Stephen Barber | 308ff60 | 2018-02-13 22:47:07 -0800 | [diff] [blame] | 94 | mac_address: Option<net_util::MacAddress>, |
Stephen Barber | 5e77e88 | 2017-08-07 17:13:38 -0700 | [diff] [blame] | 95 | vhost_net: bool, |
Jorge E. Moreira | b795280 | 2019-02-12 16:43:05 -0800 | [diff] [blame] | 96 | tap_fd: Vec<RawFd>, |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 97 | cid: Option<u64>, |
Stephen Barber | 28a5a61 | 2017-10-20 17:15:30 -0700 | [diff] [blame] | 98 | wayland_socket_path: Option<PathBuf>, |
David Reveman | 52ba4e5 | 2018-04-22 21:42:09 -0400 | [diff] [blame] | 99 | wayland_dmabuf: bool, |
Chirantan Ekbote | ebd5681 | 2018-04-16 19:32:04 -0700 | [diff] [blame] | 100 | shared_dirs: Vec<(PathBuf, String)>, |
Lepton Wu | 9105e9f | 2019-03-14 11:38:31 -0700 | [diff] [blame] | 101 | sandbox: bool, |
Dylan Reid | d0c9adc | 2017-10-02 19:04:50 -0700 | [diff] [blame] | 102 | seccomp_policy_dir: PathBuf, |
Zach Reizner | 3a8100a | 2017-09-13 19:15:43 -0700 | [diff] [blame] | 103 | gpu: bool, |
David Tolnay | 43f8e21 | 2019-02-13 17:28:16 -0800 | [diff] [blame] | 104 | software_tpm: bool, |
paulhsia | f052cfe | 2019-01-22 15:22:25 +0800 | [diff] [blame] | 105 | cras_audio: bool, |
Dylan Reid | 3082e8e | 2019-01-07 10:33:48 -0800 | [diff] [blame] | 106 | null_audio: bool, |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 107 | serial_parameters: BTreeMap<u8, SerialParameters>, |
| 108 | syslog_tag: Option<String>, |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 109 | virtio_single_touch: Option<TouchDeviceOption>, |
| 110 | virtio_trackpad: Option<TouchDeviceOption>, |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 111 | virtio_mouse: Option<PathBuf>, |
| 112 | virtio_keyboard: Option<PathBuf>, |
| 113 | virtio_input_evdevs: Vec<PathBuf>, |
Miriam Zimmerman | 26ac928 | 2019-01-29 21:21:48 -0800 | [diff] [blame] | 114 | split_irqchip: bool, |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Dylan Reid | d0c9adc | 2017-10-02 19:04:50 -0700 | [diff] [blame] | 117 | impl Default for Config { |
| 118 | fn default() -> Config { |
| 119 | Config { |
Dylan Reid | d0c9adc | 2017-10-02 19:04:50 -0700 | [diff] [blame] | 120 | vcpu_count: None, |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 121 | vcpu_affinity: Vec::new(), |
Dylan Reid | d0c9adc | 2017-10-02 19:04:50 -0700 | [diff] [blame] | 122 | memory: None, |
| 123 | kernel_path: PathBuf::default(), |
Tristan Muntsinger | 4133b01 | 2018-12-21 16:01:56 -0800 | [diff] [blame] | 124 | android_fstab: None, |
Daniel Verkamp | e403f5c | 2018-12-11 16:29:26 -0800 | [diff] [blame] | 125 | initrd_path: None, |
Zach Reizner | bb67871 | 2018-01-30 18:13:04 -0800 | [diff] [blame] | 126 | params: Vec::new(), |
Dylan Reid | d0c9adc | 2017-10-02 19:04:50 -0700 | [diff] [blame] | 127 | socket_path: None, |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 128 | plugin: None, |
Dmitry Torokhov | 0f1770d | 2018-05-11 10:57:16 -0700 | [diff] [blame] | 129 | plugin_root: None, |
Chirantan Ekbote | d41d726 | 2018-11-16 16:37:45 -0800 | [diff] [blame] | 130 | plugin_mounts: Vec::new(), |
Dmitry Torokhov | 1a6262b | 2019-03-01 00:34:03 -0800 | [diff] [blame] | 131 | plugin_gid_maps: Vec::new(), |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 132 | disks: Vec::new(), |
| 133 | host_ip: None, |
| 134 | netmask: None, |
| 135 | mac_address: None, |
| 136 | vhost_net: false, |
Jorge E. Moreira | b795280 | 2019-02-12 16:43:05 -0800 | [diff] [blame] | 137 | tap_fd: Vec::new(), |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 138 | cid: None, |
| 139 | gpu: false, |
David Tolnay | 43f8e21 | 2019-02-13 17:28:16 -0800 | [diff] [blame] | 140 | software_tpm: false, |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 141 | wayland_socket_path: None, |
| 142 | wayland_dmabuf: false, |
| 143 | shared_dirs: Vec::new(), |
Lepton Wu | 9105e9f | 2019-03-14 11:38:31 -0700 | [diff] [blame] | 144 | sandbox: !cfg!(feature = "default-no-sandbox"), |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 145 | seccomp_policy_dir: PathBuf::from(SECCOMP_POLICY_DIR), |
paulhsia | f052cfe | 2019-01-22 15:22:25 +0800 | [diff] [blame] | 146 | cras_audio: false, |
Dylan Reid | 3082e8e | 2019-01-07 10:33:48 -0800 | [diff] [blame] | 147 | null_audio: false, |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 148 | serial_parameters: BTreeMap::new(), |
| 149 | syslog_tag: None, |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 150 | virtio_single_touch: None, |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 151 | virtio_trackpad: None, |
| 152 | virtio_mouse: None, |
| 153 | virtio_keyboard: None, |
| 154 | virtio_input_evdevs: Vec::new(), |
Miriam Zimmerman | 26ac928 | 2019-01-29 21:21:48 -0800 | [diff] [blame] | 155 | split_irqchip: false, |
Dylan Reid | d0c9adc | 2017-10-02 19:04:50 -0700 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
Stephen Barber | a00753b | 2017-07-18 13:57:26 -0700 | [diff] [blame] | 160 | // Wait for all children to exit. Return true if they have all exited, false |
| 161 | // otherwise. |
| 162 | fn wait_all_children() -> bool { |
Stephen Barber | 49dd2e2 | 2018-10-29 18:29:58 -0700 | [diff] [blame] | 163 | const CHILD_WAIT_MAX_ITER: isize = 100; |
Stephen Barber | a00753b | 2017-07-18 13:57:26 -0700 | [diff] [blame] | 164 | const CHILD_WAIT_MS: u64 = 10; |
| 165 | for _ in 0..CHILD_WAIT_MAX_ITER { |
Stephen Barber | a00753b | 2017-07-18 13:57:26 -0700 | [diff] [blame] | 166 | loop { |
Zach Reizner | 56158c8 | 2017-08-24 13:50:14 -0700 | [diff] [blame] | 167 | match reap_child() { |
| 168 | Ok(0) => break, |
| 169 | // We expect ECHILD which indicates that there were no children left. |
| 170 | Err(e) if e.errno() == libc::ECHILD => return true, |
| 171 | Err(e) => { |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 172 | warn!("error while waiting for children: {}", e); |
Zach Reizner | 56158c8 | 2017-08-24 13:50:14 -0700 | [diff] [blame] | 173 | return false; |
Stephen Barber | a00753b | 2017-07-18 13:57:26 -0700 | [diff] [blame] | 174 | } |
Zach Reizner | 56158c8 | 2017-08-24 13:50:14 -0700 | [diff] [blame] | 175 | // We reaped one child, so continue reaping. |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 176 | _ => {} |
Stephen Barber | a00753b | 2017-07-18 13:57:26 -0700 | [diff] [blame] | 177 | } |
| 178 | } |
Zach Reizner | 56158c8 | 2017-08-24 13:50:14 -0700 | [diff] [blame] | 179 | // There's no timeout option for waitpid which reap_child calls internally, so our only |
| 180 | // recourse is to sleep while waiting for the children to exit. |
Stephen Barber | a00753b | 2017-07-18 13:57:26 -0700 | [diff] [blame] | 181 | sleep(Duration::from_millis(CHILD_WAIT_MS)); |
| 182 | } |
| 183 | |
| 184 | // 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] | 185 | false |
Stephen Barber | a00753b | 2017-07-18 13:57:26 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 188 | /// Parse a comma-separated list of CPU numbers and ranges and convert it to a Vec of CPU numbers. |
| 189 | fn parse_cpu_set(s: &str) -> argument::Result<Vec<usize>> { |
| 190 | let mut cpuset = Vec::new(); |
| 191 | for part in s.split(',') { |
| 192 | let range: Vec<&str> = part.split('-').collect(); |
| 193 | if range.len() == 0 || range.len() > 2 { |
| 194 | return Err(argument::Error::InvalidValue { |
| 195 | value: part.to_owned(), |
| 196 | expected: "invalid list syntax", |
| 197 | }); |
| 198 | } |
| 199 | let first_cpu: usize = range[0] |
| 200 | .parse() |
| 201 | .map_err(|_| argument::Error::InvalidValue { |
| 202 | value: part.to_owned(), |
| 203 | expected: "CPU index must be a non-negative integer", |
| 204 | })?; |
| 205 | let last_cpu: usize = if range.len() == 2 { |
| 206 | range[1] |
| 207 | .parse() |
| 208 | .map_err(|_| argument::Error::InvalidValue { |
| 209 | value: part.to_owned(), |
| 210 | expected: "CPU index must be a non-negative integer", |
| 211 | })? |
| 212 | } else { |
| 213 | first_cpu |
| 214 | }; |
| 215 | |
| 216 | if last_cpu < first_cpu { |
| 217 | return Err(argument::Error::InvalidValue { |
| 218 | value: part.to_owned(), |
| 219 | expected: "CPU ranges must be from low to high", |
| 220 | }); |
| 221 | } |
| 222 | |
| 223 | for cpu in first_cpu..=last_cpu { |
| 224 | cpuset.push(cpu); |
| 225 | } |
| 226 | } |
| 227 | Ok(cpuset) |
| 228 | } |
| 229 | |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 230 | fn parse_serial_options(s: &str) -> argument::Result<SerialParameters> { |
| 231 | let mut serial_setting = SerialParameters { |
| 232 | type_: SerialType::Sink, |
| 233 | path: None, |
| 234 | num: 0, |
| 235 | console: false, |
| 236 | }; |
| 237 | |
| 238 | let opts = s |
| 239 | .split(",") |
| 240 | .map(|frag| frag.split("=")) |
| 241 | .map(|mut kv| (kv.next().unwrap_or(""), kv.next().unwrap_or(""))); |
| 242 | |
| 243 | for (k, v) in opts { |
| 244 | match k { |
| 245 | "type" => { |
| 246 | serial_setting.type_ = v |
| 247 | .parse::<SerialType>() |
| 248 | .map_err(|e| argument::Error::UnknownArgument(format!("{}", e)))? |
| 249 | } |
| 250 | "num" => { |
| 251 | let num = v.parse::<u8>().map_err(|e| { |
| 252 | argument::Error::Syntax(format!("serial device number is not parsable: {}", e)) |
| 253 | })?; |
| 254 | if num < 1 || num > 4 { |
| 255 | return Err(argument::Error::InvalidValue { |
| 256 | value: num.to_string(), |
| 257 | expected: "Serial port num must be between 1 - 4", |
| 258 | }); |
| 259 | } |
| 260 | serial_setting.num = num; |
| 261 | } |
| 262 | "console" => { |
| 263 | serial_setting.console = v.parse::<bool>().map_err(|e| { |
| 264 | argument::Error::Syntax(format!( |
| 265 | "serial device console is not parseable: {}", |
| 266 | e |
| 267 | )) |
| 268 | })? |
| 269 | } |
Jorge E. Moreira | 9c9e0e7 | 2019-05-17 13:57:04 -0700 | [diff] [blame] | 270 | "path" => serial_setting.path = Some(PathBuf::from(v)), |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 271 | _ => { |
| 272 | return Err(argument::Error::UnknownArgument(format!( |
| 273 | "serial parameter {}", |
| 274 | k |
| 275 | ))); |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | Ok(serial_setting) |
| 281 | } |
| 282 | |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 283 | fn set_argument(cfg: &mut Config, name: &str, value: Option<&str>) -> argument::Result<()> { |
| 284 | match name { |
| 285 | "" => { |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 286 | if cfg.plugin.is_some() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 287 | return Err(argument::Error::TooManyArguments( |
| 288 | "`plugin` can not be used with kernel".to_owned(), |
| 289 | )); |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 290 | } else if !cfg.kernel_path.as_os_str().is_empty() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 291 | return Err(argument::Error::TooManyArguments( |
| 292 | "expected exactly one kernel path".to_owned(), |
| 293 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 294 | } else { |
| 295 | let kernel_path = PathBuf::from(value.unwrap()); |
| 296 | if !kernel_path.exists() { |
| 297 | return Err(argument::Error::InvalidValue { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 298 | value: value.unwrap().to_owned(), |
| 299 | expected: "this kernel path does not exist", |
| 300 | }); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 301 | } |
| 302 | cfg.kernel_path = kernel_path; |
| 303 | } |
| 304 | } |
Tristan Muntsinger | 4133b01 | 2018-12-21 16:01:56 -0800 | [diff] [blame] | 305 | "android-fstab" => { |
| 306 | if cfg.android_fstab.is_some() |
| 307 | && !cfg.android_fstab.as_ref().unwrap().as_os_str().is_empty() |
| 308 | { |
| 309 | return Err(argument::Error::TooManyArguments( |
| 310 | "expected exactly one android fstab path".to_owned(), |
| 311 | )); |
| 312 | } else { |
| 313 | let android_fstab = PathBuf::from(value.unwrap()); |
| 314 | if !android_fstab.exists() { |
| 315 | return Err(argument::Error::InvalidValue { |
| 316 | value: value.unwrap().to_owned(), |
| 317 | expected: "this android fstab path does not exist", |
| 318 | }); |
| 319 | } |
| 320 | cfg.android_fstab = Some(android_fstab); |
| 321 | } |
| 322 | } |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 323 | "params" => { |
Zach Reizner | bb67871 | 2018-01-30 18:13:04 -0800 | [diff] [blame] | 324 | cfg.params.push(value.unwrap().to_owned()); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 325 | } |
| 326 | "cpus" => { |
| 327 | if cfg.vcpu_count.is_some() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 328 | return Err(argument::Error::TooManyArguments( |
| 329 | "`cpus` already given".to_owned(), |
| 330 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 331 | } |
| 332 | cfg.vcpu_count = |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 333 | Some( |
| 334 | value |
| 335 | .unwrap() |
| 336 | .parse() |
| 337 | .map_err(|_| argument::Error::InvalidValue { |
| 338 | value: value.unwrap().to_owned(), |
| 339 | expected: "this value for `cpus` needs to be integer", |
| 340 | })?, |
| 341 | ) |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 342 | } |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 343 | "cpu-affinity" => { |
| 344 | if cfg.vcpu_affinity.len() != 0 { |
| 345 | return Err(argument::Error::TooManyArguments( |
| 346 | "`cpu-affinity` already given".to_owned(), |
| 347 | )); |
| 348 | } |
| 349 | cfg.vcpu_affinity = parse_cpu_set(value.unwrap())?; |
| 350 | } |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 351 | "mem" => { |
| 352 | if cfg.memory.is_some() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 353 | return Err(argument::Error::TooManyArguments( |
| 354 | "`mem` already given".to_owned(), |
| 355 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 356 | } |
| 357 | cfg.memory = |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 358 | Some( |
| 359 | value |
| 360 | .unwrap() |
| 361 | .parse() |
| 362 | .map_err(|_| argument::Error::InvalidValue { |
| 363 | value: value.unwrap().to_owned(), |
| 364 | expected: "this value for `mem` needs to be integer", |
| 365 | })?, |
| 366 | ) |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 367 | } |
paulhsia | f052cfe | 2019-01-22 15:22:25 +0800 | [diff] [blame] | 368 | "cras-audio" => { |
| 369 | cfg.cras_audio = true; |
| 370 | } |
Dylan Reid | 3082e8e | 2019-01-07 10:33:48 -0800 | [diff] [blame] | 371 | "null-audio" => { |
| 372 | cfg.null_audio = true; |
| 373 | } |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 374 | "serial" => { |
| 375 | let serial_params = parse_serial_options(value.unwrap())?; |
| 376 | let num = serial_params.num; |
| 377 | if cfg.serial_parameters.contains_key(&num) { |
| 378 | return Err(argument::Error::TooManyArguments(format!( |
| 379 | "serial num {}", |
| 380 | num |
| 381 | ))); |
| 382 | } |
| 383 | |
| 384 | if serial_params.console { |
| 385 | for (_, params) in &cfg.serial_parameters { |
| 386 | if params.console { |
| 387 | return Err(argument::Error::TooManyArguments(format!( |
| 388 | "serial device {} already set as console", |
| 389 | params.num |
| 390 | ))); |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | cfg.serial_parameters.insert(num, serial_params); |
| 396 | } |
| 397 | "syslog-tag" => { |
| 398 | if cfg.syslog_tag.is_some() { |
| 399 | return Err(argument::Error::TooManyArguments( |
| 400 | "`syslog-tag` already given".to_owned(), |
| 401 | )); |
| 402 | } |
| 403 | syslog::set_proc_name(value.unwrap()); |
| 404 | cfg.syslog_tag = Some(value.unwrap().to_owned()); |
| 405 | } |
Dylan Reid | 88624f8 | 2018-01-11 09:20:16 -0800 | [diff] [blame] | 406 | "root" | "disk" | "rwdisk" | "qcow" | "rwqcow" => { |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 407 | let disk_path = PathBuf::from(value.unwrap()); |
| 408 | if !disk_path.exists() { |
| 409 | return Err(argument::Error::InvalidValue { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 410 | value: value.unwrap().to_owned(), |
| 411 | expected: "this disk path does not exist", |
| 412 | }); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 413 | } |
| 414 | if name == "root" { |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 415 | if cfg.disks.len() >= 26 { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 416 | return Err(argument::Error::TooManyArguments( |
| 417 | "ran out of letters for to assign to root disk".to_owned(), |
| 418 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 419 | } |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 420 | cfg.params.push(format!( |
| 421 | "root=/dev/vd{} ro", |
David Tolnay | 5bbbf61 | 2018-12-01 17:49:30 -0800 | [diff] [blame] | 422 | char::from(b'a' + cfg.disks.len() as u8) |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 423 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 424 | } |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 425 | cfg.disks.push(DiskOption { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 426 | path: disk_path, |
| 427 | read_only: !name.starts_with("rw"), |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 428 | }); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 429 | } |
| 430 | "host_ip" => { |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 431 | if cfg.host_ip.is_some() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 432 | return Err(argument::Error::TooManyArguments( |
| 433 | "`host_ip` already given".to_owned(), |
| 434 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 435 | } |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 436 | cfg.host_ip = |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 437 | Some( |
| 438 | value |
| 439 | .unwrap() |
| 440 | .parse() |
| 441 | .map_err(|_| argument::Error::InvalidValue { |
| 442 | value: value.unwrap().to_owned(), |
| 443 | expected: "`host_ip` needs to be in the form \"x.x.x.x\"", |
| 444 | })?, |
| 445 | ) |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 446 | } |
| 447 | "netmask" => { |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 448 | if cfg.netmask.is_some() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 449 | return Err(argument::Error::TooManyArguments( |
| 450 | "`netmask` already given".to_owned(), |
| 451 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 452 | } |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 453 | cfg.netmask = |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 454 | Some( |
| 455 | value |
| 456 | .unwrap() |
| 457 | .parse() |
| 458 | .map_err(|_| argument::Error::InvalidValue { |
| 459 | value: value.unwrap().to_owned(), |
| 460 | expected: "`netmask` needs to be in the form \"x.x.x.x\"", |
| 461 | })?, |
| 462 | ) |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 463 | } |
| 464 | "mac" => { |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 465 | if cfg.mac_address.is_some() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 466 | return Err(argument::Error::TooManyArguments( |
| 467 | "`mac` already given".to_owned(), |
| 468 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 469 | } |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 470 | cfg.mac_address = |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 471 | Some( |
| 472 | value |
| 473 | .unwrap() |
| 474 | .parse() |
| 475 | .map_err(|_| argument::Error::InvalidValue { |
| 476 | value: value.unwrap().to_owned(), |
| 477 | expected: "`mac` needs to be in the form \"XX:XX:XX:XX:XX:XX\"", |
| 478 | })?, |
| 479 | ) |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 480 | } |
Stephen Barber | 28a5a61 | 2017-10-20 17:15:30 -0700 | [diff] [blame] | 481 | "wayland-sock" => { |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 482 | if cfg.wayland_socket_path.is_some() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 483 | return Err(argument::Error::TooManyArguments( |
| 484 | "`wayland-sock` already given".to_owned(), |
| 485 | )); |
Stephen Barber | 28a5a61 | 2017-10-20 17:15:30 -0700 | [diff] [blame] | 486 | } |
| 487 | let wayland_socket_path = PathBuf::from(value.unwrap()); |
| 488 | if !wayland_socket_path.exists() { |
| 489 | return Err(argument::Error::InvalidValue { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 490 | value: value.unwrap().to_string(), |
| 491 | expected: "Wayland socket does not exist", |
| 492 | }); |
Stephen Barber | 28a5a61 | 2017-10-20 17:15:30 -0700 | [diff] [blame] | 493 | } |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 494 | cfg.wayland_socket_path = Some(wayland_socket_path); |
Stephen Barber | 28a5a61 | 2017-10-20 17:15:30 -0700 | [diff] [blame] | 495 | } |
David Reveman | 52ba4e5 | 2018-04-22 21:42:09 -0400 | [diff] [blame] | 496 | #[cfg(feature = "wl-dmabuf")] |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 497 | "wayland-dmabuf" => cfg.wayland_dmabuf = true, |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 498 | "socket" => { |
| 499 | if cfg.socket_path.is_some() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 500 | return Err(argument::Error::TooManyArguments( |
| 501 | "`socket` already given".to_owned(), |
| 502 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 503 | } |
| 504 | let mut socket_path = PathBuf::from(value.unwrap()); |
| 505 | if socket_path.is_dir() { |
| 506 | socket_path.push(format!("crosvm-{}.sock", getpid())); |
| 507 | } |
| 508 | if socket_path.exists() { |
| 509 | return Err(argument::Error::InvalidValue { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 510 | value: socket_path.to_string_lossy().into_owned(), |
| 511 | expected: "this socket path already exists", |
| 512 | }); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 513 | } |
| 514 | cfg.socket_path = Some(socket_path); |
| 515 | } |
Dylan Reid | d0c9adc | 2017-10-02 19:04:50 -0700 | [diff] [blame] | 516 | "disable-sandbox" => { |
Lepton Wu | 9105e9f | 2019-03-14 11:38:31 -0700 | [diff] [blame] | 517 | cfg.sandbox = false; |
Dylan Reid | d0c9adc | 2017-10-02 19:04:50 -0700 | [diff] [blame] | 518 | } |
Chirantan Ekbote | 88f9cba | 2017-08-28 09:51:18 -0700 | [diff] [blame] | 519 | "cid" => { |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 520 | if cfg.cid.is_some() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 521 | return Err(argument::Error::TooManyArguments( |
| 522 | "`cid` alread given".to_owned(), |
| 523 | )); |
Chirantan Ekbote | 88f9cba | 2017-08-28 09:51:18 -0700 | [diff] [blame] | 524 | } |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 525 | cfg.cid = Some( |
| 526 | value |
| 527 | .unwrap() |
| 528 | .parse() |
| 529 | .map_err(|_| argument::Error::InvalidValue { |
| 530 | value: value.unwrap().to_owned(), |
| 531 | expected: "this value for `cid` must be an unsigned integer", |
| 532 | })?, |
| 533 | ); |
Chirantan Ekbote | 88f9cba | 2017-08-28 09:51:18 -0700 | [diff] [blame] | 534 | } |
Chirantan Ekbote | ebd5681 | 2018-04-16 19:32:04 -0700 | [diff] [blame] | 535 | "shared-dir" => { |
| 536 | // Formatted as <src:tag>. |
| 537 | let param = value.unwrap(); |
| 538 | let mut components = param.splitn(2, ':'); |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 539 | let src = |
| 540 | PathBuf::from( |
| 541 | components |
| 542 | .next() |
| 543 | .ok_or_else(|| argument::Error::InvalidValue { |
| 544 | value: param.to_owned(), |
| 545 | expected: "missing source path for `shared-dir`", |
| 546 | })?, |
| 547 | ); |
| 548 | let tag = components |
| 549 | .next() |
| 550 | .ok_or_else(|| argument::Error::InvalidValue { |
Chirantan Ekbote | ebd5681 | 2018-04-16 19:32:04 -0700 | [diff] [blame] | 551 | value: param.to_owned(), |
| 552 | expected: "missing tag for `shared-dir`", |
David Tolnay | 2bac1e7 | 2018-12-12 14:33:42 -0800 | [diff] [blame] | 553 | })? |
| 554 | .to_owned(); |
Chirantan Ekbote | ebd5681 | 2018-04-16 19:32:04 -0700 | [diff] [blame] | 555 | |
| 556 | if !src.is_dir() { |
| 557 | return Err(argument::Error::InvalidValue { |
| 558 | value: param.to_owned(), |
| 559 | expected: "source path for `shared-dir` must be a directory", |
| 560 | }); |
| 561 | } |
| 562 | |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 563 | cfg.shared_dirs.push((src, tag)); |
Chirantan Ekbote | ebd5681 | 2018-04-16 19:32:04 -0700 | [diff] [blame] | 564 | } |
Dylan Reid | e026ef0 | 2017-10-02 19:03:52 -0700 | [diff] [blame] | 565 | "seccomp-policy-dir" => { |
Dylan Reid | d0c9adc | 2017-10-02 19:04:50 -0700 | [diff] [blame] | 566 | // `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] | 567 | cfg.seccomp_policy_dir = PathBuf::from(value.unwrap()); |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 568 | } |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 569 | "plugin" => { |
| 570 | if !cfg.kernel_path.as_os_str().is_empty() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 571 | return Err(argument::Error::TooManyArguments( |
| 572 | "`plugin` can not be used with kernel".to_owned(), |
| 573 | )); |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 574 | } else if cfg.plugin.is_some() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 575 | return Err(argument::Error::TooManyArguments( |
| 576 | "`plugin` already given".to_owned(), |
| 577 | )); |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 578 | } |
Zach Reizner | cc30d58 | 2018-01-23 21:16:42 -0800 | [diff] [blame] | 579 | let plugin = PathBuf::from(value.unwrap().to_owned()); |
| 580 | if plugin.is_relative() { |
| 581 | return Err(argument::Error::InvalidValue { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 582 | value: plugin.to_string_lossy().into_owned(), |
| 583 | expected: "the plugin path must be an absolute path", |
| 584 | }); |
Zach Reizner | cc30d58 | 2018-01-23 21:16:42 -0800 | [diff] [blame] | 585 | } |
| 586 | cfg.plugin = Some(plugin); |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 587 | } |
Dmitry Torokhov | 0f1770d | 2018-05-11 10:57:16 -0700 | [diff] [blame] | 588 | "plugin-root" => { |
| 589 | cfg.plugin_root = Some(PathBuf::from(value.unwrap().to_owned())); |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 590 | } |
Chirantan Ekbote | d41d726 | 2018-11-16 16:37:45 -0800 | [diff] [blame] | 591 | "plugin-mount" => { |
| 592 | let components: Vec<&str> = value.unwrap().split(":").collect(); |
| 593 | if components.len() != 3 { |
| 594 | return Err(argument::Error::InvalidValue { |
| 595 | value: value.unwrap().to_owned(), |
| 596 | expected: |
| 597 | "`plugin-mount` must have exactly 3 components: <src>:<dst>:<writable>", |
| 598 | }); |
| 599 | } |
| 600 | |
| 601 | let src = PathBuf::from(components[0]); |
| 602 | if src.is_relative() { |
| 603 | return Err(argument::Error::InvalidValue { |
| 604 | value: components[0].to_owned(), |
| 605 | expected: "the source path for `plugin-mount` must be absolute", |
| 606 | }); |
| 607 | } |
| 608 | if !src.exists() { |
| 609 | return Err(argument::Error::InvalidValue { |
| 610 | value: components[0].to_owned(), |
| 611 | expected: "the source path for `plugin-mount` does not exist", |
| 612 | }); |
| 613 | } |
| 614 | |
| 615 | let dst = PathBuf::from(components[1]); |
| 616 | if dst.is_relative() { |
| 617 | return Err(argument::Error::InvalidValue { |
| 618 | value: components[1].to_owned(), |
| 619 | expected: "the destination path for `plugin-mount` must be absolute", |
| 620 | }); |
| 621 | } |
| 622 | |
| 623 | let writable: bool = |
| 624 | components[2] |
| 625 | .parse() |
| 626 | .map_err(|_| argument::Error::InvalidValue { |
| 627 | value: components[2].to_owned(), |
| 628 | expected: "the <writable> component for `plugin-mount` is not valid bool", |
| 629 | })?; |
| 630 | |
| 631 | cfg.plugin_mounts.push(BindMount { src, dst, writable }); |
| 632 | } |
Dmitry Torokhov | 1a6262b | 2019-03-01 00:34:03 -0800 | [diff] [blame] | 633 | "plugin-gid-map" => { |
| 634 | let components: Vec<&str> = value.unwrap().split(":").collect(); |
| 635 | if components.len() != 3 { |
| 636 | return Err(argument::Error::InvalidValue { |
| 637 | value: value.unwrap().to_owned(), |
| 638 | expected: |
| 639 | "`plugin-gid-map` must have exactly 3 components: <inner>:<outer>:<count>", |
| 640 | }); |
| 641 | } |
| 642 | |
| 643 | let inner: libc::gid_t = |
| 644 | components[0] |
| 645 | .parse() |
| 646 | .map_err(|_| argument::Error::InvalidValue { |
| 647 | value: components[0].to_owned(), |
| 648 | expected: "the <inner> component for `plugin-gid-map` is not valid gid", |
| 649 | })?; |
| 650 | |
| 651 | let outer: libc::gid_t = |
| 652 | components[1] |
| 653 | .parse() |
| 654 | .map_err(|_| argument::Error::InvalidValue { |
| 655 | value: components[1].to_owned(), |
| 656 | expected: "the <outer> component for `plugin-gid-map` is not valid gid", |
| 657 | })?; |
| 658 | |
| 659 | let count: u32 = components[2] |
| 660 | .parse() |
| 661 | .map_err(|_| argument::Error::InvalidValue { |
| 662 | value: components[2].to_owned(), |
| 663 | expected: "the <count> component for `plugin-gid-map` is not valid number", |
| 664 | })?; |
| 665 | |
| 666 | cfg.plugin_gid_maps.push(GidMap { |
| 667 | inner, |
| 668 | outer, |
| 669 | count, |
| 670 | }); |
| 671 | } |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 672 | "vhost-net" => cfg.vhost_net = true, |
Chirantan Ekbote | 5f78721 | 2018-05-31 15:31:31 -0700 | [diff] [blame] | 673 | "tap-fd" => { |
Jorge E. Moreira | b795280 | 2019-02-12 16:43:05 -0800 | [diff] [blame] | 674 | cfg.tap_fd.push( |
| 675 | value |
| 676 | .unwrap() |
| 677 | .parse() |
| 678 | .map_err(|_| argument::Error::InvalidValue { |
| 679 | value: value.unwrap().to_owned(), |
| 680 | expected: "this value for `tap-fd` must be an unsigned integer", |
| 681 | })?, |
| 682 | ); |
Chirantan Ekbote | 5f78721 | 2018-05-31 15:31:31 -0700 | [diff] [blame] | 683 | } |
Zach Reizner | 3a8100a | 2017-09-13 19:15:43 -0700 | [diff] [blame] | 684 | "gpu" => { |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 685 | cfg.gpu = true; |
Zach Reizner | 3a8100a | 2017-09-13 19:15:43 -0700 | [diff] [blame] | 686 | } |
David Tolnay | 43f8e21 | 2019-02-13 17:28:16 -0800 | [diff] [blame] | 687 | "software-tpm" => { |
| 688 | cfg.software_tpm = true; |
| 689 | } |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 690 | "single-touch" => { |
| 691 | if cfg.virtio_single_touch.is_some() { |
| 692 | return Err(argument::Error::TooManyArguments( |
| 693 | "`single-touch` already given".to_owned(), |
| 694 | )); |
| 695 | } |
| 696 | let mut it = value.unwrap().split(":"); |
| 697 | |
| 698 | let mut single_touch_spec = |
| 699 | TouchDeviceOption::new(PathBuf::from(it.next().unwrap().to_owned())); |
| 700 | if let Some(width) = it.next() { |
| 701 | single_touch_spec.width = width.trim().parse().unwrap(); |
| 702 | } |
| 703 | if let Some(height) = it.next() { |
| 704 | single_touch_spec.height = height.trim().parse().unwrap(); |
| 705 | } |
| 706 | |
| 707 | cfg.virtio_single_touch = Some(single_touch_spec); |
| 708 | } |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 709 | "trackpad" => { |
| 710 | if cfg.virtio_trackpad.is_some() { |
| 711 | return Err(argument::Error::TooManyArguments( |
| 712 | "`trackpad` already given".to_owned(), |
| 713 | )); |
| 714 | } |
| 715 | let mut it = value.unwrap().split(":"); |
| 716 | |
| 717 | let mut trackpad_spec = |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 718 | TouchDeviceOption::new(PathBuf::from(it.next().unwrap().to_owned())); |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 719 | if let Some(width) = it.next() { |
| 720 | trackpad_spec.width = width.trim().parse().unwrap(); |
| 721 | } |
| 722 | if let Some(height) = it.next() { |
| 723 | trackpad_spec.height = height.trim().parse().unwrap(); |
| 724 | } |
| 725 | |
| 726 | cfg.virtio_trackpad = Some(trackpad_spec); |
| 727 | } |
| 728 | "mouse" => { |
| 729 | if cfg.virtio_mouse.is_some() { |
| 730 | return Err(argument::Error::TooManyArguments( |
| 731 | "`mouse` already given".to_owned(), |
| 732 | )); |
| 733 | } |
| 734 | cfg.virtio_mouse = Some(PathBuf::from(value.unwrap().to_owned())); |
| 735 | } |
| 736 | "keyboard" => { |
| 737 | if cfg.virtio_keyboard.is_some() { |
| 738 | return Err(argument::Error::TooManyArguments( |
| 739 | "`keyboard` already given".to_owned(), |
| 740 | )); |
| 741 | } |
| 742 | cfg.virtio_keyboard = Some(PathBuf::from(value.unwrap().to_owned())); |
| 743 | } |
| 744 | "evdev" => { |
| 745 | let dev_path = PathBuf::from(value.unwrap()); |
| 746 | if !dev_path.exists() { |
| 747 | return Err(argument::Error::InvalidValue { |
| 748 | value: value.unwrap().to_owned(), |
| 749 | expected: "this input device path does not exist", |
| 750 | }); |
| 751 | } |
| 752 | cfg.virtio_input_evdevs.push(dev_path); |
| 753 | } |
Miriam Zimmerman | 26ac928 | 2019-01-29 21:21:48 -0800 | [diff] [blame] | 754 | "split-irqchip" => { |
| 755 | cfg.split_irqchip = true; |
| 756 | } |
Daniel Verkamp | e403f5c | 2018-12-11 16:29:26 -0800 | [diff] [blame] | 757 | "initrd" => { |
| 758 | cfg.initrd_path = Some(PathBuf::from(value.unwrap().to_owned())); |
| 759 | } |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 760 | "help" => return Err(argument::Error::PrintHelp), |
| 761 | _ => unreachable!(), |
| 762 | } |
| 763 | Ok(()) |
| 764 | } |
| 765 | |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 766 | fn run_vm(args: std::env::Args) -> std::result::Result<(), ()> { |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 767 | let arguments = |
| 768 | &[Argument::positional("KERNEL", "bzImage of kernel to run"), |
Tristan Muntsinger | 4133b01 | 2018-12-21 16:01:56 -0800 | [diff] [blame] | 769 | Argument::value("android-fstab", "PATH", "Path to Android fstab"), |
Daniel Verkamp | e403f5c | 2018-12-11 16:29:26 -0800 | [diff] [blame] | 770 | Argument::short_value('i', "initrd", "PATH", "Initial ramdisk to load."), |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 771 | Argument::short_value('p', |
| 772 | "params", |
| 773 | "PARAMS", |
Zach Reizner | bb67871 | 2018-01-30 18:13:04 -0800 | [diff] [blame] | 774 | "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] | 775 | Argument::short_value('c', "cpus", "N", "Number of VCPUs. (default: 1)"), |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 776 | Argument::value("cpu-affinity", "CPUSET", "Comma-separated list of CPUs or CPU ranges to run VCPUs on. (e.g. 0,1-3,5) (default: no mask)"), |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 777 | Argument::short_value('m', |
| 778 | "mem", |
| 779 | "N", |
| 780 | "Amount of guest memory in MiB. (default: 256)"), |
| 781 | Argument::short_value('r', |
| 782 | "root", |
| 783 | "PATH", |
| 784 | "Path to a root disk image. Like `--disk` but adds appropriate kernel command line option."), |
| 785 | Argument::short_value('d', "disk", "PATH", "Path to a disk image."), |
Daniel Verkamp | f02fdd1 | 2018-10-10 17:25:14 -0700 | [diff] [blame] | 786 | 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] | 787 | Argument::value("rwdisk", "PATH", "Path to a writable disk image."), |
Daniel Verkamp | f02fdd1 | 2018-10-10 17:25:14 -0700 | [diff] [blame] | 788 | 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] | 789 | Argument::value("host_ip", |
| 790 | "IP", |
| 791 | "IP address to assign to host tap interface."), |
| 792 | Argument::value("netmask", "NETMASK", "Netmask for VM subnet."), |
| 793 | Argument::value("mac", "MAC", "MAC address for VM."), |
paulhsia | f052cfe | 2019-01-22 15:22:25 +0800 | [diff] [blame] | 794 | 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] | 795 | Argument::flag("null-audio", "Add an audio device to the VM that plays samples to /dev/null"), |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 796 | Argument::value("serial", |
| 797 | "type=TYPE,[path=PATH,num=NUM,console]", |
| 798 | "Comma seperated key=value pairs for setting up serial devices. Can be given more than once. |
| 799 | Possible key values: |
Jorge E. Moreira | 9c9e0e7 | 2019-05-17 13:57:04 -0700 | [diff] [blame] | 800 | type=(stdout,syslog,sink,file) - Where to route the serial device |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 801 | num=(1,2,3,4) - Serial Device Number |
Jorge E. Moreira | 9c9e0e7 | 2019-05-17 13:57:04 -0700 | [diff] [blame] | 802 | path=PATH - The path to the file to write to when type=file |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 803 | console - Use this serial device as the guest console. Can only be given once. Will default to first serial port if not provided. |
| 804 | "), |
| 805 | Argument::value("syslog-tag", "TAG", "When logging to syslog, use the provided tag."), |
Stephen Barber | 28a5a61 | 2017-10-20 17:15:30 -0700 | [diff] [blame] | 806 | Argument::value("wayland-sock", "PATH", "Path to the Wayland socket to use."), |
David Reveman | 52ba4e5 | 2018-04-22 21:42:09 -0400 | [diff] [blame] | 807 | #[cfg(feature = "wl-dmabuf")] |
| 808 | Argument::flag("wayland-dmabuf", "Enable support for DMABufs in Wayland device."), |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 809 | Argument::short_value('s', |
| 810 | "socket", |
| 811 | "PATH", |
| 812 | "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] | 813 | Argument::flag("disable-sandbox", "Run all devices in one, non-sandboxed process."), |
Chirantan Ekbote | ebd5681 | 2018-04-16 19:32:04 -0700 | [diff] [blame] | 814 | Argument::value("cid", "CID", "Context ID for virtual sockets."), |
| 815 | Argument::value("shared-dir", "PATH:TAG", |
| 816 | "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] | 817 | Argument::value("seccomp-policy-dir", "PATH", "Path to seccomp .policy files."), |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 818 | #[cfg(feature = "plugin")] |
Zach Reizner | cc30d58 | 2018-01-23 21:16:42 -0800 | [diff] [blame] | 819 | 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] | 820 | #[cfg(feature = "plugin")] |
Dmitry Torokhov | 0f1770d | 2018-05-11 10:57:16 -0700 | [diff] [blame] | 821 | 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] | 822 | #[cfg(feature = "plugin")] |
Chirantan Ekbote | d41d726 | 2018-11-16 16:37:45 -0800 | [diff] [blame] | 823 | Argument::value("plugin-mount", "PATH:PATH:BOOL", "Path to be mounted into the plugin's root filesystem. Can be given more than once."), |
Dmitry Torokhov | 1a6262b | 2019-03-01 00:34:03 -0800 | [diff] [blame] | 824 | #[cfg(feature = "plugin")] |
| 825 | Argument::value("plugin-gid-map", "GID:GID:INT", "Supplemental GIDs that should be mapped in plugin jail. Can be given more than once."), |
Rob Bradford | 8f002f5 | 2018-02-19 16:31:11 +0000 | [diff] [blame] | 826 | Argument::flag("vhost-net", "Use vhost for networking."), |
Chirantan Ekbote | 5f78721 | 2018-05-31 15:31:31 -0700 | [diff] [blame] | 827 | Argument::value("tap-fd", |
| 828 | "fd", |
Jorge E. Moreira | b795280 | 2019-02-12 16:43:05 -0800 | [diff] [blame] | 829 | "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] | 830 | #[cfg(feature = "gpu")] |
| 831 | Argument::flag("gpu", "(EXPERIMENTAL) enable virtio-gpu device"), |
David Tolnay | 43f8e21 | 2019-02-13 17:28:16 -0800 | [diff] [blame] | 832 | #[cfg(feature = "tpm")] |
| 833 | 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] | 834 | 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"), |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 835 | Argument::value("single-touch", "PATH:WIDTH:HEIGHT", "Path to a socket from where to read single touch input events (such as those from a touchscreen) and write status updates to, optionally followed by width and height (defaults to 800x1280)."), |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 836 | 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)."), |
| 837 | Argument::value("mouse", "PATH", "Path to a socket from where to read mouse input events and write status updates to."), |
| 838 | 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] | 839 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 840 | Argument::flag("split-irqchip", "(EXPERIMENTAL) enable split-irqchip support"), |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 841 | Argument::short_flag('h', "help", "Print help message.")]; |
| 842 | |
| 843 | let mut cfg = Config::default(); |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 844 | let match_res = set_arguments(args, &arguments[..], |name, value| { |
| 845 | set_argument(&mut cfg, name, value) |
David Tolnay | 2bac1e7 | 2018-12-12 14:33:42 -0800 | [diff] [blame] | 846 | }) |
| 847 | .and_then(|_| { |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 848 | 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] | 849 | return Err(argument::Error::ExpectedArgument("`KERNEL`".to_owned())); |
| 850 | } |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 851 | if cfg.host_ip.is_some() || cfg.netmask.is_some() || cfg.mac_address.is_some() { |
| 852 | if cfg.host_ip.is_none() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 853 | return Err(argument::Error::ExpectedArgument( |
| 854 | "`host_ip` missing from network config".to_owned(), |
| 855 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 856 | } |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 857 | if cfg.netmask.is_none() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 858 | return Err(argument::Error::ExpectedArgument( |
| 859 | "`netmask` missing from network config".to_owned(), |
| 860 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 861 | } |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 862 | if cfg.mac_address.is_none() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 863 | return Err(argument::Error::ExpectedArgument( |
| 864 | "`mac` missing from network config".to_owned(), |
| 865 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 866 | } |
| 867 | } |
Dmitry Torokhov | 0f1770d | 2018-05-11 10:57:16 -0700 | [diff] [blame] | 868 | if cfg.plugin_root.is_some() && cfg.plugin.is_none() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 869 | return Err(argument::Error::ExpectedArgument( |
| 870 | "`plugin-root` requires `plugin`".to_owned(), |
| 871 | )); |
Dmitry Torokhov | 0f1770d | 2018-05-11 10:57:16 -0700 | [diff] [blame] | 872 | } |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 873 | Ok(()) |
| 874 | }); |
| 875 | |
| 876 | match match_res { |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 877 | #[cfg(feature = "plugin")] |
David Tolnay | 2bac1e7 | 2018-12-12 14:33:42 -0800 | [diff] [blame] | 878 | Ok(()) if cfg.plugin.is_some() => match plugin::run_config(cfg) { |
| 879 | Ok(_) => { |
| 880 | info!("crosvm and plugin have exited normally"); |
| 881 | Ok(()) |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 882 | } |
David Tolnay | 2bac1e7 | 2018-12-12 14:33:42 -0800 | [diff] [blame] | 883 | Err(e) => { |
| 884 | error!("{}", e); |
| 885 | Err(()) |
| 886 | } |
| 887 | }, |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 888 | Ok(()) => match linux::run_config(cfg) { |
| 889 | Ok(_) => { |
| 890 | info!("crosvm has exited normally"); |
| 891 | Ok(()) |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 892 | } |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 893 | Err(e) => { |
| 894 | error!("{}", e); |
| 895 | Err(()) |
| 896 | } |
| 897 | }, |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 898 | Err(argument::Error::PrintHelp) => { |
| 899 | print_help("crosvm run", "KERNEL", &arguments[..]); |
| 900 | Ok(()) |
| 901 | } |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 902 | Err(e) => { |
| 903 | println!("{}", e); |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 904 | Err(()) |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 905 | } |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 906 | } |
| 907 | } |
| 908 | |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 909 | fn handle_request( |
| 910 | request: &VmRequest, |
| 911 | args: std::env::Args, |
| 912 | ) -> std::result::Result<VmResponse, ()> { |
| 913 | let mut return_result = Err(()); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 914 | for socket_path in args { |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 915 | match UnixSeqpacket::connect(&socket_path) { |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 916 | Ok(s) => { |
Jakub Staron | e7c5905 | 2019-04-09 12:31:14 -0700 | [diff] [blame] | 917 | let socket: VmControlRequestSocket = MsgSocket::new(s); |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame] | 918 | if let Err(e) = socket.send(request) { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 919 | error!( |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 920 | "failed to send request to socket at '{}': {}", |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 921 | socket_path, e |
| 922 | ); |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame] | 923 | return_result = Err(()); |
| 924 | continue; |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 925 | } |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame] | 926 | match socket.recv() { |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 927 | Ok(response) => return_result = Ok(response), |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame] | 928 | Err(e) => { |
| 929 | error!( |
| 930 | "failed to send request to socket at2 '{}': {}", |
| 931 | socket_path, e |
| 932 | ); |
| 933 | return_result = Err(()); |
| 934 | continue; |
| 935 | } |
Dylan Reid | d443204 | 2017-12-06 18:20:09 -0800 | [diff] [blame] | 936 | } |
| 937 | } |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 938 | Err(e) => { |
| 939 | error!("failed to connect to socket at '{}': {}", socket_path, e); |
| 940 | return_result = Err(()); |
| 941 | } |
Dylan Reid | d443204 | 2017-12-06 18:20:09 -0800 | [diff] [blame] | 942 | } |
| 943 | } |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 944 | |
| 945 | return_result |
Dylan Reid | d443204 | 2017-12-06 18:20:09 -0800 | [diff] [blame] | 946 | } |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 947 | |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 948 | fn vms_request(request: &VmRequest, args: std::env::Args) -> std::result::Result<(), ()> { |
| 949 | let response = handle_request(request, args)?; |
| 950 | info!("request response was {}", response); |
| 951 | Ok(()) |
| 952 | } |
| 953 | |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame] | 954 | fn stop_vms(args: std::env::Args) -> std::result::Result<(), ()> { |
| 955 | if args.len() == 0 { |
| 956 | print_help("crosvm stop", "VM_SOCKET...", &[]); |
| 957 | println!("Stops the crosvm instance listening on each `VM_SOCKET` given."); |
Jianxun Zhang | 56497d2 | 2019-03-04 14:38:24 -0800 | [diff] [blame] | 958 | return Err(()); |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame] | 959 | } |
| 960 | vms_request(&VmRequest::Exit, args) |
| 961 | } |
| 962 | |
| 963 | fn suspend_vms(args: std::env::Args) -> std::result::Result<(), ()> { |
| 964 | if args.len() == 0 { |
| 965 | print_help("crosvm suspend", "VM_SOCKET...", &[]); |
| 966 | println!("Suspends the crosvm instance listening on each `VM_SOCKET` given."); |
Jianxun Zhang | 56497d2 | 2019-03-04 14:38:24 -0800 | [diff] [blame] | 967 | return Err(()); |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame] | 968 | } |
| 969 | vms_request(&VmRequest::Suspend, args) |
| 970 | } |
| 971 | |
| 972 | fn resume_vms(args: std::env::Args) -> std::result::Result<(), ()> { |
| 973 | if args.len() == 0 { |
| 974 | print_help("crosvm resume", "VM_SOCKET...", &[]); |
| 975 | println!("Resumes the crosvm instance listening on each `VM_SOCKET` given."); |
Jianxun Zhang | 56497d2 | 2019-03-04 14:38:24 -0800 | [diff] [blame] | 976 | return Err(()); |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame] | 977 | } |
| 978 | vms_request(&VmRequest::Resume, args) |
| 979 | } |
| 980 | |
| 981 | fn balloon_vms(mut args: std::env::Args) -> std::result::Result<(), ()> { |
| 982 | if args.len() < 2 { |
| 983 | print_help("crosvm balloon", "SIZE VM_SOCKET...", &[]); |
| 984 | println!("Set the ballon size of the crosvm instance to `SIZE` bytes."); |
Jianxun Zhang | 56497d2 | 2019-03-04 14:38:24 -0800 | [diff] [blame] | 985 | return Err(()); |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame] | 986 | } |
| 987 | let num_bytes = match args.nth(0).unwrap().parse::<u64>() { |
| 988 | Ok(n) => n, |
| 989 | Err(_) => { |
| 990 | error!("Failed to parse number of bytes"); |
| 991 | return Err(()); |
| 992 | } |
| 993 | }; |
| 994 | |
Jakub Staron | 1f828d7 | 2019-04-11 12:49:29 -0700 | [diff] [blame] | 995 | let command = BalloonControlCommand::Adjust { num_bytes }; |
| 996 | vms_request(&VmRequest::BalloonCommand(command), args) |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame] | 997 | } |
| 998 | |
Dylan Reid | 2dcb632 | 2018-07-13 10:42:48 -0700 | [diff] [blame] | 999 | fn create_qcow2(mut args: std::env::Args) -> std::result::Result<(), ()> { |
| 1000 | if args.len() != 2 { |
| 1001 | print_help("crosvm create_qcow2", "PATH SIZE", &[]); |
Dylan Reid | 940259c | 2018-07-20 14:22:33 -0700 | [diff] [blame] | 1002 | println!("Create a new QCOW2 image at `PATH` of the specified `SIZE` in bytes."); |
Jianxun Zhang | 56497d2 | 2019-03-04 14:38:24 -0800 | [diff] [blame] | 1003 | return Err(()); |
Dylan Reid | 2dcb632 | 2018-07-13 10:42:48 -0700 | [diff] [blame] | 1004 | } |
| 1005 | let file_path = args.nth(0).unwrap(); |
| 1006 | let size: u64 = match args.nth(0).unwrap().parse::<u64>() { |
| 1007 | Ok(n) => n, |
| 1008 | Err(_) => { |
| 1009 | error!("Failed to parse size of the disk."); |
| 1010 | return Err(()); |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1011 | } |
Dylan Reid | 2dcb632 | 2018-07-13 10:42:48 -0700 | [diff] [blame] | 1012 | }; |
| 1013 | |
| 1014 | let file = OpenOptions::new() |
| 1015 | .create(true) |
| 1016 | .read(true) |
| 1017 | .write(true) |
| 1018 | .open(&file_path) |
| 1019 | .map_err(|e| { |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 1020 | error!("Failed opening qcow file at '{}': {}", file_path, e); |
Dylan Reid | 2dcb632 | 2018-07-13 10:42:48 -0700 | [diff] [blame] | 1021 | })?; |
| 1022 | |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1023 | QcowFile::new(file, size).map_err(|e| { |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 1024 | error!("Failed to create qcow file at '{}': {}", file_path, e); |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1025 | })?; |
Dylan Reid | 2dcb632 | 2018-07-13 10:42:48 -0700 | [diff] [blame] | 1026 | |
| 1027 | Ok(()) |
| 1028 | } |
| 1029 | |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 1030 | fn disk_cmd(mut args: std::env::Args) -> std::result::Result<(), ()> { |
| 1031 | if args.len() < 2 { |
| 1032 | print_help("crosvm disk", "SUBCOMMAND VM_SOCKET...", &[]); |
| 1033 | println!("Manage attached virtual disk devices."); |
| 1034 | println!("Subcommands:"); |
| 1035 | println!(" resize DISK_INDEX NEW_SIZE VM_SOCKET"); |
Jianxun Zhang | 56497d2 | 2019-03-04 14:38:24 -0800 | [diff] [blame] | 1036 | return Err(()); |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 1037 | } |
| 1038 | let subcommand: &str = &args.nth(0).unwrap(); |
| 1039 | |
| 1040 | let request = match subcommand { |
| 1041 | "resize" => { |
| 1042 | let disk_index = match args.nth(0).unwrap().parse::<usize>() { |
| 1043 | Ok(n) => n, |
| 1044 | Err(_) => { |
| 1045 | error!("Failed to parse disk index"); |
| 1046 | return Err(()); |
| 1047 | } |
| 1048 | }; |
| 1049 | |
| 1050 | let new_size = match args.nth(0).unwrap().parse::<u64>() { |
| 1051 | Ok(n) => n, |
| 1052 | Err(_) => { |
| 1053 | error!("Failed to parse disk size"); |
| 1054 | return Err(()); |
| 1055 | } |
| 1056 | }; |
| 1057 | |
Jakub Staron | ecf81e0 | 2019-04-11 11:43:39 -0700 | [diff] [blame] | 1058 | VmRequest::DiskCommand { |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 1059 | disk_index, |
Jakub Staron | ecf81e0 | 2019-04-11 11:43:39 -0700 | [diff] [blame] | 1060 | command: DiskControlCommand::Resize { new_size }, |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 1061 | } |
| 1062 | } |
| 1063 | _ => { |
| 1064 | error!("Unknown disk subcommand '{}'", subcommand); |
| 1065 | return Err(()); |
| 1066 | } |
| 1067 | }; |
| 1068 | |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame] | 1069 | vms_request(&request, args) |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 1070 | } |
| 1071 | |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 1072 | enum ModifyUsbError { |
| 1073 | ArgMissing(&'static str), |
| 1074 | ArgParse(&'static str, String), |
| 1075 | ArgParseInt(&'static str, String, ParseIntError), |
| 1076 | FailedFdValidate(sys_util::Error), |
| 1077 | PathDoesNotExist(PathBuf), |
| 1078 | SocketFailed, |
| 1079 | UnexpectedResponse(VmResponse), |
| 1080 | UnknownCommand(String), |
| 1081 | UsbControl(UsbControlResult), |
| 1082 | } |
| 1083 | |
| 1084 | impl fmt::Display for ModifyUsbError { |
| 1085 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 1086 | use self::ModifyUsbError::*; |
| 1087 | |
| 1088 | match self { |
| 1089 | ArgMissing(a) => write!(f, "argument missing: {}", a), |
| 1090 | ArgParse(name, value) => { |
| 1091 | write!(f, "failed to parse argument {} value `{}`", name, value) |
| 1092 | } |
| 1093 | ArgParseInt(name, value, e) => write!( |
| 1094 | f, |
| 1095 | "failed to parse integer argument {} value `{}`: {}", |
| 1096 | name, value, e |
| 1097 | ), |
| 1098 | FailedFdValidate(e) => write!(f, "failed to validate file descriptor: {}", e), |
| 1099 | PathDoesNotExist(p) => write!(f, "path `{}` does not exist", p.display()), |
| 1100 | SocketFailed => write!(f, "socket failed"), |
| 1101 | UnexpectedResponse(r) => write!(f, "unexpected response: {}", r), |
| 1102 | UnknownCommand(c) => write!(f, "unknown command: `{}`", c), |
| 1103 | UsbControl(e) => write!(f, "{}", e), |
| 1104 | } |
| 1105 | } |
| 1106 | } |
| 1107 | |
| 1108 | type ModifyUsbResult<T> = std::result::Result<T, ModifyUsbError>; |
| 1109 | |
| 1110 | fn parse_bus_id_addr(v: &str) -> ModifyUsbResult<(u8, u8, u16, u16)> { |
| 1111 | debug!("parse_bus_id_addr: {}", v); |
| 1112 | let mut ids = v.split(":"); |
| 1113 | match (ids.next(), ids.next(), ids.next(), ids.next()) { |
| 1114 | (Some(bus_id), Some(addr), Some(vid), Some(pid)) => { |
| 1115 | let bus_id = bus_id |
| 1116 | .parse::<u8>() |
| 1117 | .map_err(|e| ModifyUsbError::ArgParseInt("bus_id", bus_id.to_owned(), e))?; |
| 1118 | let addr = addr |
| 1119 | .parse::<u8>() |
| 1120 | .map_err(|e| ModifyUsbError::ArgParseInt("addr", addr.to_owned(), e))?; |
| 1121 | let vid = u16::from_str_radix(&vid, 16) |
| 1122 | .map_err(|e| ModifyUsbError::ArgParseInt("vid", vid.to_owned(), e))?; |
| 1123 | let pid = u16::from_str_radix(&pid, 16) |
| 1124 | .map_err(|e| ModifyUsbError::ArgParseInt("pid", pid.to_owned(), e))?; |
| 1125 | Ok((bus_id, addr, vid, pid)) |
| 1126 | } |
| 1127 | _ => Err(ModifyUsbError::ArgParse( |
| 1128 | "BUS_ID_ADDR_BUS_NUM_DEV_NUM", |
| 1129 | v.to_owned(), |
| 1130 | )), |
| 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | fn raw_fd_from_path(path: &Path) -> ModifyUsbResult<RawFd> { |
| 1135 | if !path.exists() { |
| 1136 | return Err(ModifyUsbError::PathDoesNotExist(path.to_owned())); |
| 1137 | } |
| 1138 | let raw_fd = path |
| 1139 | .file_name() |
| 1140 | .and_then(|fd_osstr| fd_osstr.to_str()) |
| 1141 | .map_or( |
| 1142 | Err(ModifyUsbError::ArgParse( |
| 1143 | "USB_DEVICE_PATH", |
| 1144 | path.to_string_lossy().into_owned(), |
| 1145 | )), |
| 1146 | |fd_str| { |
| 1147 | fd_str.parse::<libc::c_int>().map_err(|e| { |
| 1148 | ModifyUsbError::ArgParseInt("USB_DEVICE_PATH", fd_str.to_owned(), e) |
| 1149 | }) |
| 1150 | }, |
| 1151 | )?; |
David Tolnay | 5fb3f51 | 2019-04-12 19:22:33 -0700 | [diff] [blame] | 1152 | validate_raw_fd(raw_fd).map_err(ModifyUsbError::FailedFdValidate) |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | fn usb_attach(mut args: std::env::Args) -> ModifyUsbResult<UsbControlResult> { |
| 1156 | let val = args |
| 1157 | .next() |
| 1158 | .ok_or(ModifyUsbError::ArgMissing("BUS_ID_ADDR_BUS_NUM_DEV_NUM"))?; |
| 1159 | let (bus, addr, vid, pid) = parse_bus_id_addr(&val)?; |
| 1160 | let dev_path = PathBuf::from( |
| 1161 | args.next() |
| 1162 | .ok_or(ModifyUsbError::ArgMissing("usb device path"))?, |
| 1163 | ); |
| 1164 | let usb_file: Option<File> = if dev_path == Path::new("-") { |
| 1165 | None |
| 1166 | } else if dev_path.parent() == Some(Path::new("/proc/self/fd")) { |
| 1167 | // Special case '/proc/self/fd/*' paths. The FD is already open, just use it. |
| 1168 | // Safe because we will validate |raw_fd|. |
| 1169 | Some(unsafe { File::from_raw_fd(raw_fd_from_path(&dev_path)?) }) |
| 1170 | } else { |
| 1171 | Some( |
| 1172 | OpenOptions::new() |
| 1173 | .read(true) |
| 1174 | .write(true) |
| 1175 | .open(&dev_path) |
| 1176 | .map_err(|_| ModifyUsbError::UsbControl(UsbControlResult::FailedToOpenDevice))?, |
| 1177 | ) |
| 1178 | }; |
| 1179 | |
| 1180 | let request = VmRequest::UsbCommand(UsbControlCommand::AttachDevice { |
| 1181 | bus, |
| 1182 | addr, |
| 1183 | vid, |
| 1184 | pid, |
David Tolnay | 5fb3f51 | 2019-04-12 19:22:33 -0700 | [diff] [blame] | 1185 | fd: usb_file.map(MaybeOwnedFd::Owned), |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 1186 | }); |
| 1187 | let response = handle_request(&request, args).map_err(|_| ModifyUsbError::SocketFailed)?; |
| 1188 | match response { |
| 1189 | VmResponse::UsbResponse(usb_resp) => Ok(usb_resp), |
| 1190 | r => Err(ModifyUsbError::UnexpectedResponse(r)), |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | fn usb_detach(mut args: std::env::Args) -> ModifyUsbResult<UsbControlResult> { |
| 1195 | let port: u8 = args |
| 1196 | .next() |
| 1197 | .map_or(Err(ModifyUsbError::ArgMissing("PORT")), |p| { |
| 1198 | p.parse::<u8>() |
| 1199 | .map_err(|e| ModifyUsbError::ArgParseInt("PORT", p.to_owned(), e)) |
| 1200 | })?; |
| 1201 | let request = VmRequest::UsbCommand(UsbControlCommand::DetachDevice { port }); |
| 1202 | let response = handle_request(&request, args).map_err(|_| ModifyUsbError::SocketFailed)?; |
| 1203 | match response { |
| 1204 | VmResponse::UsbResponse(usb_resp) => Ok(usb_resp), |
| 1205 | r => Err(ModifyUsbError::UnexpectedResponse(r)), |
| 1206 | } |
| 1207 | } |
| 1208 | |
Zach Reizner | aff94ca | 2019-03-18 20:58:31 -0700 | [diff] [blame] | 1209 | fn usb_list(args: std::env::Args) -> ModifyUsbResult<UsbControlResult> { |
| 1210 | let mut ports: [u8; USB_CONTROL_MAX_PORTS] = Default::default(); |
| 1211 | for (index, port) in ports.iter_mut().enumerate() { |
| 1212 | *port = index as u8 |
| 1213 | } |
| 1214 | let request = VmRequest::UsbCommand(UsbControlCommand::ListDevice { ports }); |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 1215 | let response = handle_request(&request, args).map_err(|_| ModifyUsbError::SocketFailed)?; |
| 1216 | match response { |
| 1217 | VmResponse::UsbResponse(usb_resp) => Ok(usb_resp), |
| 1218 | r => Err(ModifyUsbError::UnexpectedResponse(r)), |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | fn modify_usb(mut args: std::env::Args) -> std::result::Result<(), ()> { |
Zach Reizner | aff94ca | 2019-03-18 20:58:31 -0700 | [diff] [blame] | 1223 | if args.len() < 2 { |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 1224 | print_help("crosvm usb", |
Zach Reizner | aff94ca | 2019-03-18 20:58:31 -0700 | [diff] [blame] | 1225 | "[attach BUS_ID:ADDR:VENDOR_ID:PRODUCT_ID [USB_DEVICE_PATH|-] | detach PORT | list] VM_SOCKET...", &[]); |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 1226 | return Err(()); |
| 1227 | } |
| 1228 | |
| 1229 | // This unwrap will not panic because of the above length check. |
| 1230 | let command = args.next().unwrap(); |
| 1231 | let result = match command.as_ref() { |
| 1232 | "attach" => usb_attach(args), |
| 1233 | "detach" => usb_detach(args), |
| 1234 | "list" => usb_list(args), |
| 1235 | other => Err(ModifyUsbError::UnknownCommand(other.to_owned())), |
| 1236 | }; |
| 1237 | match result { |
| 1238 | Ok(response) => { |
| 1239 | println!("{}", response); |
| 1240 | Ok(()) |
| 1241 | } |
| 1242 | Err(e) => { |
| 1243 | println!("error {}", e); |
| 1244 | Err(()) |
| 1245 | } |
| 1246 | } |
| 1247 | } |
| 1248 | |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1249 | fn print_usage() { |
| 1250 | print_help("crosvm", "[stop|run]", &[]); |
| 1251 | println!("Commands:"); |
| 1252 | println!(" stop - Stops crosvm instances via their control sockets."); |
| 1253 | println!(" run - Start a new crosvm instance."); |
Dylan Reid | 2dcb632 | 2018-07-13 10:42:48 -0700 | [diff] [blame] | 1254 | println!(" create_qcow2 - Create a new qcow2 disk image file."); |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 1255 | println!(" disk - Manage attached virtual disk devices."); |
| 1256 | println!(" usb - Manage attached virtual USB devices."); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1257 | } |
| 1258 | |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 1259 | fn crosvm_main() -> std::result::Result<(), ()> { |
Stephen Barber | 56fbf09 | 2017-06-29 16:12:14 -0700 | [diff] [blame] | 1260 | if let Err(e) = syslog::init() { |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 1261 | println!("failed to initialize syslog: {}", e); |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 1262 | return Err(()); |
Stephen Barber | 56fbf09 | 2017-06-29 16:12:14 -0700 | [diff] [blame] | 1263 | } |
Zach Reizner | 639d967 | 2017-05-01 17:57:18 -0700 | [diff] [blame] | 1264 | |
Zach Reizner | b3fa5c9 | 2019-01-28 14:05:23 -0800 | [diff] [blame] | 1265 | panic_hook::set_panic_hook(); |
| 1266 | |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1267 | let mut args = std::env::args(); |
| 1268 | if args.next().is_none() { |
| 1269 | error!("expected executable name"); |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 1270 | return Err(()); |
Zach Reizner | 639d967 | 2017-05-01 17:57:18 -0700 | [diff] [blame] | 1271 | } |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1272 | |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 1273 | // 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] | 1274 | let ret = match args.next().as_ref().map(|a| a.as_ref()) { |
| 1275 | None => { |
| 1276 | print_usage(); |
| 1277 | Ok(()) |
| 1278 | } |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1279 | Some("stop") => stop_vms(args), |
Zach Reizner | 6a8fdd9 | 2019-01-16 14:38:41 -0800 | [diff] [blame] | 1280 | Some("suspend") => suspend_vms(args), |
| 1281 | Some("resume") => resume_vms(args), |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1282 | Some("run") => run_vm(args), |
| 1283 | Some("balloon") => balloon_vms(args), |
| 1284 | Some("create_qcow2") => create_qcow2(args), |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 1285 | Some("disk") => disk_cmd(args), |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 1286 | Some("usb") => modify_usb(args), |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1287 | Some(c) => { |
| 1288 | println!("invalid subcommand: {:?}", c); |
| 1289 | print_usage(); |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 1290 | Err(()) |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1291 | } |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 1292 | }; |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1293 | |
| 1294 | // Reap exit status from any child device processes. At this point, all devices should have been |
| 1295 | // dropped in the main process and told to shutdown. Try over a period of 100ms, since it may |
| 1296 | // take some time for the processes to shut down. |
| 1297 | if !wait_all_children() { |
| 1298 | // We gave them a chance, and it's too late. |
| 1299 | warn!("not all child processes have exited; sending SIGKILL"); |
| 1300 | if let Err(e) = kill_process_group() { |
| 1301 | // 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] | 1302 | warn!("unable to kill all child processes: {}", e); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1303 | } |
| 1304 | } |
| 1305 | |
| 1306 | // WARNING: Any code added after this point is not guaranteed to run |
| 1307 | // since we may forcibly kill this process (and its children) above. |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 1308 | ret |
| 1309 | } |
| 1310 | |
| 1311 | fn main() { |
| 1312 | std::process::exit(if crosvm_main().is_ok() { 0 } else { 1 }); |
Zach Reizner | 639d967 | 2017-05-01 17:57:18 -0700 | [diff] [blame] | 1313 | } |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 1314 | |
| 1315 | #[cfg(test)] |
| 1316 | mod tests { |
| 1317 | use super::*; |
| 1318 | |
| 1319 | #[test] |
| 1320 | fn parse_cpu_set_single() { |
| 1321 | assert_eq!(parse_cpu_set("123").expect("parse failed"), vec![123]); |
| 1322 | } |
| 1323 | |
| 1324 | #[test] |
| 1325 | fn parse_cpu_set_list() { |
| 1326 | assert_eq!( |
| 1327 | parse_cpu_set("0,1,2,3").expect("parse failed"), |
| 1328 | vec![0, 1, 2, 3] |
| 1329 | ); |
| 1330 | } |
| 1331 | |
| 1332 | #[test] |
| 1333 | fn parse_cpu_set_range() { |
| 1334 | assert_eq!( |
| 1335 | parse_cpu_set("0-3").expect("parse failed"), |
| 1336 | vec![0, 1, 2, 3] |
| 1337 | ); |
| 1338 | } |
| 1339 | |
| 1340 | #[test] |
| 1341 | fn parse_cpu_set_list_of_ranges() { |
| 1342 | assert_eq!( |
| 1343 | parse_cpu_set("3-4,7-9,18").expect("parse failed"), |
| 1344 | vec![3, 4, 7, 8, 9, 18] |
| 1345 | ); |
| 1346 | } |
| 1347 | |
| 1348 | #[test] |
| 1349 | fn parse_cpu_set_repeated() { |
| 1350 | // For now, allow duplicates - they will be handled gracefully by the vec to cpu_set_t conversion. |
| 1351 | assert_eq!(parse_cpu_set("1,1,1").expect("parse failed"), vec![1, 1, 1]); |
| 1352 | } |
| 1353 | |
| 1354 | #[test] |
| 1355 | fn parse_cpu_set_negative() { |
| 1356 | // Negative CPU numbers are not allowed. |
| 1357 | parse_cpu_set("-3").expect_err("parse should have failed"); |
| 1358 | } |
| 1359 | |
| 1360 | #[test] |
| 1361 | fn parse_cpu_set_reverse_range() { |
| 1362 | // Ranges must be from low to high. |
| 1363 | parse_cpu_set("5-2").expect_err("parse should have failed"); |
| 1364 | } |
| 1365 | |
| 1366 | #[test] |
| 1367 | fn parse_cpu_set_open_range() { |
| 1368 | parse_cpu_set("3-").expect_err("parse should have failed"); |
| 1369 | } |
| 1370 | |
| 1371 | #[test] |
| 1372 | fn parse_cpu_set_extra_comma() { |
| 1373 | parse_cpu_set("0,1,2,").expect_err("parse should have failed"); |
| 1374 | } |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 1375 | |
| 1376 | #[test] |
| 1377 | fn parse_serial_vaild() { |
| 1378 | parse_serial_options("type=syslog,num=1,console=true").expect("parse should have succeded"); |
| 1379 | } |
| 1380 | |
| 1381 | #[test] |
| 1382 | fn parse_serial_invalid_type() { |
| 1383 | parse_serial_options("type=wormhole,num=1").expect_err("parse should have failed"); |
| 1384 | } |
| 1385 | |
| 1386 | #[test] |
| 1387 | fn parse_serial_invalid_num_upper() { |
| 1388 | parse_serial_options("type=syslog,num=5").expect_err("parse should have failed"); |
| 1389 | } |
| 1390 | |
| 1391 | #[test] |
| 1392 | fn parse_serial_invalid_num_lower() { |
| 1393 | parse_serial_options("type=syslog,num=0").expect_err("parse should have failed"); |
| 1394 | } |
| 1395 | |
| 1396 | #[test] |
| 1397 | fn parse_serial_invalid_num_string() { |
| 1398 | parse_serial_options("type=syslog,num=number3").expect_err("parse should have failed"); |
| 1399 | } |
| 1400 | |
| 1401 | #[test] |
| 1402 | fn parse_serial_invalid_option() { |
| 1403 | parse_serial_options("type=syslog,speed=lightspeed").expect_err("parse should have failed"); |
| 1404 | } |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 1405 | } |