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 | b3fa5c9 | 2019-01-28 14:05:23 -0800 | [diff] [blame] | 7 | pub mod panic_hook; |
Zach Reizner | 29ad3c7 | 2017-08-04 15:12:58 -0700 | [diff] [blame] | 8 | |
Judy Hsiao | d5c1e96 | 2020-02-04 12:30:01 +0800 | [diff] [blame] | 9 | use std::default::Default; |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 10 | use std::fmt; |
| 11 | use std::fs::{File, OpenOptions}; |
Dmitry Torokhov | 0f4c5ff | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 12 | use std::io::{BufRead, BufReader}; |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 13 | use std::num::ParseIntError; |
| 14 | use std::os::unix::io::{FromRawFd, RawFd}; |
| 15 | use std::path::{Path, PathBuf}; |
Zach Reizner | 639d967 | 2017-05-01 17:57:18 -0700 | [diff] [blame] | 16 | use std::string::String; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 17 | use std::thread::sleep; |
Stephen Barber | 56fbf09 | 2017-06-29 16:12:14 -0700 | [diff] [blame] | 18 | use std::time::Duration; |
Zach Reizner | 639d967 | 2017-05-01 17:57:18 -0700 | [diff] [blame] | 19 | |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame^] | 20 | use arch::{set_default_serial_parameters, Pstore, SerialHardware, SerialParameters, SerialType}; |
Judy Hsiao | d5c1e96 | 2020-02-04 12:30:01 +0800 | [diff] [blame] | 21 | use audio_streams::StreamEffect; |
Zach Reizner | 267f2c8 | 2019-07-31 17:07:27 -0700 | [diff] [blame] | 22 | use crosvm::{ |
| 23 | argument::{self, print_help, set_arguments, Argument}, |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 24 | linux, BindMount, Config, DiskOption, Executable, GidMap, SharedDir, TouchDeviceOption, |
Zach Reizner | 267f2c8 | 2019-07-31 17:07:27 -0700 | [diff] [blame] | 25 | }; |
Jason Macnak | cc7070b | 2019-11-06 14:48:12 -0800 | [diff] [blame] | 26 | #[cfg(feature = "gpu")] |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 27 | use devices::virtio::gpu::{GpuMode, GpuParameters}; |
Daniel Verkamp | fbd6122 | 2020-02-14 16:46:36 -0800 | [diff] [blame] | 28 | use devices::{Ac97Backend, Ac97Parameters}; |
Daniel Verkamp | f2eecc4 | 2019-12-17 17:04:58 -0800 | [diff] [blame] | 29 | use disk::QcowFile; |
David Tolnay | fe3ef7d | 2019-03-08 15:57:49 -0800 | [diff] [blame] | 30 | use msg_socket::{MsgReceiver, MsgSender, MsgSocket}; |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 31 | use sys_util::{ |
David Tolnay | 633426a | 2019-04-12 12:18:35 -0700 | [diff] [blame] | 32 | debug, error, getpid, info, kill_process_group, net::UnixSeqpacket, reap_child, syslog, |
| 33 | validate_raw_fd, warn, |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 34 | }; |
Jakub Staron | e7c5905 | 2019-04-09 12:31:14 -0700 | [diff] [blame] | 35 | use vm_control::{ |
Jakub Staron | 1f828d7 | 2019-04-11 12:49:29 -0700 | [diff] [blame] | 36 | BalloonControlCommand, DiskControlCommand, MaybeOwnedFd, UsbControlCommand, UsbControlResult, |
Zach Reizner | aff94ca | 2019-03-18 20:58:31 -0700 | [diff] [blame] | 37 | VmControlRequestSocket, VmRequest, VmResponse, USB_CONTROL_MAX_PORTS, |
Jakub Staron | e7c5905 | 2019-04-09 12:31:14 -0700 | [diff] [blame] | 38 | }; |
Zach Reizner | 639d967 | 2017-05-01 17:57:18 -0700 | [diff] [blame] | 39 | |
Cody Schuffelen | 6d1ab50 | 2019-05-21 12:12:38 -0700 | [diff] [blame] | 40 | fn executable_is_plugin(executable: &Option<Executable>) -> bool { |
| 41 | match executable { |
| 42 | Some(Executable::Plugin(_)) => true, |
| 43 | _ => false, |
| 44 | } |
| 45 | } |
| 46 | |
Stephen Barber | a00753b | 2017-07-18 13:57:26 -0700 | [diff] [blame] | 47 | // Wait for all children to exit. Return true if they have all exited, false |
| 48 | // otherwise. |
| 49 | fn wait_all_children() -> bool { |
Stephen Barber | 49dd2e2 | 2018-10-29 18:29:58 -0700 | [diff] [blame] | 50 | const CHILD_WAIT_MAX_ITER: isize = 100; |
Stephen Barber | a00753b | 2017-07-18 13:57:26 -0700 | [diff] [blame] | 51 | const CHILD_WAIT_MS: u64 = 10; |
| 52 | for _ in 0..CHILD_WAIT_MAX_ITER { |
Stephen Barber | a00753b | 2017-07-18 13:57:26 -0700 | [diff] [blame] | 53 | loop { |
Zach Reizner | 56158c8 | 2017-08-24 13:50:14 -0700 | [diff] [blame] | 54 | match reap_child() { |
| 55 | Ok(0) => break, |
| 56 | // We expect ECHILD which indicates that there were no children left. |
| 57 | Err(e) if e.errno() == libc::ECHILD => return true, |
| 58 | Err(e) => { |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 59 | warn!("error while waiting for children: {}", e); |
Zach Reizner | 56158c8 | 2017-08-24 13:50:14 -0700 | [diff] [blame] | 60 | return false; |
Stephen Barber | a00753b | 2017-07-18 13:57:26 -0700 | [diff] [blame] | 61 | } |
Zach Reizner | 56158c8 | 2017-08-24 13:50:14 -0700 | [diff] [blame] | 62 | // We reaped one child, so continue reaping. |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 63 | _ => {} |
Stephen Barber | a00753b | 2017-07-18 13:57:26 -0700 | [diff] [blame] | 64 | } |
| 65 | } |
Zach Reizner | 56158c8 | 2017-08-24 13:50:14 -0700 | [diff] [blame] | 66 | // There's no timeout option for waitpid which reap_child calls internally, so our only |
| 67 | // recourse is to sleep while waiting for the children to exit. |
Stephen Barber | a00753b | 2017-07-18 13:57:26 -0700 | [diff] [blame] | 68 | sleep(Duration::from_millis(CHILD_WAIT_MS)); |
| 69 | } |
| 70 | |
| 71 | // 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] | 72 | false |
Stephen Barber | a00753b | 2017-07-18 13:57:26 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 75 | /// Parse a comma-separated list of CPU numbers and ranges and convert it to a Vec of CPU numbers. |
| 76 | fn parse_cpu_set(s: &str) -> argument::Result<Vec<usize>> { |
| 77 | let mut cpuset = Vec::new(); |
| 78 | for part in s.split(',') { |
| 79 | let range: Vec<&str> = part.split('-').collect(); |
Keiichi Watanabe | 275c1ef | 2020-04-10 21:49:10 +0900 | [diff] [blame] | 80 | if range.is_empty() || range.len() > 2 { |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 81 | return Err(argument::Error::InvalidValue { |
| 82 | value: part.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 83 | expected: String::from("invalid list syntax"), |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 84 | }); |
| 85 | } |
| 86 | let first_cpu: usize = range[0] |
| 87 | .parse() |
| 88 | .map_err(|_| argument::Error::InvalidValue { |
| 89 | value: part.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 90 | expected: String::from("CPU index must be a non-negative integer"), |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 91 | })?; |
| 92 | let last_cpu: usize = if range.len() == 2 { |
| 93 | range[1] |
| 94 | .parse() |
| 95 | .map_err(|_| argument::Error::InvalidValue { |
| 96 | value: part.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 97 | expected: String::from("CPU index must be a non-negative integer"), |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 98 | })? |
| 99 | } else { |
| 100 | first_cpu |
| 101 | }; |
| 102 | |
| 103 | if last_cpu < first_cpu { |
| 104 | return Err(argument::Error::InvalidValue { |
| 105 | value: part.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 106 | expected: String::from("CPU ranges must be from low to high"), |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 107 | }); |
| 108 | } |
| 109 | |
| 110 | for cpu in first_cpu..=last_cpu { |
| 111 | cpuset.push(cpu); |
| 112 | } |
| 113 | } |
| 114 | Ok(cpuset) |
| 115 | } |
| 116 | |
Jason Macnak | cc7070b | 2019-11-06 14:48:12 -0800 | [diff] [blame] | 117 | #[cfg(feature = "gpu")] |
| 118 | fn parse_gpu_options(s: Option<&str>) -> argument::Result<GpuParameters> { |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 119 | let mut gpu_params: GpuParameters = Default::default(); |
Jason Macnak | cc7070b | 2019-11-06 14:48:12 -0800 | [diff] [blame] | 120 | |
| 121 | if let Some(s) = s { |
| 122 | let opts = s |
Keiichi Watanabe | 275c1ef | 2020-04-10 21:49:10 +0900 | [diff] [blame] | 123 | .split(',') |
| 124 | .map(|frag| frag.split('=')) |
Jason Macnak | cc7070b | 2019-11-06 14:48:12 -0800 | [diff] [blame] | 125 | .map(|mut kv| (kv.next().unwrap_or(""), kv.next().unwrap_or(""))); |
| 126 | |
| 127 | for (k, v) in opts { |
| 128 | match k { |
Lingfeng Yang | ddbe8b7 | 2020-01-30 10:00:36 -0800 | [diff] [blame] | 129 | // Deprecated: Specifying --gpu=<mode> Not great as the mode can be set multiple |
| 130 | // times if the user specifies several modes (--gpu=2d,3d,gfxstream) |
Jason Macnak | 327fc24 | 2020-01-10 12:45:36 -0800 | [diff] [blame] | 131 | "2d" | "2D" => { |
| 132 | gpu_params.mode = GpuMode::Mode2D; |
| 133 | } |
| 134 | "3d" | "3D" => { |
| 135 | gpu_params.mode = GpuMode::Mode3D; |
| 136 | } |
Lingfeng Yang | ddbe8b7 | 2020-01-30 10:00:36 -0800 | [diff] [blame] | 137 | #[cfg(feature = "gfxstream")] |
| 138 | "gfxstream" => { |
| 139 | gpu_params.mode = GpuMode::ModeGfxStream; |
| 140 | } |
| 141 | // Preferred: Specifying --gpu,backend=<mode> |
| 142 | "backend" => match v { |
| 143 | "2d" | "2D" => { |
| 144 | gpu_params.mode = GpuMode::Mode2D; |
| 145 | } |
| 146 | "3d" | "3D" => { |
| 147 | gpu_params.mode = GpuMode::Mode3D; |
| 148 | } |
| 149 | #[cfg(feature = "gfxstream")] |
| 150 | "gfxstream" => { |
| 151 | gpu_params.mode = GpuMode::ModeGfxStream; |
| 152 | } |
| 153 | _ => { |
| 154 | return Err(argument::Error::InvalidValue { |
| 155 | value: v.to_string(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 156 | expected: String::from( |
| 157 | "gpu parameter 'backend' should be one of (2d|3d|gfxstream)", |
| 158 | ), |
Lingfeng Yang | ddbe8b7 | 2020-01-30 10:00:36 -0800 | [diff] [blame] | 159 | }); |
| 160 | } |
| 161 | }, |
Jason Macnak | bf19558 | 2019-11-20 16:25:49 -0800 | [diff] [blame] | 162 | "egl" => match v { |
| 163 | "true" | "" => { |
| 164 | gpu_params.renderer_use_egl = true; |
| 165 | } |
| 166 | "false" => { |
| 167 | gpu_params.renderer_use_egl = false; |
| 168 | } |
| 169 | _ => { |
| 170 | return Err(argument::Error::InvalidValue { |
| 171 | value: v.to_string(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 172 | expected: String::from("gpu parameter 'egl' should be a boolean"), |
Jason Macnak | bf19558 | 2019-11-20 16:25:49 -0800 | [diff] [blame] | 173 | }); |
| 174 | } |
| 175 | }, |
| 176 | "gles" => match v { |
| 177 | "true" | "" => { |
| 178 | gpu_params.renderer_use_gles = true; |
| 179 | } |
| 180 | "false" => { |
| 181 | gpu_params.renderer_use_gles = false; |
| 182 | } |
| 183 | _ => { |
| 184 | return Err(argument::Error::InvalidValue { |
| 185 | value: v.to_string(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 186 | expected: String::from("gpu parameter 'gles' should be a boolean"), |
Jason Macnak | bf19558 | 2019-11-20 16:25:49 -0800 | [diff] [blame] | 187 | }); |
| 188 | } |
| 189 | }, |
| 190 | "glx" => match v { |
| 191 | "true" | "" => { |
| 192 | gpu_params.renderer_use_glx = true; |
| 193 | } |
| 194 | "false" => { |
| 195 | gpu_params.renderer_use_glx = false; |
| 196 | } |
| 197 | _ => { |
| 198 | return Err(argument::Error::InvalidValue { |
| 199 | value: v.to_string(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 200 | expected: String::from("gpu parameter 'glx' should be a boolean"), |
Jason Macnak | bf19558 | 2019-11-20 16:25:49 -0800 | [diff] [blame] | 201 | }); |
| 202 | } |
| 203 | }, |
| 204 | "surfaceless" => match v { |
| 205 | "true" | "" => { |
| 206 | gpu_params.renderer_use_surfaceless = true; |
| 207 | } |
| 208 | "false" => { |
| 209 | gpu_params.renderer_use_surfaceless = false; |
| 210 | } |
| 211 | _ => { |
| 212 | return Err(argument::Error::InvalidValue { |
| 213 | value: v.to_string(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 214 | expected: String::from( |
| 215 | "gpu parameter 'surfaceless' should be a boolean", |
| 216 | ), |
Jason Macnak | bf19558 | 2019-11-20 16:25:49 -0800 | [diff] [blame] | 217 | }); |
| 218 | } |
| 219 | }, |
Jason Macnak | cc7070b | 2019-11-06 14:48:12 -0800 | [diff] [blame] | 220 | "width" => { |
| 221 | gpu_params.display_width = |
| 222 | v.parse::<u32>() |
| 223 | .map_err(|_| argument::Error::InvalidValue { |
| 224 | value: v.to_string(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 225 | expected: String::from( |
| 226 | "gpu parameter 'width' must be a valid integer", |
| 227 | ), |
Jason Macnak | cc7070b | 2019-11-06 14:48:12 -0800 | [diff] [blame] | 228 | })?; |
| 229 | } |
| 230 | "height" => { |
| 231 | gpu_params.display_height = |
| 232 | v.parse::<u32>() |
| 233 | .map_err(|_| argument::Error::InvalidValue { |
| 234 | value: v.to_string(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 235 | expected: String::from( |
| 236 | "gpu parameter 'height' must be a valid integer", |
| 237 | ), |
Jason Macnak | cc7070b | 2019-11-06 14:48:12 -0800 | [diff] [blame] | 238 | })?; |
| 239 | } |
| 240 | "" => {} |
| 241 | _ => { |
| 242 | return Err(argument::Error::UnknownArgument(format!( |
| 243 | "gpu parameter {}", |
| 244 | k |
| 245 | ))); |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | Ok(gpu_params) |
| 252 | } |
| 253 | |
Judy Hsiao | d5c1e96 | 2020-02-04 12:30:01 +0800 | [diff] [blame] | 254 | fn parse_ac97_options(s: &str) -> argument::Result<Ac97Parameters> { |
| 255 | let mut ac97_params: Ac97Parameters = Default::default(); |
| 256 | |
| 257 | let opts = s |
Keiichi Watanabe | 275c1ef | 2020-04-10 21:49:10 +0900 | [diff] [blame] | 258 | .split(',') |
| 259 | .map(|frag| frag.split('=')) |
Judy Hsiao | d5c1e96 | 2020-02-04 12:30:01 +0800 | [diff] [blame] | 260 | .map(|mut kv| (kv.next().unwrap_or(""), kv.next().unwrap_or(""))); |
| 261 | |
| 262 | for (k, v) in opts { |
| 263 | match k { |
| 264 | "backend" => { |
| 265 | ac97_params.backend = |
| 266 | v.parse::<Ac97Backend>() |
| 267 | .map_err(|e| argument::Error::InvalidValue { |
| 268 | value: v.to_string(), |
| 269 | expected: e.to_string(), |
| 270 | })?; |
| 271 | } |
| 272 | "capture" => { |
| 273 | ac97_params.capture = v.parse::<bool>().map_err(|e| { |
| 274 | argument::Error::Syntax(format!("invalid capture option: {}", e)) |
| 275 | })?; |
| 276 | } |
| 277 | "capture_effects" => { |
| 278 | ac97_params.capture_effects = v |
Keiichi Watanabe | 275c1ef | 2020-04-10 21:49:10 +0900 | [diff] [blame] | 279 | .split('|') |
Judy Hsiao | d5c1e96 | 2020-02-04 12:30:01 +0800 | [diff] [blame] | 280 | .map(|val| { |
| 281 | val.parse::<StreamEffect>() |
| 282 | .map_err(|e| argument::Error::InvalidValue { |
| 283 | value: val.to_string(), |
| 284 | expected: e.to_string(), |
| 285 | }) |
| 286 | }) |
| 287 | .collect::<argument::Result<Vec<_>>>()?; |
| 288 | } |
| 289 | _ => { |
| 290 | return Err(argument::Error::UnknownArgument(format!( |
| 291 | "unknown ac97 parameter {}", |
| 292 | k |
| 293 | ))); |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | Ok(ac97_params) |
| 299 | } |
| 300 | |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 301 | fn parse_serial_options(s: &str) -> argument::Result<SerialParameters> { |
| 302 | let mut serial_setting = SerialParameters { |
| 303 | type_: SerialType::Sink, |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame^] | 304 | hardware: SerialHardware::Serial, |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 305 | path: None, |
Iliyan Malchev | 2c1417b | 2020-04-14 09:40:41 -0700 | [diff] [blame] | 306 | input: None, |
Trent Begin | 923bab0 | 2019-06-17 13:48:06 -0600 | [diff] [blame] | 307 | num: 1, |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 308 | console: false, |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame^] | 309 | earlycon: false, |
Jorge E. Moreira | 1e26230 | 2019-08-01 14:40:03 -0700 | [diff] [blame] | 310 | stdin: false, |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 311 | }; |
| 312 | |
| 313 | let opts = s |
Keiichi Watanabe | 275c1ef | 2020-04-10 21:49:10 +0900 | [diff] [blame] | 314 | .split(',') |
| 315 | .map(|frag| frag.split('=')) |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 316 | .map(|mut kv| (kv.next().unwrap_or(""), kv.next().unwrap_or(""))); |
| 317 | |
| 318 | for (k, v) in opts { |
| 319 | match k { |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame^] | 320 | "hardware" => { |
| 321 | serial_setting.hardware = v |
| 322 | .parse::<SerialHardware>() |
| 323 | .map_err(|e| argument::Error::UnknownArgument(format!("{}", e)))? |
| 324 | } |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 325 | "type" => { |
| 326 | serial_setting.type_ = v |
| 327 | .parse::<SerialType>() |
| 328 | .map_err(|e| argument::Error::UnknownArgument(format!("{}", e)))? |
| 329 | } |
| 330 | "num" => { |
| 331 | let num = v.parse::<u8>().map_err(|e| { |
| 332 | argument::Error::Syntax(format!("serial device number is not parsable: {}", e)) |
| 333 | })?; |
| 334 | if num < 1 || num > 4 { |
| 335 | return Err(argument::Error::InvalidValue { |
| 336 | value: num.to_string(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 337 | expected: String::from("Serial port num must be between 1 - 4"), |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 338 | }); |
| 339 | } |
| 340 | serial_setting.num = num; |
| 341 | } |
| 342 | "console" => { |
| 343 | serial_setting.console = v.parse::<bool>().map_err(|e| { |
| 344 | argument::Error::Syntax(format!( |
| 345 | "serial device console is not parseable: {}", |
| 346 | e |
| 347 | )) |
| 348 | })? |
| 349 | } |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame^] | 350 | "earlycon" => { |
| 351 | serial_setting.earlycon = v.parse::<bool>().map_err(|e| { |
| 352 | argument::Error::Syntax(format!( |
| 353 | "serial device earlycon is not parseable: {}", |
| 354 | e, |
| 355 | )) |
| 356 | })? |
| 357 | } |
Jorge E. Moreira | 1e26230 | 2019-08-01 14:40:03 -0700 | [diff] [blame] | 358 | "stdin" => { |
| 359 | serial_setting.stdin = v.parse::<bool>().map_err(|e| { |
| 360 | argument::Error::Syntax(format!("serial device stdin is not parseable: {}", e)) |
Iliyan Malchev | 2c1417b | 2020-04-14 09:40:41 -0700 | [diff] [blame] | 361 | })?; |
| 362 | if serial_setting.stdin && serial_setting.input.is_some() { |
| 363 | return Err(argument::Error::TooManyArguments( |
| 364 | "Cannot specify both stdin and input options".to_string(), |
| 365 | )); |
| 366 | } |
Jorge E. Moreira | 1e26230 | 2019-08-01 14:40:03 -0700 | [diff] [blame] | 367 | } |
Jorge E. Moreira | 9c9e0e7 | 2019-05-17 13:57:04 -0700 | [diff] [blame] | 368 | "path" => serial_setting.path = Some(PathBuf::from(v)), |
Iliyan Malchev | 2c1417b | 2020-04-14 09:40:41 -0700 | [diff] [blame] | 369 | "input" => { |
| 370 | if serial_setting.stdin { |
| 371 | return Err(argument::Error::TooManyArguments( |
| 372 | "Cannot specify both stdin and input options".to_string(), |
| 373 | )); |
| 374 | } |
| 375 | serial_setting.input = Some(PathBuf::from(v)); |
| 376 | } |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 377 | _ => { |
| 378 | return Err(argument::Error::UnknownArgument(format!( |
| 379 | "serial parameter {}", |
| 380 | k |
| 381 | ))); |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | Ok(serial_setting) |
| 387 | } |
| 388 | |
Dmitry Torokhov | c689fd9 | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 389 | fn parse_plugin_mount_option(value: &str) -> argument::Result<BindMount> { |
Keiichi Watanabe | 275c1ef | 2020-04-10 21:49:10 +0900 | [diff] [blame] | 390 | let components: Vec<&str> = value.split(':').collect(); |
Dmitry Torokhov | 458bb64 | 2019-12-13 11:47:52 -0800 | [diff] [blame] | 391 | if components.is_empty() || components.len() > 3 || components[0].is_empty() { |
Dmitry Torokhov | c689fd9 | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 392 | return Err(argument::Error::InvalidValue { |
| 393 | value: value.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 394 | expected: String::from( |
| 395 | "`plugin-mount` should be in a form of: <src>[:[<dst>][:<writable>]]", |
| 396 | ), |
Dmitry Torokhov | c689fd9 | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 397 | }); |
| 398 | } |
| 399 | |
| 400 | let src = PathBuf::from(components[0]); |
| 401 | if src.is_relative() { |
| 402 | return Err(argument::Error::InvalidValue { |
| 403 | value: components[0].to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 404 | expected: String::from("the source path for `plugin-mount` must be absolute"), |
Dmitry Torokhov | c689fd9 | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 405 | }); |
| 406 | } |
| 407 | if !src.exists() { |
| 408 | return Err(argument::Error::InvalidValue { |
| 409 | value: components[0].to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 410 | expected: String::from("the source path for `plugin-mount` does not exist"), |
Dmitry Torokhov | c689fd9 | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 411 | }); |
| 412 | } |
| 413 | |
Dmitry Torokhov | 458bb64 | 2019-12-13 11:47:52 -0800 | [diff] [blame] | 414 | let dst = PathBuf::from(match components.get(1) { |
| 415 | None | Some(&"") => components[0], |
| 416 | Some(path) => path, |
| 417 | }); |
Dmitry Torokhov | c689fd9 | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 418 | if dst.is_relative() { |
| 419 | return Err(argument::Error::InvalidValue { |
| 420 | value: components[1].to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 421 | expected: String::from("the destination path for `plugin-mount` must be absolute"), |
Dmitry Torokhov | c689fd9 | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 422 | }); |
| 423 | } |
| 424 | |
Dmitry Torokhov | 458bb64 | 2019-12-13 11:47:52 -0800 | [diff] [blame] | 425 | let writable: bool = match components.get(2) { |
| 426 | None => false, |
| 427 | Some(s) => s.parse().map_err(|_| argument::Error::InvalidValue { |
Dmitry Torokhov | c689fd9 | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 428 | value: components[2].to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 429 | expected: String::from("the <writable> component for `plugin-mount` is not valid bool"), |
Dmitry Torokhov | 458bb64 | 2019-12-13 11:47:52 -0800 | [diff] [blame] | 430 | })?, |
| 431 | }; |
Dmitry Torokhov | c689fd9 | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 432 | |
| 433 | Ok(BindMount { src, dst, writable }) |
| 434 | } |
| 435 | |
| 436 | fn parse_plugin_gid_map_option(value: &str) -> argument::Result<GidMap> { |
Keiichi Watanabe | 275c1ef | 2020-04-10 21:49:10 +0900 | [diff] [blame] | 437 | let components: Vec<&str> = value.split(':').collect(); |
Dmitry Torokhov | 458bb64 | 2019-12-13 11:47:52 -0800 | [diff] [blame] | 438 | if components.is_empty() || components.len() > 3 || components[0].is_empty() { |
Dmitry Torokhov | c689fd9 | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 439 | return Err(argument::Error::InvalidValue { |
| 440 | value: value.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 441 | expected: String::from( |
Dmitry Torokhov | 458bb64 | 2019-12-13 11:47:52 -0800 | [diff] [blame] | 442 | "`plugin-gid-map` must have exactly 3 components: <inner>[:[<outer>][:<count>]]", |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 443 | ), |
Dmitry Torokhov | c689fd9 | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 444 | }); |
| 445 | } |
| 446 | |
| 447 | let inner: libc::gid_t = components[0] |
| 448 | .parse() |
| 449 | .map_err(|_| argument::Error::InvalidValue { |
| 450 | value: components[0].to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 451 | expected: String::from("the <inner> component for `plugin-gid-map` is not valid gid"), |
Dmitry Torokhov | c689fd9 | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 452 | })?; |
| 453 | |
Dmitry Torokhov | 458bb64 | 2019-12-13 11:47:52 -0800 | [diff] [blame] | 454 | let outer: libc::gid_t = match components.get(1) { |
| 455 | None | Some(&"") => inner, |
| 456 | Some(s) => s.parse().map_err(|_| argument::Error::InvalidValue { |
Dmitry Torokhov | c689fd9 | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 457 | value: components[1].to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 458 | expected: String::from("the <outer> component for `plugin-gid-map` is not valid gid"), |
Dmitry Torokhov | 458bb64 | 2019-12-13 11:47:52 -0800 | [diff] [blame] | 459 | })?, |
| 460 | }; |
Dmitry Torokhov | c689fd9 | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 461 | |
Dmitry Torokhov | 458bb64 | 2019-12-13 11:47:52 -0800 | [diff] [blame] | 462 | let count: u32 = match components.get(2) { |
| 463 | None => 1, |
| 464 | Some(s) => s.parse().map_err(|_| argument::Error::InvalidValue { |
Dmitry Torokhov | c689fd9 | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 465 | value: components[2].to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 466 | expected: String::from( |
| 467 | "the <count> component for `plugin-gid-map` is not valid number", |
| 468 | ), |
Dmitry Torokhov | 458bb64 | 2019-12-13 11:47:52 -0800 | [diff] [blame] | 469 | })?, |
| 470 | }; |
Dmitry Torokhov | c689fd9 | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 471 | |
| 472 | Ok(GidMap { |
| 473 | inner, |
| 474 | outer, |
| 475 | count, |
| 476 | }) |
| 477 | } |
| 478 | |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 479 | fn set_argument(cfg: &mut Config, name: &str, value: Option<&str>) -> argument::Result<()> { |
| 480 | match name { |
| 481 | "" => { |
Cody Schuffelen | 6d1ab50 | 2019-05-21 12:12:38 -0700 | [diff] [blame] | 482 | if cfg.executable_path.is_some() { |
| 483 | return Err(argument::Error::TooManyArguments(format!( |
| 484 | "A VM executable was already specified: {:?}", |
| 485 | cfg.executable_path |
| 486 | ))); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 487 | } |
Cody Schuffelen | 6d1ab50 | 2019-05-21 12:12:38 -0700 | [diff] [blame] | 488 | let kernel_path = PathBuf::from(value.unwrap()); |
| 489 | if !kernel_path.exists() { |
| 490 | return Err(argument::Error::InvalidValue { |
| 491 | value: value.unwrap().to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 492 | expected: String::from("this kernel path does not exist"), |
Cody Schuffelen | 6d1ab50 | 2019-05-21 12:12:38 -0700 | [diff] [blame] | 493 | }); |
| 494 | } |
| 495 | cfg.executable_path = Some(Executable::Kernel(kernel_path)); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 496 | } |
Tristan Muntsinger | 4133b01 | 2018-12-21 16:01:56 -0800 | [diff] [blame] | 497 | "android-fstab" => { |
| 498 | if cfg.android_fstab.is_some() |
| 499 | && !cfg.android_fstab.as_ref().unwrap().as_os_str().is_empty() |
| 500 | { |
| 501 | return Err(argument::Error::TooManyArguments( |
| 502 | "expected exactly one android fstab path".to_owned(), |
| 503 | )); |
| 504 | } else { |
| 505 | let android_fstab = PathBuf::from(value.unwrap()); |
| 506 | if !android_fstab.exists() { |
| 507 | return Err(argument::Error::InvalidValue { |
| 508 | value: value.unwrap().to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 509 | expected: String::from("this android fstab path does not exist"), |
Tristan Muntsinger | 4133b01 | 2018-12-21 16:01:56 -0800 | [diff] [blame] | 510 | }); |
| 511 | } |
| 512 | cfg.android_fstab = Some(android_fstab); |
| 513 | } |
| 514 | } |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 515 | "params" => { |
Zach Reizner | bb67871 | 2018-01-30 18:13:04 -0800 | [diff] [blame] | 516 | cfg.params.push(value.unwrap().to_owned()); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 517 | } |
| 518 | "cpus" => { |
| 519 | if cfg.vcpu_count.is_some() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 520 | return Err(argument::Error::TooManyArguments( |
| 521 | "`cpus` already given".to_owned(), |
| 522 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 523 | } |
| 524 | cfg.vcpu_count = |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 525 | Some( |
| 526 | value |
| 527 | .unwrap() |
| 528 | .parse() |
| 529 | .map_err(|_| argument::Error::InvalidValue { |
| 530 | value: value.unwrap().to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 531 | expected: String::from("this value for `cpus` needs to be integer"), |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 532 | })?, |
| 533 | ) |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 534 | } |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 535 | "cpu-affinity" => { |
Keiichi Watanabe | 275c1ef | 2020-04-10 21:49:10 +0900 | [diff] [blame] | 536 | if !cfg.vcpu_affinity.is_empty() { |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 537 | return Err(argument::Error::TooManyArguments( |
| 538 | "`cpu-affinity` already given".to_owned(), |
| 539 | )); |
| 540 | } |
| 541 | cfg.vcpu_affinity = parse_cpu_set(value.unwrap())?; |
| 542 | } |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 543 | "mem" => { |
| 544 | if cfg.memory.is_some() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 545 | return Err(argument::Error::TooManyArguments( |
| 546 | "`mem` already given".to_owned(), |
| 547 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 548 | } |
| 549 | cfg.memory = |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 550 | Some( |
| 551 | value |
| 552 | .unwrap() |
| 553 | .parse() |
| 554 | .map_err(|_| argument::Error::InvalidValue { |
| 555 | value: value.unwrap().to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 556 | expected: String::from("this value for `mem` needs to be integer"), |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 557 | })?, |
| 558 | ) |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 559 | } |
Judy Hsiao | d5c1e96 | 2020-02-04 12:30:01 +0800 | [diff] [blame] | 560 | "ac97" => { |
| 561 | let ac97_params = parse_ac97_options(value.unwrap())?; |
| 562 | cfg.ac97_parameters.push(ac97_params); |
Dylan Reid | 3082e8e | 2019-01-07 10:33:48 -0800 | [diff] [blame] | 563 | } |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 564 | "serial" => { |
| 565 | let serial_params = parse_serial_options(value.unwrap())?; |
| 566 | let num = serial_params.num; |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame^] | 567 | let key = (serial_params.hardware, num); |
| 568 | if cfg.serial_parameters.contains_key(&key) { |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 569 | return Err(argument::Error::TooManyArguments(format!( |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame^] | 570 | "serial hardware {} num {}", |
| 571 | serial_params.hardware, num, |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 572 | ))); |
| 573 | } |
| 574 | |
| 575 | if serial_params.console { |
Jakub Staron | b6515a9 | 2019-06-05 15:18:25 -0700 | [diff] [blame] | 576 | for params in cfg.serial_parameters.values() { |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 577 | if params.console { |
| 578 | return Err(argument::Error::TooManyArguments(format!( |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame^] | 579 | "{} device {} already set as console", |
| 580 | params.hardware, params.num, |
| 581 | ))); |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | if serial_params.earlycon { |
| 587 | // Only SerialHardware::Serial supports earlycon= currently. |
| 588 | match serial_params.hardware { |
| 589 | SerialHardware::Serial => {} |
| 590 | _ => { |
| 591 | return Err(argument::Error::InvalidValue { |
| 592 | value: serial_params.hardware.to_string().to_owned(), |
| 593 | expected: String::from("earlycon not supported for hardware"), |
| 594 | }); |
| 595 | } |
| 596 | } |
| 597 | for params in cfg.serial_parameters.values() { |
| 598 | if params.earlycon { |
| 599 | return Err(argument::Error::TooManyArguments(format!( |
| 600 | "{} device {} already set as earlycon", |
| 601 | params.hardware, params.num, |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 602 | ))); |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | |
Jorge E. Moreira | 1e26230 | 2019-08-01 14:40:03 -0700 | [diff] [blame] | 607 | if serial_params.stdin { |
| 608 | if let Some(previous_stdin) = cfg.serial_parameters.values().find(|sp| sp.stdin) { |
| 609 | return Err(argument::Error::TooManyArguments(format!( |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame^] | 610 | "{} device {} already connected to standard input", |
| 611 | previous_stdin.hardware, previous_stdin.num, |
Jorge E. Moreira | 1e26230 | 2019-08-01 14:40:03 -0700 | [diff] [blame] | 612 | ))); |
| 613 | } |
| 614 | } |
| 615 | |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame^] | 616 | cfg.serial_parameters.insert(key, serial_params); |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 617 | } |
| 618 | "syslog-tag" => { |
| 619 | if cfg.syslog_tag.is_some() { |
| 620 | return Err(argument::Error::TooManyArguments( |
| 621 | "`syslog-tag` already given".to_owned(), |
| 622 | )); |
| 623 | } |
| 624 | syslog::set_proc_name(value.unwrap()); |
| 625 | cfg.syslog_tag = Some(value.unwrap().to_owned()); |
| 626 | } |
Daniel Verkamp | 4b62cd9 | 2019-11-08 13:09:27 -0800 | [diff] [blame] | 627 | "root" | "rwroot" | "disk" | "rwdisk" => { |
Daniel Verkamp | e73c80f | 2019-11-08 10:11:16 -0800 | [diff] [blame] | 628 | let param = value.unwrap(); |
| 629 | let mut components = param.split(','); |
Daniel Verkamp | 6a8cd10 | 2019-06-26 15:17:46 -0700 | [diff] [blame] | 630 | let read_only = !name.starts_with("rw"); |
Daniel Verkamp | e73c80f | 2019-11-08 10:11:16 -0800 | [diff] [blame] | 631 | let disk_path = |
| 632 | PathBuf::from( |
| 633 | components |
| 634 | .next() |
| 635 | .ok_or_else(|| argument::Error::InvalidValue { |
| 636 | value: param.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 637 | expected: String::from("missing disk path"), |
Daniel Verkamp | e73c80f | 2019-11-08 10:11:16 -0800 | [diff] [blame] | 638 | })?, |
| 639 | ); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 640 | if !disk_path.exists() { |
| 641 | return Err(argument::Error::InvalidValue { |
Daniel Verkamp | e73c80f | 2019-11-08 10:11:16 -0800 | [diff] [blame] | 642 | value: param.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 643 | expected: String::from("this disk path does not exist"), |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 644 | }); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 645 | } |
Daniel Verkamp | 6a8cd10 | 2019-06-26 15:17:46 -0700 | [diff] [blame] | 646 | if name.ends_with("root") { |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 647 | if cfg.disks.len() >= 26 { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 648 | return Err(argument::Error::TooManyArguments( |
| 649 | "ran out of letters for to assign to root disk".to_owned(), |
| 650 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 651 | } |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 652 | cfg.params.push(format!( |
Daniel Verkamp | 6a8cd10 | 2019-06-26 15:17:46 -0700 | [diff] [blame] | 653 | "root=/dev/vd{} {}", |
| 654 | char::from(b'a' + cfg.disks.len() as u8), |
| 655 | if read_only { "ro" } else { "rw" } |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 656 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 657 | } |
Daniel Verkamp | e73c80f | 2019-11-08 10:11:16 -0800 | [diff] [blame] | 658 | |
| 659 | let mut disk = DiskOption { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 660 | path: disk_path, |
Daniel Verkamp | 6a8cd10 | 2019-06-26 15:17:46 -0700 | [diff] [blame] | 661 | read_only, |
Daniel Verkamp | e73c80f | 2019-11-08 10:11:16 -0800 | [diff] [blame] | 662 | sparse: true, |
Daniel Verkamp | 2767223 | 2019-12-06 17:26:55 +1100 | [diff] [blame] | 663 | block_size: 512, |
Daniel Verkamp | e73c80f | 2019-11-08 10:11:16 -0800 | [diff] [blame] | 664 | }; |
| 665 | |
| 666 | for opt in components { |
| 667 | let mut o = opt.splitn(2, '='); |
| 668 | let kind = o.next().ok_or_else(|| argument::Error::InvalidValue { |
| 669 | value: opt.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 670 | expected: String::from("disk options must not be empty"), |
Daniel Verkamp | e73c80f | 2019-11-08 10:11:16 -0800 | [diff] [blame] | 671 | })?; |
| 672 | let value = o.next().ok_or_else(|| argument::Error::InvalidValue { |
| 673 | value: opt.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 674 | expected: String::from("disk options must be of the form `kind=value`"), |
Daniel Verkamp | e73c80f | 2019-11-08 10:11:16 -0800 | [diff] [blame] | 675 | })?; |
| 676 | |
| 677 | match kind { |
| 678 | "sparse" => { |
| 679 | let sparse = value.parse().map_err(|_| argument::Error::InvalidValue { |
| 680 | value: value.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 681 | expected: String::from("`sparse` must be a boolean"), |
Daniel Verkamp | e73c80f | 2019-11-08 10:11:16 -0800 | [diff] [blame] | 682 | })?; |
| 683 | disk.sparse = sparse; |
| 684 | } |
Daniel Verkamp | 2767223 | 2019-12-06 17:26:55 +1100 | [diff] [blame] | 685 | "block_size" => { |
| 686 | let block_size = |
| 687 | value.parse().map_err(|_| argument::Error::InvalidValue { |
| 688 | value: value.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 689 | expected: String::from("`block_size` must be an integer"), |
Daniel Verkamp | 2767223 | 2019-12-06 17:26:55 +1100 | [diff] [blame] | 690 | })?; |
| 691 | disk.block_size = block_size; |
| 692 | } |
Daniel Verkamp | e73c80f | 2019-11-08 10:11:16 -0800 | [diff] [blame] | 693 | _ => { |
| 694 | return Err(argument::Error::InvalidValue { |
| 695 | value: kind.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 696 | expected: String::from("unrecognized disk option"), |
Daniel Verkamp | e73c80f | 2019-11-08 10:11:16 -0800 | [diff] [blame] | 697 | }); |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | cfg.disks.push(disk); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 703 | } |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 704 | "pmem-device" | "rw-pmem-device" => { |
| 705 | let disk_path = PathBuf::from(value.unwrap()); |
| 706 | if !disk_path.exists() { |
| 707 | return Err(argument::Error::InvalidValue { |
| 708 | value: value.unwrap().to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 709 | expected: String::from("this disk path does not exist"), |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 710 | }); |
| 711 | } |
| 712 | |
| 713 | cfg.pmem_devices.push(DiskOption { |
| 714 | path: disk_path, |
| 715 | read_only: !name.starts_with("rw"), |
Daniel Verkamp | e73c80f | 2019-11-08 10:11:16 -0800 | [diff] [blame] | 716 | sparse: false, |
Daniel Verkamp | 2767223 | 2019-12-06 17:26:55 +1100 | [diff] [blame] | 717 | block_size: sys_util::pagesize() as u32, |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 718 | }); |
| 719 | } |
Kansho Nishida | 282115b | 2019-12-18 13:13:14 +0900 | [diff] [blame] | 720 | "pstore" => { |
| 721 | if cfg.pstore.is_some() { |
| 722 | return Err(argument::Error::TooManyArguments( |
| 723 | "`pstore` already given".to_owned(), |
| 724 | )); |
| 725 | } |
| 726 | |
| 727 | let value = value.unwrap(); |
| 728 | let components: Vec<&str> = value.split(',').collect(); |
| 729 | if components.len() != 2 { |
| 730 | return Err(argument::Error::InvalidValue { |
| 731 | value: value.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 732 | expected: String::from( |
| 733 | "pstore must have exactly 2 components: path=<path>,size=<size>", |
| 734 | ), |
Kansho Nishida | 282115b | 2019-12-18 13:13:14 +0900 | [diff] [blame] | 735 | }); |
| 736 | } |
| 737 | cfg.pstore = Some(Pstore { |
| 738 | path: { |
| 739 | if components[0].len() <= 5 || !components[0].starts_with("path=") { |
| 740 | return Err(argument::Error::InvalidValue { |
| 741 | value: components[0].to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 742 | expected: String::from("pstore path must follow with `path=`"), |
Kansho Nishida | 282115b | 2019-12-18 13:13:14 +0900 | [diff] [blame] | 743 | }); |
| 744 | }; |
| 745 | PathBuf::from(&components[0][5..]) |
| 746 | }, |
| 747 | size: { |
| 748 | if components[1].len() <= 5 || !components[1].starts_with("size=") { |
| 749 | return Err(argument::Error::InvalidValue { |
| 750 | value: components[1].to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 751 | expected: String::from("pstore size must follow with `size=`"), |
Kansho Nishida | 282115b | 2019-12-18 13:13:14 +0900 | [diff] [blame] | 752 | }); |
| 753 | }; |
| 754 | components[1][5..] |
| 755 | .parse() |
| 756 | .map_err(|_| argument::Error::InvalidValue { |
| 757 | value: value.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 758 | expected: String::from("pstore size must be an integer"), |
Kansho Nishida | 282115b | 2019-12-18 13:13:14 +0900 | [diff] [blame] | 759 | })? |
| 760 | }, |
| 761 | }); |
| 762 | } |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 763 | "host_ip" => { |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 764 | if cfg.host_ip.is_some() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 765 | return Err(argument::Error::TooManyArguments( |
| 766 | "`host_ip` already given".to_owned(), |
| 767 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 768 | } |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 769 | cfg.host_ip = |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 770 | Some( |
| 771 | value |
| 772 | .unwrap() |
| 773 | .parse() |
| 774 | .map_err(|_| argument::Error::InvalidValue { |
| 775 | value: value.unwrap().to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 776 | expected: String::from("`host_ip` needs to be in the form \"x.x.x.x\""), |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 777 | })?, |
| 778 | ) |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 779 | } |
| 780 | "netmask" => { |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 781 | if cfg.netmask.is_some() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 782 | return Err(argument::Error::TooManyArguments( |
| 783 | "`netmask` already given".to_owned(), |
| 784 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 785 | } |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 786 | cfg.netmask = |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 787 | Some( |
| 788 | value |
| 789 | .unwrap() |
| 790 | .parse() |
| 791 | .map_err(|_| argument::Error::InvalidValue { |
| 792 | value: value.unwrap().to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 793 | expected: String::from("`netmask` needs to be in the form \"x.x.x.x\""), |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 794 | })?, |
| 795 | ) |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 796 | } |
| 797 | "mac" => { |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 798 | if cfg.mac_address.is_some() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 799 | return Err(argument::Error::TooManyArguments( |
| 800 | "`mac` already given".to_owned(), |
| 801 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 802 | } |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 803 | cfg.mac_address = |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 804 | Some( |
| 805 | value |
| 806 | .unwrap() |
| 807 | .parse() |
| 808 | .map_err(|_| argument::Error::InvalidValue { |
| 809 | value: value.unwrap().to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 810 | expected: String::from( |
| 811 | "`mac` needs to be in the form \"XX:XX:XX:XX:XX:XX\"", |
| 812 | ), |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 813 | })?, |
| 814 | ) |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 815 | } |
Xiong Zhang | 773c707 | 2020-03-20 10:39:55 +0800 | [diff] [blame] | 816 | "net-vq-pairs" => { |
| 817 | if cfg.net_vq_pairs.is_some() { |
| 818 | return Err(argument::Error::TooManyArguments( |
| 819 | "`net-vq-pairs` already given".to_owned(), |
| 820 | )); |
| 821 | } |
| 822 | cfg.net_vq_pairs = |
| 823 | Some( |
| 824 | value |
| 825 | .unwrap() |
| 826 | .parse() |
| 827 | .map_err(|_| argument::Error::InvalidValue { |
| 828 | value: value.unwrap().to_owned(), |
| 829 | expected: String::from( |
| 830 | "this value for `net-vq-pairs` needs to be integer", |
| 831 | ), |
| 832 | })?, |
| 833 | ) |
| 834 | } |
| 835 | |
Stephen Barber | 28a5a61 | 2017-10-20 17:15:30 -0700 | [diff] [blame] | 836 | "wayland-sock" => { |
Ryo Hashimoto | 0b788de | 2019-12-10 17:14:13 +0900 | [diff] [blame] | 837 | let mut components = value.unwrap().split(','); |
| 838 | let path = |
| 839 | PathBuf::from( |
| 840 | components |
| 841 | .next() |
| 842 | .ok_or_else(|| argument::Error::InvalidValue { |
| 843 | value: value.unwrap().to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 844 | expected: String::from("missing socket path"), |
Ryo Hashimoto | 0b788de | 2019-12-10 17:14:13 +0900 | [diff] [blame] | 845 | })?, |
| 846 | ); |
| 847 | let mut name = ""; |
| 848 | for c in components { |
| 849 | let mut kv = c.splitn(2, '='); |
| 850 | let (kind, value) = match (kv.next(), kv.next()) { |
| 851 | (Some(kind), Some(value)) => (kind, value), |
| 852 | _ => { |
| 853 | return Err(argument::Error::InvalidValue { |
| 854 | value: c.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 855 | expected: String::from("option must be of the form `kind=value`"), |
Ryo Hashimoto | 0b788de | 2019-12-10 17:14:13 +0900 | [diff] [blame] | 856 | }) |
| 857 | } |
| 858 | }; |
| 859 | match kind { |
| 860 | "name" => name = value, |
| 861 | _ => { |
| 862 | return Err(argument::Error::InvalidValue { |
| 863 | value: kind.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 864 | expected: String::from("unrecognized option"), |
Ryo Hashimoto | 0b788de | 2019-12-10 17:14:13 +0900 | [diff] [blame] | 865 | }) |
| 866 | } |
| 867 | } |
Stephen Barber | 28a5a61 | 2017-10-20 17:15:30 -0700 | [diff] [blame] | 868 | } |
Ryo Hashimoto | 0b788de | 2019-12-10 17:14:13 +0900 | [diff] [blame] | 869 | if cfg.wayland_socket_paths.contains_key(name) { |
| 870 | return Err(argument::Error::TooManyArguments(format!( |
| 871 | "wayland socket name already used: '{}'", |
| 872 | name |
| 873 | ))); |
Stephen Barber | 28a5a61 | 2017-10-20 17:15:30 -0700 | [diff] [blame] | 874 | } |
Ryo Hashimoto | 0b788de | 2019-12-10 17:14:13 +0900 | [diff] [blame] | 875 | cfg.wayland_socket_paths.insert(name.to_string(), path); |
Stephen Barber | 28a5a61 | 2017-10-20 17:15:30 -0700 | [diff] [blame] | 876 | } |
David Reveman | 52ba4e5 | 2018-04-22 21:42:09 -0400 | [diff] [blame] | 877 | #[cfg(feature = "wl-dmabuf")] |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 878 | "wayland-dmabuf" => cfg.wayland_dmabuf = true, |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 879 | "x-display" => { |
| 880 | if cfg.x_display.is_some() { |
| 881 | return Err(argument::Error::TooManyArguments( |
| 882 | "`x-display` already given".to_owned(), |
| 883 | )); |
| 884 | } |
| 885 | cfg.x_display = Some(value.unwrap().to_owned()); |
| 886 | } |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 887 | "display-window-keyboard" => { |
| 888 | cfg.display_window_keyboard = true; |
| 889 | } |
| 890 | "display-window-mouse" => { |
| 891 | cfg.display_window_mouse = true; |
| 892 | } |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 893 | "socket" => { |
| 894 | if cfg.socket_path.is_some() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 895 | return Err(argument::Error::TooManyArguments( |
| 896 | "`socket` already given".to_owned(), |
| 897 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 898 | } |
| 899 | let mut socket_path = PathBuf::from(value.unwrap()); |
| 900 | if socket_path.is_dir() { |
| 901 | socket_path.push(format!("crosvm-{}.sock", getpid())); |
| 902 | } |
| 903 | if socket_path.exists() { |
| 904 | return Err(argument::Error::InvalidValue { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 905 | value: socket_path.to_string_lossy().into_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 906 | expected: String::from("this socket path already exists"), |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 907 | }); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 908 | } |
| 909 | cfg.socket_path = Some(socket_path); |
| 910 | } |
Dylan Reid | d0c9adc | 2017-10-02 19:04:50 -0700 | [diff] [blame] | 911 | "disable-sandbox" => { |
Lepton Wu | 9105e9f | 2019-03-14 11:38:31 -0700 | [diff] [blame] | 912 | cfg.sandbox = false; |
Dylan Reid | d0c9adc | 2017-10-02 19:04:50 -0700 | [diff] [blame] | 913 | } |
Chirantan Ekbote | 88f9cba | 2017-08-28 09:51:18 -0700 | [diff] [blame] | 914 | "cid" => { |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 915 | if cfg.cid.is_some() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 916 | return Err(argument::Error::TooManyArguments( |
| 917 | "`cid` alread given".to_owned(), |
| 918 | )); |
Chirantan Ekbote | 88f9cba | 2017-08-28 09:51:18 -0700 | [diff] [blame] | 919 | } |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 920 | cfg.cid = Some( |
| 921 | value |
| 922 | .unwrap() |
| 923 | .parse() |
| 924 | .map_err(|_| argument::Error::InvalidValue { |
| 925 | value: value.unwrap().to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 926 | expected: String::from("this value for `cid` must be an unsigned integer"), |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 927 | })?, |
| 928 | ); |
Chirantan Ekbote | 88f9cba | 2017-08-28 09:51:18 -0700 | [diff] [blame] | 929 | } |
Chirantan Ekbote | ebd5681 | 2018-04-16 19:32:04 -0700 | [diff] [blame] | 930 | "shared-dir" => { |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 931 | // This is formatted as multiple fields, each separated by ":". The first 2 fields are |
| 932 | // fixed (src:tag). The rest may appear in any order: |
| 933 | // |
| 934 | // * type=TYPE - must be one of "p9" or "fs" (default: p9) |
| 935 | // * uidmap=UIDMAP - a uid map in the format "inner outer count[,inner outer count]" |
| 936 | // (default: "0 <current euid> 1") |
| 937 | // * gidmap=GIDMAP - a gid map in the same format as uidmap |
| 938 | // (default: "0 <current egid> 1") |
| 939 | // * timeout=TIMEOUT - a timeout value in seconds, which indicates how long attributes |
| 940 | // and directory contents should be considered valid (default: 5) |
| 941 | // * cache=CACHE - one of "never", "always", or "auto" (default: auto) |
| 942 | // * writeback=BOOL - indicates whether writeback caching should be enabled (default: false) |
Chirantan Ekbote | ebd5681 | 2018-04-16 19:32:04 -0700 | [diff] [blame] | 943 | let param = value.unwrap(); |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 944 | let mut components = param.split(':'); |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 945 | let src = |
| 946 | PathBuf::from( |
| 947 | components |
| 948 | .next() |
| 949 | .ok_or_else(|| argument::Error::InvalidValue { |
| 950 | value: param.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 951 | expected: String::from("missing source path for `shared-dir`"), |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 952 | })?, |
| 953 | ); |
| 954 | let tag = components |
| 955 | .next() |
| 956 | .ok_or_else(|| argument::Error::InvalidValue { |
Chirantan Ekbote | ebd5681 | 2018-04-16 19:32:04 -0700 | [diff] [blame] | 957 | value: param.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 958 | expected: String::from("missing tag for `shared-dir`"), |
David Tolnay | 2bac1e7 | 2018-12-12 14:33:42 -0800 | [diff] [blame] | 959 | })? |
| 960 | .to_owned(); |
Chirantan Ekbote | ebd5681 | 2018-04-16 19:32:04 -0700 | [diff] [blame] | 961 | |
| 962 | if !src.is_dir() { |
| 963 | return Err(argument::Error::InvalidValue { |
| 964 | value: param.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 965 | expected: String::from("source path for `shared-dir` must be a directory"), |
Chirantan Ekbote | ebd5681 | 2018-04-16 19:32:04 -0700 | [diff] [blame] | 966 | }); |
| 967 | } |
| 968 | |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 969 | let mut shared_dir = SharedDir { |
| 970 | src, |
| 971 | tag, |
| 972 | ..Default::default() |
| 973 | }; |
| 974 | for opt in components { |
| 975 | let mut o = opt.splitn(2, '='); |
| 976 | let kind = o.next().ok_or_else(|| argument::Error::InvalidValue { |
| 977 | value: opt.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 978 | expected: String::from("`shared-dir` options must not be empty"), |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 979 | })?; |
| 980 | let value = o.next().ok_or_else(|| argument::Error::InvalidValue { |
| 981 | value: opt.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 982 | expected: String::from("`shared-dir` options must be of the form `kind=value`"), |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 983 | })?; |
| 984 | |
| 985 | match kind { |
| 986 | "type" => { |
| 987 | shared_dir.kind = |
| 988 | value.parse().map_err(|_| argument::Error::InvalidValue { |
| 989 | value: value.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 990 | expected: String::from("`type` must be one of `fs` or `9p`"), |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 991 | })? |
| 992 | } |
| 993 | "uidmap" => shared_dir.uid_map = value.into(), |
| 994 | "gidmap" => shared_dir.gid_map = value.into(), |
| 995 | "timeout" => { |
| 996 | let seconds = value.parse().map_err(|_| argument::Error::InvalidValue { |
| 997 | value: value.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 998 | expected: String::from("`timeout` must be an integer"), |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 999 | })?; |
| 1000 | |
| 1001 | let dur = Duration::from_secs(seconds); |
| 1002 | shared_dir.cfg.entry_timeout = dur.clone(); |
| 1003 | shared_dir.cfg.attr_timeout = dur; |
| 1004 | } |
| 1005 | "cache" => { |
| 1006 | let policy = value.parse().map_err(|_| argument::Error::InvalidValue { |
| 1007 | value: value.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1008 | expected: String::from( |
| 1009 | "`cache` must be one of `never`, `always`, or `auto`", |
| 1010 | ), |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1011 | })?; |
| 1012 | shared_dir.cfg.cache_policy = policy; |
| 1013 | } |
| 1014 | "writeback" => { |
| 1015 | let writeback = |
| 1016 | value.parse().map_err(|_| argument::Error::InvalidValue { |
| 1017 | value: value.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1018 | expected: String::from("`writeback` must be a boolean"), |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1019 | })?; |
| 1020 | shared_dir.cfg.writeback = writeback; |
| 1021 | } |
| 1022 | _ => { |
| 1023 | return Err(argument::Error::InvalidValue { |
| 1024 | value: kind.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1025 | expected: String::from("unrecognized option for `shared-dir`"), |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1026 | }) |
| 1027 | } |
| 1028 | } |
| 1029 | } |
| 1030 | cfg.shared_dirs.push(shared_dir); |
Chirantan Ekbote | ebd5681 | 2018-04-16 19:32:04 -0700 | [diff] [blame] | 1031 | } |
Dylan Reid | e026ef0 | 2017-10-02 19:03:52 -0700 | [diff] [blame] | 1032 | "seccomp-policy-dir" => { |
Dylan Reid | d0c9adc | 2017-10-02 19:04:50 -0700 | [diff] [blame] | 1033 | // `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] | 1034 | cfg.seccomp_policy_dir = PathBuf::from(value.unwrap()); |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1035 | } |
Zach Reizner | 4486379 | 2019-06-26 14:22:08 -0700 | [diff] [blame] | 1036 | "seccomp-log-failures" => { |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 1037 | // A side-effect of this flag is to force the use of .policy files |
| 1038 | // instead of .bpf files (.bpf files are expected and assumed to be |
| 1039 | // compiled to fail an unpermitted action with "trap"). |
| 1040 | // Normally crosvm will first attempt to use a .bpf file, and if |
| 1041 | // not present it will then try to use a .policy file. It's up |
| 1042 | // to the build to decide which of these files is present for |
| 1043 | // crosvm to use (for CrOS the build will use .bpf files for |
| 1044 | // x64 builds and .policy files for arm/arm64 builds). |
| 1045 | // |
| 1046 | // This flag will likely work as expected for builds that use |
| 1047 | // .policy files. For builds that only use .bpf files the initial |
| 1048 | // result when using this flag is likely to be a file-not-found |
| 1049 | // error (since the .policy files are not present). |
| 1050 | // For .bpf builds you can either 1) manually add the .policy files, |
| 1051 | // or 2) do not use this command-line parameter and instead |
| 1052 | // temporarily change the build by passing "log" rather than |
| 1053 | // "trap" as the "--default-action" to compile_seccomp_policy.py. |
Zach Reizner | 4486379 | 2019-06-26 14:22:08 -0700 | [diff] [blame] | 1054 | cfg.seccomp_log_failures = true; |
| 1055 | } |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 1056 | "plugin" => { |
Cody Schuffelen | 6d1ab50 | 2019-05-21 12:12:38 -0700 | [diff] [blame] | 1057 | if cfg.executable_path.is_some() { |
| 1058 | return Err(argument::Error::TooManyArguments(format!( |
| 1059 | "A VM executable was already specified: {:?}", |
| 1060 | cfg.executable_path |
| 1061 | ))); |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 1062 | } |
Zach Reizner | cc30d58 | 2018-01-23 21:16:42 -0800 | [diff] [blame] | 1063 | let plugin = PathBuf::from(value.unwrap().to_owned()); |
| 1064 | if plugin.is_relative() { |
| 1065 | return Err(argument::Error::InvalidValue { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1066 | value: plugin.to_string_lossy().into_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1067 | expected: String::from("the plugin path must be an absolute path"), |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1068 | }); |
Zach Reizner | cc30d58 | 2018-01-23 21:16:42 -0800 | [diff] [blame] | 1069 | } |
Cody Schuffelen | 6d1ab50 | 2019-05-21 12:12:38 -0700 | [diff] [blame] | 1070 | cfg.executable_path = Some(Executable::Plugin(plugin)); |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1071 | } |
Dmitry Torokhov | 0f1770d | 2018-05-11 10:57:16 -0700 | [diff] [blame] | 1072 | "plugin-root" => { |
| 1073 | cfg.plugin_root = Some(PathBuf::from(value.unwrap().to_owned())); |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1074 | } |
Chirantan Ekbote | d41d726 | 2018-11-16 16:37:45 -0800 | [diff] [blame] | 1075 | "plugin-mount" => { |
Dmitry Torokhov | c689fd9 | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 1076 | let mount = parse_plugin_mount_option(value.unwrap())?; |
| 1077 | cfg.plugin_mounts.push(mount); |
Chirantan Ekbote | d41d726 | 2018-11-16 16:37:45 -0800 | [diff] [blame] | 1078 | } |
Dmitry Torokhov | 0f4c5ff | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 1079 | "plugin-mount-file" => { |
| 1080 | let file = File::open(value.unwrap()).map_err(|_| argument::Error::InvalidValue { |
| 1081 | value: value.unwrap().to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1082 | expected: String::from("unable to open `plugin-mount-file` file"), |
Dmitry Torokhov | 0f4c5ff | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 1083 | })?; |
| 1084 | let reader = BufReader::new(file); |
| 1085 | for l in reader.lines() { |
| 1086 | let line = l.unwrap(); |
Keiichi Watanabe | 275c1ef | 2020-04-10 21:49:10 +0900 | [diff] [blame] | 1087 | let trimmed_line = line.splitn(2, '#').next().unwrap().trim(); |
Dmitry Torokhov | 0f4c5ff | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 1088 | if !trimmed_line.is_empty() { |
| 1089 | let mount = parse_plugin_mount_option(trimmed_line)?; |
| 1090 | cfg.plugin_mounts.push(mount); |
| 1091 | } |
| 1092 | } |
| 1093 | } |
Dmitry Torokhov | 1a6262b | 2019-03-01 00:34:03 -0800 | [diff] [blame] | 1094 | "plugin-gid-map" => { |
Dmitry Torokhov | c689fd9 | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 1095 | let map = parse_plugin_gid_map_option(value.unwrap())?; |
| 1096 | cfg.plugin_gid_maps.push(map); |
Dmitry Torokhov | 1a6262b | 2019-03-01 00:34:03 -0800 | [diff] [blame] | 1097 | } |
Dmitry Torokhov | 0f4c5ff | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 1098 | "plugin-gid-map-file" => { |
| 1099 | let file = File::open(value.unwrap()).map_err(|_| argument::Error::InvalidValue { |
| 1100 | value: value.unwrap().to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1101 | expected: String::from("unable to open `plugin-gid-map-file` file"), |
Dmitry Torokhov | 0f4c5ff | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 1102 | })?; |
| 1103 | let reader = BufReader::new(file); |
| 1104 | for l in reader.lines() { |
| 1105 | let line = l.unwrap(); |
Keiichi Watanabe | 275c1ef | 2020-04-10 21:49:10 +0900 | [diff] [blame] | 1106 | let trimmed_line = line.splitn(2, '#').next().unwrap().trim(); |
Dmitry Torokhov | 0f4c5ff | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 1107 | if !trimmed_line.is_empty() { |
| 1108 | let map = parse_plugin_gid_map_option(trimmed_line)?; |
| 1109 | cfg.plugin_gid_maps.push(map); |
| 1110 | } |
| 1111 | } |
| 1112 | } |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 1113 | "vhost-net" => cfg.vhost_net = true, |
Chirantan Ekbote | 5f78721 | 2018-05-31 15:31:31 -0700 | [diff] [blame] | 1114 | "tap-fd" => { |
Jorge E. Moreira | b795280 | 2019-02-12 16:43:05 -0800 | [diff] [blame] | 1115 | cfg.tap_fd.push( |
| 1116 | value |
| 1117 | .unwrap() |
| 1118 | .parse() |
| 1119 | .map_err(|_| argument::Error::InvalidValue { |
| 1120 | value: value.unwrap().to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1121 | expected: String::from( |
| 1122 | "this value for `tap-fd` must be an unsigned integer", |
| 1123 | ), |
Jorge E. Moreira | b795280 | 2019-02-12 16:43:05 -0800 | [diff] [blame] | 1124 | })?, |
| 1125 | ); |
Chirantan Ekbote | 5f78721 | 2018-05-31 15:31:31 -0700 | [diff] [blame] | 1126 | } |
Jason Macnak | cc7070b | 2019-11-06 14:48:12 -0800 | [diff] [blame] | 1127 | #[cfg(feature = "gpu")] |
Zach Reizner | 3a8100a | 2017-09-13 19:15:43 -0700 | [diff] [blame] | 1128 | "gpu" => { |
Jason Macnak | cc7070b | 2019-11-06 14:48:12 -0800 | [diff] [blame] | 1129 | let params = parse_gpu_options(value)?; |
| 1130 | cfg.gpu_parameters = Some(params); |
Zach Reizner | 3a8100a | 2017-09-13 19:15:43 -0700 | [diff] [blame] | 1131 | } |
David Tolnay | 43f8e21 | 2019-02-13 17:28:16 -0800 | [diff] [blame] | 1132 | "software-tpm" => { |
| 1133 | cfg.software_tpm = true; |
| 1134 | } |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 1135 | "single-touch" => { |
| 1136 | if cfg.virtio_single_touch.is_some() { |
| 1137 | return Err(argument::Error::TooManyArguments( |
| 1138 | "`single-touch` already given".to_owned(), |
| 1139 | )); |
| 1140 | } |
Keiichi Watanabe | 275c1ef | 2020-04-10 21:49:10 +0900 | [diff] [blame] | 1141 | let mut it = value.unwrap().split(':'); |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 1142 | |
| 1143 | let mut single_touch_spec = |
| 1144 | TouchDeviceOption::new(PathBuf::from(it.next().unwrap().to_owned())); |
| 1145 | if let Some(width) = it.next() { |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 1146 | single_touch_spec.set_width(width.trim().parse().unwrap()); |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 1147 | } |
| 1148 | if let Some(height) = it.next() { |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 1149 | single_touch_spec.set_height(height.trim().parse().unwrap()); |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 1150 | } |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 1151 | cfg.virtio_single_touch = Some(single_touch_spec); |
| 1152 | } |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1153 | "trackpad" => { |
| 1154 | if cfg.virtio_trackpad.is_some() { |
| 1155 | return Err(argument::Error::TooManyArguments( |
| 1156 | "`trackpad` already given".to_owned(), |
| 1157 | )); |
| 1158 | } |
Keiichi Watanabe | 275c1ef | 2020-04-10 21:49:10 +0900 | [diff] [blame] | 1159 | let mut it = value.unwrap().split(':'); |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1160 | |
| 1161 | let mut trackpad_spec = |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 1162 | TouchDeviceOption::new(PathBuf::from(it.next().unwrap().to_owned())); |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1163 | if let Some(width) = it.next() { |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 1164 | trackpad_spec.set_width(width.trim().parse().unwrap()); |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1165 | } |
| 1166 | if let Some(height) = it.next() { |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 1167 | trackpad_spec.set_height(height.trim().parse().unwrap()); |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1168 | } |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1169 | cfg.virtio_trackpad = Some(trackpad_spec); |
| 1170 | } |
| 1171 | "mouse" => { |
| 1172 | if cfg.virtio_mouse.is_some() { |
| 1173 | return Err(argument::Error::TooManyArguments( |
| 1174 | "`mouse` already given".to_owned(), |
| 1175 | )); |
| 1176 | } |
| 1177 | cfg.virtio_mouse = Some(PathBuf::from(value.unwrap().to_owned())); |
| 1178 | } |
| 1179 | "keyboard" => { |
| 1180 | if cfg.virtio_keyboard.is_some() { |
| 1181 | return Err(argument::Error::TooManyArguments( |
| 1182 | "`keyboard` already given".to_owned(), |
| 1183 | )); |
| 1184 | } |
| 1185 | cfg.virtio_keyboard = Some(PathBuf::from(value.unwrap().to_owned())); |
| 1186 | } |
| 1187 | "evdev" => { |
| 1188 | let dev_path = PathBuf::from(value.unwrap()); |
| 1189 | if !dev_path.exists() { |
| 1190 | return Err(argument::Error::InvalidValue { |
| 1191 | value: value.unwrap().to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1192 | expected: String::from("this input device path does not exist"), |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1193 | }); |
| 1194 | } |
| 1195 | cfg.virtio_input_evdevs.push(dev_path); |
| 1196 | } |
Miriam Zimmerman | 26ac928 | 2019-01-29 21:21:48 -0800 | [diff] [blame] | 1197 | "split-irqchip" => { |
| 1198 | cfg.split_irqchip = true; |
| 1199 | } |
Daniel Verkamp | e403f5c | 2018-12-11 16:29:26 -0800 | [diff] [blame] | 1200 | "initrd" => { |
| 1201 | cfg.initrd_path = Some(PathBuf::from(value.unwrap().to_owned())); |
| 1202 | } |
Cody Schuffelen | 6d1ab50 | 2019-05-21 12:12:38 -0700 | [diff] [blame] | 1203 | "bios" => { |
| 1204 | if cfg.executable_path.is_some() { |
| 1205 | return Err(argument::Error::TooManyArguments(format!( |
| 1206 | "A VM executable was already specified: {:?}", |
| 1207 | cfg.executable_path |
| 1208 | ))); |
| 1209 | } |
| 1210 | cfg.executable_path = Some(Executable::Bios(PathBuf::from(value.unwrap().to_owned()))); |
| 1211 | } |
Xiong Zhang | 17b0daf | 2019-04-23 17:14:50 +0800 | [diff] [blame] | 1212 | "vfio" => { |
| 1213 | let vfio_path = PathBuf::from(value.unwrap()); |
| 1214 | if !vfio_path.exists() { |
| 1215 | return Err(argument::Error::InvalidValue { |
| 1216 | value: value.unwrap().to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1217 | expected: String::from("the vfio path does not exist"), |
Xiong Zhang | 17b0daf | 2019-04-23 17:14:50 +0800 | [diff] [blame] | 1218 | }); |
| 1219 | } |
| 1220 | if !vfio_path.is_dir() { |
| 1221 | return Err(argument::Error::InvalidValue { |
| 1222 | value: value.unwrap().to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1223 | expected: String::from("the vfio path should be directory"), |
Xiong Zhang | 17b0daf | 2019-04-23 17:14:50 +0800 | [diff] [blame] | 1224 | }); |
| 1225 | } |
| 1226 | |
Xiong Zhang | 8bb4faa | 2019-11-12 10:06:13 +0800 | [diff] [blame] | 1227 | cfg.vfio.push(vfio_path); |
Xiong Zhang | 17b0daf | 2019-04-23 17:14:50 +0800 | [diff] [blame] | 1228 | } |
| 1229 | |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1230 | "help" => return Err(argument::Error::PrintHelp), |
| 1231 | _ => unreachable!(), |
| 1232 | } |
| 1233 | Ok(()) |
| 1234 | } |
| 1235 | |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 1236 | fn validate_arguments(cfg: &mut Config) -> std::result::Result<(), argument::Error> { |
| 1237 | if cfg.executable_path.is_none() { |
| 1238 | return Err(argument::Error::ExpectedArgument("`KERNEL`".to_owned())); |
| 1239 | } |
| 1240 | if cfg.host_ip.is_some() || cfg.netmask.is_some() || cfg.mac_address.is_some() { |
| 1241 | if cfg.host_ip.is_none() { |
| 1242 | return Err(argument::Error::ExpectedArgument( |
| 1243 | "`host_ip` missing from network config".to_owned(), |
| 1244 | )); |
| 1245 | } |
| 1246 | if cfg.netmask.is_none() { |
| 1247 | return Err(argument::Error::ExpectedArgument( |
| 1248 | "`netmask` missing from network config".to_owned(), |
| 1249 | )); |
| 1250 | } |
| 1251 | if cfg.mac_address.is_none() { |
| 1252 | return Err(argument::Error::ExpectedArgument( |
| 1253 | "`mac` missing from network config".to_owned(), |
| 1254 | )); |
| 1255 | } |
| 1256 | } |
| 1257 | if cfg.plugin_root.is_some() && !executable_is_plugin(&cfg.executable_path) { |
| 1258 | return Err(argument::Error::ExpectedArgument( |
| 1259 | "`plugin-root` requires `plugin`".to_owned(), |
| 1260 | )); |
| 1261 | } |
| 1262 | #[cfg(feature = "gpu")] |
| 1263 | { |
| 1264 | if let Some(gpu_parameters) = cfg.gpu_parameters.as_ref() { |
| 1265 | let (width, height) = (gpu_parameters.display_width, gpu_parameters.display_height); |
| 1266 | if let Some(virtio_single_touch) = cfg.virtio_single_touch.as_mut() { |
| 1267 | virtio_single_touch.set_default_size(width, height); |
| 1268 | } |
| 1269 | } |
| 1270 | } |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame^] | 1271 | set_default_serial_parameters(&mut cfg.serial_parameters); |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 1272 | Ok(()) |
| 1273 | } |
| 1274 | |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 1275 | fn run_vm(args: std::env::Args) -> std::result::Result<(), ()> { |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1276 | let arguments = |
| 1277 | &[Argument::positional("KERNEL", "bzImage of kernel to run"), |
Tristan Muntsinger | 4133b01 | 2018-12-21 16:01:56 -0800 | [diff] [blame] | 1278 | Argument::value("android-fstab", "PATH", "Path to Android fstab"), |
Daniel Verkamp | e403f5c | 2018-12-11 16:29:26 -0800 | [diff] [blame] | 1279 | Argument::short_value('i', "initrd", "PATH", "Initial ramdisk to load."), |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1280 | Argument::short_value('p', |
| 1281 | "params", |
| 1282 | "PARAMS", |
Zach Reizner | bb67871 | 2018-01-30 18:13:04 -0800 | [diff] [blame] | 1283 | "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] | 1284 | Argument::short_value('c', "cpus", "N", "Number of VCPUs. (default: 1)"), |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 1285 | 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] | 1286 | Argument::short_value('m', |
| 1287 | "mem", |
| 1288 | "N", |
| 1289 | "Amount of guest memory in MiB. (default: 256)"), |
| 1290 | Argument::short_value('r', |
| 1291 | "root", |
Daniel Verkamp | e73c80f | 2019-11-08 10:11:16 -0800 | [diff] [blame] | 1292 | "PATH[,key=value[,key=value[,...]]", |
| 1293 | "Path to a root disk image followed by optional comma-separated options. |
| 1294 | Like `--disk` but adds appropriate kernel command line option. |
| 1295 | See --disk for valid options."), |
| 1296 | Argument::value("rwroot", "PATH[,key=value[,key=value[,...]]", "Path to a writable root disk image followed by optional comma-separated options. |
| 1297 | See --disk for valid options."), |
| 1298 | Argument::short_value('d', "disk", "PATH[,key=value[,key=value[,...]]", "Path to a disk image followed by optional comma-separated options. |
| 1299 | Valid keys: |
Daniel Verkamp | 2767223 | 2019-12-06 17:26:55 +1100 | [diff] [blame] | 1300 | sparse=BOOL - Indicates whether the disk should support the discard operation (default: true) |
| 1301 | block_size=BYTES - Set the reported block size of the disk (default: 512)"), |
Daniel Verkamp | e73c80f | 2019-11-08 10:11:16 -0800 | [diff] [blame] | 1302 | Argument::value("rwdisk", "PATH[,key=value[,key=value[,...]]", "Path to a writable disk image followed by optional comma-separated options. |
| 1303 | See --disk for valid options."), |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1304 | Argument::value("rw-pmem-device", "PATH", "Path to a writable disk image."), |
| 1305 | Argument::value("pmem-device", "PATH", "Path to a disk image."), |
Kansho Nishida | 282115b | 2019-12-18 13:13:14 +0900 | [diff] [blame] | 1306 | Argument::value("pstore", "path=PATH,size=SIZE", "Path to pstore buffer backend file follewed by size."), |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1307 | Argument::value("host_ip", |
| 1308 | "IP", |
| 1309 | "IP address to assign to host tap interface."), |
| 1310 | Argument::value("netmask", "NETMASK", "Netmask for VM subnet."), |
| 1311 | Argument::value("mac", "MAC", "MAC address for VM."), |
Xiong Zhang | 773c707 | 2020-03-20 10:39:55 +0800 | [diff] [blame] | 1312 | Argument::value("net-vq-pairs", "N", "virtio net virtual queue paris. (default: 1)"), |
Judy Hsiao | d5c1e96 | 2020-02-04 12:30:01 +0800 | [diff] [blame] | 1313 | Argument::value("ac97", |
| 1314 | "[backend=BACKEND,capture=true,capture_effect=EFFECT]", |
| 1315 | "Comma separated key=value pairs for setting up Ac97 devices. Can be given more than once . |
| 1316 | Possible key values: |
| 1317 | backend=(null, cras) - Where to route the audio device. If not provided, backend will default to null. |
| 1318 | `null` for /dev/null, and cras for CRAS server. |
| 1319 | capture - Enable audio capture |
| 1320 | capture_effects - | separated effects to be enabled for recording. The only supported effect value now is EchoCancellation or aec."), |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 1321 | Argument::value("serial", |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame^] | 1322 | "type=TYPE,[hardware=HW,num=NUM,path=PATH,input=PATH,console,earlycon,stdin]", |
Jason Macnak | cc7070b | 2019-11-06 14:48:12 -0800 | [diff] [blame] | 1323 | "Comma separated key=value pairs for setting up serial devices. Can be given more than once. |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 1324 | Possible key values: |
Jorge E. Moreira | 9c9e0e7 | 2019-05-17 13:57:04 -0700 | [diff] [blame] | 1325 | type=(stdout,syslog,sink,file) - Where to route the serial device |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame^] | 1326 | hardware=(serial,virtio-console) - Which type of serial hardware to emulate. Defaults to 8250 UART (serial). |
Trent Begin | 923bab0 | 2019-06-17 13:48:06 -0600 | [diff] [blame] | 1327 | num=(1,2,3,4) - Serial Device Number. If not provided, num will default to 1. |
Jorge E. Moreira | 9c9e0e7 | 2019-05-17 13:57:04 -0700 | [diff] [blame] | 1328 | path=PATH - The path to the file to write to when type=file |
Iliyan Malchev | 2c1417b | 2020-04-14 09:40:41 -0700 | [diff] [blame] | 1329 | input=PATH - The path to the file to read from when not stdin |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 1330 | console - Use this serial device as the guest console. Can only be given once. Will default to first serial port if not provided. |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame^] | 1331 | earlycon - Use this serial device as the early console. Can only be given once. |
Jorge E. Moreira | 1e26230 | 2019-08-01 14:40:03 -0700 | [diff] [blame] | 1332 | stdin - Direct standard input to this serial device. Can only be given once. Will default to first serial port if not provided. |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 1333 | "), |
| 1334 | Argument::value("syslog-tag", "TAG", "When logging to syslog, use the provided tag."), |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 1335 | Argument::value("x-display", "DISPLAY", "X11 display name to use."), |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 1336 | Argument::flag("display-window-keyboard", "Capture keyboard input from the display window."), |
| 1337 | Argument::flag("display-window-mouse", "Capture keyboard input from the display window."), |
Ryo Hashimoto | 0b788de | 2019-12-10 17:14:13 +0900 | [diff] [blame] | 1338 | Argument::value("wayland-sock", "PATH[,name=NAME]", "Path to the Wayland socket to use. The unnamed one is used for displaying virtual screens. Named ones are only for IPC."), |
David Reveman | 52ba4e5 | 2018-04-22 21:42:09 -0400 | [diff] [blame] | 1339 | #[cfg(feature = "wl-dmabuf")] |
| 1340 | Argument::flag("wayland-dmabuf", "Enable support for DMABufs in Wayland device."), |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1341 | Argument::short_value('s', |
| 1342 | "socket", |
| 1343 | "PATH", |
| 1344 | "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] | 1345 | Argument::flag("disable-sandbox", "Run all devices in one, non-sandboxed process."), |
Chirantan Ekbote | ebd5681 | 2018-04-16 19:32:04 -0700 | [diff] [blame] | 1346 | Argument::value("cid", "CID", "Context ID for virtual sockets."), |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1347 | Argument::value("shared-dir", "PATH:TAG[:type=TYPE:writeback=BOOL:timeout=SECONDS:uidmap=UIDMAP:gidmap=GIDMAP:cache=CACHE]", |
| 1348 | "Colon-separated options for configuring a directory to be shared with the VM. |
| 1349 | The first field is the directory to be shared and the second field is the tag that the VM can use to identify the device. |
| 1350 | The remaining fields are key=value pairs that may appear in any order. Valid keys are: |
| 1351 | type=(p9, fs) - Indicates whether the directory should be shared via virtio-9p or virtio-fs (default: p9). |
| 1352 | uidmap=UIDMAP - The uid map to use for the device's jail in the format \"inner outer count[,inner outer count]\" (default: 0 <current euid> 1). |
| 1353 | gidmap=GIDMAP - The gid map to use for the device's jail in the format \"inner outer count[,inner outer count]\" (default: 0 <current egid> 1). |
| 1354 | cache=(never, auto, always) - Indicates whether the VM can cache the contents of the shared directory (default: auto). When set to \"auto\" and the type is \"fs\", the VM will use close-to-open consistency for file contents. |
| 1355 | timeout=SECONDS - How long the VM should consider file attributes and directory entries to be valid (default: 5). If the VM has exclusive access to the directory, then this should be a large value. If the directory can be modified by other processes, then this should be 0. |
| 1356 | writeback=BOOL - Indicates whether the VM can use writeback caching (default: false). This is only safe to do when the VM has exclusive access to the files in a directory. Additionally, the server should have read permission for all files as the VM may issue read requests even for files that are opened write-only. |
| 1357 | "), |
Dylan Reid | e026ef0 | 2017-10-02 19:03:52 -0700 | [diff] [blame] | 1358 | Argument::value("seccomp-policy-dir", "PATH", "Path to seccomp .policy files."), |
Zach Reizner | 4486379 | 2019-06-26 14:22:08 -0700 | [diff] [blame] | 1359 | Argument::flag("seccomp-log-failures", "Instead of seccomp filter failures being fatal, they will be logged instead."), |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 1360 | #[cfg(feature = "plugin")] |
Zach Reizner | cc30d58 | 2018-01-23 21:16:42 -0800 | [diff] [blame] | 1361 | 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] | 1362 | #[cfg(feature = "plugin")] |
Dmitry Torokhov | 0f1770d | 2018-05-11 10:57:16 -0700 | [diff] [blame] | 1363 | 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] | 1364 | #[cfg(feature = "plugin")] |
Chirantan Ekbote | d41d726 | 2018-11-16 16:37:45 -0800 | [diff] [blame] | 1365 | 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] | 1366 | #[cfg(feature = "plugin")] |
Dmitry Torokhov | 0f4c5ff | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 1367 | Argument::value("plugin-mount-file", "PATH", "Path to the file listing paths be mounted into the plugin's root filesystem. Can be given more than once."), |
| 1368 | #[cfg(feature = "plugin")] |
Dmitry Torokhov | 1a6262b | 2019-03-01 00:34:03 -0800 | [diff] [blame] | 1369 | Argument::value("plugin-gid-map", "GID:GID:INT", "Supplemental GIDs that should be mapped in plugin jail. Can be given more than once."), |
Dmitry Torokhov | 0f4c5ff | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 1370 | #[cfg(feature = "plugin")] |
| 1371 | Argument::value("plugin-gid-map-file", "PATH", "Path to the file listing 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] | 1372 | Argument::flag("vhost-net", "Use vhost for networking."), |
Chirantan Ekbote | 5f78721 | 2018-05-31 15:31:31 -0700 | [diff] [blame] | 1373 | Argument::value("tap-fd", |
| 1374 | "fd", |
Jorge E. Moreira | b795280 | 2019-02-12 16:43:05 -0800 | [diff] [blame] | 1375 | "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] | 1376 | #[cfg(feature = "gpu")] |
Jason Macnak | cc7070b | 2019-11-06 14:48:12 -0800 | [diff] [blame] | 1377 | Argument::flag_or_value("gpu", |
| 1378 | "[width=INT,height=INT]", |
| 1379 | "(EXPERIMENTAL) Comma separated key=value pairs for setting up a virtio-gpu device |
| 1380 | Possible key values: |
Lingfeng Yang | ddbe8b7 | 2020-01-30 10:00:36 -0800 | [diff] [blame] | 1381 | backend=(2d|3d|gfxstream) - Which backend to use for virtio-gpu (determining rendering protocol) |
Jason Macnak | cc7070b | 2019-11-06 14:48:12 -0800 | [diff] [blame] | 1382 | width=INT - The width of the virtual display connected to the virtio-gpu. |
| 1383 | height=INT - The height of the virtual display connected to the virtio-gpu. |
Jason Macnak | bf19558 | 2019-11-20 16:25:49 -0800 | [diff] [blame] | 1384 | egl[=true|=false] - If the virtio-gpu backend should use a EGL context for rendering. |
| 1385 | glx[=true|=false] - If the virtio-gpu backend should use a GLX context for rendering. |
| 1386 | surfaceless[=true|=false] - If the virtio-gpu backend should use a surfaceless context for rendering. |
Jason Macnak | cc7070b | 2019-11-06 14:48:12 -0800 | [diff] [blame] | 1387 | "), |
David Tolnay | 43f8e21 | 2019-02-13 17:28:16 -0800 | [diff] [blame] | 1388 | #[cfg(feature = "tpm")] |
| 1389 | 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] | 1390 | 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] | 1391 | 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] | 1392 | 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)."), |
| 1393 | Argument::value("mouse", "PATH", "Path to a socket from where to read mouse input events and write status updates to."), |
| 1394 | 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] | 1395 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 1396 | Argument::flag("split-irqchip", "(EXPERIMENTAL) enable split-irqchip support"), |
Cody Schuffelen | 6d1ab50 | 2019-05-21 12:12:38 -0700 | [diff] [blame] | 1397 | Argument::value("bios", "PATH", "Path to BIOS/firmware ROM"), |
Xiong Zhang | 17b0daf | 2019-04-23 17:14:50 +0800 | [diff] [blame] | 1398 | Argument::value("vfio", "PATH", "Path to sysfs of pass through or mdev device"), |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1399 | Argument::short_flag('h', "help", "Print help message.")]; |
| 1400 | |
| 1401 | let mut cfg = Config::default(); |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1402 | let match_res = set_arguments(args, &arguments[..], |name, value| { |
| 1403 | set_argument(&mut cfg, name, value) |
David Tolnay | 2bac1e7 | 2018-12-12 14:33:42 -0800 | [diff] [blame] | 1404 | }) |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 1405 | .and_then(|_| validate_arguments(&mut cfg)); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1406 | |
| 1407 | match match_res { |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 1408 | #[cfg(feature = "plugin")] |
Zach Reizner | 267f2c8 | 2019-07-31 17:07:27 -0700 | [diff] [blame] | 1409 | Ok(()) if executable_is_plugin(&cfg.executable_path) => { |
| 1410 | match crosvm::plugin::run_config(cfg) { |
| 1411 | Ok(_) => { |
| 1412 | info!("crosvm and plugin have exited normally"); |
| 1413 | Ok(()) |
| 1414 | } |
| 1415 | Err(e) => { |
| 1416 | error!("{}", e); |
| 1417 | Err(()) |
| 1418 | } |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 1419 | } |
Zach Reizner | 267f2c8 | 2019-07-31 17:07:27 -0700 | [diff] [blame] | 1420 | } |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1421 | Ok(()) => match linux::run_config(cfg) { |
| 1422 | Ok(_) => { |
| 1423 | info!("crosvm has exited normally"); |
| 1424 | Ok(()) |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1425 | } |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1426 | Err(e) => { |
| 1427 | error!("{}", e); |
| 1428 | Err(()) |
| 1429 | } |
| 1430 | }, |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 1431 | Err(argument::Error::PrintHelp) => { |
| 1432 | print_help("crosvm run", "KERNEL", &arguments[..]); |
| 1433 | Ok(()) |
| 1434 | } |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 1435 | Err(e) => { |
Dmitry Torokhov | 470b1e7 | 2020-01-15 12:46:49 -0800 | [diff] [blame] | 1436 | error!("{}", e); |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 1437 | Err(()) |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 1438 | } |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1439 | } |
| 1440 | } |
| 1441 | |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 1442 | fn handle_request( |
| 1443 | request: &VmRequest, |
| 1444 | args: std::env::Args, |
| 1445 | ) -> std::result::Result<VmResponse, ()> { |
| 1446 | let mut return_result = Err(()); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1447 | for socket_path in args { |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 1448 | match UnixSeqpacket::connect(&socket_path) { |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1449 | Ok(s) => { |
Jakub Staron | e7c5905 | 2019-04-09 12:31:14 -0700 | [diff] [blame] | 1450 | let socket: VmControlRequestSocket = MsgSocket::new(s); |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame] | 1451 | if let Err(e) = socket.send(request) { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1452 | error!( |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 1453 | "failed to send request to socket at '{}': {}", |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1454 | socket_path, e |
| 1455 | ); |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame] | 1456 | return_result = Err(()); |
| 1457 | continue; |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1458 | } |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame] | 1459 | match socket.recv() { |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 1460 | Ok(response) => return_result = Ok(response), |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame] | 1461 | Err(e) => { |
| 1462 | error!( |
| 1463 | "failed to send request to socket at2 '{}': {}", |
| 1464 | socket_path, e |
| 1465 | ); |
| 1466 | return_result = Err(()); |
| 1467 | continue; |
| 1468 | } |
Dylan Reid | d443204 | 2017-12-06 18:20:09 -0800 | [diff] [blame] | 1469 | } |
| 1470 | } |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 1471 | Err(e) => { |
| 1472 | error!("failed to connect to socket at '{}': {}", socket_path, e); |
| 1473 | return_result = Err(()); |
| 1474 | } |
Dylan Reid | d443204 | 2017-12-06 18:20:09 -0800 | [diff] [blame] | 1475 | } |
| 1476 | } |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 1477 | |
| 1478 | return_result |
Dylan Reid | d443204 | 2017-12-06 18:20:09 -0800 | [diff] [blame] | 1479 | } |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1480 | |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 1481 | fn vms_request(request: &VmRequest, args: std::env::Args) -> std::result::Result<(), ()> { |
| 1482 | let response = handle_request(request, args)?; |
| 1483 | info!("request response was {}", response); |
| 1484 | Ok(()) |
| 1485 | } |
| 1486 | |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame] | 1487 | fn stop_vms(args: std::env::Args) -> std::result::Result<(), ()> { |
| 1488 | if args.len() == 0 { |
| 1489 | print_help("crosvm stop", "VM_SOCKET...", &[]); |
| 1490 | println!("Stops the crosvm instance listening on each `VM_SOCKET` given."); |
Jianxun Zhang | 56497d2 | 2019-03-04 14:38:24 -0800 | [diff] [blame] | 1491 | return Err(()); |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame] | 1492 | } |
| 1493 | vms_request(&VmRequest::Exit, args) |
| 1494 | } |
| 1495 | |
| 1496 | fn suspend_vms(args: std::env::Args) -> std::result::Result<(), ()> { |
| 1497 | if args.len() == 0 { |
| 1498 | print_help("crosvm suspend", "VM_SOCKET...", &[]); |
| 1499 | println!("Suspends the crosvm instance listening on each `VM_SOCKET` given."); |
Jianxun Zhang | 56497d2 | 2019-03-04 14:38:24 -0800 | [diff] [blame] | 1500 | return Err(()); |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame] | 1501 | } |
| 1502 | vms_request(&VmRequest::Suspend, args) |
| 1503 | } |
| 1504 | |
| 1505 | fn resume_vms(args: std::env::Args) -> std::result::Result<(), ()> { |
| 1506 | if args.len() == 0 { |
| 1507 | print_help("crosvm resume", "VM_SOCKET...", &[]); |
| 1508 | println!("Resumes the crosvm instance listening on each `VM_SOCKET` given."); |
Jianxun Zhang | 56497d2 | 2019-03-04 14:38:24 -0800 | [diff] [blame] | 1509 | return Err(()); |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame] | 1510 | } |
| 1511 | vms_request(&VmRequest::Resume, args) |
| 1512 | } |
| 1513 | |
| 1514 | fn balloon_vms(mut args: std::env::Args) -> std::result::Result<(), ()> { |
| 1515 | if args.len() < 2 { |
| 1516 | print_help("crosvm balloon", "SIZE VM_SOCKET...", &[]); |
| 1517 | println!("Set the ballon size of the crosvm instance to `SIZE` bytes."); |
Jianxun Zhang | 56497d2 | 2019-03-04 14:38:24 -0800 | [diff] [blame] | 1518 | return Err(()); |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame] | 1519 | } |
Keiichi Watanabe | 275c1ef | 2020-04-10 21:49:10 +0900 | [diff] [blame] | 1520 | let num_bytes = match args.next().unwrap().parse::<u64>() { |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame] | 1521 | Ok(n) => n, |
| 1522 | Err(_) => { |
| 1523 | error!("Failed to parse number of bytes"); |
| 1524 | return Err(()); |
| 1525 | } |
| 1526 | }; |
| 1527 | |
Jakub Staron | 1f828d7 | 2019-04-11 12:49:29 -0700 | [diff] [blame] | 1528 | let command = BalloonControlCommand::Adjust { num_bytes }; |
| 1529 | vms_request(&VmRequest::BalloonCommand(command), args) |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame] | 1530 | } |
| 1531 | |
Charles William Dick | ed22f6b | 2020-04-08 11:05:24 +0900 | [diff] [blame] | 1532 | fn balloon_stats(args: std::env::Args) -> std::result::Result<(), ()> { |
| 1533 | if args.len() != 1 { |
| 1534 | print_help("crosvm balloon_stats", "VM_SOCKET", &[]); |
| 1535 | println!("Prints virtio balloon statistics for a `VM_SOCKET`."); |
| 1536 | return Err(()); |
| 1537 | } |
| 1538 | let command = BalloonControlCommand::Stats {}; |
| 1539 | let request = &VmRequest::BalloonCommand(command); |
| 1540 | let response = handle_request(request, args)?; |
| 1541 | println!("{}", response); |
| 1542 | Ok(()) |
| 1543 | } |
| 1544 | |
A. Cody Schuffelen | 9ca6039 | 2019-12-23 18:27:11 -0800 | [diff] [blame] | 1545 | fn create_qcow2(args: std::env::Args) -> std::result::Result<(), ()> { |
| 1546 | let arguments = [ |
| 1547 | Argument::positional("PATH", "where to create the qcow2 image"), |
| 1548 | Argument::positional("[SIZE]", "the expanded size of the image"), |
| 1549 | Argument::value( |
| 1550 | "backing_file", |
| 1551 | "path/to/file", |
| 1552 | " the file to back the image", |
| 1553 | ), |
| 1554 | ]; |
| 1555 | let mut positional_index = 0; |
| 1556 | let mut file_path = String::from(""); |
| 1557 | let mut size: Option<u64> = None; |
| 1558 | let mut backing_file: Option<String> = None; |
| 1559 | set_arguments(args, &arguments[..], |name, value| { |
| 1560 | match (name, positional_index) { |
| 1561 | ("", 0) => { |
| 1562 | // NAME |
| 1563 | positional_index += 1; |
| 1564 | file_path = value.unwrap().to_owned(); |
| 1565 | } |
| 1566 | ("", 1) => { |
| 1567 | // [SIZE] |
| 1568 | positional_index += 1; |
| 1569 | size = Some(value.unwrap().parse::<u64>().map_err(|_| { |
| 1570 | argument::Error::InvalidValue { |
| 1571 | value: value.unwrap().to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1572 | expected: String::from("SIZE should be a nonnegative integer"), |
A. Cody Schuffelen | 9ca6039 | 2019-12-23 18:27:11 -0800 | [diff] [blame] | 1573 | } |
| 1574 | })?); |
| 1575 | } |
| 1576 | ("", _) => { |
| 1577 | return Err(argument::Error::TooManyArguments( |
| 1578 | "Expected at most 2 positional arguments".to_owned(), |
| 1579 | )); |
| 1580 | } |
| 1581 | ("backing_file", _) => { |
| 1582 | backing_file = value.map(|x| x.to_owned()); |
| 1583 | } |
| 1584 | _ => unreachable!(), |
| 1585 | }; |
| 1586 | Ok(()) |
| 1587 | }) |
| 1588 | .map_err(|e| { |
| 1589 | error!("Unable to parse command line arguments: {}", e); |
| 1590 | })?; |
Keiichi Watanabe | 275c1ef | 2020-04-10 21:49:10 +0900 | [diff] [blame] | 1591 | if file_path.is_empty() || !(size.is_some() ^ backing_file.is_some()) { |
A. Cody Schuffelen | 9ca6039 | 2019-12-23 18:27:11 -0800 | [diff] [blame] | 1592 | print_help("crosvm create_qcow2", "PATH [SIZE]", &arguments); |
| 1593 | println!( |
| 1594 | "Create a new QCOW2 image at `PATH` of either the specified `SIZE` in bytes or |
| 1595 | with a '--backing_file'." |
| 1596 | ); |
Jianxun Zhang | 56497d2 | 2019-03-04 14:38:24 -0800 | [diff] [blame] | 1597 | return Err(()); |
Dylan Reid | 2dcb632 | 2018-07-13 10:42:48 -0700 | [diff] [blame] | 1598 | } |
Dylan Reid | 2dcb632 | 2018-07-13 10:42:48 -0700 | [diff] [blame] | 1599 | |
| 1600 | let file = OpenOptions::new() |
| 1601 | .create(true) |
| 1602 | .read(true) |
| 1603 | .write(true) |
A. Cody Schuffelen | 9ca6039 | 2019-12-23 18:27:11 -0800 | [diff] [blame] | 1604 | .truncate(true) |
Dylan Reid | 2dcb632 | 2018-07-13 10:42:48 -0700 | [diff] [blame] | 1605 | .open(&file_path) |
| 1606 | .map_err(|e| { |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 1607 | error!("Failed opening qcow file at '{}': {}", file_path, e); |
Dylan Reid | 2dcb632 | 2018-07-13 10:42:48 -0700 | [diff] [blame] | 1608 | })?; |
| 1609 | |
A. Cody Schuffelen | 9ca6039 | 2019-12-23 18:27:11 -0800 | [diff] [blame] | 1610 | match (size, backing_file) { |
| 1611 | (Some(size), None) => QcowFile::new(file, size).map_err(|e| { |
| 1612 | error!("Failed to create qcow file at '{}': {}", file_path, e); |
| 1613 | })?, |
| 1614 | (None, Some(backing_file)) => { |
| 1615 | QcowFile::new_from_backing(file, &backing_file).map_err(|e| { |
| 1616 | error!("Failed to create qcow file at '{}': {}", file_path, e); |
| 1617 | })? |
| 1618 | } |
| 1619 | _ => unreachable!(), |
| 1620 | }; |
Dylan Reid | 2dcb632 | 2018-07-13 10:42:48 -0700 | [diff] [blame] | 1621 | Ok(()) |
| 1622 | } |
| 1623 | |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 1624 | fn disk_cmd(mut args: std::env::Args) -> std::result::Result<(), ()> { |
| 1625 | if args.len() < 2 { |
| 1626 | print_help("crosvm disk", "SUBCOMMAND VM_SOCKET...", &[]); |
| 1627 | println!("Manage attached virtual disk devices."); |
| 1628 | println!("Subcommands:"); |
| 1629 | println!(" resize DISK_INDEX NEW_SIZE VM_SOCKET"); |
Jianxun Zhang | 56497d2 | 2019-03-04 14:38:24 -0800 | [diff] [blame] | 1630 | return Err(()); |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 1631 | } |
Keiichi Watanabe | 275c1ef | 2020-04-10 21:49:10 +0900 | [diff] [blame] | 1632 | let subcommand: &str = &args.next().unwrap(); |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 1633 | |
| 1634 | let request = match subcommand { |
| 1635 | "resize" => { |
Keiichi Watanabe | 275c1ef | 2020-04-10 21:49:10 +0900 | [diff] [blame] | 1636 | let disk_index = match args.next().unwrap().parse::<usize>() { |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 1637 | Ok(n) => n, |
| 1638 | Err(_) => { |
| 1639 | error!("Failed to parse disk index"); |
| 1640 | return Err(()); |
| 1641 | } |
| 1642 | }; |
| 1643 | |
Keiichi Watanabe | 275c1ef | 2020-04-10 21:49:10 +0900 | [diff] [blame] | 1644 | let new_size = match args.next().unwrap().parse::<u64>() { |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 1645 | Ok(n) => n, |
| 1646 | Err(_) => { |
| 1647 | error!("Failed to parse disk size"); |
| 1648 | return Err(()); |
| 1649 | } |
| 1650 | }; |
| 1651 | |
Jakub Staron | ecf81e0 | 2019-04-11 11:43:39 -0700 | [diff] [blame] | 1652 | VmRequest::DiskCommand { |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 1653 | disk_index, |
Jakub Staron | ecf81e0 | 2019-04-11 11:43:39 -0700 | [diff] [blame] | 1654 | command: DiskControlCommand::Resize { new_size }, |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 1655 | } |
| 1656 | } |
| 1657 | _ => { |
| 1658 | error!("Unknown disk subcommand '{}'", subcommand); |
| 1659 | return Err(()); |
| 1660 | } |
| 1661 | }; |
| 1662 | |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame] | 1663 | vms_request(&request, args) |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 1664 | } |
| 1665 | |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 1666 | enum ModifyUsbError { |
| 1667 | ArgMissing(&'static str), |
| 1668 | ArgParse(&'static str, String), |
| 1669 | ArgParseInt(&'static str, String, ParseIntError), |
| 1670 | FailedFdValidate(sys_util::Error), |
| 1671 | PathDoesNotExist(PathBuf), |
| 1672 | SocketFailed, |
| 1673 | UnexpectedResponse(VmResponse), |
| 1674 | UnknownCommand(String), |
| 1675 | UsbControl(UsbControlResult), |
| 1676 | } |
| 1677 | |
| 1678 | impl fmt::Display for ModifyUsbError { |
| 1679 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 1680 | use self::ModifyUsbError::*; |
| 1681 | |
| 1682 | match self { |
| 1683 | ArgMissing(a) => write!(f, "argument missing: {}", a), |
| 1684 | ArgParse(name, value) => { |
| 1685 | write!(f, "failed to parse argument {} value `{}`", name, value) |
| 1686 | } |
| 1687 | ArgParseInt(name, value, e) => write!( |
| 1688 | f, |
| 1689 | "failed to parse integer argument {} value `{}`: {}", |
| 1690 | name, value, e |
| 1691 | ), |
| 1692 | FailedFdValidate(e) => write!(f, "failed to validate file descriptor: {}", e), |
| 1693 | PathDoesNotExist(p) => write!(f, "path `{}` does not exist", p.display()), |
| 1694 | SocketFailed => write!(f, "socket failed"), |
| 1695 | UnexpectedResponse(r) => write!(f, "unexpected response: {}", r), |
| 1696 | UnknownCommand(c) => write!(f, "unknown command: `{}`", c), |
| 1697 | UsbControl(e) => write!(f, "{}", e), |
| 1698 | } |
| 1699 | } |
| 1700 | } |
| 1701 | |
| 1702 | type ModifyUsbResult<T> = std::result::Result<T, ModifyUsbError>; |
| 1703 | |
| 1704 | fn parse_bus_id_addr(v: &str) -> ModifyUsbResult<(u8, u8, u16, u16)> { |
| 1705 | debug!("parse_bus_id_addr: {}", v); |
Keiichi Watanabe | 275c1ef | 2020-04-10 21:49:10 +0900 | [diff] [blame] | 1706 | let mut ids = v.split(':'); |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 1707 | match (ids.next(), ids.next(), ids.next(), ids.next()) { |
| 1708 | (Some(bus_id), Some(addr), Some(vid), Some(pid)) => { |
| 1709 | let bus_id = bus_id |
| 1710 | .parse::<u8>() |
| 1711 | .map_err(|e| ModifyUsbError::ArgParseInt("bus_id", bus_id.to_owned(), e))?; |
| 1712 | let addr = addr |
| 1713 | .parse::<u8>() |
| 1714 | .map_err(|e| ModifyUsbError::ArgParseInt("addr", addr.to_owned(), e))?; |
| 1715 | let vid = u16::from_str_radix(&vid, 16) |
| 1716 | .map_err(|e| ModifyUsbError::ArgParseInt("vid", vid.to_owned(), e))?; |
| 1717 | let pid = u16::from_str_radix(&pid, 16) |
| 1718 | .map_err(|e| ModifyUsbError::ArgParseInt("pid", pid.to_owned(), e))?; |
| 1719 | Ok((bus_id, addr, vid, pid)) |
| 1720 | } |
| 1721 | _ => Err(ModifyUsbError::ArgParse( |
| 1722 | "BUS_ID_ADDR_BUS_NUM_DEV_NUM", |
| 1723 | v.to_owned(), |
| 1724 | )), |
| 1725 | } |
| 1726 | } |
| 1727 | |
| 1728 | fn raw_fd_from_path(path: &Path) -> ModifyUsbResult<RawFd> { |
| 1729 | if !path.exists() { |
| 1730 | return Err(ModifyUsbError::PathDoesNotExist(path.to_owned())); |
| 1731 | } |
| 1732 | let raw_fd = path |
| 1733 | .file_name() |
| 1734 | .and_then(|fd_osstr| fd_osstr.to_str()) |
| 1735 | .map_or( |
| 1736 | Err(ModifyUsbError::ArgParse( |
| 1737 | "USB_DEVICE_PATH", |
| 1738 | path.to_string_lossy().into_owned(), |
| 1739 | )), |
| 1740 | |fd_str| { |
| 1741 | fd_str.parse::<libc::c_int>().map_err(|e| { |
| 1742 | ModifyUsbError::ArgParseInt("USB_DEVICE_PATH", fd_str.to_owned(), e) |
| 1743 | }) |
| 1744 | }, |
| 1745 | )?; |
David Tolnay | 5fb3f51 | 2019-04-12 19:22:33 -0700 | [diff] [blame] | 1746 | validate_raw_fd(raw_fd).map_err(ModifyUsbError::FailedFdValidate) |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 1747 | } |
| 1748 | |
| 1749 | fn usb_attach(mut args: std::env::Args) -> ModifyUsbResult<UsbControlResult> { |
| 1750 | let val = args |
| 1751 | .next() |
| 1752 | .ok_or(ModifyUsbError::ArgMissing("BUS_ID_ADDR_BUS_NUM_DEV_NUM"))?; |
| 1753 | let (bus, addr, vid, pid) = parse_bus_id_addr(&val)?; |
| 1754 | let dev_path = PathBuf::from( |
| 1755 | args.next() |
| 1756 | .ok_or(ModifyUsbError::ArgMissing("usb device path"))?, |
| 1757 | ); |
| 1758 | let usb_file: Option<File> = if dev_path == Path::new("-") { |
| 1759 | None |
| 1760 | } else if dev_path.parent() == Some(Path::new("/proc/self/fd")) { |
| 1761 | // Special case '/proc/self/fd/*' paths. The FD is already open, just use it. |
| 1762 | // Safe because we will validate |raw_fd|. |
| 1763 | Some(unsafe { File::from_raw_fd(raw_fd_from_path(&dev_path)?) }) |
| 1764 | } else { |
| 1765 | Some( |
| 1766 | OpenOptions::new() |
| 1767 | .read(true) |
| 1768 | .write(true) |
| 1769 | .open(&dev_path) |
| 1770 | .map_err(|_| ModifyUsbError::UsbControl(UsbControlResult::FailedToOpenDevice))?, |
| 1771 | ) |
| 1772 | }; |
| 1773 | |
| 1774 | let request = VmRequest::UsbCommand(UsbControlCommand::AttachDevice { |
| 1775 | bus, |
| 1776 | addr, |
| 1777 | vid, |
| 1778 | pid, |
David Tolnay | 5fb3f51 | 2019-04-12 19:22:33 -0700 | [diff] [blame] | 1779 | fd: usb_file.map(MaybeOwnedFd::Owned), |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 1780 | }); |
| 1781 | let response = handle_request(&request, args).map_err(|_| ModifyUsbError::SocketFailed)?; |
| 1782 | match response { |
| 1783 | VmResponse::UsbResponse(usb_resp) => Ok(usb_resp), |
| 1784 | r => Err(ModifyUsbError::UnexpectedResponse(r)), |
| 1785 | } |
| 1786 | } |
| 1787 | |
| 1788 | fn usb_detach(mut args: std::env::Args) -> ModifyUsbResult<UsbControlResult> { |
| 1789 | let port: u8 = args |
| 1790 | .next() |
| 1791 | .map_or(Err(ModifyUsbError::ArgMissing("PORT")), |p| { |
| 1792 | p.parse::<u8>() |
| 1793 | .map_err(|e| ModifyUsbError::ArgParseInt("PORT", p.to_owned(), e)) |
| 1794 | })?; |
| 1795 | let request = VmRequest::UsbCommand(UsbControlCommand::DetachDevice { port }); |
| 1796 | let response = handle_request(&request, args).map_err(|_| ModifyUsbError::SocketFailed)?; |
| 1797 | match response { |
| 1798 | VmResponse::UsbResponse(usb_resp) => Ok(usb_resp), |
| 1799 | r => Err(ModifyUsbError::UnexpectedResponse(r)), |
| 1800 | } |
| 1801 | } |
| 1802 | |
Zach Reizner | aff94ca | 2019-03-18 20:58:31 -0700 | [diff] [blame] | 1803 | fn usb_list(args: std::env::Args) -> ModifyUsbResult<UsbControlResult> { |
| 1804 | let mut ports: [u8; USB_CONTROL_MAX_PORTS] = Default::default(); |
| 1805 | for (index, port) in ports.iter_mut().enumerate() { |
| 1806 | *port = index as u8 |
| 1807 | } |
| 1808 | let request = VmRequest::UsbCommand(UsbControlCommand::ListDevice { ports }); |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 1809 | let response = handle_request(&request, args).map_err(|_| ModifyUsbError::SocketFailed)?; |
| 1810 | match response { |
| 1811 | VmResponse::UsbResponse(usb_resp) => Ok(usb_resp), |
| 1812 | r => Err(ModifyUsbError::UnexpectedResponse(r)), |
| 1813 | } |
| 1814 | } |
| 1815 | |
| 1816 | fn modify_usb(mut args: std::env::Args) -> std::result::Result<(), ()> { |
Zach Reizner | aff94ca | 2019-03-18 20:58:31 -0700 | [diff] [blame] | 1817 | if args.len() < 2 { |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 1818 | print_help("crosvm usb", |
Zach Reizner | aff94ca | 2019-03-18 20:58:31 -0700 | [diff] [blame] | 1819 | "[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] | 1820 | return Err(()); |
| 1821 | } |
| 1822 | |
| 1823 | // This unwrap will not panic because of the above length check. |
| 1824 | let command = args.next().unwrap(); |
| 1825 | let result = match command.as_ref() { |
| 1826 | "attach" => usb_attach(args), |
| 1827 | "detach" => usb_detach(args), |
| 1828 | "list" => usb_list(args), |
| 1829 | other => Err(ModifyUsbError::UnknownCommand(other.to_owned())), |
| 1830 | }; |
| 1831 | match result { |
| 1832 | Ok(response) => { |
| 1833 | println!("{}", response); |
| 1834 | Ok(()) |
| 1835 | } |
| 1836 | Err(e) => { |
| 1837 | println!("error {}", e); |
| 1838 | Err(()) |
| 1839 | } |
| 1840 | } |
| 1841 | } |
| 1842 | |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1843 | fn print_usage() { |
| 1844 | print_help("crosvm", "[stop|run]", &[]); |
| 1845 | println!("Commands:"); |
| 1846 | println!(" stop - Stops crosvm instances via their control sockets."); |
| 1847 | println!(" run - Start a new crosvm instance."); |
Dylan Reid | 2dcb632 | 2018-07-13 10:42:48 -0700 | [diff] [blame] | 1848 | println!(" create_qcow2 - Create a new qcow2 disk image file."); |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 1849 | println!(" disk - Manage attached virtual disk devices."); |
| 1850 | println!(" usb - Manage attached virtual USB devices."); |
Yi Sun | 54305cd | 2020-01-04 00:19:37 +0800 | [diff] [blame] | 1851 | println!(" version - Show package version."); |
| 1852 | } |
| 1853 | |
| 1854 | fn pkg_version() -> std::result::Result<(), ()> { |
| 1855 | const VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION"); |
| 1856 | const PKG_VERSION: Option<&'static str> = option_env!("PKG_VERSION"); |
| 1857 | |
| 1858 | print!("crosvm {}", VERSION.unwrap_or("UNKNOWN")); |
| 1859 | match PKG_VERSION { |
| 1860 | Some(v) => println!("-{}", v), |
Keiichi Watanabe | 275c1ef | 2020-04-10 21:49:10 +0900 | [diff] [blame] | 1861 | None => println!(), |
Yi Sun | 54305cd | 2020-01-04 00:19:37 +0800 | [diff] [blame] | 1862 | } |
| 1863 | Ok(()) |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1864 | } |
| 1865 | |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 1866 | fn crosvm_main() -> std::result::Result<(), ()> { |
Stephen Barber | 56fbf09 | 2017-06-29 16:12:14 -0700 | [diff] [blame] | 1867 | if let Err(e) = syslog::init() { |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 1868 | println!("failed to initialize syslog: {}", e); |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 1869 | return Err(()); |
Stephen Barber | 56fbf09 | 2017-06-29 16:12:14 -0700 | [diff] [blame] | 1870 | } |
Zach Reizner | 639d967 | 2017-05-01 17:57:18 -0700 | [diff] [blame] | 1871 | |
Zach Reizner | b3fa5c9 | 2019-01-28 14:05:23 -0800 | [diff] [blame] | 1872 | panic_hook::set_panic_hook(); |
| 1873 | |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1874 | let mut args = std::env::args(); |
| 1875 | if args.next().is_none() { |
| 1876 | error!("expected executable name"); |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 1877 | return Err(()); |
Zach Reizner | 639d967 | 2017-05-01 17:57:18 -0700 | [diff] [blame] | 1878 | } |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1879 | |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 1880 | // 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] | 1881 | let ret = match args.next().as_ref().map(|a| a.as_ref()) { |
| 1882 | None => { |
| 1883 | print_usage(); |
| 1884 | Ok(()) |
| 1885 | } |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1886 | Some("stop") => stop_vms(args), |
Zach Reizner | 6a8fdd9 | 2019-01-16 14:38:41 -0800 | [diff] [blame] | 1887 | Some("suspend") => suspend_vms(args), |
| 1888 | Some("resume") => resume_vms(args), |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1889 | Some("run") => run_vm(args), |
| 1890 | Some("balloon") => balloon_vms(args), |
Charles William Dick | ed22f6b | 2020-04-08 11:05:24 +0900 | [diff] [blame] | 1891 | Some("balloon_stats") => balloon_stats(args), |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1892 | Some("create_qcow2") => create_qcow2(args), |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 1893 | Some("disk") => disk_cmd(args), |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 1894 | Some("usb") => modify_usb(args), |
Yi Sun | 54305cd | 2020-01-04 00:19:37 +0800 | [diff] [blame] | 1895 | Some("version") => pkg_version(), |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1896 | Some(c) => { |
| 1897 | println!("invalid subcommand: {:?}", c); |
| 1898 | print_usage(); |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 1899 | Err(()) |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1900 | } |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 1901 | }; |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1902 | |
| 1903 | // Reap exit status from any child device processes. At this point, all devices should have been |
| 1904 | // dropped in the main process and told to shutdown. Try over a period of 100ms, since it may |
| 1905 | // take some time for the processes to shut down. |
| 1906 | if !wait_all_children() { |
| 1907 | // We gave them a chance, and it's too late. |
| 1908 | warn!("not all child processes have exited; sending SIGKILL"); |
| 1909 | if let Err(e) = kill_process_group() { |
| 1910 | // 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] | 1911 | warn!("unable to kill all child processes: {}", e); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1912 | } |
| 1913 | } |
| 1914 | |
| 1915 | // WARNING: Any code added after this point is not guaranteed to run |
| 1916 | // since we may forcibly kill this process (and its children) above. |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 1917 | ret |
| 1918 | } |
| 1919 | |
| 1920 | fn main() { |
| 1921 | std::process::exit(if crosvm_main().is_ok() { 0 } else { 1 }); |
Zach Reizner | 639d967 | 2017-05-01 17:57:18 -0700 | [diff] [blame] | 1922 | } |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 1923 | |
| 1924 | #[cfg(test)] |
| 1925 | mod tests { |
| 1926 | use super::*; |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 1927 | use crosvm::{DEFAULT_TOUCH_DEVICE_HEIGHT, DEFAULT_TOUCH_DEVICE_WIDTH}; |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 1928 | |
| 1929 | #[test] |
| 1930 | fn parse_cpu_set_single() { |
| 1931 | assert_eq!(parse_cpu_set("123").expect("parse failed"), vec![123]); |
| 1932 | } |
| 1933 | |
| 1934 | #[test] |
| 1935 | fn parse_cpu_set_list() { |
| 1936 | assert_eq!( |
| 1937 | parse_cpu_set("0,1,2,3").expect("parse failed"), |
| 1938 | vec![0, 1, 2, 3] |
| 1939 | ); |
| 1940 | } |
| 1941 | |
| 1942 | #[test] |
| 1943 | fn parse_cpu_set_range() { |
| 1944 | assert_eq!( |
| 1945 | parse_cpu_set("0-3").expect("parse failed"), |
| 1946 | vec![0, 1, 2, 3] |
| 1947 | ); |
| 1948 | } |
| 1949 | |
| 1950 | #[test] |
| 1951 | fn parse_cpu_set_list_of_ranges() { |
| 1952 | assert_eq!( |
| 1953 | parse_cpu_set("3-4,7-9,18").expect("parse failed"), |
| 1954 | vec![3, 4, 7, 8, 9, 18] |
| 1955 | ); |
| 1956 | } |
| 1957 | |
| 1958 | #[test] |
| 1959 | fn parse_cpu_set_repeated() { |
| 1960 | // For now, allow duplicates - they will be handled gracefully by the vec to cpu_set_t conversion. |
| 1961 | assert_eq!(parse_cpu_set("1,1,1").expect("parse failed"), vec![1, 1, 1]); |
| 1962 | } |
| 1963 | |
| 1964 | #[test] |
| 1965 | fn parse_cpu_set_negative() { |
| 1966 | // Negative CPU numbers are not allowed. |
| 1967 | parse_cpu_set("-3").expect_err("parse should have failed"); |
| 1968 | } |
| 1969 | |
| 1970 | #[test] |
| 1971 | fn parse_cpu_set_reverse_range() { |
| 1972 | // Ranges must be from low to high. |
| 1973 | parse_cpu_set("5-2").expect_err("parse should have failed"); |
| 1974 | } |
| 1975 | |
| 1976 | #[test] |
| 1977 | fn parse_cpu_set_open_range() { |
| 1978 | parse_cpu_set("3-").expect_err("parse should have failed"); |
| 1979 | } |
| 1980 | |
| 1981 | #[test] |
| 1982 | fn parse_cpu_set_extra_comma() { |
| 1983 | parse_cpu_set("0,1,2,").expect_err("parse should have failed"); |
| 1984 | } |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 1985 | |
| 1986 | #[test] |
Judy Hsiao | d5c1e96 | 2020-02-04 12:30:01 +0800 | [diff] [blame] | 1987 | fn parse_ac97_vaild() { |
| 1988 | parse_ac97_options("backend=cras").expect("parse should have succeded"); |
| 1989 | } |
| 1990 | |
| 1991 | #[test] |
| 1992 | fn parse_ac97_null_vaild() { |
| 1993 | parse_ac97_options("backend=null").expect("parse should have succeded"); |
| 1994 | } |
| 1995 | |
| 1996 | #[test] |
| 1997 | fn parse_ac97_dup_effect_vaild() { |
| 1998 | parse_ac97_options("backend=cras,capture=true,capture_effects=aec|aec") |
| 1999 | .expect("parse should have succeded"); |
| 2000 | } |
| 2001 | |
| 2002 | #[test] |
| 2003 | fn parse_ac97_effect_invaild() { |
| 2004 | parse_ac97_options("backend=cras,capture=true,capture_effects=abc") |
| 2005 | .expect_err("parse should have failed"); |
| 2006 | } |
| 2007 | |
| 2008 | #[test] |
| 2009 | fn parse_ac97_effect_vaild() { |
| 2010 | parse_ac97_options("backend=cras,capture=true,capture_effects=aec") |
| 2011 | .expect("parse should have succeded"); |
| 2012 | } |
| 2013 | |
| 2014 | #[test] |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 2015 | fn parse_serial_vaild() { |
Jorge E. Moreira | 1e26230 | 2019-08-01 14:40:03 -0700 | [diff] [blame] | 2016 | parse_serial_options("type=syslog,num=1,console=true,stdin=true") |
| 2017 | .expect("parse should have succeded"); |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 2018 | } |
| 2019 | |
| 2020 | #[test] |
Trent Begin | 923bab0 | 2019-06-17 13:48:06 -0600 | [diff] [blame] | 2021 | fn parse_serial_valid_no_num() { |
| 2022 | parse_serial_options("type=syslog").expect("parse should have succeded"); |
| 2023 | } |
| 2024 | |
| 2025 | #[test] |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 2026 | fn parse_serial_invalid_type() { |
| 2027 | parse_serial_options("type=wormhole,num=1").expect_err("parse should have failed"); |
| 2028 | } |
| 2029 | |
| 2030 | #[test] |
| 2031 | fn parse_serial_invalid_num_upper() { |
| 2032 | parse_serial_options("type=syslog,num=5").expect_err("parse should have failed"); |
| 2033 | } |
| 2034 | |
| 2035 | #[test] |
| 2036 | fn parse_serial_invalid_num_lower() { |
| 2037 | parse_serial_options("type=syslog,num=0").expect_err("parse should have failed"); |
| 2038 | } |
| 2039 | |
| 2040 | #[test] |
| 2041 | fn parse_serial_invalid_num_string() { |
| 2042 | parse_serial_options("type=syslog,num=number3").expect_err("parse should have failed"); |
| 2043 | } |
| 2044 | |
| 2045 | #[test] |
| 2046 | fn parse_serial_invalid_option() { |
| 2047 | parse_serial_options("type=syslog,speed=lightspeed").expect_err("parse should have failed"); |
| 2048 | } |
Jorge E. Moreira | 1e26230 | 2019-08-01 14:40:03 -0700 | [diff] [blame] | 2049 | |
| 2050 | #[test] |
| 2051 | fn parse_serial_invalid_two_stdin() { |
| 2052 | let mut config = Config::default(); |
| 2053 | set_argument(&mut config, "serial", Some("num=1,type=stdout,stdin=true")) |
| 2054 | .expect("should parse the first serial argument"); |
| 2055 | set_argument(&mut config, "serial", Some("num=2,type=stdout,stdin=true")) |
| 2056 | .expect_err("should fail to parse a second serial port connected to stdin"); |
| 2057 | } |
Dmitry Torokhov | 458bb64 | 2019-12-13 11:47:52 -0800 | [diff] [blame] | 2058 | |
| 2059 | #[test] |
| 2060 | fn parse_plugin_mount_valid() { |
| 2061 | let mut config = Config::default(); |
| 2062 | set_argument( |
| 2063 | &mut config, |
| 2064 | "plugin-mount", |
| 2065 | Some("/dev/null:/dev/zero:true"), |
| 2066 | ) |
| 2067 | .expect("parse should succeed"); |
| 2068 | assert_eq!(config.plugin_mounts[0].src, PathBuf::from("/dev/null")); |
| 2069 | assert_eq!(config.plugin_mounts[0].dst, PathBuf::from("/dev/zero")); |
| 2070 | assert_eq!(config.plugin_mounts[0].writable, true); |
| 2071 | } |
| 2072 | |
| 2073 | #[test] |
| 2074 | fn parse_plugin_mount_valid_shorthand() { |
| 2075 | let mut config = Config::default(); |
| 2076 | set_argument(&mut config, "plugin-mount", Some("/dev/null")).expect("parse should succeed"); |
| 2077 | assert_eq!(config.plugin_mounts[0].dst, PathBuf::from("/dev/null")); |
| 2078 | assert_eq!(config.plugin_mounts[0].writable, false); |
| 2079 | set_argument(&mut config, "plugin-mount", Some("/dev/null:/dev/zero")) |
| 2080 | .expect("parse should succeed"); |
| 2081 | assert_eq!(config.plugin_mounts[1].dst, PathBuf::from("/dev/zero")); |
| 2082 | assert_eq!(config.plugin_mounts[1].writable, false); |
| 2083 | set_argument(&mut config, "plugin-mount", Some("/dev/null::true")) |
| 2084 | .expect("parse should succeed"); |
| 2085 | assert_eq!(config.plugin_mounts[2].dst, PathBuf::from("/dev/null")); |
| 2086 | assert_eq!(config.plugin_mounts[2].writable, true); |
| 2087 | } |
| 2088 | |
| 2089 | #[test] |
| 2090 | fn parse_plugin_mount_invalid() { |
| 2091 | let mut config = Config::default(); |
| 2092 | set_argument(&mut config, "plugin-mount", Some("")).expect_err("parse should fail"); |
| 2093 | set_argument( |
| 2094 | &mut config, |
| 2095 | "plugin-mount", |
| 2096 | Some("/dev/null:/dev/null:true:false"), |
| 2097 | ) |
| 2098 | .expect_err("parse should fail because too many arguments"); |
| 2099 | set_argument(&mut config, "plugin-mount", Some("null:/dev/null:true")) |
| 2100 | .expect_err("parse should fail because source is not absolute"); |
| 2101 | set_argument(&mut config, "plugin-mount", Some("/dev/null:null:true")) |
| 2102 | .expect_err("parse should fail because source is not absolute"); |
| 2103 | set_argument(&mut config, "plugin-mount", Some("/dev/null:null:blah")) |
| 2104 | .expect_err("parse should fail because flag is not boolean"); |
| 2105 | } |
| 2106 | |
| 2107 | #[test] |
| 2108 | fn parse_plugin_gid_map_valid() { |
| 2109 | let mut config = Config::default(); |
| 2110 | set_argument(&mut config, "plugin-gid-map", Some("1:2:3")).expect("parse should succeed"); |
| 2111 | assert_eq!(config.plugin_gid_maps[0].inner, 1); |
| 2112 | assert_eq!(config.plugin_gid_maps[0].outer, 2); |
| 2113 | assert_eq!(config.plugin_gid_maps[0].count, 3); |
| 2114 | } |
| 2115 | |
| 2116 | #[test] |
| 2117 | fn parse_plugin_gid_map_valid_shorthand() { |
| 2118 | let mut config = Config::default(); |
| 2119 | set_argument(&mut config, "plugin-gid-map", Some("1")).expect("parse should succeed"); |
| 2120 | assert_eq!(config.plugin_gid_maps[0].inner, 1); |
| 2121 | assert_eq!(config.plugin_gid_maps[0].outer, 1); |
| 2122 | assert_eq!(config.plugin_gid_maps[0].count, 1); |
| 2123 | set_argument(&mut config, "plugin-gid-map", Some("1:2")).expect("parse should succeed"); |
| 2124 | assert_eq!(config.plugin_gid_maps[1].inner, 1); |
| 2125 | assert_eq!(config.plugin_gid_maps[1].outer, 2); |
| 2126 | assert_eq!(config.plugin_gid_maps[1].count, 1); |
| 2127 | set_argument(&mut config, "plugin-gid-map", Some("1::3")).expect("parse should succeed"); |
| 2128 | assert_eq!(config.plugin_gid_maps[2].inner, 1); |
| 2129 | assert_eq!(config.plugin_gid_maps[2].outer, 1); |
| 2130 | assert_eq!(config.plugin_gid_maps[2].count, 3); |
| 2131 | } |
| 2132 | |
| 2133 | #[test] |
| 2134 | fn parse_plugin_gid_map_invalid() { |
| 2135 | let mut config = Config::default(); |
| 2136 | set_argument(&mut config, "plugin-gid-map", Some("")).expect_err("parse should fail"); |
| 2137 | set_argument(&mut config, "plugin-gid-map", Some("1:2:3:4")) |
| 2138 | .expect_err("parse should fail because too many arguments"); |
| 2139 | set_argument(&mut config, "plugin-gid-map", Some("blah:2:3")) |
| 2140 | .expect_err("parse should fail because inner is not a number"); |
| 2141 | set_argument(&mut config, "plugin-gid-map", Some("1:blah:3")) |
| 2142 | .expect_err("parse should fail because outer is not a number"); |
| 2143 | set_argument(&mut config, "plugin-gid-map", Some("1:2:blah")) |
| 2144 | .expect_err("parse should fail because count is not a number"); |
| 2145 | } |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 2146 | |
| 2147 | #[test] |
| 2148 | fn single_touch_spec_and_track_pad_spec_default_size() { |
| 2149 | let mut config = Config::default(); |
| 2150 | config |
| 2151 | .executable_path |
| 2152 | .replace(Executable::Kernel(PathBuf::from("kernel"))); |
| 2153 | set_argument(&mut config, "single-touch", Some("/dev/single-touch-test")).unwrap(); |
| 2154 | set_argument(&mut config, "trackpad", Some("/dev/single-touch-test")).unwrap(); |
| 2155 | validate_arguments(&mut config).unwrap(); |
| 2156 | assert_eq!( |
| 2157 | config.virtio_single_touch.unwrap().get_size(), |
| 2158 | (DEFAULT_TOUCH_DEVICE_WIDTH, DEFAULT_TOUCH_DEVICE_HEIGHT) |
| 2159 | ); |
| 2160 | assert_eq!( |
| 2161 | config.virtio_trackpad.unwrap().get_size(), |
| 2162 | (DEFAULT_TOUCH_DEVICE_WIDTH, DEFAULT_TOUCH_DEVICE_HEIGHT) |
| 2163 | ); |
| 2164 | } |
| 2165 | |
| 2166 | #[cfg(feature = "gpu")] |
| 2167 | #[test] |
| 2168 | fn single_touch_spec_default_size_from_gpu() { |
| 2169 | let width = 12345u32; |
| 2170 | let height = 54321u32; |
| 2171 | let mut config = Config::default(); |
| 2172 | config |
| 2173 | .executable_path |
| 2174 | .replace(Executable::Kernel(PathBuf::from("kernel"))); |
| 2175 | set_argument(&mut config, "single-touch", Some("/dev/single-touch-test")).unwrap(); |
| 2176 | set_argument( |
| 2177 | &mut config, |
| 2178 | "gpu", |
| 2179 | Some(&format!("width={},height={}", width, height)), |
| 2180 | ) |
| 2181 | .unwrap(); |
| 2182 | validate_arguments(&mut config).unwrap(); |
| 2183 | assert_eq!( |
| 2184 | config.virtio_single_touch.unwrap().get_size(), |
| 2185 | (width, height) |
| 2186 | ); |
| 2187 | } |
| 2188 | |
| 2189 | #[test] |
| 2190 | fn single_touch_spec_and_track_pad_spec_with_size() { |
| 2191 | let width = 12345u32; |
| 2192 | let height = 54321u32; |
| 2193 | let mut config = Config::default(); |
| 2194 | config |
| 2195 | .executable_path |
| 2196 | .replace(Executable::Kernel(PathBuf::from("kernel"))); |
| 2197 | set_argument( |
| 2198 | &mut config, |
| 2199 | "single-touch", |
| 2200 | Some(&format!("/dev/single-touch-test:{}:{}", width, height)), |
| 2201 | ) |
| 2202 | .unwrap(); |
| 2203 | set_argument( |
| 2204 | &mut config, |
| 2205 | "trackpad", |
| 2206 | Some(&format!("/dev/single-touch-test:{}:{}", width, height)), |
| 2207 | ) |
| 2208 | .unwrap(); |
| 2209 | validate_arguments(&mut config).unwrap(); |
| 2210 | assert_eq!( |
| 2211 | config.virtio_single_touch.unwrap().get_size(), |
| 2212 | (width, height) |
| 2213 | ); |
| 2214 | assert_eq!(config.virtio_trackpad.unwrap().get_size(), (width, height)); |
| 2215 | } |
| 2216 | |
| 2217 | #[cfg(feature = "gpu")] |
| 2218 | #[test] |
| 2219 | fn single_touch_spec_with_size_independent_from_gpu() { |
| 2220 | let touch_width = 12345u32; |
| 2221 | let touch_height = 54321u32; |
| 2222 | let display_width = 1234u32; |
| 2223 | let display_height = 5432u32; |
| 2224 | let mut config = Config::default(); |
| 2225 | config |
| 2226 | .executable_path |
| 2227 | .replace(Executable::Kernel(PathBuf::from("kernel"))); |
| 2228 | set_argument( |
| 2229 | &mut config, |
| 2230 | "single-touch", |
| 2231 | Some(&format!( |
| 2232 | "/dev/single-touch-test:{}:{}", |
| 2233 | touch_width, touch_height |
| 2234 | )), |
| 2235 | ) |
| 2236 | .unwrap(); |
| 2237 | set_argument( |
| 2238 | &mut config, |
| 2239 | "gpu", |
| 2240 | Some(&format!( |
| 2241 | "width={},height={}", |
| 2242 | display_width, display_height |
| 2243 | )), |
| 2244 | ) |
| 2245 | .unwrap(); |
| 2246 | validate_arguments(&mut config).unwrap(); |
| 2247 | assert_eq!( |
| 2248 | config.virtio_single_touch.unwrap().get_size(), |
| 2249 | (touch_width, touch_height) |
| 2250 | ); |
| 2251 | } |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 2252 | } |