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 | |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 5 | //! Runs a virtual machine |
Zach Reizner | 639d967 | 2017-05-01 17:57:18 -0700 | [diff] [blame] | 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 | |
Daniel Verkamp | c677fb4 | 2020-09-08 13:47:49 -0700 | [diff] [blame] | 9 | use std::collections::BTreeMap; |
Judy Hsiao | d5c1e96 | 2020-02-04 12:30:01 +0800 | [diff] [blame] | 10 | use std::default::Default; |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 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::path::{Path, PathBuf}; |
Jorge E. Moreira | 359e7de | 2020-12-02 18:25:53 -0800 | [diff] [blame] | 14 | use std::str::FromStr; |
Zach Reizner | 639d967 | 2017-05-01 17:57:18 -0700 | [diff] [blame] | 15 | use std::string::String; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 16 | use std::thread::sleep; |
Stephen Barber | 56fbf09 | 2017-06-29 16:12:14 -0700 | [diff] [blame] | 17 | use std::time::Duration; |
Zach Reizner | 639d967 | 2017-05-01 17:57:18 -0700 | [diff] [blame] | 18 | |
Daniel Verkamp | c677fb4 | 2020-09-08 13:47:49 -0700 | [diff] [blame] | 19 | use arch::{ |
| 20 | set_default_serial_parameters, Pstore, SerialHardware, SerialParameters, SerialType, |
| 21 | VcpuAffinity, |
| 22 | }; |
Kevin Hamacher | 6fc5f20 | 2021-03-18 12:41:23 +0100 | [diff] [blame] | 23 | use base::{debug, error, getpid, info, kill_process_group, reap_child, syslog, warn}; |
Tomasz Jeznach | 3ce7476 | 2021-02-26 01:01:53 -0800 | [diff] [blame] | 24 | #[cfg(feature = "direct")] |
| 25 | use crosvm::DirectIoOption; |
Zach Reizner | 267f2c8 | 2019-07-31 17:07:27 -0700 | [diff] [blame] | 26 | use crosvm::{ |
| 27 | argument::{self, print_help, set_arguments, Argument}, |
Michael Hoyle | e47a500 | 2020-10-15 16:24:13 -0700 | [diff] [blame] | 28 | platform, BindMount, Config, DiskOption, Executable, GidMap, SharedDir, TouchDeviceOption, |
Chirantan Ekbote | 2ee9dcd | 2021-05-26 18:21:44 +0900 | [diff] [blame] | 29 | VhostUserFsOption, VhostUserOption, VhostUserWlOption, DISK_ID_LEN, |
Zach Reizner | 267f2c8 | 2019-07-31 17:07:27 -0700 | [diff] [blame] | 30 | }; |
Jason Macnak | cc7070b | 2019-11-06 14:48:12 -0800 | [diff] [blame] | 31 | #[cfg(feature = "gpu")] |
Jason Macnak | d659a0d | 2021-03-15 15:33:01 -0700 | [diff] [blame^] | 32 | use devices::virtio::gpu::{ |
| 33 | GpuDisplayParameters, GpuMode, GpuParameters, DEFAULT_DISPLAY_HEIGHT, DEFAULT_DISPLAY_WIDTH, |
| 34 | }; |
Andrew Walbran | 413f854 | 2021-01-08 13:29:03 +0000 | [diff] [blame] | 35 | use devices::ProtectionType; |
Andrew Scull | 1590e6f | 2020-03-18 18:00:47 +0000 | [diff] [blame] | 36 | #[cfg(feature = "audio")] |
Daniel Verkamp | fbd6122 | 2020-02-14 16:46:36 -0800 | [diff] [blame] | 37 | use devices::{Ac97Backend, Ac97Parameters}; |
Daniel Verkamp | f2eecc4 | 2019-12-17 17:04:58 -0800 | [diff] [blame] | 38 | use disk::QcowFile; |
Jakub Staron | e7c5905 | 2019-04-09 12:31:14 -0700 | [diff] [blame] | 39 | use vm_control::{ |
Kevin Hamacher | 6fc5f20 | 2021-03-18 12:41:23 +0100 | [diff] [blame] | 40 | client::{ |
| 41 | do_modify_battery, do_usb_attach, do_usb_detach, do_usb_list, handle_request, vms_request, |
| 42 | ModifyUsbError, ModifyUsbResult, |
| 43 | }, |
| 44 | BalloonControlCommand, BatteryType, DiskControlCommand, UsbControlResult, VmRequest, |
Hikaru Nishida | 6b51c75 | 2021-05-21 12:37:43 +0900 | [diff] [blame] | 45 | VmResponse, |
Jakub Staron | e7c5905 | 2019-04-09 12:31:14 -0700 | [diff] [blame] | 46 | }; |
Zach Reizner | 639d967 | 2017-05-01 17:57:18 -0700 | [diff] [blame] | 47 | |
Cody Schuffelen | 6d1ab50 | 2019-05-21 12:12:38 -0700 | [diff] [blame] | 48 | fn executable_is_plugin(executable: &Option<Executable>) -> bool { |
Daniel Verkamp | c26d20b | 2020-11-04 14:39:31 -0800 | [diff] [blame] | 49 | matches!(executable, Some(Executable::Plugin(_))) |
Cody Schuffelen | 6d1ab50 | 2019-05-21 12:12:38 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Stephen Barber | a00753b | 2017-07-18 13:57:26 -0700 | [diff] [blame] | 52 | // Wait for all children to exit. Return true if they have all exited, false |
| 53 | // otherwise. |
| 54 | fn wait_all_children() -> bool { |
Stephen Barber | 49dd2e2 | 2018-10-29 18:29:58 -0700 | [diff] [blame] | 55 | const CHILD_WAIT_MAX_ITER: isize = 100; |
Stephen Barber | a00753b | 2017-07-18 13:57:26 -0700 | [diff] [blame] | 56 | const CHILD_WAIT_MS: u64 = 10; |
| 57 | for _ in 0..CHILD_WAIT_MAX_ITER { |
Stephen Barber | a00753b | 2017-07-18 13:57:26 -0700 | [diff] [blame] | 58 | loop { |
Zach Reizner | 56158c8 | 2017-08-24 13:50:14 -0700 | [diff] [blame] | 59 | match reap_child() { |
| 60 | Ok(0) => break, |
| 61 | // We expect ECHILD which indicates that there were no children left. |
| 62 | Err(e) if e.errno() == libc::ECHILD => return true, |
| 63 | Err(e) => { |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 64 | warn!("error while waiting for children: {}", e); |
Zach Reizner | 56158c8 | 2017-08-24 13:50:14 -0700 | [diff] [blame] | 65 | return false; |
Stephen Barber | a00753b | 2017-07-18 13:57:26 -0700 | [diff] [blame] | 66 | } |
Zach Reizner | 56158c8 | 2017-08-24 13:50:14 -0700 | [diff] [blame] | 67 | // We reaped one child, so continue reaping. |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 68 | _ => {} |
Stephen Barber | a00753b | 2017-07-18 13:57:26 -0700 | [diff] [blame] | 69 | } |
| 70 | } |
Zach Reizner | 56158c8 | 2017-08-24 13:50:14 -0700 | [diff] [blame] | 71 | // There's no timeout option for waitpid which reap_child calls internally, so our only |
| 72 | // recourse is to sleep while waiting for the children to exit. |
Stephen Barber | a00753b | 2017-07-18 13:57:26 -0700 | [diff] [blame] | 73 | sleep(Duration::from_millis(CHILD_WAIT_MS)); |
| 74 | } |
| 75 | |
| 76 | // 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] | 77 | false |
Stephen Barber | a00753b | 2017-07-18 13:57:26 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 80 | /// Parse a comma-separated list of CPU numbers and ranges and convert it to a Vec of CPU numbers. |
| 81 | fn parse_cpu_set(s: &str) -> argument::Result<Vec<usize>> { |
| 82 | let mut cpuset = Vec::new(); |
| 83 | for part in s.split(',') { |
| 84 | let range: Vec<&str> = part.split('-').collect(); |
Keiichi Watanabe | 275c1ef | 2020-04-10 21:49:10 +0900 | [diff] [blame] | 85 | if range.is_empty() || range.len() > 2 { |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 86 | return Err(argument::Error::InvalidValue { |
| 87 | value: part.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 88 | expected: String::from("invalid list syntax"), |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 89 | }); |
| 90 | } |
| 91 | let first_cpu: usize = range[0] |
| 92 | .parse() |
| 93 | .map_err(|_| argument::Error::InvalidValue { |
| 94 | value: part.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 95 | expected: String::from("CPU index must be a non-negative integer"), |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 96 | })?; |
| 97 | let last_cpu: usize = if range.len() == 2 { |
| 98 | range[1] |
| 99 | .parse() |
| 100 | .map_err(|_| argument::Error::InvalidValue { |
| 101 | value: part.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 102 | expected: String::from("CPU index must be a non-negative integer"), |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 103 | })? |
| 104 | } else { |
| 105 | first_cpu |
| 106 | }; |
| 107 | |
| 108 | if last_cpu < first_cpu { |
| 109 | return Err(argument::Error::InvalidValue { |
| 110 | value: part.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 111 | expected: String::from("CPU ranges must be from low to high"), |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 112 | }); |
| 113 | } |
| 114 | |
| 115 | for cpu in first_cpu..=last_cpu { |
| 116 | cpuset.push(cpu); |
| 117 | } |
| 118 | } |
| 119 | Ok(cpuset) |
| 120 | } |
| 121 | |
Daniel Verkamp | c677fb4 | 2020-09-08 13:47:49 -0700 | [diff] [blame] | 122 | /// Parse a list of guest to host CPU mappings. |
| 123 | /// |
| 124 | /// Each mapping consists of a single guest CPU index mapped to one or more host CPUs in the form |
| 125 | /// accepted by `parse_cpu_set`: |
| 126 | /// |
| 127 | /// `<GUEST-CPU>=<HOST-CPU-SET>[:<GUEST-CPU>=<HOST-CPU-SET>[:...]]` |
| 128 | fn parse_cpu_affinity(s: &str) -> argument::Result<VcpuAffinity> { |
| 129 | if s.contains('=') { |
| 130 | let mut affinity_map = BTreeMap::new(); |
| 131 | for cpu_pair in s.split(':') { |
| 132 | let assignment: Vec<&str> = cpu_pair.split('=').collect(); |
| 133 | if assignment.len() != 2 { |
| 134 | return Err(argument::Error::InvalidValue { |
| 135 | value: cpu_pair.to_owned(), |
| 136 | expected: String::from("invalid VCPU assignment syntax"), |
| 137 | }); |
| 138 | } |
| 139 | let guest_cpu = assignment[0] |
| 140 | .parse() |
| 141 | .map_err(|_| argument::Error::InvalidValue { |
| 142 | value: assignment[0].to_owned(), |
| 143 | expected: String::from("CPU index must be a non-negative integer"), |
| 144 | })?; |
| 145 | let host_cpu_set = parse_cpu_set(assignment[1])?; |
| 146 | if affinity_map.insert(guest_cpu, host_cpu_set).is_some() { |
| 147 | return Err(argument::Error::InvalidValue { |
| 148 | value: cpu_pair.to_owned(), |
| 149 | expected: String::from("VCPU index must be unique"), |
| 150 | }); |
| 151 | } |
| 152 | } |
| 153 | Ok(VcpuAffinity::PerVcpu(affinity_map)) |
| 154 | } else { |
| 155 | Ok(VcpuAffinity::Global(parse_cpu_set(s)?)) |
| 156 | } |
| 157 | } |
| 158 | |
Daniel Verkamp | 8a72afc | 2021-03-15 17:55:52 -0700 | [diff] [blame] | 159 | fn parse_cpu_capacity(s: &str, cpu_capacity: &mut BTreeMap<usize, u32>) -> argument::Result<()> { |
| 160 | for cpu_pair in s.split(',') { |
| 161 | let assignment: Vec<&str> = cpu_pair.split('=').collect(); |
| 162 | if assignment.len() != 2 { |
| 163 | return Err(argument::Error::InvalidValue { |
| 164 | value: cpu_pair.to_owned(), |
| 165 | expected: String::from("invalid CPU capacity syntax"), |
| 166 | }); |
| 167 | } |
| 168 | let cpu = assignment[0] |
| 169 | .parse() |
| 170 | .map_err(|_| argument::Error::InvalidValue { |
| 171 | value: assignment[0].to_owned(), |
| 172 | expected: String::from("CPU index must be a non-negative integer"), |
| 173 | })?; |
| 174 | let capacity = assignment[1] |
| 175 | .parse() |
| 176 | .map_err(|_| argument::Error::InvalidValue { |
| 177 | value: assignment[1].to_owned(), |
| 178 | expected: String::from("CPU capacity must be a non-negative integer"), |
| 179 | })?; |
| 180 | if cpu_capacity.insert(cpu, capacity).is_some() { |
| 181 | return Err(argument::Error::InvalidValue { |
| 182 | value: cpu_pair.to_owned(), |
| 183 | expected: String::from("CPU index must be unique"), |
| 184 | }); |
| 185 | } |
| 186 | } |
| 187 | Ok(()) |
| 188 | } |
| 189 | |
Jason Macnak | cc7070b | 2019-11-06 14:48:12 -0800 | [diff] [blame] | 190 | #[cfg(feature = "gpu")] |
Jason Macnak | d659a0d | 2021-03-15 15:33:01 -0700 | [diff] [blame^] | 191 | fn parse_gpu_options(s: Option<&str>, gpu_params: &mut GpuParameters) -> argument::Result<()> { |
Kaiyi Li | dd348a4 | 2020-07-13 11:49:46 -0700 | [diff] [blame] | 192 | #[cfg(feature = "gfxstream")] |
| 193 | let mut vulkan_specified = false; |
| 194 | #[cfg(feature = "gfxstream")] |
| 195 | let mut syncfd_specified = false; |
Jason Macnak | 046ed14 | 2020-10-08 09:11:53 -0700 | [diff] [blame] | 196 | #[cfg(feature = "gfxstream")] |
| 197 | let mut angle_specified = false; |
Jason Macnak | cc7070b | 2019-11-06 14:48:12 -0800 | [diff] [blame] | 198 | |
Jason Macnak | d659a0d | 2021-03-15 15:33:01 -0700 | [diff] [blame^] | 199 | let mut display_w: Option<u32> = None; |
| 200 | let mut display_h: Option<u32> = None; |
| 201 | |
Jason Macnak | cc7070b | 2019-11-06 14:48:12 -0800 | [diff] [blame] | 202 | if let Some(s) = s { |
| 203 | let opts = s |
Keiichi Watanabe | 275c1ef | 2020-04-10 21:49:10 +0900 | [diff] [blame] | 204 | .split(',') |
| 205 | .map(|frag| frag.split('=')) |
Jason Macnak | cc7070b | 2019-11-06 14:48:12 -0800 | [diff] [blame] | 206 | .map(|mut kv| (kv.next().unwrap_or(""), kv.next().unwrap_or(""))); |
| 207 | |
| 208 | for (k, v) in opts { |
| 209 | match k { |
Lingfeng Yang | ddbe8b7 | 2020-01-30 10:00:36 -0800 | [diff] [blame] | 210 | // Deprecated: Specifying --gpu=<mode> Not great as the mode can be set multiple |
Chia-I Wu | e25859b | 2021-04-14 13:57:55 -0700 | [diff] [blame] | 211 | // times if the user specifies several modes (--gpu=2d,virglrenderer,gfxstream) |
Jason Macnak | 327fc24 | 2020-01-10 12:45:36 -0800 | [diff] [blame] | 212 | "2d" | "2D" => { |
| 213 | gpu_params.mode = GpuMode::Mode2D; |
| 214 | } |
Chia-I Wu | e25859b | 2021-04-14 13:57:55 -0700 | [diff] [blame] | 215 | "3d" | "3D" | "virglrenderer" => { |
| 216 | gpu_params.mode = GpuMode::ModeVirglRenderer; |
Jason Macnak | 327fc24 | 2020-01-10 12:45:36 -0800 | [diff] [blame] | 217 | } |
Lingfeng Yang | ddbe8b7 | 2020-01-30 10:00:36 -0800 | [diff] [blame] | 218 | #[cfg(feature = "gfxstream")] |
| 219 | "gfxstream" => { |
Gurchetan Singh | b1394f7 | 2020-10-19 18:31:13 -0700 | [diff] [blame] | 220 | gpu_params.mode = GpuMode::ModeGfxstream; |
Lingfeng Yang | ddbe8b7 | 2020-01-30 10:00:36 -0800 | [diff] [blame] | 221 | } |
| 222 | // Preferred: Specifying --gpu,backend=<mode> |
| 223 | "backend" => match v { |
| 224 | "2d" | "2D" => { |
| 225 | gpu_params.mode = GpuMode::Mode2D; |
| 226 | } |
Chia-I Wu | e25859b | 2021-04-14 13:57:55 -0700 | [diff] [blame] | 227 | "3d" | "3D" | "virglrenderer" => { |
| 228 | gpu_params.mode = GpuMode::ModeVirglRenderer; |
Lingfeng Yang | ddbe8b7 | 2020-01-30 10:00:36 -0800 | [diff] [blame] | 229 | } |
| 230 | #[cfg(feature = "gfxstream")] |
| 231 | "gfxstream" => { |
Gurchetan Singh | b1394f7 | 2020-10-19 18:31:13 -0700 | [diff] [blame] | 232 | gpu_params.mode = GpuMode::ModeGfxstream; |
Lingfeng Yang | ddbe8b7 | 2020-01-30 10:00:36 -0800 | [diff] [blame] | 233 | } |
| 234 | _ => { |
| 235 | return Err(argument::Error::InvalidValue { |
| 236 | value: v.to_string(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 237 | expected: String::from( |
Chia-I Wu | e25859b | 2021-04-14 13:57:55 -0700 | [diff] [blame] | 238 | "gpu parameter 'backend' should be one of (2d|virglrenderer|gfxstream)", |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 239 | ), |
Lingfeng Yang | ddbe8b7 | 2020-01-30 10:00:36 -0800 | [diff] [blame] | 240 | }); |
| 241 | } |
| 242 | }, |
Jason Macnak | bf19558 | 2019-11-20 16:25:49 -0800 | [diff] [blame] | 243 | "egl" => match v { |
| 244 | "true" | "" => { |
| 245 | gpu_params.renderer_use_egl = true; |
| 246 | } |
| 247 | "false" => { |
| 248 | gpu_params.renderer_use_egl = false; |
| 249 | } |
| 250 | _ => { |
| 251 | return Err(argument::Error::InvalidValue { |
| 252 | value: v.to_string(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 253 | expected: String::from("gpu parameter 'egl' should be a boolean"), |
Jason Macnak | bf19558 | 2019-11-20 16:25:49 -0800 | [diff] [blame] | 254 | }); |
| 255 | } |
| 256 | }, |
| 257 | "gles" => match v { |
| 258 | "true" | "" => { |
| 259 | gpu_params.renderer_use_gles = true; |
| 260 | } |
| 261 | "false" => { |
| 262 | gpu_params.renderer_use_gles = false; |
| 263 | } |
| 264 | _ => { |
| 265 | return Err(argument::Error::InvalidValue { |
| 266 | value: v.to_string(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 267 | expected: String::from("gpu parameter 'gles' should be a boolean"), |
Jason Macnak | bf19558 | 2019-11-20 16:25:49 -0800 | [diff] [blame] | 268 | }); |
| 269 | } |
| 270 | }, |
| 271 | "glx" => match v { |
| 272 | "true" | "" => { |
| 273 | gpu_params.renderer_use_glx = true; |
| 274 | } |
| 275 | "false" => { |
| 276 | gpu_params.renderer_use_glx = false; |
| 277 | } |
| 278 | _ => { |
| 279 | return Err(argument::Error::InvalidValue { |
| 280 | value: v.to_string(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 281 | expected: String::from("gpu parameter 'glx' should be a boolean"), |
Jason Macnak | bf19558 | 2019-11-20 16:25:49 -0800 | [diff] [blame] | 282 | }); |
| 283 | } |
| 284 | }, |
| 285 | "surfaceless" => match v { |
| 286 | "true" | "" => { |
| 287 | gpu_params.renderer_use_surfaceless = true; |
| 288 | } |
| 289 | "false" => { |
| 290 | gpu_params.renderer_use_surfaceless = false; |
| 291 | } |
| 292 | _ => { |
| 293 | return Err(argument::Error::InvalidValue { |
| 294 | value: v.to_string(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 295 | expected: String::from( |
| 296 | "gpu parameter 'surfaceless' should be a boolean", |
| 297 | ), |
Jason Macnak | bf19558 | 2019-11-20 16:25:49 -0800 | [diff] [blame] | 298 | }); |
| 299 | } |
| 300 | }, |
Kaiyi Li | 6404e45 | 2020-07-07 19:12:03 -0700 | [diff] [blame] | 301 | #[cfg(feature = "gfxstream")] |
Kaiyi Li | dd348a4 | 2020-07-13 11:49:46 -0700 | [diff] [blame] | 302 | "syncfd" => { |
| 303 | syncfd_specified = true; |
| 304 | match v { |
| 305 | "true" | "" => { |
| 306 | gpu_params.gfxstream_use_syncfd = true; |
| 307 | } |
| 308 | "false" => { |
| 309 | gpu_params.gfxstream_use_syncfd = false; |
| 310 | } |
| 311 | _ => { |
| 312 | return Err(argument::Error::InvalidValue { |
| 313 | value: v.to_string(), |
| 314 | expected: String::from( |
| 315 | "gpu parameter 'syncfd' should be a boolean", |
| 316 | ), |
| 317 | }); |
| 318 | } |
Kaiyi Li | 6404e45 | 2020-07-07 19:12:03 -0700 | [diff] [blame] | 319 | } |
Kaiyi Li | dd348a4 | 2020-07-13 11:49:46 -0700 | [diff] [blame] | 320 | } |
Kaiyi Li | 6c52a2e | 2020-07-07 19:28:32 -0700 | [diff] [blame] | 321 | #[cfg(feature = "gfxstream")] |
Jason Macnak | 046ed14 | 2020-10-08 09:11:53 -0700 | [diff] [blame] | 322 | "angle" => { |
| 323 | angle_specified = true; |
| 324 | match v { |
| 325 | "true" | "" => { |
| 326 | gpu_params.gfxstream_use_guest_angle = true; |
| 327 | } |
| 328 | "false" => { |
| 329 | gpu_params.gfxstream_use_guest_angle = false; |
| 330 | } |
| 331 | _ => { |
| 332 | return Err(argument::Error::InvalidValue { |
| 333 | value: v.to_string(), |
| 334 | expected: String::from("gpu parameter 'angle' should be a boolean"), |
| 335 | }); |
| 336 | } |
| 337 | } |
| 338 | } |
Kaiyi Li | dd348a4 | 2020-07-13 11:49:46 -0700 | [diff] [blame] | 339 | "vulkan" => { |
Chia-I Wu | 6d473b3 | 2021-04-12 10:14:24 -0700 | [diff] [blame] | 340 | #[cfg(feature = "gfxstream")] |
| 341 | { |
| 342 | vulkan_specified = true; |
| 343 | } |
Kaiyi Li | dd348a4 | 2020-07-13 11:49:46 -0700 | [diff] [blame] | 344 | match v { |
| 345 | "true" | "" => { |
Chia-I Wu | 0beb246 | 2021-04-12 09:36:08 -0700 | [diff] [blame] | 346 | gpu_params.use_vulkan = true; |
Kaiyi Li | dd348a4 | 2020-07-13 11:49:46 -0700 | [diff] [blame] | 347 | } |
| 348 | "false" => { |
Chia-I Wu | 0beb246 | 2021-04-12 09:36:08 -0700 | [diff] [blame] | 349 | gpu_params.use_vulkan = false; |
Kaiyi Li | dd348a4 | 2020-07-13 11:49:46 -0700 | [diff] [blame] | 350 | } |
| 351 | _ => { |
| 352 | return Err(argument::Error::InvalidValue { |
| 353 | value: v.to_string(), |
| 354 | expected: String::from( |
| 355 | "gpu parameter 'vulkan' should be a boolean", |
| 356 | ), |
| 357 | }); |
| 358 | } |
Kaiyi Li | 6c52a2e | 2020-07-07 19:28:32 -0700 | [diff] [blame] | 359 | } |
Kaiyi Li | dd348a4 | 2020-07-13 11:49:46 -0700 | [diff] [blame] | 360 | } |
Jason Macnak | cc7070b | 2019-11-06 14:48:12 -0800 | [diff] [blame] | 361 | "width" => { |
Jason Macnak | d659a0d | 2021-03-15 15:33:01 -0700 | [diff] [blame^] | 362 | let width = v |
| 363 | .parse::<u32>() |
| 364 | .map_err(|_| argument::Error::InvalidValue { |
| 365 | value: v.to_string(), |
| 366 | expected: String::from("gpu parameter 'width' must be a valid integer"), |
| 367 | })?; |
| 368 | display_w = Some(width); |
Jason Macnak | cc7070b | 2019-11-06 14:48:12 -0800 | [diff] [blame] | 369 | } |
| 370 | "height" => { |
Jason Macnak | d659a0d | 2021-03-15 15:33:01 -0700 | [diff] [blame^] | 371 | let height = v |
| 372 | .parse::<u32>() |
| 373 | .map_err(|_| argument::Error::InvalidValue { |
| 374 | value: v.to_string(), |
| 375 | expected: String::from( |
| 376 | "gpu parameter 'height' must be a valid integer", |
| 377 | ), |
| 378 | })?; |
| 379 | display_h = Some(height); |
Jason Macnak | cc7070b | 2019-11-06 14:48:12 -0800 | [diff] [blame] | 380 | } |
John Bates | b220eac | 2020-09-14 17:03:02 -0700 | [diff] [blame] | 381 | "cache-path" => gpu_params.cache_path = Some(v.to_string()), |
| 382 | "cache-size" => gpu_params.cache_size = Some(v.to_string()), |
Gurchetan Singh | 401340e | 2021-03-23 09:34:00 -0700 | [diff] [blame] | 383 | "udmabuf" => match v { |
| 384 | "true" | "" => { |
| 385 | gpu_params.udmabuf = true; |
| 386 | } |
| 387 | "false" => { |
| 388 | gpu_params.udmabuf = false; |
| 389 | } |
| 390 | _ => { |
| 391 | return Err(argument::Error::InvalidValue { |
| 392 | value: v.to_string(), |
| 393 | expected: String::from("gpu parameter 'udmabuf' should be a boolean"), |
| 394 | }); |
| 395 | } |
| 396 | }, |
Jason Macnak | cc7070b | 2019-11-06 14:48:12 -0800 | [diff] [blame] | 397 | "" => {} |
| 398 | _ => { |
| 399 | return Err(argument::Error::UnknownArgument(format!( |
| 400 | "gpu parameter {}", |
| 401 | k |
| 402 | ))); |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
Jason Macnak | d659a0d | 2021-03-15 15:33:01 -0700 | [diff] [blame^] | 408 | if display_w.is_some() || display_h.is_some() { |
| 409 | if display_w.is_none() || display_h.is_none() { |
| 410 | return Err(argument::Error::InvalidValue { |
| 411 | value: s.unwrap_or("").to_string(), |
| 412 | expected: String::from( |
| 413 | "gpu must include both 'width' and 'height' if either is supplied", |
| 414 | ), |
| 415 | }); |
| 416 | } |
| 417 | |
| 418 | gpu_params.displays.push(GpuDisplayParameters { |
| 419 | width: display_w.unwrap(), |
| 420 | height: display_h.unwrap(), |
| 421 | }); |
| 422 | } |
| 423 | |
Kaiyi Li | dd348a4 | 2020-07-13 11:49:46 -0700 | [diff] [blame] | 424 | #[cfg(feature = "gfxstream")] |
| 425 | { |
Chia-I Wu | 91df656 | 2021-04-12 09:47:38 -0700 | [diff] [blame] | 426 | if !vulkan_specified && gpu_params.mode == GpuMode::ModeGfxstream { |
| 427 | gpu_params.use_vulkan = true; |
| 428 | } |
| 429 | |
Chia-I Wu | 6d473b3 | 2021-04-12 10:14:24 -0700 | [diff] [blame] | 430 | if syncfd_specified || angle_specified { |
Kaiyi Li | dd348a4 | 2020-07-13 11:49:46 -0700 | [diff] [blame] | 431 | match gpu_params.mode { |
Gurchetan Singh | b1394f7 | 2020-10-19 18:31:13 -0700 | [diff] [blame] | 432 | GpuMode::ModeGfxstream => {} |
Kaiyi Li | dd348a4 | 2020-07-13 11:49:46 -0700 | [diff] [blame] | 433 | _ => { |
| 434 | return Err(argument::Error::UnknownArgument( |
Chia-I Wu | 6d473b3 | 2021-04-12 10:14:24 -0700 | [diff] [blame] | 435 | "gpu parameter syncfd and angle are only supported for gfxstream backend" |
Kaiyi Li | dd348a4 | 2020-07-13 11:49:46 -0700 | [diff] [blame] | 436 | .to_string(), |
| 437 | )); |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | |
Jason Macnak | d659a0d | 2021-03-15 15:33:01 -0700 | [diff] [blame^] | 443 | Ok(()) |
| 444 | } |
| 445 | |
| 446 | #[cfg(feature = "gpu")] |
| 447 | fn parse_gpu_display_options( |
| 448 | s: Option<&str>, |
| 449 | gpu_params: &mut GpuParameters, |
| 450 | ) -> argument::Result<()> { |
| 451 | let mut display_w: Option<u32> = None; |
| 452 | let mut display_h: Option<u32> = None; |
| 453 | |
| 454 | if let Some(s) = s { |
| 455 | let opts = s |
| 456 | .split(',') |
| 457 | .map(|frag| frag.split('=')) |
| 458 | .map(|mut kv| (kv.next().unwrap_or(""), kv.next().unwrap_or(""))); |
| 459 | |
| 460 | for (k, v) in opts { |
| 461 | match k { |
| 462 | "width" => { |
| 463 | let width = v |
| 464 | .parse::<u32>() |
| 465 | .map_err(|_| argument::Error::InvalidValue { |
| 466 | value: v.to_string(), |
| 467 | expected: String::from("gpu parameter 'width' must be a valid integer"), |
| 468 | })?; |
| 469 | display_w = Some(width); |
| 470 | } |
| 471 | "height" => { |
| 472 | let height = v |
| 473 | .parse::<u32>() |
| 474 | .map_err(|_| argument::Error::InvalidValue { |
| 475 | value: v.to_string(), |
| 476 | expected: String::from( |
| 477 | "gpu parameter 'height' must be a valid integer", |
| 478 | ), |
| 479 | })?; |
| 480 | display_h = Some(height); |
| 481 | } |
| 482 | "" => {} |
| 483 | _ => { |
| 484 | return Err(argument::Error::UnknownArgument(format!( |
| 485 | "gpu-display parameter {}", |
| 486 | k |
| 487 | ))); |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | if display_w.is_none() || display_h.is_none() { |
| 494 | return Err(argument::Error::InvalidValue { |
| 495 | value: s.unwrap_or("").to_string(), |
| 496 | expected: String::from("gpu-display must include both 'width' and 'height'"), |
| 497 | }); |
| 498 | } |
| 499 | |
| 500 | gpu_params.displays.push(GpuDisplayParameters { |
| 501 | width: display_w.unwrap(), |
| 502 | height: display_h.unwrap(), |
| 503 | }); |
| 504 | |
| 505 | Ok(()) |
Jason Macnak | cc7070b | 2019-11-06 14:48:12 -0800 | [diff] [blame] | 506 | } |
| 507 | |
Andrew Scull | 1590e6f | 2020-03-18 18:00:47 +0000 | [diff] [blame] | 508 | #[cfg(feature = "audio")] |
Judy Hsiao | d5c1e96 | 2020-02-04 12:30:01 +0800 | [diff] [blame] | 509 | fn parse_ac97_options(s: &str) -> argument::Result<Ac97Parameters> { |
| 510 | let mut ac97_params: Ac97Parameters = Default::default(); |
| 511 | |
| 512 | let opts = s |
Keiichi Watanabe | 275c1ef | 2020-04-10 21:49:10 +0900 | [diff] [blame] | 513 | .split(',') |
| 514 | .map(|frag| frag.split('=')) |
Judy Hsiao | d5c1e96 | 2020-02-04 12:30:01 +0800 | [diff] [blame] | 515 | .map(|mut kv| (kv.next().unwrap_or(""), kv.next().unwrap_or(""))); |
| 516 | |
| 517 | for (k, v) in opts { |
| 518 | match k { |
| 519 | "backend" => { |
| 520 | ac97_params.backend = |
| 521 | v.parse::<Ac97Backend>() |
| 522 | .map_err(|e| argument::Error::InvalidValue { |
| 523 | value: v.to_string(), |
| 524 | expected: e.to_string(), |
| 525 | })?; |
| 526 | } |
| 527 | "capture" => { |
| 528 | ac97_params.capture = v.parse::<bool>().map_err(|e| { |
| 529 | argument::Error::Syntax(format!("invalid capture option: {}", e)) |
| 530 | })?; |
| 531 | } |
paulhsia | 83d5160 | 2021-03-09 17:13:14 +0800 | [diff] [blame] | 532 | "client_type" => { |
| 533 | ac97_params |
| 534 | .set_client_type(v) |
| 535 | .map_err(|e| argument::Error::InvalidValue { |
| 536 | value: v.to_string(), |
| 537 | expected: e.to_string(), |
| 538 | })?; |
| 539 | } |
Jorge E. Moreira | 6a88a5d | 2021-03-12 15:34:46 -0800 | [diff] [blame] | 540 | #[cfg(any(target_os = "linux", target_os = "android"))] |
Jorge E. Moreira | 359e7de | 2020-12-02 18:25:53 -0800 | [diff] [blame] | 541 | "server" => { |
| 542 | ac97_params.vios_server_path = |
| 543 | Some( |
| 544 | PathBuf::from_str(v).map_err(|e| argument::Error::InvalidValue { |
| 545 | value: v.to_string(), |
| 546 | expected: e.to_string(), |
| 547 | })?, |
| 548 | ); |
| 549 | } |
Judy Hsiao | d5c1e96 | 2020-02-04 12:30:01 +0800 | [diff] [blame] | 550 | _ => { |
| 551 | return Err(argument::Error::UnknownArgument(format!( |
| 552 | "unknown ac97 parameter {}", |
| 553 | k |
| 554 | ))); |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | |
Jorge E. Moreira | 359e7de | 2020-12-02 18:25:53 -0800 | [diff] [blame] | 559 | // server is required for and exclusive to vios backend |
Jorge E. Moreira | 6a88a5d | 2021-03-12 15:34:46 -0800 | [diff] [blame] | 560 | #[cfg(any(target_os = "linux", target_os = "android"))] |
Jorge E. Moreira | 359e7de | 2020-12-02 18:25:53 -0800 | [diff] [blame] | 561 | match ac97_params.backend { |
| 562 | Ac97Backend::VIOS => { |
| 563 | if ac97_params.vios_server_path.is_none() { |
| 564 | return Err(argument::Error::ExpectedArgument(String::from( |
| 565 | "server argument is required for VIOS backend", |
| 566 | ))); |
| 567 | } |
| 568 | } |
| 569 | _ => { |
| 570 | if ac97_params.vios_server_path.is_some() { |
| 571 | return Err(argument::Error::UnexpectedValue(String::from( |
| 572 | "server argument is exclusive to the VIOS backend", |
| 573 | ))); |
| 574 | } |
| 575 | } |
| 576 | } |
| 577 | |
Judy Hsiao | d5c1e96 | 2020-02-04 12:30:01 +0800 | [diff] [blame] | 578 | Ok(ac97_params) |
| 579 | } |
| 580 | |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 581 | fn parse_serial_options(s: &str) -> argument::Result<SerialParameters> { |
| 582 | let mut serial_setting = SerialParameters { |
| 583 | type_: SerialType::Sink, |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 584 | hardware: SerialHardware::Serial, |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 585 | path: None, |
Iliyan Malchev | 2c1417b | 2020-04-14 09:40:41 -0700 | [diff] [blame] | 586 | input: None, |
Trent Begin | 923bab0 | 2019-06-17 13:48:06 -0600 | [diff] [blame] | 587 | num: 1, |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 588 | console: false, |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 589 | earlycon: false, |
Jorge E. Moreira | 1e26230 | 2019-08-01 14:40:03 -0700 | [diff] [blame] | 590 | stdin: false, |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 591 | }; |
| 592 | |
| 593 | let opts = s |
Keiichi Watanabe | 275c1ef | 2020-04-10 21:49:10 +0900 | [diff] [blame] | 594 | .split(',') |
Nicholas Hollingum | c76c2da | 2020-09-18 15:53:16 +1000 | [diff] [blame] | 595 | .map(|frag| frag.splitn(2, '=')) |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 596 | .map(|mut kv| (kv.next().unwrap_or(""), kv.next().unwrap_or(""))); |
| 597 | |
| 598 | for (k, v) in opts { |
| 599 | match k { |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 600 | "hardware" => { |
| 601 | serial_setting.hardware = v |
| 602 | .parse::<SerialHardware>() |
| 603 | .map_err(|e| argument::Error::UnknownArgument(format!("{}", e)))? |
| 604 | } |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 605 | "type" => { |
| 606 | serial_setting.type_ = v |
| 607 | .parse::<SerialType>() |
| 608 | .map_err(|e| argument::Error::UnknownArgument(format!("{}", e)))? |
| 609 | } |
| 610 | "num" => { |
| 611 | let num = v.parse::<u8>().map_err(|e| { |
| 612 | argument::Error::Syntax(format!("serial device number is not parsable: {}", e)) |
| 613 | })?; |
A. Cody Schuffelen | 3faab5a | 2020-10-05 17:29:16 -0700 | [diff] [blame] | 614 | if num < 1 { |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 615 | return Err(argument::Error::InvalidValue { |
| 616 | value: num.to_string(), |
A. Cody Schuffelen | 3faab5a | 2020-10-05 17:29:16 -0700 | [diff] [blame] | 617 | expected: String::from("Serial port num must be at least 1"), |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 618 | }); |
| 619 | } |
| 620 | serial_setting.num = num; |
| 621 | } |
| 622 | "console" => { |
| 623 | serial_setting.console = v.parse::<bool>().map_err(|e| { |
| 624 | argument::Error::Syntax(format!( |
| 625 | "serial device console is not parseable: {}", |
| 626 | e |
| 627 | )) |
| 628 | })? |
| 629 | } |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 630 | "earlycon" => { |
| 631 | serial_setting.earlycon = v.parse::<bool>().map_err(|e| { |
| 632 | argument::Error::Syntax(format!( |
| 633 | "serial device earlycon is not parseable: {}", |
| 634 | e, |
| 635 | )) |
| 636 | })? |
| 637 | } |
Jorge E. Moreira | 1e26230 | 2019-08-01 14:40:03 -0700 | [diff] [blame] | 638 | "stdin" => { |
| 639 | serial_setting.stdin = v.parse::<bool>().map_err(|e| { |
| 640 | argument::Error::Syntax(format!("serial device stdin is not parseable: {}", e)) |
Iliyan Malchev | 2c1417b | 2020-04-14 09:40:41 -0700 | [diff] [blame] | 641 | })?; |
| 642 | if serial_setting.stdin && serial_setting.input.is_some() { |
| 643 | return Err(argument::Error::TooManyArguments( |
| 644 | "Cannot specify both stdin and input options".to_string(), |
| 645 | )); |
| 646 | } |
Jorge E. Moreira | 1e26230 | 2019-08-01 14:40:03 -0700 | [diff] [blame] | 647 | } |
Jorge E. Moreira | 9c9e0e7 | 2019-05-17 13:57:04 -0700 | [diff] [blame] | 648 | "path" => serial_setting.path = Some(PathBuf::from(v)), |
Iliyan Malchev | 2c1417b | 2020-04-14 09:40:41 -0700 | [diff] [blame] | 649 | "input" => { |
| 650 | if serial_setting.stdin { |
| 651 | return Err(argument::Error::TooManyArguments( |
| 652 | "Cannot specify both stdin and input options".to_string(), |
| 653 | )); |
| 654 | } |
| 655 | serial_setting.input = Some(PathBuf::from(v)); |
| 656 | } |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 657 | _ => { |
| 658 | return Err(argument::Error::UnknownArgument(format!( |
| 659 | "serial parameter {}", |
| 660 | k |
| 661 | ))); |
| 662 | } |
| 663 | } |
| 664 | } |
| 665 | |
A. Cody Schuffelen | 3faab5a | 2020-10-05 17:29:16 -0700 | [diff] [blame] | 666 | if serial_setting.hardware == SerialHardware::Serial && serial_setting.num > 4 { |
| 667 | return Err(argument::Error::InvalidValue { |
| 668 | value: serial_setting.num.to_string(), |
| 669 | expected: String::from("Serial port num must be 4 or less"), |
| 670 | }); |
| 671 | } |
| 672 | |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 673 | Ok(serial_setting) |
| 674 | } |
| 675 | |
Dmitry Torokhov | c689fd9 | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 676 | fn parse_plugin_mount_option(value: &str) -> argument::Result<BindMount> { |
Keiichi Watanabe | 275c1ef | 2020-04-10 21:49:10 +0900 | [diff] [blame] | 677 | let components: Vec<&str> = value.split(':').collect(); |
Dmitry Torokhov | 458bb64 | 2019-12-13 11:47:52 -0800 | [diff] [blame] | 678 | if components.is_empty() || components.len() > 3 || components[0].is_empty() { |
Dmitry Torokhov | c689fd9 | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 679 | return 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( |
| 682 | "`plugin-mount` should be in a form of: <src>[:[<dst>][:<writable>]]", |
| 683 | ), |
Dmitry Torokhov | c689fd9 | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 684 | }); |
| 685 | } |
| 686 | |
| 687 | let src = PathBuf::from(components[0]); |
| 688 | if src.is_relative() { |
| 689 | return Err(argument::Error::InvalidValue { |
| 690 | value: components[0].to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 691 | expected: String::from("the source path for `plugin-mount` must be absolute"), |
Dmitry Torokhov | c689fd9 | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 692 | }); |
| 693 | } |
| 694 | if !src.exists() { |
| 695 | return Err(argument::Error::InvalidValue { |
| 696 | value: components[0].to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 697 | expected: String::from("the source path for `plugin-mount` does not exist"), |
Dmitry Torokhov | c689fd9 | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 698 | }); |
| 699 | } |
| 700 | |
Dmitry Torokhov | 458bb64 | 2019-12-13 11:47:52 -0800 | [diff] [blame] | 701 | let dst = PathBuf::from(match components.get(1) { |
| 702 | None | Some(&"") => components[0], |
| 703 | Some(path) => path, |
| 704 | }); |
Dmitry Torokhov | c689fd9 | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 705 | if dst.is_relative() { |
| 706 | return Err(argument::Error::InvalidValue { |
| 707 | value: components[1].to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 708 | expected: String::from("the destination path for `plugin-mount` must be absolute"), |
Dmitry Torokhov | c689fd9 | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 709 | }); |
| 710 | } |
| 711 | |
Dmitry Torokhov | 458bb64 | 2019-12-13 11:47:52 -0800 | [diff] [blame] | 712 | let writable: bool = match components.get(2) { |
| 713 | None => false, |
| 714 | Some(s) => s.parse().map_err(|_| argument::Error::InvalidValue { |
Dmitry Torokhov | c689fd9 | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 715 | value: components[2].to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 716 | 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] | 717 | })?, |
| 718 | }; |
Dmitry Torokhov | c689fd9 | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 719 | |
| 720 | Ok(BindMount { src, dst, writable }) |
| 721 | } |
| 722 | |
| 723 | fn parse_plugin_gid_map_option(value: &str) -> argument::Result<GidMap> { |
Keiichi Watanabe | 275c1ef | 2020-04-10 21:49:10 +0900 | [diff] [blame] | 724 | let components: Vec<&str> = value.split(':').collect(); |
Dmitry Torokhov | 458bb64 | 2019-12-13 11:47:52 -0800 | [diff] [blame] | 725 | if components.is_empty() || components.len() > 3 || components[0].is_empty() { |
Dmitry Torokhov | c689fd9 | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 726 | return Err(argument::Error::InvalidValue { |
| 727 | value: value.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 728 | expected: String::from( |
Dmitry Torokhov | 458bb64 | 2019-12-13 11:47:52 -0800 | [diff] [blame] | 729 | "`plugin-gid-map` must have exactly 3 components: <inner>[:[<outer>][:<count>]]", |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 730 | ), |
Dmitry Torokhov | c689fd9 | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 731 | }); |
| 732 | } |
| 733 | |
| 734 | let inner: libc::gid_t = components[0] |
| 735 | .parse() |
| 736 | .map_err(|_| argument::Error::InvalidValue { |
| 737 | value: components[0].to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 738 | 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] | 739 | })?; |
| 740 | |
Dmitry Torokhov | 458bb64 | 2019-12-13 11:47:52 -0800 | [diff] [blame] | 741 | let outer: libc::gid_t = match components.get(1) { |
| 742 | None | Some(&"") => inner, |
| 743 | Some(s) => s.parse().map_err(|_| argument::Error::InvalidValue { |
Dmitry Torokhov | c689fd9 | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 744 | value: components[1].to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 745 | 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] | 746 | })?, |
| 747 | }; |
Dmitry Torokhov | c689fd9 | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 748 | |
Dmitry Torokhov | 458bb64 | 2019-12-13 11:47:52 -0800 | [diff] [blame] | 749 | let count: u32 = match components.get(2) { |
| 750 | None => 1, |
| 751 | Some(s) => s.parse().map_err(|_| argument::Error::InvalidValue { |
Dmitry Torokhov | c689fd9 | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 752 | value: components[2].to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 753 | expected: String::from( |
| 754 | "the <count> component for `plugin-gid-map` is not valid number", |
| 755 | ), |
Dmitry Torokhov | 458bb64 | 2019-12-13 11:47:52 -0800 | [diff] [blame] | 756 | })?, |
| 757 | }; |
Dmitry Torokhov | c689fd9 | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 758 | |
| 759 | Ok(GidMap { |
| 760 | inner, |
| 761 | outer, |
| 762 | count, |
| 763 | }) |
| 764 | } |
| 765 | |
Chuanxiao Dong | fd5626c | 2020-04-27 16:35:33 +0800 | [diff] [blame] | 766 | fn parse_battery_options(s: Option<&str>) -> argument::Result<BatteryType> { |
| 767 | let mut battery_type: BatteryType = Default::default(); |
| 768 | |
| 769 | if let Some(s) = s { |
| 770 | let opts = s |
Andrew Walbran | 9cfdbd9 | 2021-01-11 17:40:34 +0000 | [diff] [blame] | 771 | .split(',') |
| 772 | .map(|frag| frag.split('=')) |
Chuanxiao Dong | fd5626c | 2020-04-27 16:35:33 +0800 | [diff] [blame] | 773 | .map(|mut kv| (kv.next().unwrap_or(""), kv.next().unwrap_or(""))); |
| 774 | |
| 775 | for (k, v) in opts { |
| 776 | match k { |
| 777 | "type" => match v.parse::<BatteryType>() { |
| 778 | Ok(type_) => battery_type = type_, |
| 779 | Err(e) => { |
| 780 | return Err(argument::Error::InvalidValue { |
| 781 | value: v.to_string(), |
| 782 | expected: e.to_string(), |
| 783 | }); |
| 784 | } |
| 785 | }, |
| 786 | "" => {} |
| 787 | _ => { |
| 788 | return Err(argument::Error::UnknownArgument(format!( |
| 789 | "battery parameter {}", |
| 790 | k |
| 791 | ))); |
| 792 | } |
| 793 | } |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | Ok(battery_type) |
| 798 | } |
| 799 | |
Tomasz Jeznach | 3ce7476 | 2021-02-26 01:01:53 -0800 | [diff] [blame] | 800 | #[cfg(feature = "direct")] |
| 801 | fn parse_direct_io_options(s: Option<&str>) -> argument::Result<DirectIoOption> { |
| 802 | let s = s.ok_or(argument::Error::ExpectedValue(String::from( |
| 803 | "expected path@range[,range] value", |
| 804 | )))?; |
| 805 | let parts: Vec<&str> = s.splitn(2, '@').collect(); |
| 806 | if parts.len() != 2 { |
| 807 | return Err(argument::Error::InvalidValue { |
| 808 | value: s.to_string(), |
| 809 | expected: String::from("missing port range, use /path@X-Y,Z,.. syntax"), |
| 810 | }); |
| 811 | } |
| 812 | let path = PathBuf::from(parts[0]); |
| 813 | if !path.exists() { |
| 814 | return Err(argument::Error::InvalidValue { |
| 815 | value: parts[0].to_owned(), |
| 816 | expected: String::from("the path does not exist"), |
| 817 | }); |
| 818 | }; |
| 819 | let ranges: argument::Result<Vec<(u64, u64)>> = parts[1] |
| 820 | .split(',') |
| 821 | .map(|frag| frag.split('-')) |
| 822 | .map(|mut range| { |
| 823 | let base = range |
| 824 | .next() |
| 825 | .map(|v| v.parse::<u64>()) |
| 826 | .map_or(Ok(None), |r| r.map(Some)); |
| 827 | let last = range |
| 828 | .next() |
| 829 | .map(|v| v.parse::<u64>()) |
| 830 | .map_or(Ok(None), |r| r.map(Some)); |
| 831 | (base, last) |
| 832 | }) |
| 833 | .map(|range| match range { |
| 834 | (Ok(Some(base)), Ok(None)) => Ok((base, 1)), |
| 835 | (Ok(Some(base)), Ok(Some(last))) => { |
| 836 | Ok((base, last.saturating_sub(base).saturating_add(1))) |
| 837 | } |
| 838 | (Err(e), _) => Err(argument::Error::InvalidValue { |
| 839 | value: e.to_string(), |
| 840 | expected: String::from("invalid base range value"), |
| 841 | }), |
| 842 | (_, Err(e)) => Err(argument::Error::InvalidValue { |
| 843 | value: e.to_string(), |
| 844 | expected: String::from("invalid last range value"), |
| 845 | }), |
| 846 | _ => Err(argument::Error::InvalidValue { |
| 847 | value: s.to_owned(), |
| 848 | expected: String::from("invalid range format"), |
| 849 | }), |
| 850 | }) |
| 851 | .collect(); |
| 852 | Ok(DirectIoOption { |
| 853 | path, |
| 854 | ranges: ranges?, |
| 855 | }) |
| 856 | } |
| 857 | |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 858 | fn set_argument(cfg: &mut Config, name: &str, value: Option<&str>) -> argument::Result<()> { |
| 859 | match name { |
| 860 | "" => { |
Cody Schuffelen | 6d1ab50 | 2019-05-21 12:12:38 -0700 | [diff] [blame] | 861 | if cfg.executable_path.is_some() { |
| 862 | return Err(argument::Error::TooManyArguments(format!( |
| 863 | "A VM executable was already specified: {:?}", |
| 864 | cfg.executable_path |
| 865 | ))); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 866 | } |
Cody Schuffelen | 6d1ab50 | 2019-05-21 12:12:38 -0700 | [diff] [blame] | 867 | let kernel_path = PathBuf::from(value.unwrap()); |
| 868 | if !kernel_path.exists() { |
| 869 | return Err(argument::Error::InvalidValue { |
| 870 | value: value.unwrap().to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 871 | expected: String::from("this kernel path does not exist"), |
Cody Schuffelen | 6d1ab50 | 2019-05-21 12:12:38 -0700 | [diff] [blame] | 872 | }); |
| 873 | } |
| 874 | cfg.executable_path = Some(Executable::Kernel(kernel_path)); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 875 | } |
Christian Blichmann | 33d5677 | 2021-03-04 19:03:54 +0100 | [diff] [blame] | 876 | "kvm-device" => { |
| 877 | let kvm_device_path = PathBuf::from(value.unwrap()); |
| 878 | if !kvm_device_path.exists() { |
| 879 | return Err(argument::Error::InvalidValue { |
| 880 | value: value.unwrap().to_owned(), |
| 881 | expected: String::from("this kvm device path does not exist"), |
| 882 | }); |
| 883 | } |
| 884 | |
| 885 | cfg.kvm_device_path = kvm_device_path; |
| 886 | } |
Christian Blichmann | 2f5d4b6 | 2021-03-10 18:08:08 +0100 | [diff] [blame] | 887 | "vhost-vsock-device" => { |
| 888 | let vhost_vsock_device_path = PathBuf::from(value.unwrap()); |
| 889 | if !vhost_vsock_device_path.exists() { |
| 890 | return Err(argument::Error::InvalidValue { |
| 891 | value: value.unwrap().to_owned(), |
| 892 | expected: String::from("this vhost-vsock device path does not exist"), |
| 893 | }); |
| 894 | } |
| 895 | |
| 896 | cfg.vhost_vsock_device_path = vhost_vsock_device_path; |
| 897 | } |
| 898 | "vhost-net-device" => { |
| 899 | let vhost_net_device_path = PathBuf::from(value.unwrap()); |
| 900 | if !vhost_net_device_path.exists() { |
| 901 | return Err(argument::Error::InvalidValue { |
| 902 | value: value.unwrap().to_owned(), |
| 903 | expected: String::from("this vhost-vsock device path does not exist"), |
| 904 | }); |
| 905 | } |
| 906 | |
| 907 | cfg.vhost_net_device_path = vhost_net_device_path; |
| 908 | } |
Tristan Muntsinger | 4133b01 | 2018-12-21 16:01:56 -0800 | [diff] [blame] | 909 | "android-fstab" => { |
| 910 | if cfg.android_fstab.is_some() |
| 911 | && !cfg.android_fstab.as_ref().unwrap().as_os_str().is_empty() |
| 912 | { |
| 913 | return Err(argument::Error::TooManyArguments( |
| 914 | "expected exactly one android fstab path".to_owned(), |
| 915 | )); |
| 916 | } else { |
| 917 | let android_fstab = PathBuf::from(value.unwrap()); |
| 918 | if !android_fstab.exists() { |
| 919 | return Err(argument::Error::InvalidValue { |
| 920 | value: value.unwrap().to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 921 | expected: String::from("this android fstab path does not exist"), |
Tristan Muntsinger | 4133b01 | 2018-12-21 16:01:56 -0800 | [diff] [blame] | 922 | }); |
| 923 | } |
| 924 | cfg.android_fstab = Some(android_fstab); |
| 925 | } |
| 926 | } |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 927 | "params" => { |
Zach Reizner | bb67871 | 2018-01-30 18:13:04 -0800 | [diff] [blame] | 928 | cfg.params.push(value.unwrap().to_owned()); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 929 | } |
| 930 | "cpus" => { |
| 931 | if cfg.vcpu_count.is_some() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 932 | return Err(argument::Error::TooManyArguments( |
| 933 | "`cpus` already given".to_owned(), |
| 934 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 935 | } |
| 936 | cfg.vcpu_count = |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 937 | Some( |
| 938 | value |
| 939 | .unwrap() |
| 940 | .parse() |
| 941 | .map_err(|_| argument::Error::InvalidValue { |
| 942 | value: value.unwrap().to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 943 | expected: String::from("this value for `cpus` needs to be integer"), |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 944 | })?, |
| 945 | ) |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 946 | } |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 947 | "cpu-affinity" => { |
Daniel Verkamp | c677fb4 | 2020-09-08 13:47:49 -0700 | [diff] [blame] | 948 | if cfg.vcpu_affinity.is_some() { |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 949 | return Err(argument::Error::TooManyArguments( |
| 950 | "`cpu-affinity` already given".to_owned(), |
| 951 | )); |
| 952 | } |
Daniel Verkamp | c677fb4 | 2020-09-08 13:47:49 -0700 | [diff] [blame] | 953 | cfg.vcpu_affinity = Some(parse_cpu_affinity(value.unwrap())?); |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 954 | } |
Daniel Verkamp | 8a72afc | 2021-03-15 17:55:52 -0700 | [diff] [blame] | 955 | "cpu-cluster" => { |
| 956 | cfg.cpu_clusters.push(parse_cpu_set(value.unwrap())?); |
| 957 | } |
| 958 | "cpu-capacity" => { |
| 959 | parse_cpu_capacity(value.unwrap(), &mut cfg.cpu_capacity)?; |
| 960 | } |
Suleiman Souhlal | 015c3c1 | 2020-10-07 14:15:41 +0900 | [diff] [blame] | 961 | "no-smt" => { |
| 962 | cfg.no_smt = true; |
| 963 | } |
Kansho Nishida | ab205af | 2020-08-13 18:17:50 +0900 | [diff] [blame] | 964 | "rt-cpus" => { |
| 965 | if !cfg.rt_cpus.is_empty() { |
| 966 | return Err(argument::Error::TooManyArguments( |
| 967 | "`rt-cpus` already given".to_owned(), |
| 968 | )); |
| 969 | } |
| 970 | cfg.rt_cpus = parse_cpu_set(value.unwrap())?; |
| 971 | } |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 972 | "mem" => { |
| 973 | if cfg.memory.is_some() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 974 | return Err(argument::Error::TooManyArguments( |
| 975 | "`mem` already given".to_owned(), |
| 976 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 977 | } |
| 978 | cfg.memory = |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 979 | Some( |
| 980 | value |
| 981 | .unwrap() |
| 982 | .parse() |
| 983 | .map_err(|_| argument::Error::InvalidValue { |
| 984 | value: value.unwrap().to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 985 | expected: String::from("this value for `mem` needs to be integer"), |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 986 | })?, |
| 987 | ) |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 988 | } |
Sergey Senozhatsky | 1e369c5 | 2021-04-13 20:23:51 +0900 | [diff] [blame] | 989 | "hugepages" => { |
| 990 | cfg.hugepages = true; |
| 991 | } |
Andrew Scull | 1590e6f | 2020-03-18 18:00:47 +0000 | [diff] [blame] | 992 | #[cfg(feature = "audio")] |
Judy Hsiao | d5c1e96 | 2020-02-04 12:30:01 +0800 | [diff] [blame] | 993 | "ac97" => { |
| 994 | let ac97_params = parse_ac97_options(value.unwrap())?; |
paulhsia | 1647824 | 2020-11-27 14:04:58 +0800 | [diff] [blame] | 995 | // Add kernel parameters related to the intel8x0 driver for ac97 devices once. |
| 996 | if cfg.ac97_parameters.is_empty() { |
| 997 | // Set `inside_vm=1` to save some register read ops in the driver. |
| 998 | cfg.params.push("snd_intel8x0.inside_vm=1".to_string()); |
| 999 | // Set `ac97_clock=48000` to save intel8x0_measure_ac97_clock call in the driver. |
| 1000 | cfg.params.push("snd_intel8x0.ac97_clock=48000".to_string()); |
| 1001 | } |
Judy Hsiao | d5c1e96 | 2020-02-04 12:30:01 +0800 | [diff] [blame] | 1002 | cfg.ac97_parameters.push(ac97_params); |
Dylan Reid | 3082e8e | 2019-01-07 10:33:48 -0800 | [diff] [blame] | 1003 | } |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 1004 | "serial" => { |
| 1005 | let serial_params = parse_serial_options(value.unwrap())?; |
| 1006 | let num = serial_params.num; |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 1007 | let key = (serial_params.hardware, num); |
| 1008 | if cfg.serial_parameters.contains_key(&key) { |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 1009 | return Err(argument::Error::TooManyArguments(format!( |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 1010 | "serial hardware {} num {}", |
| 1011 | serial_params.hardware, num, |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 1012 | ))); |
| 1013 | } |
| 1014 | |
| 1015 | if serial_params.console { |
Jakub Staron | b6515a9 | 2019-06-05 15:18:25 -0700 | [diff] [blame] | 1016 | for params in cfg.serial_parameters.values() { |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 1017 | if params.console { |
| 1018 | return Err(argument::Error::TooManyArguments(format!( |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 1019 | "{} device {} already set as console", |
| 1020 | params.hardware, params.num, |
| 1021 | ))); |
| 1022 | } |
| 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | if serial_params.earlycon { |
| 1027 | // Only SerialHardware::Serial supports earlycon= currently. |
| 1028 | match serial_params.hardware { |
| 1029 | SerialHardware::Serial => {} |
| 1030 | _ => { |
| 1031 | return Err(argument::Error::InvalidValue { |
Andrew Walbran | 9cfdbd9 | 2021-01-11 17:40:34 +0000 | [diff] [blame] | 1032 | value: serial_params.hardware.to_string(), |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 1033 | expected: String::from("earlycon not supported for hardware"), |
| 1034 | }); |
| 1035 | } |
| 1036 | } |
| 1037 | for params in cfg.serial_parameters.values() { |
| 1038 | if params.earlycon { |
| 1039 | return Err(argument::Error::TooManyArguments(format!( |
| 1040 | "{} device {} already set as earlycon", |
| 1041 | params.hardware, params.num, |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 1042 | ))); |
| 1043 | } |
| 1044 | } |
| 1045 | } |
| 1046 | |
Jorge E. Moreira | 1e26230 | 2019-08-01 14:40:03 -0700 | [diff] [blame] | 1047 | if serial_params.stdin { |
| 1048 | if let Some(previous_stdin) = cfg.serial_parameters.values().find(|sp| sp.stdin) { |
| 1049 | return Err(argument::Error::TooManyArguments(format!( |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 1050 | "{} device {} already connected to standard input", |
| 1051 | previous_stdin.hardware, previous_stdin.num, |
Jorge E. Moreira | 1e26230 | 2019-08-01 14:40:03 -0700 | [diff] [blame] | 1052 | ))); |
| 1053 | } |
| 1054 | } |
| 1055 | |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 1056 | cfg.serial_parameters.insert(key, serial_params); |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 1057 | } |
| 1058 | "syslog-tag" => { |
| 1059 | if cfg.syslog_tag.is_some() { |
| 1060 | return Err(argument::Error::TooManyArguments( |
| 1061 | "`syslog-tag` already given".to_owned(), |
| 1062 | )); |
| 1063 | } |
| 1064 | syslog::set_proc_name(value.unwrap()); |
| 1065 | cfg.syslog_tag = Some(value.unwrap().to_owned()); |
| 1066 | } |
Daniel Verkamp | 4b62cd9 | 2019-11-08 13:09:27 -0800 | [diff] [blame] | 1067 | "root" | "rwroot" | "disk" | "rwdisk" => { |
Daniel Verkamp | e73c80f | 2019-11-08 10:11:16 -0800 | [diff] [blame] | 1068 | let param = value.unwrap(); |
| 1069 | let mut components = param.split(','); |
Daniel Verkamp | 6a8cd10 | 2019-06-26 15:17:46 -0700 | [diff] [blame] | 1070 | let read_only = !name.starts_with("rw"); |
Daniel Verkamp | e73c80f | 2019-11-08 10:11:16 -0800 | [diff] [blame] | 1071 | let disk_path = |
| 1072 | PathBuf::from( |
| 1073 | components |
| 1074 | .next() |
| 1075 | .ok_or_else(|| argument::Error::InvalidValue { |
| 1076 | value: param.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1077 | expected: String::from("missing disk path"), |
Daniel Verkamp | e73c80f | 2019-11-08 10:11:16 -0800 | [diff] [blame] | 1078 | })?, |
| 1079 | ); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1080 | if !disk_path.exists() { |
| 1081 | return Err(argument::Error::InvalidValue { |
Daniel Verkamp | e73c80f | 2019-11-08 10:11:16 -0800 | [diff] [blame] | 1082 | value: param.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1083 | expected: String::from("this disk path does not exist"), |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1084 | }); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1085 | } |
Daniel Verkamp | 6a8cd10 | 2019-06-26 15:17:46 -0700 | [diff] [blame] | 1086 | if name.ends_with("root") { |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 1087 | if cfg.disks.len() >= 26 { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1088 | return Err(argument::Error::TooManyArguments( |
| 1089 | "ran out of letters for to assign to root disk".to_owned(), |
| 1090 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1091 | } |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1092 | cfg.params.push(format!( |
Daniel Verkamp | 6a8cd10 | 2019-06-26 15:17:46 -0700 | [diff] [blame] | 1093 | "root=/dev/vd{} {}", |
| 1094 | char::from(b'a' + cfg.disks.len() as u8), |
| 1095 | if read_only { "ro" } else { "rw" } |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1096 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1097 | } |
Daniel Verkamp | e73c80f | 2019-11-08 10:11:16 -0800 | [diff] [blame] | 1098 | |
| 1099 | let mut disk = DiskOption { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1100 | path: disk_path, |
Daniel Verkamp | 6a8cd10 | 2019-06-26 15:17:46 -0700 | [diff] [blame] | 1101 | read_only, |
Daniel Verkamp | e73c80f | 2019-11-08 10:11:16 -0800 | [diff] [blame] | 1102 | sparse: true, |
Daniel Verkamp | 2767223 | 2019-12-06 17:26:55 +1100 | [diff] [blame] | 1103 | block_size: 512, |
Daniel Verkamp | 4e1f99a | 2020-06-01 12:47:21 -0700 | [diff] [blame] | 1104 | id: None, |
Daniel Verkamp | e73c80f | 2019-11-08 10:11:16 -0800 | [diff] [blame] | 1105 | }; |
| 1106 | |
| 1107 | for opt in components { |
| 1108 | let mut o = opt.splitn(2, '='); |
| 1109 | let kind = o.next().ok_or_else(|| argument::Error::InvalidValue { |
| 1110 | value: opt.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1111 | expected: String::from("disk options must not be empty"), |
Daniel Verkamp | e73c80f | 2019-11-08 10:11:16 -0800 | [diff] [blame] | 1112 | })?; |
| 1113 | let value = o.next().ok_or_else(|| argument::Error::InvalidValue { |
| 1114 | value: opt.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1115 | expected: String::from("disk options must be of the form `kind=value`"), |
Daniel Verkamp | e73c80f | 2019-11-08 10:11:16 -0800 | [diff] [blame] | 1116 | })?; |
| 1117 | |
| 1118 | match kind { |
| 1119 | "sparse" => { |
| 1120 | let sparse = value.parse().map_err(|_| argument::Error::InvalidValue { |
| 1121 | value: value.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1122 | expected: String::from("`sparse` must be a boolean"), |
Daniel Verkamp | e73c80f | 2019-11-08 10:11:16 -0800 | [diff] [blame] | 1123 | })?; |
| 1124 | disk.sparse = sparse; |
| 1125 | } |
Daniel Verkamp | 2767223 | 2019-12-06 17:26:55 +1100 | [diff] [blame] | 1126 | "block_size" => { |
| 1127 | let block_size = |
| 1128 | value.parse().map_err(|_| argument::Error::InvalidValue { |
| 1129 | value: value.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1130 | expected: String::from("`block_size` must be an integer"), |
Daniel Verkamp | 2767223 | 2019-12-06 17:26:55 +1100 | [diff] [blame] | 1131 | })?; |
| 1132 | disk.block_size = block_size; |
| 1133 | } |
Daniel Verkamp | 4e1f99a | 2020-06-01 12:47:21 -0700 | [diff] [blame] | 1134 | "id" => { |
| 1135 | if value.len() > DISK_ID_LEN { |
| 1136 | return Err(argument::Error::InvalidValue { |
| 1137 | value: value.to_owned(), |
| 1138 | expected: format!( |
| 1139 | "`id` must be {} or fewer characters", |
| 1140 | DISK_ID_LEN |
| 1141 | ), |
| 1142 | }); |
| 1143 | } |
| 1144 | let mut id = [0u8; DISK_ID_LEN]; |
| 1145 | // Slicing id to value's length will never panic |
| 1146 | // because we checked that value will fit into id above. |
| 1147 | id[..value.len()].copy_from_slice(value.as_bytes()); |
| 1148 | disk.id = Some(id); |
| 1149 | } |
Daniel Verkamp | e73c80f | 2019-11-08 10:11:16 -0800 | [diff] [blame] | 1150 | _ => { |
| 1151 | return Err(argument::Error::InvalidValue { |
| 1152 | value: kind.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1153 | expected: String::from("unrecognized disk option"), |
Daniel Verkamp | e73c80f | 2019-11-08 10:11:16 -0800 | [diff] [blame] | 1154 | }); |
| 1155 | } |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | cfg.disks.push(disk); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1160 | } |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1161 | "pmem-device" | "rw-pmem-device" => { |
| 1162 | let disk_path = PathBuf::from(value.unwrap()); |
| 1163 | if !disk_path.exists() { |
| 1164 | return Err(argument::Error::InvalidValue { |
| 1165 | value: value.unwrap().to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1166 | expected: String::from("this disk path does not exist"), |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1167 | }); |
| 1168 | } |
| 1169 | |
| 1170 | cfg.pmem_devices.push(DiskOption { |
| 1171 | path: disk_path, |
| 1172 | read_only: !name.starts_with("rw"), |
Daniel Verkamp | e73c80f | 2019-11-08 10:11:16 -0800 | [diff] [blame] | 1173 | sparse: false, |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 1174 | block_size: base::pagesize() as u32, |
Daniel Verkamp | 4e1f99a | 2020-06-01 12:47:21 -0700 | [diff] [blame] | 1175 | id: None, |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1176 | }); |
| 1177 | } |
Kansho Nishida | 282115b | 2019-12-18 13:13:14 +0900 | [diff] [blame] | 1178 | "pstore" => { |
| 1179 | if cfg.pstore.is_some() { |
| 1180 | return Err(argument::Error::TooManyArguments( |
| 1181 | "`pstore` already given".to_owned(), |
| 1182 | )); |
| 1183 | } |
| 1184 | |
| 1185 | let value = value.unwrap(); |
| 1186 | let components: Vec<&str> = value.split(',').collect(); |
| 1187 | if components.len() != 2 { |
| 1188 | return Err(argument::Error::InvalidValue { |
| 1189 | value: value.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1190 | expected: String::from( |
| 1191 | "pstore must have exactly 2 components: path=<path>,size=<size>", |
| 1192 | ), |
Kansho Nishida | 282115b | 2019-12-18 13:13:14 +0900 | [diff] [blame] | 1193 | }); |
| 1194 | } |
| 1195 | cfg.pstore = Some(Pstore { |
| 1196 | path: { |
| 1197 | if components[0].len() <= 5 || !components[0].starts_with("path=") { |
| 1198 | return Err(argument::Error::InvalidValue { |
| 1199 | value: components[0].to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1200 | expected: String::from("pstore path must follow with `path=`"), |
Kansho Nishida | 282115b | 2019-12-18 13:13:14 +0900 | [diff] [blame] | 1201 | }); |
| 1202 | }; |
| 1203 | PathBuf::from(&components[0][5..]) |
| 1204 | }, |
| 1205 | size: { |
| 1206 | if components[1].len() <= 5 || !components[1].starts_with("size=") { |
| 1207 | return Err(argument::Error::InvalidValue { |
| 1208 | value: components[1].to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1209 | expected: String::from("pstore size must follow with `size=`"), |
Kansho Nishida | 282115b | 2019-12-18 13:13:14 +0900 | [diff] [blame] | 1210 | }); |
| 1211 | }; |
| 1212 | components[1][5..] |
| 1213 | .parse() |
| 1214 | .map_err(|_| argument::Error::InvalidValue { |
| 1215 | value: value.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1216 | expected: String::from("pstore size must be an integer"), |
Kansho Nishida | 282115b | 2019-12-18 13:13:14 +0900 | [diff] [blame] | 1217 | })? |
| 1218 | }, |
| 1219 | }); |
| 1220 | } |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1221 | "host_ip" => { |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 1222 | if cfg.host_ip.is_some() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1223 | return Err(argument::Error::TooManyArguments( |
| 1224 | "`host_ip` already given".to_owned(), |
| 1225 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1226 | } |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 1227 | cfg.host_ip = |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1228 | Some( |
| 1229 | value |
| 1230 | .unwrap() |
| 1231 | .parse() |
| 1232 | .map_err(|_| argument::Error::InvalidValue { |
| 1233 | value: value.unwrap().to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1234 | 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] | 1235 | })?, |
| 1236 | ) |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1237 | } |
| 1238 | "netmask" => { |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 1239 | if cfg.netmask.is_some() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1240 | return Err(argument::Error::TooManyArguments( |
| 1241 | "`netmask` already given".to_owned(), |
| 1242 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1243 | } |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 1244 | cfg.netmask = |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1245 | Some( |
| 1246 | value |
| 1247 | .unwrap() |
| 1248 | .parse() |
| 1249 | .map_err(|_| argument::Error::InvalidValue { |
| 1250 | value: value.unwrap().to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1251 | 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] | 1252 | })?, |
| 1253 | ) |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1254 | } |
| 1255 | "mac" => { |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 1256 | if cfg.mac_address.is_some() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1257 | return Err(argument::Error::TooManyArguments( |
| 1258 | "`mac` already given".to_owned(), |
| 1259 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1260 | } |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 1261 | cfg.mac_address = |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1262 | Some( |
| 1263 | value |
| 1264 | .unwrap() |
| 1265 | .parse() |
| 1266 | .map_err(|_| argument::Error::InvalidValue { |
| 1267 | value: value.unwrap().to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1268 | expected: String::from( |
| 1269 | "`mac` needs to be in the form \"XX:XX:XX:XX:XX:XX\"", |
| 1270 | ), |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1271 | })?, |
| 1272 | ) |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1273 | } |
Xiong Zhang | 773c707 | 2020-03-20 10:39:55 +0800 | [diff] [blame] | 1274 | "net-vq-pairs" => { |
| 1275 | if cfg.net_vq_pairs.is_some() { |
| 1276 | return Err(argument::Error::TooManyArguments( |
| 1277 | "`net-vq-pairs` already given".to_owned(), |
| 1278 | )); |
| 1279 | } |
| 1280 | cfg.net_vq_pairs = |
| 1281 | Some( |
| 1282 | value |
| 1283 | .unwrap() |
| 1284 | .parse() |
| 1285 | .map_err(|_| argument::Error::InvalidValue { |
| 1286 | value: value.unwrap().to_owned(), |
| 1287 | expected: String::from( |
| 1288 | "this value for `net-vq-pairs` needs to be integer", |
| 1289 | ), |
| 1290 | })?, |
| 1291 | ) |
| 1292 | } |
| 1293 | |
Stephen Barber | 28a5a61 | 2017-10-20 17:15:30 -0700 | [diff] [blame] | 1294 | "wayland-sock" => { |
Ryo Hashimoto | 0b788de | 2019-12-10 17:14:13 +0900 | [diff] [blame] | 1295 | let mut components = value.unwrap().split(','); |
| 1296 | let path = |
| 1297 | PathBuf::from( |
| 1298 | components |
| 1299 | .next() |
| 1300 | .ok_or_else(|| argument::Error::InvalidValue { |
| 1301 | value: value.unwrap().to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1302 | expected: String::from("missing socket path"), |
Ryo Hashimoto | 0b788de | 2019-12-10 17:14:13 +0900 | [diff] [blame] | 1303 | })?, |
| 1304 | ); |
| 1305 | let mut name = ""; |
| 1306 | for c in components { |
| 1307 | let mut kv = c.splitn(2, '='); |
| 1308 | let (kind, value) = match (kv.next(), kv.next()) { |
| 1309 | (Some(kind), Some(value)) => (kind, value), |
| 1310 | _ => { |
| 1311 | return Err(argument::Error::InvalidValue { |
| 1312 | value: c.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1313 | expected: String::from("option must be of the form `kind=value`"), |
Ryo Hashimoto | 0b788de | 2019-12-10 17:14:13 +0900 | [diff] [blame] | 1314 | }) |
| 1315 | } |
| 1316 | }; |
| 1317 | match kind { |
| 1318 | "name" => name = value, |
| 1319 | _ => { |
| 1320 | return Err(argument::Error::InvalidValue { |
| 1321 | value: kind.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1322 | expected: String::from("unrecognized option"), |
Ryo Hashimoto | 0b788de | 2019-12-10 17:14:13 +0900 | [diff] [blame] | 1323 | }) |
| 1324 | } |
| 1325 | } |
Stephen Barber | 28a5a61 | 2017-10-20 17:15:30 -0700 | [diff] [blame] | 1326 | } |
Ryo Hashimoto | 0b788de | 2019-12-10 17:14:13 +0900 | [diff] [blame] | 1327 | if cfg.wayland_socket_paths.contains_key(name) { |
| 1328 | return Err(argument::Error::TooManyArguments(format!( |
| 1329 | "wayland socket name already used: '{}'", |
| 1330 | name |
| 1331 | ))); |
Stephen Barber | 28a5a61 | 2017-10-20 17:15:30 -0700 | [diff] [blame] | 1332 | } |
Ryo Hashimoto | 0b788de | 2019-12-10 17:14:13 +0900 | [diff] [blame] | 1333 | cfg.wayland_socket_paths.insert(name.to_string(), path); |
Stephen Barber | 28a5a61 | 2017-10-20 17:15:30 -0700 | [diff] [blame] | 1334 | } |
David Reveman | 52ba4e5 | 2018-04-22 21:42:09 -0400 | [diff] [blame] | 1335 | #[cfg(feature = "wl-dmabuf")] |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 1336 | "wayland-dmabuf" => cfg.wayland_dmabuf = true, |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 1337 | "x-display" => { |
| 1338 | if cfg.x_display.is_some() { |
| 1339 | return Err(argument::Error::TooManyArguments( |
| 1340 | "`x-display` already given".to_owned(), |
| 1341 | )); |
| 1342 | } |
| 1343 | cfg.x_display = Some(value.unwrap().to_owned()); |
| 1344 | } |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 1345 | "display-window-keyboard" => { |
| 1346 | cfg.display_window_keyboard = true; |
| 1347 | } |
| 1348 | "display-window-mouse" => { |
| 1349 | cfg.display_window_mouse = true; |
| 1350 | } |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1351 | "socket" => { |
| 1352 | if cfg.socket_path.is_some() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1353 | return Err(argument::Error::TooManyArguments( |
| 1354 | "`socket` already given".to_owned(), |
| 1355 | )); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1356 | } |
| 1357 | let mut socket_path = PathBuf::from(value.unwrap()); |
| 1358 | if socket_path.is_dir() { |
| 1359 | socket_path.push(format!("crosvm-{}.sock", getpid())); |
| 1360 | } |
| 1361 | if socket_path.exists() { |
| 1362 | return Err(argument::Error::InvalidValue { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1363 | value: socket_path.to_string_lossy().into_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1364 | expected: String::from("this socket path already exists"), |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1365 | }); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1366 | } |
| 1367 | cfg.socket_path = Some(socket_path); |
| 1368 | } |
Dylan Reid | d0c9adc | 2017-10-02 19:04:50 -0700 | [diff] [blame] | 1369 | "disable-sandbox" => { |
Lepton Wu | 9105e9f | 2019-03-14 11:38:31 -0700 | [diff] [blame] | 1370 | cfg.sandbox = false; |
Dylan Reid | d0c9adc | 2017-10-02 19:04:50 -0700 | [diff] [blame] | 1371 | } |
Chirantan Ekbote | 88f9cba | 2017-08-28 09:51:18 -0700 | [diff] [blame] | 1372 | "cid" => { |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 1373 | if cfg.cid.is_some() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1374 | return Err(argument::Error::TooManyArguments( |
| 1375 | "`cid` alread given".to_owned(), |
| 1376 | )); |
Chirantan Ekbote | 88f9cba | 2017-08-28 09:51:18 -0700 | [diff] [blame] | 1377 | } |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 1378 | cfg.cid = Some( |
| 1379 | value |
| 1380 | .unwrap() |
| 1381 | .parse() |
| 1382 | .map_err(|_| argument::Error::InvalidValue { |
| 1383 | value: value.unwrap().to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1384 | expected: String::from("this value for `cid` must be an unsigned integer"), |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 1385 | })?, |
| 1386 | ); |
Chirantan Ekbote | 88f9cba | 2017-08-28 09:51:18 -0700 | [diff] [blame] | 1387 | } |
Chirantan Ekbote | ebd5681 | 2018-04-16 19:32:04 -0700 | [diff] [blame] | 1388 | "shared-dir" => { |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1389 | // This is formatted as multiple fields, each separated by ":". The first 2 fields are |
| 1390 | // fixed (src:tag). The rest may appear in any order: |
| 1391 | // |
| 1392 | // * type=TYPE - must be one of "p9" or "fs" (default: p9) |
| 1393 | // * uidmap=UIDMAP - a uid map in the format "inner outer count[,inner outer count]" |
| 1394 | // (default: "0 <current euid> 1") |
| 1395 | // * gidmap=GIDMAP - a gid map in the same format as uidmap |
| 1396 | // (default: "0 <current egid> 1") |
| 1397 | // * timeout=TIMEOUT - a timeout value in seconds, which indicates how long attributes |
| 1398 | // and directory contents should be considered valid (default: 5) |
| 1399 | // * cache=CACHE - one of "never", "always", or "auto" (default: auto) |
| 1400 | // * writeback=BOOL - indicates whether writeback caching should be enabled (default: false) |
Chirantan Ekbote | ebd5681 | 2018-04-16 19:32:04 -0700 | [diff] [blame] | 1401 | let param = value.unwrap(); |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1402 | let mut components = param.split(':'); |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1403 | let src = |
| 1404 | PathBuf::from( |
| 1405 | components |
| 1406 | .next() |
| 1407 | .ok_or_else(|| argument::Error::InvalidValue { |
| 1408 | value: param.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1409 | expected: String::from("missing source path for `shared-dir`"), |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1410 | })?, |
| 1411 | ); |
| 1412 | let tag = components |
| 1413 | .next() |
| 1414 | .ok_or_else(|| argument::Error::InvalidValue { |
Chirantan Ekbote | ebd5681 | 2018-04-16 19:32:04 -0700 | [diff] [blame] | 1415 | value: param.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1416 | expected: String::from("missing tag for `shared-dir`"), |
David Tolnay | 2bac1e7 | 2018-12-12 14:33:42 -0800 | [diff] [blame] | 1417 | })? |
| 1418 | .to_owned(); |
Chirantan Ekbote | ebd5681 | 2018-04-16 19:32:04 -0700 | [diff] [blame] | 1419 | |
| 1420 | if !src.is_dir() { |
| 1421 | return Err(argument::Error::InvalidValue { |
| 1422 | value: param.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1423 | expected: String::from("source path for `shared-dir` must be a directory"), |
Chirantan Ekbote | ebd5681 | 2018-04-16 19:32:04 -0700 | [diff] [blame] | 1424 | }); |
| 1425 | } |
| 1426 | |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1427 | let mut shared_dir = SharedDir { |
| 1428 | src, |
| 1429 | tag, |
| 1430 | ..Default::default() |
| 1431 | }; |
| 1432 | for opt in components { |
| 1433 | let mut o = opt.splitn(2, '='); |
| 1434 | let kind = o.next().ok_or_else(|| argument::Error::InvalidValue { |
| 1435 | value: opt.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1436 | expected: String::from("`shared-dir` options must not be empty"), |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1437 | })?; |
| 1438 | let value = o.next().ok_or_else(|| argument::Error::InvalidValue { |
| 1439 | value: opt.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1440 | 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] | 1441 | })?; |
| 1442 | |
| 1443 | match kind { |
| 1444 | "type" => { |
| 1445 | shared_dir.kind = |
| 1446 | value.parse().map_err(|_| argument::Error::InvalidValue { |
| 1447 | value: value.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1448 | expected: String::from("`type` must be one of `fs` or `9p`"), |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1449 | })? |
| 1450 | } |
| 1451 | "uidmap" => shared_dir.uid_map = value.into(), |
| 1452 | "gidmap" => shared_dir.gid_map = value.into(), |
| 1453 | "timeout" => { |
| 1454 | let seconds = value.parse().map_err(|_| argument::Error::InvalidValue { |
| 1455 | value: value.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1456 | expected: String::from("`timeout` must be an integer"), |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1457 | })?; |
| 1458 | |
| 1459 | let dur = Duration::from_secs(seconds); |
Andrew Walbran | 9cfdbd9 | 2021-01-11 17:40:34 +0000 | [diff] [blame] | 1460 | shared_dir.fs_cfg.entry_timeout = dur; |
Chirantan Ekbote | 75ba875 | 2020-10-27 18:33:02 +0900 | [diff] [blame] | 1461 | shared_dir.fs_cfg.attr_timeout = dur; |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1462 | } |
| 1463 | "cache" => { |
| 1464 | let policy = value.parse().map_err(|_| argument::Error::InvalidValue { |
| 1465 | value: value.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1466 | expected: String::from( |
| 1467 | "`cache` must be one of `never`, `always`, or `auto`", |
| 1468 | ), |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1469 | })?; |
Chirantan Ekbote | 75ba875 | 2020-10-27 18:33:02 +0900 | [diff] [blame] | 1470 | shared_dir.fs_cfg.cache_policy = policy; |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1471 | } |
| 1472 | "writeback" => { |
| 1473 | let writeback = |
| 1474 | value.parse().map_err(|_| argument::Error::InvalidValue { |
| 1475 | value: value.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1476 | expected: String::from("`writeback` must be a boolean"), |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1477 | })?; |
Chirantan Ekbote | 75ba875 | 2020-10-27 18:33:02 +0900 | [diff] [blame] | 1478 | shared_dir.fs_cfg.writeback = writeback; |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1479 | } |
Chirantan Ekbote | d994e51 | 2020-06-12 18:46:52 +0900 | [diff] [blame] | 1480 | "rewrite-security-xattrs" => { |
| 1481 | let rewrite_security_xattrs = |
| 1482 | value.parse().map_err(|_| argument::Error::InvalidValue { |
| 1483 | value: value.to_owned(), |
| 1484 | expected: String::from( |
| 1485 | "`rewrite-security-xattrs` must be a boolean", |
| 1486 | ), |
| 1487 | })?; |
Chirantan Ekbote | 75ba875 | 2020-10-27 18:33:02 +0900 | [diff] [blame] | 1488 | shared_dir.fs_cfg.rewrite_security_xattrs = rewrite_security_xattrs; |
Chirantan Ekbote | d994e51 | 2020-06-12 18:46:52 +0900 | [diff] [blame] | 1489 | } |
Chirantan Ekbote | 09357c8 | 2020-09-10 20:08:28 +0900 | [diff] [blame] | 1490 | "ascii_casefold" => { |
| 1491 | let ascii_casefold = |
| 1492 | value.parse().map_err(|_| argument::Error::InvalidValue { |
| 1493 | value: value.to_owned(), |
| 1494 | expected: String::from("`ascii_casefold` must be a boolean"), |
| 1495 | })?; |
Chirantan Ekbote | 75ba875 | 2020-10-27 18:33:02 +0900 | [diff] [blame] | 1496 | shared_dir.fs_cfg.ascii_casefold = ascii_casefold; |
| 1497 | shared_dir.p9_cfg.ascii_casefold = ascii_casefold; |
Chirantan Ekbote | 09357c8 | 2020-09-10 20:08:28 +0900 | [diff] [blame] | 1498 | } |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1499 | _ => { |
| 1500 | return Err(argument::Error::InvalidValue { |
| 1501 | value: kind.to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1502 | expected: String::from("unrecognized option for `shared-dir`"), |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1503 | }) |
| 1504 | } |
| 1505 | } |
| 1506 | } |
| 1507 | cfg.shared_dirs.push(shared_dir); |
Chirantan Ekbote | ebd5681 | 2018-04-16 19:32:04 -0700 | [diff] [blame] | 1508 | } |
Dylan Reid | e026ef0 | 2017-10-02 19:03:52 -0700 | [diff] [blame] | 1509 | "seccomp-policy-dir" => { |
Dylan Reid | d0c9adc | 2017-10-02 19:04:50 -0700 | [diff] [blame] | 1510 | // `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] | 1511 | cfg.seccomp_policy_dir = PathBuf::from(value.unwrap()); |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1512 | } |
Zach Reizner | 4486379 | 2019-06-26 14:22:08 -0700 | [diff] [blame] | 1513 | "seccomp-log-failures" => { |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 1514 | // A side-effect of this flag is to force the use of .policy files |
| 1515 | // instead of .bpf files (.bpf files are expected and assumed to be |
| 1516 | // compiled to fail an unpermitted action with "trap"). |
| 1517 | // Normally crosvm will first attempt to use a .bpf file, and if |
| 1518 | // not present it will then try to use a .policy file. It's up |
| 1519 | // to the build to decide which of these files is present for |
| 1520 | // crosvm to use (for CrOS the build will use .bpf files for |
| 1521 | // x64 builds and .policy files for arm/arm64 builds). |
| 1522 | // |
| 1523 | // This flag will likely work as expected for builds that use |
| 1524 | // .policy files. For builds that only use .bpf files the initial |
| 1525 | // result when using this flag is likely to be a file-not-found |
| 1526 | // error (since the .policy files are not present). |
| 1527 | // For .bpf builds you can either 1) manually add the .policy files, |
| 1528 | // or 2) do not use this command-line parameter and instead |
| 1529 | // temporarily change the build by passing "log" rather than |
| 1530 | // "trap" as the "--default-action" to compile_seccomp_policy.py. |
Zach Reizner | 4486379 | 2019-06-26 14:22:08 -0700 | [diff] [blame] | 1531 | cfg.seccomp_log_failures = true; |
| 1532 | } |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 1533 | "plugin" => { |
Cody Schuffelen | 6d1ab50 | 2019-05-21 12:12:38 -0700 | [diff] [blame] | 1534 | if cfg.executable_path.is_some() { |
| 1535 | return Err(argument::Error::TooManyArguments(format!( |
| 1536 | "A VM executable was already specified: {:?}", |
| 1537 | cfg.executable_path |
| 1538 | ))); |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 1539 | } |
Zach Reizner | cc30d58 | 2018-01-23 21:16:42 -0800 | [diff] [blame] | 1540 | let plugin = PathBuf::from(value.unwrap().to_owned()); |
| 1541 | if plugin.is_relative() { |
| 1542 | return Err(argument::Error::InvalidValue { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1543 | value: plugin.to_string_lossy().into_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1544 | expected: String::from("the plugin path must be an absolute path"), |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1545 | }); |
Zach Reizner | cc30d58 | 2018-01-23 21:16:42 -0800 | [diff] [blame] | 1546 | } |
Cody Schuffelen | 6d1ab50 | 2019-05-21 12:12:38 -0700 | [diff] [blame] | 1547 | cfg.executable_path = Some(Executable::Plugin(plugin)); |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1548 | } |
Dmitry Torokhov | 0f1770d | 2018-05-11 10:57:16 -0700 | [diff] [blame] | 1549 | "plugin-root" => { |
| 1550 | cfg.plugin_root = Some(PathBuf::from(value.unwrap().to_owned())); |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1551 | } |
Chirantan Ekbote | d41d726 | 2018-11-16 16:37:45 -0800 | [diff] [blame] | 1552 | "plugin-mount" => { |
Dmitry Torokhov | c689fd9 | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 1553 | let mount = parse_plugin_mount_option(value.unwrap())?; |
| 1554 | cfg.plugin_mounts.push(mount); |
Chirantan Ekbote | d41d726 | 2018-11-16 16:37:45 -0800 | [diff] [blame] | 1555 | } |
Dmitry Torokhov | 0f4c5ff | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 1556 | "plugin-mount-file" => { |
| 1557 | let file = File::open(value.unwrap()).map_err(|_| argument::Error::InvalidValue { |
| 1558 | value: value.unwrap().to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1559 | expected: String::from("unable to open `plugin-mount-file` file"), |
Dmitry Torokhov | 0f4c5ff | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 1560 | })?; |
| 1561 | let reader = BufReader::new(file); |
| 1562 | for l in reader.lines() { |
| 1563 | let line = l.unwrap(); |
Keiichi Watanabe | 275c1ef | 2020-04-10 21:49:10 +0900 | [diff] [blame] | 1564 | let trimmed_line = line.splitn(2, '#').next().unwrap().trim(); |
Dmitry Torokhov | 0f4c5ff | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 1565 | if !trimmed_line.is_empty() { |
| 1566 | let mount = parse_plugin_mount_option(trimmed_line)?; |
| 1567 | cfg.plugin_mounts.push(mount); |
| 1568 | } |
| 1569 | } |
| 1570 | } |
Dmitry Torokhov | 1a6262b | 2019-03-01 00:34:03 -0800 | [diff] [blame] | 1571 | "plugin-gid-map" => { |
Dmitry Torokhov | c689fd9 | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 1572 | let map = parse_plugin_gid_map_option(value.unwrap())?; |
| 1573 | cfg.plugin_gid_maps.push(map); |
Dmitry Torokhov | 1a6262b | 2019-03-01 00:34:03 -0800 | [diff] [blame] | 1574 | } |
Dmitry Torokhov | 0f4c5ff | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 1575 | "plugin-gid-map-file" => { |
| 1576 | let file = File::open(value.unwrap()).map_err(|_| argument::Error::InvalidValue { |
| 1577 | value: value.unwrap().to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1578 | expected: String::from("unable to open `plugin-gid-map-file` file"), |
Dmitry Torokhov | 0f4c5ff | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 1579 | })?; |
| 1580 | let reader = BufReader::new(file); |
| 1581 | for l in reader.lines() { |
| 1582 | let line = l.unwrap(); |
Keiichi Watanabe | 275c1ef | 2020-04-10 21:49:10 +0900 | [diff] [blame] | 1583 | let trimmed_line = line.splitn(2, '#').next().unwrap().trim(); |
Dmitry Torokhov | 0f4c5ff | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 1584 | if !trimmed_line.is_empty() { |
| 1585 | let map = parse_plugin_gid_map_option(trimmed_line)?; |
| 1586 | cfg.plugin_gid_maps.push(map); |
| 1587 | } |
| 1588 | } |
| 1589 | } |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 1590 | "vhost-net" => cfg.vhost_net = true, |
Chirantan Ekbote | 5f78721 | 2018-05-31 15:31:31 -0700 | [diff] [blame] | 1591 | "tap-fd" => { |
Jorge E. Moreira | b795280 | 2019-02-12 16:43:05 -0800 | [diff] [blame] | 1592 | cfg.tap_fd.push( |
| 1593 | value |
| 1594 | .unwrap() |
| 1595 | .parse() |
| 1596 | .map_err(|_| argument::Error::InvalidValue { |
| 1597 | value: value.unwrap().to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1598 | expected: String::from( |
| 1599 | "this value for `tap-fd` must be an unsigned integer", |
| 1600 | ), |
Jorge E. Moreira | b795280 | 2019-02-12 16:43:05 -0800 | [diff] [blame] | 1601 | })?, |
| 1602 | ); |
Chirantan Ekbote | 5f78721 | 2018-05-31 15:31:31 -0700 | [diff] [blame] | 1603 | } |
Jason Macnak | cc7070b | 2019-11-06 14:48:12 -0800 | [diff] [blame] | 1604 | #[cfg(feature = "gpu")] |
Zach Reizner | 3a8100a | 2017-09-13 19:15:43 -0700 | [diff] [blame] | 1605 | "gpu" => { |
Jason Macnak | d659a0d | 2021-03-15 15:33:01 -0700 | [diff] [blame^] | 1606 | if cfg.gpu_parameters.is_none() { |
| 1607 | cfg.gpu_parameters = Some(Default::default()); |
| 1608 | } |
| 1609 | parse_gpu_options(value, cfg.gpu_parameters.as_mut().unwrap())?; |
| 1610 | } |
| 1611 | #[cfg(feature = "gpu")] |
| 1612 | "gpu-display" => { |
| 1613 | if cfg.gpu_parameters.is_none() { |
| 1614 | cfg.gpu_parameters = Some(Default::default()); |
| 1615 | } |
| 1616 | parse_gpu_display_options(value, cfg.gpu_parameters.as_mut().unwrap())?; |
Zach Reizner | 3a8100a | 2017-09-13 19:15:43 -0700 | [diff] [blame] | 1617 | } |
David Tolnay | 43f8e21 | 2019-02-13 17:28:16 -0800 | [diff] [blame] | 1618 | "software-tpm" => { |
| 1619 | cfg.software_tpm = true; |
| 1620 | } |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 1621 | "single-touch" => { |
Keiichi Watanabe | 275c1ef | 2020-04-10 21:49:10 +0900 | [diff] [blame] | 1622 | let mut it = value.unwrap().split(':'); |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 1623 | |
| 1624 | let mut single_touch_spec = |
| 1625 | TouchDeviceOption::new(PathBuf::from(it.next().unwrap().to_owned())); |
| 1626 | if let Some(width) = it.next() { |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 1627 | single_touch_spec.set_width(width.trim().parse().unwrap()); |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 1628 | } |
| 1629 | if let Some(height) = it.next() { |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 1630 | single_touch_spec.set_height(height.trim().parse().unwrap()); |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 1631 | } |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 1632 | cfg.virtio_single_touch.push(single_touch_spec); |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 1633 | } |
Tristan Muntsinger | 486cffc | 2020-09-29 22:05:41 +0000 | [diff] [blame] | 1634 | "multi-touch" => { |
Tristan Muntsinger | 486cffc | 2020-09-29 22:05:41 +0000 | [diff] [blame] | 1635 | let mut it = value.unwrap().split(':'); |
| 1636 | |
| 1637 | let mut multi_touch_spec = |
| 1638 | TouchDeviceOption::new(PathBuf::from(it.next().unwrap().to_owned())); |
| 1639 | if let Some(width) = it.next() { |
| 1640 | multi_touch_spec.set_width(width.trim().parse().unwrap()); |
| 1641 | } |
| 1642 | if let Some(height) = it.next() { |
| 1643 | multi_touch_spec.set_height(height.trim().parse().unwrap()); |
| 1644 | } |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 1645 | cfg.virtio_multi_touch.push(multi_touch_spec); |
Tristan Muntsinger | 486cffc | 2020-09-29 22:05:41 +0000 | [diff] [blame] | 1646 | } |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1647 | "trackpad" => { |
Keiichi Watanabe | 275c1ef | 2020-04-10 21:49:10 +0900 | [diff] [blame] | 1648 | let mut it = value.unwrap().split(':'); |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1649 | |
| 1650 | let mut trackpad_spec = |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 1651 | TouchDeviceOption::new(PathBuf::from(it.next().unwrap().to_owned())); |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1652 | if let Some(width) = it.next() { |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 1653 | trackpad_spec.set_width(width.trim().parse().unwrap()); |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1654 | } |
| 1655 | if let Some(height) = it.next() { |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 1656 | trackpad_spec.set_height(height.trim().parse().unwrap()); |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1657 | } |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 1658 | cfg.virtio_trackpad.push(trackpad_spec); |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1659 | } |
| 1660 | "mouse" => { |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 1661 | cfg.virtio_mice |
| 1662 | .push(PathBuf::from(value.unwrap().to_owned())); |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1663 | } |
| 1664 | "keyboard" => { |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 1665 | cfg.virtio_keyboard |
| 1666 | .push(PathBuf::from(value.unwrap().to_owned())); |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1667 | } |
Daniel Norman | 5e23df7 | 2021-03-11 10:11:02 -0800 | [diff] [blame] | 1668 | "switches" => { |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 1669 | cfg.virtio_switches |
| 1670 | .push(PathBuf::from(value.unwrap().to_owned())); |
Daniel Norman | 5e23df7 | 2021-03-11 10:11:02 -0800 | [diff] [blame] | 1671 | } |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1672 | "evdev" => { |
| 1673 | let dev_path = PathBuf::from(value.unwrap()); |
| 1674 | if !dev_path.exists() { |
| 1675 | return Err(argument::Error::InvalidValue { |
| 1676 | value: value.unwrap().to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1677 | expected: String::from("this input device path does not exist"), |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1678 | }); |
| 1679 | } |
| 1680 | cfg.virtio_input_evdevs.push(dev_path); |
| 1681 | } |
Miriam Zimmerman | 26ac928 | 2019-01-29 21:21:48 -0800 | [diff] [blame] | 1682 | "split-irqchip" => { |
| 1683 | cfg.split_irqchip = true; |
| 1684 | } |
Daniel Verkamp | e403f5c | 2018-12-11 16:29:26 -0800 | [diff] [blame] | 1685 | "initrd" => { |
| 1686 | cfg.initrd_path = Some(PathBuf::from(value.unwrap().to_owned())); |
| 1687 | } |
Cody Schuffelen | 6d1ab50 | 2019-05-21 12:12:38 -0700 | [diff] [blame] | 1688 | "bios" => { |
| 1689 | if cfg.executable_path.is_some() { |
| 1690 | return Err(argument::Error::TooManyArguments(format!( |
| 1691 | "A VM executable was already specified: {:?}", |
| 1692 | cfg.executable_path |
| 1693 | ))); |
| 1694 | } |
| 1695 | cfg.executable_path = Some(Executable::Bios(PathBuf::from(value.unwrap().to_owned()))); |
| 1696 | } |
Xiong Zhang | 17b0daf | 2019-04-23 17:14:50 +0800 | [diff] [blame] | 1697 | "vfio" => { |
| 1698 | let vfio_path = PathBuf::from(value.unwrap()); |
| 1699 | if !vfio_path.exists() { |
| 1700 | return Err(argument::Error::InvalidValue { |
| 1701 | value: value.unwrap().to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1702 | expected: String::from("the vfio path does not exist"), |
Xiong Zhang | 17b0daf | 2019-04-23 17:14:50 +0800 | [diff] [blame] | 1703 | }); |
| 1704 | } |
| 1705 | if !vfio_path.is_dir() { |
| 1706 | return Err(argument::Error::InvalidValue { |
| 1707 | value: value.unwrap().to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 1708 | expected: String::from("the vfio path should be directory"), |
Xiong Zhang | 17b0daf | 2019-04-23 17:14:50 +0800 | [diff] [blame] | 1709 | }); |
| 1710 | } |
| 1711 | |
Xiong Zhang | 8bb4faa | 2019-11-12 10:06:13 +0800 | [diff] [blame] | 1712 | cfg.vfio.push(vfio_path); |
Xiong Zhang | 17b0daf | 2019-04-23 17:14:50 +0800 | [diff] [blame] | 1713 | } |
Keiichi Watanabe | 57df6a0 | 2019-12-06 22:24:40 +0900 | [diff] [blame] | 1714 | "video-decoder" => { |
| 1715 | cfg.video_dec = true; |
| 1716 | } |
| 1717 | "video-encoder" => { |
| 1718 | cfg.video_enc = true; |
| 1719 | } |
Tomasz Jeznach | 4264464 | 2020-05-20 23:27:59 -0700 | [diff] [blame] | 1720 | "acpi-table" => { |
| 1721 | let acpi_table = PathBuf::from(value.unwrap()); |
| 1722 | if !acpi_table.exists() { |
| 1723 | return Err(argument::Error::InvalidValue { |
| 1724 | value: value.unwrap().to_owned(), |
| 1725 | expected: String::from("the acpi-table path does not exist"), |
| 1726 | }); |
| 1727 | } |
| 1728 | if !acpi_table.is_file() { |
| 1729 | return Err(argument::Error::InvalidValue { |
| 1730 | value: value.unwrap().to_owned(), |
| 1731 | expected: String::from("the acpi-table path should be a file"), |
| 1732 | }); |
| 1733 | } |
| 1734 | |
| 1735 | cfg.acpi_tables.push(acpi_table); |
| 1736 | } |
Will Deacon | 6560c18 | 2020-10-06 18:47:18 +0100 | [diff] [blame] | 1737 | "protected-vm" => { |
Andrew Walbran | 413f854 | 2021-01-08 13:29:03 +0000 | [diff] [blame] | 1738 | cfg.protected_vm = ProtectionType::Protected; |
Will Deacon | 6560c18 | 2020-10-06 18:47:18 +0100 | [diff] [blame] | 1739 | cfg.params.push("swiotlb=force".to_string()); |
| 1740 | } |
Chuanxiao Dong | fd5626c | 2020-04-27 16:35:33 +0800 | [diff] [blame] | 1741 | "battery" => { |
| 1742 | let params = parse_battery_options(value)?; |
| 1743 | cfg.battery_type = Some(params); |
| 1744 | } |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 1745 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 1746 | "gdb" => { |
| 1747 | let port = value |
| 1748 | .unwrap() |
| 1749 | .parse() |
| 1750 | .map_err(|_| argument::Error::InvalidValue { |
| 1751 | value: value.unwrap().to_owned(), |
| 1752 | expected: String::from("expected a valid port number"), |
| 1753 | })?; |
| 1754 | cfg.gdb = Some(port); |
| 1755 | } |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 1756 | "balloon_bias_mib" => { |
| 1757 | cfg.balloon_bias = |
| 1758 | value |
| 1759 | .unwrap() |
| 1760 | .parse::<i64>() |
| 1761 | .map_err(|_| argument::Error::InvalidValue { |
| 1762 | value: value.unwrap().to_owned(), |
| 1763 | expected: String::from("expected a valid ballon bias in MiB"), |
| 1764 | })? |
| 1765 | * 1024 |
| 1766 | * 1024; // cfg.balloon_bias is in bytes. |
| 1767 | } |
Keiichi Watanabe | f3a37f4 | 2021-01-21 15:41:11 +0900 | [diff] [blame] | 1768 | "vhost-user-blk" => cfg.vhost_user_blk.push(VhostUserOption { |
| 1769 | socket: PathBuf::from(value.unwrap()), |
| 1770 | }), |
Keiichi Watanabe | 6068658 | 2021-03-12 04:53:51 +0900 | [diff] [blame] | 1771 | "vhost-user-net" => cfg.vhost_user_net.push(VhostUserOption { |
| 1772 | socket: PathBuf::from(value.unwrap()), |
| 1773 | }), |
Chirantan Ekbote | 2ee9dcd | 2021-05-26 18:21:44 +0900 | [diff] [blame] | 1774 | "vhost-user-wl" => { |
| 1775 | let mut components = value.unwrap().splitn(2, ":"); |
| 1776 | let socket = components.next().map(PathBuf::from).ok_or_else(|| { |
| 1777 | argument::Error::InvalidValue { |
| 1778 | value: value.unwrap().to_owned(), |
| 1779 | expected: String::from("missing socket path"), |
| 1780 | } |
| 1781 | })?; |
| 1782 | let vm_tube = components.next().map(PathBuf::from).ok_or_else(|| { |
| 1783 | argument::Error::InvalidValue { |
| 1784 | value: value.unwrap().to_owned(), |
| 1785 | expected: String::from("missing vm tube path"), |
| 1786 | } |
| 1787 | })?; |
| 1788 | cfg.vhost_user_wl |
| 1789 | .push(VhostUserWlOption { socket, vm_tube }); |
| 1790 | } |
Woody Chow | 5890b70 | 2021-02-12 14:57:02 +0900 | [diff] [blame] | 1791 | "vhost-user-fs" => { |
| 1792 | // (socket:tag) |
| 1793 | let param = value.unwrap(); |
| 1794 | let mut components = param.split(':'); |
| 1795 | let socket = |
| 1796 | PathBuf::from( |
| 1797 | components |
| 1798 | .next() |
| 1799 | .ok_or_else(|| argument::Error::InvalidValue { |
| 1800 | value: param.to_owned(), |
| 1801 | expected: String::from("missing socket path for `vhost-user-fs`"), |
| 1802 | })?, |
| 1803 | ); |
| 1804 | let tag = components |
| 1805 | .next() |
| 1806 | .ok_or_else(|| argument::Error::InvalidValue { |
| 1807 | value: param.to_owned(), |
| 1808 | expected: String::from("missing tag for `vhost-user-fs`"), |
| 1809 | })? |
| 1810 | .to_owned(); |
| 1811 | cfg.vhost_user_fs.push(VhostUserFsOption { socket, tag }); |
| 1812 | } |
Tomasz Jeznach | 3ce7476 | 2021-02-26 01:01:53 -0800 | [diff] [blame] | 1813 | #[cfg(feature = "direct")] |
| 1814 | "direct-pmio" => { |
| 1815 | if cfg.direct_pmio.is_some() { |
| 1816 | return Err(argument::Error::TooManyArguments( |
| 1817 | "`direct_pmio` already given".to_owned(), |
| 1818 | )); |
| 1819 | } |
| 1820 | cfg.direct_pmio = Some(parse_direct_io_options(value)?); |
| 1821 | } |
Tomasz Jeznach | 7271f75 | 2021-03-04 01:44:06 -0800 | [diff] [blame] | 1822 | #[cfg(feature = "direct")] |
| 1823 | "direct-level-irq" => { |
| 1824 | cfg.direct_level_irq |
| 1825 | .push( |
| 1826 | value |
| 1827 | .unwrap() |
| 1828 | .parse() |
| 1829 | .map_err(|_| argument::Error::InvalidValue { |
| 1830 | value: value.unwrap().to_owned(), |
| 1831 | expected: String::from( |
| 1832 | "this value for `direct-level-irq` must be an unsigned integer", |
| 1833 | ), |
| 1834 | })?, |
| 1835 | ); |
| 1836 | } |
| 1837 | #[cfg(feature = "direct")] |
| 1838 | "direct-edge-irq" => { |
| 1839 | cfg.direct_edge_irq |
| 1840 | .push( |
| 1841 | value |
| 1842 | .unwrap() |
| 1843 | .parse() |
| 1844 | .map_err(|_| argument::Error::InvalidValue { |
| 1845 | value: value.unwrap().to_owned(), |
| 1846 | expected: String::from( |
| 1847 | "this value for `direct-edge-irq` must be an unsigned integer", |
| 1848 | ), |
| 1849 | })?, |
| 1850 | ); |
| 1851 | } |
Tomasz Jeznach | ccb2694 | 2021-03-30 22:44:11 -0700 | [diff] [blame] | 1852 | "dmi" => { |
| 1853 | if cfg.dmi_path.is_some() { |
| 1854 | return Err(argument::Error::TooManyArguments( |
| 1855 | "`dmi` already given".to_owned(), |
| 1856 | )); |
| 1857 | } |
| 1858 | let dmi_path = PathBuf::from(value.unwrap()); |
| 1859 | if !dmi_path.exists() { |
| 1860 | return Err(argument::Error::InvalidValue { |
| 1861 | value: value.unwrap().to_owned(), |
| 1862 | expected: String::from("the dmi path does not exist"), |
| 1863 | }); |
| 1864 | } |
| 1865 | if !dmi_path.is_dir() { |
| 1866 | return Err(argument::Error::InvalidValue { |
| 1867 | value: value.unwrap().to_owned(), |
| 1868 | expected: String::from("the dmi path should be directory"), |
| 1869 | }); |
| 1870 | } |
| 1871 | cfg.dmi_path = Some(dmi_path); |
| 1872 | } |
Tomasz Jeznach | d93c29f | 2021-04-12 11:00:24 -0700 | [diff] [blame] | 1873 | "no-legacy" => { |
| 1874 | cfg.no_legacy = true; |
| 1875 | } |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1876 | "help" => return Err(argument::Error::PrintHelp), |
| 1877 | _ => unreachable!(), |
| 1878 | } |
| 1879 | Ok(()) |
| 1880 | } |
| 1881 | |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 1882 | fn validate_arguments(cfg: &mut Config) -> std::result::Result<(), argument::Error> { |
| 1883 | if cfg.executable_path.is_none() { |
| 1884 | return Err(argument::Error::ExpectedArgument("`KERNEL`".to_owned())); |
| 1885 | } |
| 1886 | if cfg.host_ip.is_some() || cfg.netmask.is_some() || cfg.mac_address.is_some() { |
| 1887 | if cfg.host_ip.is_none() { |
| 1888 | return Err(argument::Error::ExpectedArgument( |
| 1889 | "`host_ip` missing from network config".to_owned(), |
| 1890 | )); |
| 1891 | } |
| 1892 | if cfg.netmask.is_none() { |
| 1893 | return Err(argument::Error::ExpectedArgument( |
| 1894 | "`netmask` missing from network config".to_owned(), |
| 1895 | )); |
| 1896 | } |
| 1897 | if cfg.mac_address.is_none() { |
| 1898 | return Err(argument::Error::ExpectedArgument( |
| 1899 | "`mac` missing from network config".to_owned(), |
| 1900 | )); |
| 1901 | } |
| 1902 | } |
| 1903 | if cfg.plugin_root.is_some() && !executable_is_plugin(&cfg.executable_path) { |
| 1904 | return Err(argument::Error::ExpectedArgument( |
| 1905 | "`plugin-root` requires `plugin`".to_owned(), |
| 1906 | )); |
| 1907 | } |
| 1908 | #[cfg(feature = "gpu")] |
| 1909 | { |
Jason Macnak | d659a0d | 2021-03-15 15:33:01 -0700 | [diff] [blame^] | 1910 | if let Some(gpu_parameters) = cfg.gpu_parameters.as_mut() { |
| 1911 | if gpu_parameters.displays.is_empty() { |
| 1912 | gpu_parameters.displays.push(GpuDisplayParameters { |
| 1913 | width: DEFAULT_DISPLAY_WIDTH, |
| 1914 | height: DEFAULT_DISPLAY_HEIGHT, |
| 1915 | }); |
| 1916 | } |
| 1917 | |
| 1918 | let width = gpu_parameters.displays[0].width; |
| 1919 | let height = gpu_parameters.displays[0].height; |
| 1920 | |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 1921 | if let Some(virtio_multi_touch) = cfg.virtio_multi_touch.first_mut() { |
Tristan Muntsinger | 486cffc | 2020-09-29 22:05:41 +0000 | [diff] [blame] | 1922 | virtio_multi_touch.set_default_size(width, height); |
| 1923 | } |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 1924 | if let Some(virtio_single_touch) = cfg.virtio_single_touch.first_mut() { |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 1925 | virtio_single_touch.set_default_size(width, height); |
| 1926 | } |
| 1927 | } |
| 1928 | } |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 1929 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 1930 | if cfg.gdb.is_some() { |
| 1931 | if cfg.vcpu_count.unwrap_or(1) != 1 { |
| 1932 | return Err(argument::Error::ExpectedArgument( |
| 1933 | "`gdb` requires the number of vCPU to be 1".to_owned(), |
| 1934 | )); |
| 1935 | } |
| 1936 | } |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 1937 | set_default_serial_parameters(&mut cfg.serial_parameters); |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 1938 | Ok(()) |
| 1939 | } |
| 1940 | |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 1941 | fn run_vm(args: std::env::Args) -> std::result::Result<(), ()> { |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1942 | let arguments = |
| 1943 | &[Argument::positional("KERNEL", "bzImage of kernel to run"), |
Christian Blichmann | 33d5677 | 2021-03-04 19:03:54 +0100 | [diff] [blame] | 1944 | Argument::value("kvm-device", "PATH", "Path to the KVM device. (default /dev/kvm)"), |
Christian Blichmann | 2f5d4b6 | 2021-03-10 18:08:08 +0100 | [diff] [blame] | 1945 | Argument::value("vhost-vsock-device", "PATH", "Path to the vhost-vsock device. (default /dev/vhost-vsock)"), |
| 1946 | Argument::value("vhost-net-device", "PATH", "Path to the vhost-net device. (default /dev/vhost-net)"), |
Tristan Muntsinger | 4133b01 | 2018-12-21 16:01:56 -0800 | [diff] [blame] | 1947 | Argument::value("android-fstab", "PATH", "Path to Android fstab"), |
Daniel Verkamp | e403f5c | 2018-12-11 16:29:26 -0800 | [diff] [blame] | 1948 | Argument::short_value('i', "initrd", "PATH", "Initial ramdisk to load."), |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1949 | Argument::short_value('p', |
| 1950 | "params", |
| 1951 | "PARAMS", |
Zach Reizner | bb67871 | 2018-01-30 18:13:04 -0800 | [diff] [blame] | 1952 | "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] | 1953 | Argument::short_value('c', "cpus", "N", "Number of VCPUs. (default: 1)"), |
Daniel Verkamp | c677fb4 | 2020-09-08 13:47:49 -0700 | [diff] [blame] | 1954 | Argument::value("cpu-affinity", "CPUSET", "Comma-separated list of CPUs or CPU ranges to run VCPUs on (e.g. 0,1-3,5) |
| 1955 | or colon-separated list of assignments of guest to host CPU assignments (e.g. 0=0:1=1:2=2) (default: no mask)"), |
Daniel Verkamp | 8a72afc | 2021-03-15 17:55:52 -0700 | [diff] [blame] | 1956 | Argument::value("cpu-cluster", "CPUSET", "Group the given CPUs into a cluster (default: no clusters)"), |
| 1957 | Argument::value("cpu-capacity", "CPU=CAP[,CPU=CAP[,...]]", "Set the relative capacity of the given CPU (default: no capacity)"), |
Suleiman Souhlal | 015c3c1 | 2020-10-07 14:15:41 +0900 | [diff] [blame] | 1958 | Argument::flag("no-smt", "Don't use SMT in the guest"), |
Kansho Nishida | ab205af | 2020-08-13 18:17:50 +0900 | [diff] [blame] | 1959 | Argument::value("rt-cpus", "CPUSET", "Comma-separated list of CPUs or CPU ranges to run VCPUs on. (e.g. 0,1-3,5) (default: none)"), |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1960 | Argument::short_value('m', |
| 1961 | "mem", |
| 1962 | "N", |
| 1963 | "Amount of guest memory in MiB. (default: 256)"), |
Sergey Senozhatsky | 1e369c5 | 2021-04-13 20:23:51 +0900 | [diff] [blame] | 1964 | Argument::flag("hugepages", "Advise the kernel to use Huge Pages for guest memory mappings."), |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 1965 | Argument::short_value('r', |
| 1966 | "root", |
Daniel Verkamp | e73c80f | 2019-11-08 10:11:16 -0800 | [diff] [blame] | 1967 | "PATH[,key=value[,key=value[,...]]", |
| 1968 | "Path to a root disk image followed by optional comma-separated options. |
| 1969 | Like `--disk` but adds appropriate kernel command line option. |
| 1970 | See --disk for valid options."), |
| 1971 | Argument::value("rwroot", "PATH[,key=value[,key=value[,...]]", "Path to a writable root disk image followed by optional comma-separated options. |
| 1972 | See --disk for valid options."), |
| 1973 | Argument::short_value('d', "disk", "PATH[,key=value[,key=value[,...]]", "Path to a disk image followed by optional comma-separated options. |
| 1974 | Valid keys: |
Daniel Verkamp | 2767223 | 2019-12-06 17:26:55 +1100 | [diff] [blame] | 1975 | sparse=BOOL - Indicates whether the disk should support the discard operation (default: true) |
Daniel Verkamp | 4e1f99a | 2020-06-01 12:47:21 -0700 | [diff] [blame] | 1976 | block_size=BYTES - Set the reported block size of the disk (default: 512) |
| 1977 | id=STRING - Set the block device identifier to an ASCII string, up to 20 characters (default: no ID)"), |
Daniel Verkamp | e73c80f | 2019-11-08 10:11:16 -0800 | [diff] [blame] | 1978 | Argument::value("rwdisk", "PATH[,key=value[,key=value[,...]]", "Path to a writable disk image followed by optional comma-separated options. |
| 1979 | See --disk for valid options."), |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1980 | Argument::value("rw-pmem-device", "PATH", "Path to a writable disk image."), |
| 1981 | Argument::value("pmem-device", "PATH", "Path to a disk image."), |
Kansho Nishida | 282115b | 2019-12-18 13:13:14 +0900 | [diff] [blame] | 1982 | 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] | 1983 | Argument::value("host_ip", |
| 1984 | "IP", |
| 1985 | "IP address to assign to host tap interface."), |
| 1986 | Argument::value("netmask", "NETMASK", "Netmask for VM subnet."), |
| 1987 | Argument::value("mac", "MAC", "MAC address for VM."), |
Xiong Zhang | 773c707 | 2020-03-20 10:39:55 +0800 | [diff] [blame] | 1988 | Argument::value("net-vq-pairs", "N", "virtio net virtual queue paris. (default: 1)"), |
Andrew Scull | 1590e6f | 2020-03-18 18:00:47 +0000 | [diff] [blame] | 1989 | #[cfg(feature = "audio")] |
Judy Hsiao | d5c1e96 | 2020-02-04 12:30:01 +0800 | [diff] [blame] | 1990 | Argument::value("ac97", |
paulhsia | 83d5160 | 2021-03-09 17:13:14 +0800 | [diff] [blame] | 1991 | "[backend=BACKEND,capture=true,capture_effect=EFFECT,client_type=TYPE,shm-fd=FD,client-fd=FD,server-fd=FD]", |
Judy Hsiao | d5c1e96 | 2020-02-04 12:30:01 +0800 | [diff] [blame] | 1992 | "Comma separated key=value pairs for setting up Ac97 devices. Can be given more than once . |
| 1993 | Possible key values: |
Jorge E. Moreira | 359e7de | 2020-12-02 18:25:53 -0800 | [diff] [blame] | 1994 | backend=(null, cras, vios) - Where to route the audio device. If not provided, backend will default to null. |
| 1995 | `null` for /dev/null, cras for CRAS server and vios for VioS server. |
Judy Hsiao | d5c1e96 | 2020-02-04 12:30:01 +0800 | [diff] [blame] | 1996 | capture - Enable audio capture |
Jorge E. Moreira | 359e7de | 2020-12-02 18:25:53 -0800 | [diff] [blame] | 1997 | capture_effects - | separated effects to be enabled for recording. The only supported effect value now is EchoCancellation or aec. |
paulhsia | 83d5160 | 2021-03-09 17:13:14 +0800 | [diff] [blame] | 1998 | client_type - Set specific client type for cras backend. |
Jorge E. Moreira | 359e7de | 2020-12-02 18:25:53 -0800 | [diff] [blame] | 1999 | server - The to the VIOS server (unix socket)."), |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 2000 | Argument::value("serial", |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 2001 | "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] | 2002 | "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] | 2003 | Possible key values: |
Jorge E. Moreira | 9c9e0e7 | 2019-05-17 13:57:04 -0700 | [diff] [blame] | 2004 | type=(stdout,syslog,sink,file) - Where to route the serial device |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 2005 | 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] | 2006 | 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] | 2007 | 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] | 2008 | 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] | 2009 | 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] | 2010 | 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] | 2011 | 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] | 2012 | "), |
| 2013 | 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] | 2014 | Argument::value("x-display", "DISPLAY", "X11 display name to use."), |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 2015 | Argument::flag("display-window-keyboard", "Capture keyboard input from the display window."), |
| 2016 | Argument::flag("display-window-mouse", "Capture keyboard input from the display window."), |
Ryo Hashimoto | 0b788de | 2019-12-10 17:14:13 +0900 | [diff] [blame] | 2017 | 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] | 2018 | #[cfg(feature = "wl-dmabuf")] |
| 2019 | Argument::flag("wayland-dmabuf", "Enable support for DMABufs in Wayland device."), |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 2020 | Argument::short_value('s', |
| 2021 | "socket", |
| 2022 | "PATH", |
| 2023 | "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] | 2024 | Argument::flag("disable-sandbox", "Run all devices in one, non-sandboxed process."), |
Chirantan Ekbote | ebd5681 | 2018-04-16 19:32:04 -0700 | [diff] [blame] | 2025 | Argument::value("cid", "CID", "Context ID for virtual sockets."), |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 2026 | Argument::value("shared-dir", "PATH:TAG[:type=TYPE:writeback=BOOL:timeout=SECONDS:uidmap=UIDMAP:gidmap=GIDMAP:cache=CACHE]", |
| 2027 | "Colon-separated options for configuring a directory to be shared with the VM. |
| 2028 | 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. |
| 2029 | The remaining fields are key=value pairs that may appear in any order. Valid keys are: |
| 2030 | type=(p9, fs) - Indicates whether the directory should be shared via virtio-9p or virtio-fs (default: p9). |
| 2031 | 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). |
| 2032 | 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). |
| 2033 | 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. |
| 2034 | 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. |
| 2035 | 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. |
| 2036 | "), |
Dylan Reid | e026ef0 | 2017-10-02 19:03:52 -0700 | [diff] [blame] | 2037 | Argument::value("seccomp-policy-dir", "PATH", "Path to seccomp .policy files."), |
Zach Reizner | 4486379 | 2019-06-26 14:22:08 -0700 | [diff] [blame] | 2038 | 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] | 2039 | #[cfg(feature = "plugin")] |
Zach Reizner | cc30d58 | 2018-01-23 21:16:42 -0800 | [diff] [blame] | 2040 | 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] | 2041 | #[cfg(feature = "plugin")] |
Dmitry Torokhov | 0f1770d | 2018-05-11 10:57:16 -0700 | [diff] [blame] | 2042 | 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] | 2043 | #[cfg(feature = "plugin")] |
Chirantan Ekbote | d41d726 | 2018-11-16 16:37:45 -0800 | [diff] [blame] | 2044 | 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] | 2045 | #[cfg(feature = "plugin")] |
Dmitry Torokhov | 0f4c5ff | 2019-12-11 13:36:08 -0800 | [diff] [blame] | 2046 | 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."), |
| 2047 | #[cfg(feature = "plugin")] |
Dmitry Torokhov | 1a6262b | 2019-03-01 00:34:03 -0800 | [diff] [blame] | 2048 | 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] | 2049 | #[cfg(feature = "plugin")] |
| 2050 | 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] | 2051 | Argument::flag("vhost-net", "Use vhost for networking."), |
Chirantan Ekbote | 5f78721 | 2018-05-31 15:31:31 -0700 | [diff] [blame] | 2052 | Argument::value("tap-fd", |
| 2053 | "fd", |
Jorge E. Moreira | b795280 | 2019-02-12 16:43:05 -0800 | [diff] [blame] | 2054 | "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] | 2055 | #[cfg(feature = "gpu")] |
Jason Macnak | cc7070b | 2019-11-06 14:48:12 -0800 | [diff] [blame] | 2056 | Argument::flag_or_value("gpu", |
| 2057 | "[width=INT,height=INT]", |
| 2058 | "(EXPERIMENTAL) Comma separated key=value pairs for setting up a virtio-gpu device |
| 2059 | Possible key values: |
Chia-I Wu | e25859b | 2021-04-14 13:57:55 -0700 | [diff] [blame] | 2060 | backend=(2d|virglrenderer|gfxstream) - Which backend to use for virtio-gpu (determining rendering protocol) |
Jason Macnak | cc7070b | 2019-11-06 14:48:12 -0800 | [diff] [blame] | 2061 | width=INT - The width of the virtual display connected to the virtio-gpu. |
| 2062 | height=INT - The height of the virtual display connected to the virtio-gpu. |
Chia-I Wu | e25859b | 2021-04-14 13:57:55 -0700 | [diff] [blame] | 2063 | egl[=true|=false] - If the backend should use a EGL context for rendering. |
| 2064 | glx[=true|=false] - If the backend should use a GLX context for rendering. |
| 2065 | surfaceless[=true|=false] - If the backend should use a surfaceless context for rendering. |
| 2066 | angle[=true|=false] - If the gfxstream backend should use ANGLE (OpenGL on Vulkan) as its native OpenGL driver. |
Kaiyi Li | 6404e45 | 2020-07-07 19:12:03 -0700 | [diff] [blame] | 2067 | syncfd[=true|=false] - If the gfxstream backend should support EGL_ANDROID_native_fence_sync |
Chia-I Wu | 6d473b3 | 2021-04-12 10:14:24 -0700 | [diff] [blame] | 2068 | vulkan[=true|=false] - If the backend should support vulkan |
Jason Macnak | cc7070b | 2019-11-06 14:48:12 -0800 | [diff] [blame] | 2069 | "), |
Jason Macnak | d659a0d | 2021-03-15 15:33:01 -0700 | [diff] [blame^] | 2070 | #[cfg(feature = "gpu")] |
| 2071 | Argument::flag_or_value("gpu-display", |
| 2072 | "[width=INT,height=INT]", |
| 2073 | "(EXPERIMENTAL) Comma separated key=value pairs for setting up a display on the virtio-gpu device |
| 2074 | Possible key values: |
| 2075 | width=INT - The width of the virtual display connected to the virtio-gpu. |
| 2076 | height=INT - The height of the virtual display connected to the virtio-gpu. |
| 2077 | "), |
David Tolnay | 43f8e21 | 2019-02-13 17:28:16 -0800 | [diff] [blame] | 2078 | #[cfg(feature = "tpm")] |
| 2079 | 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] | 2080 | 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] | 2081 | 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)."), |
Tristan Muntsinger | 486cffc | 2020-09-29 22:05:41 +0000 | [diff] [blame] | 2082 | Argument::value("multi-touch", "PATH:WIDTH:HEIGHT", "Path to a socket from where to read multi 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] | 2083 | 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)."), |
| 2084 | Argument::value("mouse", "PATH", "Path to a socket from where to read mouse input events and write status updates to."), |
| 2085 | Argument::value("keyboard", "PATH", "Path to a socket from where to read keyboard input events and write status updates to."), |
Daniel Norman | 5e23df7 | 2021-03-11 10:11:02 -0800 | [diff] [blame] | 2086 | Argument::value("switches", "PATH", "Path to a socket from where to read switch input events and write status updates to."), |
Miriam Zimmerman | 26ac928 | 2019-01-29 21:21:48 -0800 | [diff] [blame] | 2087 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 2088 | Argument::flag("split-irqchip", "(EXPERIMENTAL) enable split-irqchip support"), |
Cody Schuffelen | 6d1ab50 | 2019-05-21 12:12:38 -0700 | [diff] [blame] | 2089 | Argument::value("bios", "PATH", "Path to BIOS/firmware ROM"), |
Xiong Zhang | 17b0daf | 2019-04-23 17:14:50 +0800 | [diff] [blame] | 2090 | Argument::value("vfio", "PATH", "Path to sysfs of pass through or mdev device"), |
Keiichi Watanabe | 57df6a0 | 2019-12-06 22:24:40 +0900 | [diff] [blame] | 2091 | #[cfg(feature = "video-decoder")] |
| 2092 | Argument::flag("video-decoder", "(EXPERIMENTAL) enable virtio-video decoder device"), |
| 2093 | #[cfg(feature = "video-encoder")] |
| 2094 | Argument::flag("video-encoder", "(EXPERIMENTAL) enable virtio-video encoder device"), |
Tomasz Jeznach | 4264464 | 2020-05-20 23:27:59 -0700 | [diff] [blame] | 2095 | Argument::value("acpi-table", "PATH", "Path to user provided ACPI table"), |
Will Deacon | 6560c18 | 2020-10-06 18:47:18 +0100 | [diff] [blame] | 2096 | Argument::flag("protected-vm", "(EXPERIMENTAL) prevent host access to guest memory"), |
Chuanxiao Dong | fd5626c | 2020-04-27 16:35:33 +0800 | [diff] [blame] | 2097 | Argument::flag_or_value("battery", |
| 2098 | "[type=TYPE]", |
| 2099 | "Comma separated key=value pairs for setting up battery device |
| 2100 | Possible key values: |
| 2101 | type=goldfish - type of battery emulation, defaults to goldfish |
| 2102 | "), |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 2103 | Argument::value("gdb", "PORT", "(EXPERIMENTAL) gdb on the given port"), |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 2104 | Argument::value("balloon_bias_mib", "N", "Amount to bias balance of memory between host and guest as the balloon inflates, in MiB."), |
Keiichi Watanabe | f3a37f4 | 2021-01-21 15:41:11 +0900 | [diff] [blame] | 2105 | Argument::value("vhost-user-blk", "SOCKET_PATH", "Path to a socket for vhost-user block"), |
Keiichi Watanabe | 6068658 | 2021-03-12 04:53:51 +0900 | [diff] [blame] | 2106 | Argument::value("vhost-user-net", "SOCKET_PATH", "Path to a socket for vhost-user net"), |
Chirantan Ekbote | 2ee9dcd | 2021-05-26 18:21:44 +0900 | [diff] [blame] | 2107 | Argument::value("vhost-user-wl", "SOCKET_PATH:TUBE_PATH", "Paths to a vhost-user socket for wayland and a Tube socket for additional wayland-specific messages"), |
Woody Chow | 5890b70 | 2021-02-12 14:57:02 +0900 | [diff] [blame] | 2108 | Argument::value("vhost-user-fs", "SOCKET_PATH:TAG", |
| 2109 | "Path to a socket path for vhost-user fs, and tag for the shared dir"), |
Tomasz Jeznach | 3ce7476 | 2021-02-26 01:01:53 -0800 | [diff] [blame] | 2110 | #[cfg(feature = "direct")] |
| 2111 | Argument::value("direct-pmio", "PATH@RANGE[,RANGE[,...]]", "Path and ranges for direct port I/O access"), |
Tomasz Jeznach | 7271f75 | 2021-03-04 01:44:06 -0800 | [diff] [blame] | 2112 | #[cfg(feature = "direct")] |
| 2113 | Argument::value("direct-level-irq", "irq", "Enable interrupt passthrough"), |
| 2114 | #[cfg(feature = "direct")] |
| 2115 | Argument::value("direct-edge-irq", "irq", "Enable interrupt passthrough"), |
Tomasz Jeznach | ccb2694 | 2021-03-30 22:44:11 -0700 | [diff] [blame] | 2116 | Argument::value("dmi", "DIR", "Directory with smbios_entry_point/DMI files"), |
Tomasz Jeznach | d93c29f | 2021-04-12 11:00:24 -0700 | [diff] [blame] | 2117 | Argument::flag("no-legacy", "Don't use legacy KBD/RTC devices emulation"), |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 2118 | Argument::short_flag('h', "help", "Print help message.")]; |
| 2119 | |
| 2120 | let mut cfg = Config::default(); |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 2121 | let match_res = set_arguments(args, &arguments[..], |name, value| { |
| 2122 | set_argument(&mut cfg, name, value) |
David Tolnay | 2bac1e7 | 2018-12-12 14:33:42 -0800 | [diff] [blame] | 2123 | }) |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 2124 | .and_then(|_| validate_arguments(&mut cfg)); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 2125 | |
| 2126 | match match_res { |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 2127 | #[cfg(feature = "plugin")] |
Zach Reizner | 267f2c8 | 2019-07-31 17:07:27 -0700 | [diff] [blame] | 2128 | Ok(()) if executable_is_plugin(&cfg.executable_path) => { |
| 2129 | match crosvm::plugin::run_config(cfg) { |
| 2130 | Ok(_) => { |
| 2131 | info!("crosvm and plugin have exited normally"); |
| 2132 | Ok(()) |
| 2133 | } |
| 2134 | Err(e) => { |
| 2135 | error!("{}", e); |
| 2136 | Err(()) |
| 2137 | } |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 2138 | } |
Zach Reizner | 267f2c8 | 2019-07-31 17:07:27 -0700 | [diff] [blame] | 2139 | } |
Michael Hoyle | e47a500 | 2020-10-15 16:24:13 -0700 | [diff] [blame] | 2140 | Ok(()) => match platform::run_config(cfg) { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 2141 | Ok(_) => { |
| 2142 | info!("crosvm has exited normally"); |
| 2143 | Ok(()) |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 2144 | } |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 2145 | Err(e) => { |
Junichi Uekawa | c7aa523 | 2020-09-07 14:58:36 +0900 | [diff] [blame] | 2146 | error!("crosvm has exited with error: {}", e); |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 2147 | Err(()) |
| 2148 | } |
| 2149 | }, |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 2150 | Err(argument::Error::PrintHelp) => { |
| 2151 | print_help("crosvm run", "KERNEL", &arguments[..]); |
| 2152 | Ok(()) |
| 2153 | } |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 2154 | Err(e) => { |
Dmitry Torokhov | 470b1e7 | 2020-01-15 12:46:49 -0800 | [diff] [blame] | 2155 | error!("{}", e); |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 2156 | Err(()) |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 2157 | } |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 2158 | } |
| 2159 | } |
| 2160 | |
Kevin Hamacher | 6fc5f20 | 2021-03-18 12:41:23 +0100 | [diff] [blame] | 2161 | fn stop_vms(mut args: std::env::Args) -> std::result::Result<(), ()> { |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame] | 2162 | if args.len() == 0 { |
| 2163 | print_help("crosvm stop", "VM_SOCKET...", &[]); |
| 2164 | println!("Stops the crosvm instance listening on each `VM_SOCKET` given."); |
Jianxun Zhang | 56497d2 | 2019-03-04 14:38:24 -0800 | [diff] [blame] | 2165 | return Err(()); |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame] | 2166 | } |
Kevin Hamacher | 6fc5f20 | 2021-03-18 12:41:23 +0100 | [diff] [blame] | 2167 | let socket_path = &args.next().unwrap(); |
| 2168 | let socket_path = Path::new(&socket_path); |
| 2169 | vms_request(&VmRequest::Exit, socket_path) |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame] | 2170 | } |
| 2171 | |
Kevin Hamacher | 6fc5f20 | 2021-03-18 12:41:23 +0100 | [diff] [blame] | 2172 | fn suspend_vms(mut args: std::env::Args) -> std::result::Result<(), ()> { |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame] | 2173 | if args.len() == 0 { |
| 2174 | print_help("crosvm suspend", "VM_SOCKET...", &[]); |
| 2175 | println!("Suspends the crosvm instance listening on each `VM_SOCKET` given."); |
Jianxun Zhang | 56497d2 | 2019-03-04 14:38:24 -0800 | [diff] [blame] | 2176 | return Err(()); |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame] | 2177 | } |
Kevin Hamacher | 6fc5f20 | 2021-03-18 12:41:23 +0100 | [diff] [blame] | 2178 | let socket_path = &args.next().unwrap(); |
| 2179 | let socket_path = Path::new(&socket_path); |
| 2180 | vms_request(&VmRequest::Suspend, socket_path) |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame] | 2181 | } |
| 2182 | |
Kevin Hamacher | 6fc5f20 | 2021-03-18 12:41:23 +0100 | [diff] [blame] | 2183 | fn resume_vms(mut args: std::env::Args) -> std::result::Result<(), ()> { |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame] | 2184 | if args.len() == 0 { |
| 2185 | print_help("crosvm resume", "VM_SOCKET...", &[]); |
| 2186 | println!("Resumes the crosvm instance listening on each `VM_SOCKET` given."); |
Jianxun Zhang | 56497d2 | 2019-03-04 14:38:24 -0800 | [diff] [blame] | 2187 | return Err(()); |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame] | 2188 | } |
Kevin Hamacher | 6fc5f20 | 2021-03-18 12:41:23 +0100 | [diff] [blame] | 2189 | let socket_path = &args.next().unwrap(); |
| 2190 | let socket_path = Path::new(&socket_path); |
| 2191 | vms_request(&VmRequest::Resume, socket_path) |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame] | 2192 | } |
| 2193 | |
| 2194 | fn balloon_vms(mut args: std::env::Args) -> std::result::Result<(), ()> { |
| 2195 | if args.len() < 2 { |
| 2196 | print_help("crosvm balloon", "SIZE VM_SOCKET...", &[]); |
| 2197 | println!("Set the ballon size of the crosvm instance to `SIZE` bytes."); |
Jianxun Zhang | 56497d2 | 2019-03-04 14:38:24 -0800 | [diff] [blame] | 2198 | return Err(()); |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame] | 2199 | } |
Keiichi Watanabe | 275c1ef | 2020-04-10 21:49:10 +0900 | [diff] [blame] | 2200 | let num_bytes = match args.next().unwrap().parse::<u64>() { |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame] | 2201 | Ok(n) => n, |
| 2202 | Err(_) => { |
| 2203 | error!("Failed to parse number of bytes"); |
| 2204 | return Err(()); |
| 2205 | } |
| 2206 | }; |
| 2207 | |
Jakub Staron | 1f828d7 | 2019-04-11 12:49:29 -0700 | [diff] [blame] | 2208 | let command = BalloonControlCommand::Adjust { num_bytes }; |
Kevin Hamacher | 6fc5f20 | 2021-03-18 12:41:23 +0100 | [diff] [blame] | 2209 | let socket_path = &args.next().unwrap(); |
| 2210 | let socket_path = Path::new(&socket_path); |
| 2211 | vms_request(&VmRequest::BalloonCommand(command), socket_path) |
Zach Reizner | 7898632 | 2019-02-21 20:43:21 -0800 | [diff] [blame] | 2212 | } |
| 2213 | |
Kevin Hamacher | 6fc5f20 | 2021-03-18 12:41:23 +0100 | [diff] [blame] | 2214 | fn balloon_stats(mut args: std::env::Args) -> std::result::Result<(), ()> { |
Charles William Dick | ed22f6b | 2020-04-08 11:05:24 +0900 | [diff] [blame] | 2215 | if args.len() != 1 { |
| 2216 | print_help("crosvm balloon_stats", "VM_SOCKET", &[]); |
| 2217 | println!("Prints virtio balloon statistics for a `VM_SOCKET`."); |
| 2218 | return Err(()); |
| 2219 | } |
| 2220 | let command = BalloonControlCommand::Stats {}; |
| 2221 | let request = &VmRequest::BalloonCommand(command); |
Kevin Hamacher | 6fc5f20 | 2021-03-18 12:41:23 +0100 | [diff] [blame] | 2222 | let socket_path = &args.next().unwrap(); |
| 2223 | let socket_path = Path::new(&socket_path); |
| 2224 | let response = handle_request(request, socket_path)?; |
Hikaru Nishida | a0e381b | 2021-05-24 17:13:45 +0900 | [diff] [blame] | 2225 | match serde_json::to_string_pretty(&response) { |
| 2226 | Ok(response_json) => println!("{}", response_json), |
| 2227 | Err(e) => { |
| 2228 | error!("Failed to serialize into JSON: {}", e); |
| 2229 | return Err(()); |
| 2230 | } |
| 2231 | } |
Hikaru Nishida | 6b51c75 | 2021-05-21 12:37:43 +0900 | [diff] [blame] | 2232 | match response { |
| 2233 | VmResponse::BalloonStats { .. } => Ok(()), |
| 2234 | _ => Err(()), |
| 2235 | } |
Charles William Dick | ed22f6b | 2020-04-08 11:05:24 +0900 | [diff] [blame] | 2236 | } |
| 2237 | |
A. Cody Schuffelen | 9ca6039 | 2019-12-23 18:27:11 -0800 | [diff] [blame] | 2238 | fn create_qcow2(args: std::env::Args) -> std::result::Result<(), ()> { |
| 2239 | let arguments = [ |
| 2240 | Argument::positional("PATH", "where to create the qcow2 image"), |
| 2241 | Argument::positional("[SIZE]", "the expanded size of the image"), |
| 2242 | Argument::value( |
| 2243 | "backing_file", |
| 2244 | "path/to/file", |
| 2245 | " the file to back the image", |
| 2246 | ), |
| 2247 | ]; |
| 2248 | let mut positional_index = 0; |
| 2249 | let mut file_path = String::from(""); |
| 2250 | let mut size: Option<u64> = None; |
| 2251 | let mut backing_file: Option<String> = None; |
| 2252 | set_arguments(args, &arguments[..], |name, value| { |
| 2253 | match (name, positional_index) { |
| 2254 | ("", 0) => { |
| 2255 | // NAME |
| 2256 | positional_index += 1; |
| 2257 | file_path = value.unwrap().to_owned(); |
| 2258 | } |
| 2259 | ("", 1) => { |
| 2260 | // [SIZE] |
| 2261 | positional_index += 1; |
| 2262 | size = Some(value.unwrap().parse::<u64>().map_err(|_| { |
| 2263 | argument::Error::InvalidValue { |
| 2264 | value: value.unwrap().to_owned(), |
Judy Hsiao | 5934305 | 2020-03-16 15:58:03 +0800 | [diff] [blame] | 2265 | expected: String::from("SIZE should be a nonnegative integer"), |
A. Cody Schuffelen | 9ca6039 | 2019-12-23 18:27:11 -0800 | [diff] [blame] | 2266 | } |
| 2267 | })?); |
| 2268 | } |
| 2269 | ("", _) => { |
| 2270 | return Err(argument::Error::TooManyArguments( |
| 2271 | "Expected at most 2 positional arguments".to_owned(), |
| 2272 | )); |
| 2273 | } |
| 2274 | ("backing_file", _) => { |
| 2275 | backing_file = value.map(|x| x.to_owned()); |
| 2276 | } |
| 2277 | _ => unreachable!(), |
| 2278 | }; |
| 2279 | Ok(()) |
| 2280 | }) |
| 2281 | .map_err(|e| { |
| 2282 | error!("Unable to parse command line arguments: {}", e); |
| 2283 | })?; |
Keiichi Watanabe | 275c1ef | 2020-04-10 21:49:10 +0900 | [diff] [blame] | 2284 | 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] | 2285 | print_help("crosvm create_qcow2", "PATH [SIZE]", &arguments); |
| 2286 | println!( |
| 2287 | "Create a new QCOW2 image at `PATH` of either the specified `SIZE` in bytes or |
| 2288 | with a '--backing_file'." |
| 2289 | ); |
Jianxun Zhang | 56497d2 | 2019-03-04 14:38:24 -0800 | [diff] [blame] | 2290 | return Err(()); |
Dylan Reid | 2dcb632 | 2018-07-13 10:42:48 -0700 | [diff] [blame] | 2291 | } |
Dylan Reid | 2dcb632 | 2018-07-13 10:42:48 -0700 | [diff] [blame] | 2292 | |
| 2293 | let file = OpenOptions::new() |
| 2294 | .create(true) |
| 2295 | .read(true) |
| 2296 | .write(true) |
A. Cody Schuffelen | 9ca6039 | 2019-12-23 18:27:11 -0800 | [diff] [blame] | 2297 | .truncate(true) |
Dylan Reid | 2dcb632 | 2018-07-13 10:42:48 -0700 | [diff] [blame] | 2298 | .open(&file_path) |
| 2299 | .map_err(|e| { |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 2300 | error!("Failed opening qcow file at '{}': {}", file_path, e); |
Dylan Reid | 2dcb632 | 2018-07-13 10:42:48 -0700 | [diff] [blame] | 2301 | })?; |
| 2302 | |
A. Cody Schuffelen | 9ca6039 | 2019-12-23 18:27:11 -0800 | [diff] [blame] | 2303 | match (size, backing_file) { |
| 2304 | (Some(size), None) => QcowFile::new(file, size).map_err(|e| { |
| 2305 | error!("Failed to create qcow file at '{}': {}", file_path, e); |
| 2306 | })?, |
| 2307 | (None, Some(backing_file)) => { |
| 2308 | QcowFile::new_from_backing(file, &backing_file).map_err(|e| { |
| 2309 | error!("Failed to create qcow file at '{}': {}", file_path, e); |
| 2310 | })? |
| 2311 | } |
| 2312 | _ => unreachable!(), |
| 2313 | }; |
Dylan Reid | 2dcb632 | 2018-07-13 10:42:48 -0700 | [diff] [blame] | 2314 | Ok(()) |
| 2315 | } |
| 2316 | |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 2317 | fn disk_cmd(mut args: std::env::Args) -> std::result::Result<(), ()> { |
| 2318 | if args.len() < 2 { |
| 2319 | print_help("crosvm disk", "SUBCOMMAND VM_SOCKET...", &[]); |
| 2320 | println!("Manage attached virtual disk devices."); |
| 2321 | println!("Subcommands:"); |
| 2322 | println!(" resize DISK_INDEX NEW_SIZE VM_SOCKET"); |
Jianxun Zhang | 56497d2 | 2019-03-04 14:38:24 -0800 | [diff] [blame] | 2323 | return Err(()); |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 2324 | } |
Keiichi Watanabe | 275c1ef | 2020-04-10 21:49:10 +0900 | [diff] [blame] | 2325 | let subcommand: &str = &args.next().unwrap(); |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 2326 | |
| 2327 | let request = match subcommand { |
| 2328 | "resize" => { |
Keiichi Watanabe | 275c1ef | 2020-04-10 21:49:10 +0900 | [diff] [blame] | 2329 | let disk_index = match args.next().unwrap().parse::<usize>() { |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 2330 | Ok(n) => n, |
| 2331 | Err(_) => { |
| 2332 | error!("Failed to parse disk index"); |
| 2333 | return Err(()); |
| 2334 | } |
| 2335 | }; |
| 2336 | |
Keiichi Watanabe | 275c1ef | 2020-04-10 21:49:10 +0900 | [diff] [blame] | 2337 | let new_size = match args.next().unwrap().parse::<u64>() { |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 2338 | Ok(n) => n, |
| 2339 | Err(_) => { |
| 2340 | error!("Failed to parse disk size"); |
| 2341 | return Err(()); |
| 2342 | } |
| 2343 | }; |
| 2344 | |
Jakub Staron | ecf81e0 | 2019-04-11 11:43:39 -0700 | [diff] [blame] | 2345 | VmRequest::DiskCommand { |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 2346 | disk_index, |
Jakub Staron | ecf81e0 | 2019-04-11 11:43:39 -0700 | [diff] [blame] | 2347 | command: DiskControlCommand::Resize { new_size }, |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 2348 | } |
| 2349 | } |
| 2350 | _ => { |
| 2351 | error!("Unknown disk subcommand '{}'", subcommand); |
| 2352 | return Err(()); |
| 2353 | } |
| 2354 | }; |
| 2355 | |
Kevin Hamacher | 6fc5f20 | 2021-03-18 12:41:23 +0100 | [diff] [blame] | 2356 | let socket_path = &args.next().unwrap(); |
| 2357 | let socket_path = Path::new(&socket_path); |
| 2358 | vms_request(&request, socket_path) |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 2359 | } |
| 2360 | |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 2361 | fn parse_bus_id_addr(v: &str) -> ModifyUsbResult<(u8, u8, u16, u16)> { |
| 2362 | debug!("parse_bus_id_addr: {}", v); |
Keiichi Watanabe | 275c1ef | 2020-04-10 21:49:10 +0900 | [diff] [blame] | 2363 | let mut ids = v.split(':'); |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 2364 | match (ids.next(), ids.next(), ids.next(), ids.next()) { |
| 2365 | (Some(bus_id), Some(addr), Some(vid), Some(pid)) => { |
| 2366 | let bus_id = bus_id |
| 2367 | .parse::<u8>() |
| 2368 | .map_err(|e| ModifyUsbError::ArgParseInt("bus_id", bus_id.to_owned(), e))?; |
| 2369 | let addr = addr |
| 2370 | .parse::<u8>() |
| 2371 | .map_err(|e| ModifyUsbError::ArgParseInt("addr", addr.to_owned(), e))?; |
| 2372 | let vid = u16::from_str_radix(&vid, 16) |
| 2373 | .map_err(|e| ModifyUsbError::ArgParseInt("vid", vid.to_owned(), e))?; |
| 2374 | let pid = u16::from_str_radix(&pid, 16) |
| 2375 | .map_err(|e| ModifyUsbError::ArgParseInt("pid", pid.to_owned(), e))?; |
| 2376 | Ok((bus_id, addr, vid, pid)) |
| 2377 | } |
| 2378 | _ => Err(ModifyUsbError::ArgParse( |
| 2379 | "BUS_ID_ADDR_BUS_NUM_DEV_NUM", |
| 2380 | v.to_owned(), |
| 2381 | )), |
| 2382 | } |
| 2383 | } |
| 2384 | |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 2385 | fn usb_attach(mut args: std::env::Args) -> ModifyUsbResult<UsbControlResult> { |
| 2386 | let val = args |
| 2387 | .next() |
| 2388 | .ok_or(ModifyUsbError::ArgMissing("BUS_ID_ADDR_BUS_NUM_DEV_NUM"))?; |
| 2389 | let (bus, addr, vid, pid) = parse_bus_id_addr(&val)?; |
| 2390 | let dev_path = PathBuf::from( |
| 2391 | args.next() |
| 2392 | .ok_or(ModifyUsbError::ArgMissing("usb device path"))?, |
| 2393 | ); |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 2394 | |
Kevin Hamacher | 6fc5f20 | 2021-03-18 12:41:23 +0100 | [diff] [blame] | 2395 | let socket_path = args |
| 2396 | .next() |
| 2397 | .ok_or(ModifyUsbError::ArgMissing("control socket path"))?; |
| 2398 | let socket_path = Path::new(&socket_path); |
| 2399 | |
| 2400 | do_usb_attach(&socket_path, bus, addr, vid, pid, &dev_path) |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 2401 | } |
| 2402 | |
| 2403 | fn usb_detach(mut args: std::env::Args) -> ModifyUsbResult<UsbControlResult> { |
| 2404 | let port: u8 = args |
| 2405 | .next() |
| 2406 | .map_or(Err(ModifyUsbError::ArgMissing("PORT")), |p| { |
| 2407 | p.parse::<u8>() |
| 2408 | .map_err(|e| ModifyUsbError::ArgParseInt("PORT", p.to_owned(), e)) |
| 2409 | })?; |
Kevin Hamacher | 6fc5f20 | 2021-03-18 12:41:23 +0100 | [diff] [blame] | 2410 | let socket_path = args |
| 2411 | .next() |
| 2412 | .ok_or(ModifyUsbError::ArgMissing("control socket path"))?; |
| 2413 | let socket_path = Path::new(&socket_path); |
| 2414 | do_usb_detach(&socket_path, port) |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 2415 | } |
| 2416 | |
Kevin Hamacher | 6fc5f20 | 2021-03-18 12:41:23 +0100 | [diff] [blame] | 2417 | fn usb_list(mut args: std::env::Args) -> ModifyUsbResult<UsbControlResult> { |
| 2418 | let socket_path = args |
| 2419 | .next() |
| 2420 | .ok_or(ModifyUsbError::ArgMissing("control socket path"))?; |
| 2421 | let socket_path = Path::new(&socket_path); |
| 2422 | do_usb_list(&socket_path) |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 2423 | } |
| 2424 | |
| 2425 | fn modify_usb(mut args: std::env::Args) -> std::result::Result<(), ()> { |
Zach Reizner | aff94ca | 2019-03-18 20:58:31 -0700 | [diff] [blame] | 2426 | if args.len() < 2 { |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 2427 | print_help("crosvm usb", |
Zach Reizner | aff94ca | 2019-03-18 20:58:31 -0700 | [diff] [blame] | 2428 | "[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] | 2429 | return Err(()); |
| 2430 | } |
| 2431 | |
| 2432 | // This unwrap will not panic because of the above length check. |
Kevin Hamacher | 6fc5f20 | 2021-03-18 12:41:23 +0100 | [diff] [blame] | 2433 | let command = &args.next().unwrap(); |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 2434 | let result = match command.as_ref() { |
| 2435 | "attach" => usb_attach(args), |
| 2436 | "detach" => usb_detach(args), |
| 2437 | "list" => usb_list(args), |
| 2438 | other => Err(ModifyUsbError::UnknownCommand(other.to_owned())), |
| 2439 | }; |
| 2440 | match result { |
| 2441 | Ok(response) => { |
| 2442 | println!("{}", response); |
| 2443 | Ok(()) |
| 2444 | } |
| 2445 | Err(e) => { |
| 2446 | println!("error {}", e); |
| 2447 | Err(()) |
| 2448 | } |
| 2449 | } |
| 2450 | } |
| 2451 | |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 2452 | fn print_usage() { |
Junichi Uekawa | f0a7723 | 2021-04-08 16:44:44 +0900 | [diff] [blame] | 2453 | print_help("crosvm", "[command]", &[]); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 2454 | println!("Commands:"); |
Junichi Uekawa | f0a7723 | 2021-04-08 16:44:44 +0900 | [diff] [blame] | 2455 | println!(" balloon - Set balloon size of the crosvm instance."); |
| 2456 | println!(" balloon_stats - Prints virtio balloon statistics."); |
| 2457 | println!(" battery - Modify battery."); |
Dylan Reid | 2dcb632 | 2018-07-13 10:42:48 -0700 | [diff] [blame] | 2458 | println!(" create_qcow2 - Create a new qcow2 disk image file."); |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 2459 | println!(" disk - Manage attached virtual disk devices."); |
Junichi Uekawa | f0a7723 | 2021-04-08 16:44:44 +0900 | [diff] [blame] | 2460 | println!(" resume - Resumes the crosvm instance."); |
| 2461 | println!(" run - Start a new crosvm instance."); |
| 2462 | println!(" stop - Stops crosvm instances via their control sockets."); |
| 2463 | println!(" suspend - Suspends the crosvm instance."); |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 2464 | println!(" usb - Manage attached virtual USB devices."); |
Yi Sun | 54305cd | 2020-01-04 00:19:37 +0800 | [diff] [blame] | 2465 | println!(" version - Show package version."); |
| 2466 | } |
| 2467 | |
Zach Reizner | d09392e | 2021-03-12 12:03:48 -0800 | [diff] [blame] | 2468 | #[allow(clippy::unnecessary_wraps)] |
Yi Sun | 54305cd | 2020-01-04 00:19:37 +0800 | [diff] [blame] | 2469 | fn pkg_version() -> std::result::Result<(), ()> { |
| 2470 | const VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION"); |
| 2471 | const PKG_VERSION: Option<&'static str> = option_env!("PKG_VERSION"); |
| 2472 | |
| 2473 | print!("crosvm {}", VERSION.unwrap_or("UNKNOWN")); |
| 2474 | match PKG_VERSION { |
| 2475 | Some(v) => println!("-{}", v), |
Keiichi Watanabe | 275c1ef | 2020-04-10 21:49:10 +0900 | [diff] [blame] | 2476 | None => println!(), |
Yi Sun | 54305cd | 2020-01-04 00:19:37 +0800 | [diff] [blame] | 2477 | } |
| 2478 | Ok(()) |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 2479 | } |
| 2480 | |
Chuanxiao Dong | 256be3a | 2020-04-27 16:39:33 +0800 | [diff] [blame] | 2481 | fn modify_battery(mut args: std::env::Args) -> std::result::Result<(), ()> { |
| 2482 | if args.len() < 4 { |
| 2483 | print_help("crosvm battery BATTERY_TYPE ", |
| 2484 | "[status STATUS | present PRESENT | health HEALTH | capacity CAPACITY | aconline ACONLINE ] VM_SOCKET...", &[]); |
| 2485 | return Err(()); |
| 2486 | } |
| 2487 | |
| 2488 | // This unwrap will not panic because of the above length check. |
| 2489 | let battery_type = args.next().unwrap(); |
| 2490 | let property = args.next().unwrap(); |
| 2491 | let target = args.next().unwrap(); |
| 2492 | |
Kevin Hamacher | 6fc5f20 | 2021-03-18 12:41:23 +0100 | [diff] [blame] | 2493 | let socket_path = args.next().unwrap(); |
| 2494 | let socket_path = Path::new(&socket_path); |
Chuanxiao Dong | 256be3a | 2020-04-27 16:39:33 +0800 | [diff] [blame] | 2495 | |
Kevin Hamacher | 6fc5f20 | 2021-03-18 12:41:23 +0100 | [diff] [blame] | 2496 | do_modify_battery(&socket_path, &*battery_type, &*property, &*target) |
Chuanxiao Dong | 256be3a | 2020-04-27 16:39:33 +0800 | [diff] [blame] | 2497 | } |
| 2498 | |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 2499 | fn crosvm_main() -> std::result::Result<(), ()> { |
Stephen Barber | 56fbf09 | 2017-06-29 16:12:14 -0700 | [diff] [blame] | 2500 | if let Err(e) = syslog::init() { |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 2501 | println!("failed to initialize syslog: {}", e); |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 2502 | return Err(()); |
Stephen Barber | 56fbf09 | 2017-06-29 16:12:14 -0700 | [diff] [blame] | 2503 | } |
Zach Reizner | 639d967 | 2017-05-01 17:57:18 -0700 | [diff] [blame] | 2504 | |
Zach Reizner | b3fa5c9 | 2019-01-28 14:05:23 -0800 | [diff] [blame] | 2505 | panic_hook::set_panic_hook(); |
| 2506 | |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 2507 | let mut args = std::env::args(); |
| 2508 | if args.next().is_none() { |
| 2509 | error!("expected executable name"); |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 2510 | return Err(()); |
Zach Reizner | 639d967 | 2017-05-01 17:57:18 -0700 | [diff] [blame] | 2511 | } |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 2512 | |
Zach Reizner | 8864cb0 | 2018-01-16 17:59:03 -0800 | [diff] [blame] | 2513 | // 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] | 2514 | let ret = match args.next().as_ref().map(|a| a.as_ref()) { |
| 2515 | None => { |
| 2516 | print_usage(); |
| 2517 | Ok(()) |
| 2518 | } |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 2519 | Some("stop") => stop_vms(args), |
Zach Reizner | 6a8fdd9 | 2019-01-16 14:38:41 -0800 | [diff] [blame] | 2520 | Some("suspend") => suspend_vms(args), |
| 2521 | Some("resume") => resume_vms(args), |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 2522 | Some("run") => run_vm(args), |
| 2523 | Some("balloon") => balloon_vms(args), |
Charles William Dick | ed22f6b | 2020-04-08 11:05:24 +0900 | [diff] [blame] | 2524 | Some("balloon_stats") => balloon_stats(args), |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 2525 | Some("create_qcow2") => create_qcow2(args), |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 2526 | Some("disk") => disk_cmd(args), |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 2527 | Some("usb") => modify_usb(args), |
Yi Sun | 54305cd | 2020-01-04 00:19:37 +0800 | [diff] [blame] | 2528 | Some("version") => pkg_version(), |
Chuanxiao Dong | 256be3a | 2020-04-27 16:39:33 +0800 | [diff] [blame] | 2529 | Some("battery") => modify_battery(args), |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 2530 | Some(c) => { |
| 2531 | println!("invalid subcommand: {:?}", c); |
| 2532 | print_usage(); |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 2533 | Err(()) |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 2534 | } |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 2535 | }; |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 2536 | |
| 2537 | // Reap exit status from any child device processes. At this point, all devices should have been |
| 2538 | // dropped in the main process and told to shutdown. Try over a period of 100ms, since it may |
| 2539 | // take some time for the processes to shut down. |
| 2540 | if !wait_all_children() { |
| 2541 | // We gave them a chance, and it's too late. |
| 2542 | warn!("not all child processes have exited; sending SIGKILL"); |
| 2543 | if let Err(e) = kill_process_group() { |
| 2544 | // 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] | 2545 | warn!("unable to kill all child processes: {}", e); |
Zach Reizner | efe9578 | 2017-08-26 18:05:48 -0700 | [diff] [blame] | 2546 | } |
| 2547 | } |
| 2548 | |
| 2549 | // WARNING: Any code added after this point is not guaranteed to run |
| 2550 | // since we may forcibly kill this process (and its children) above. |
Dylan Reid | bfba993 | 2018-02-05 15:51:59 -0800 | [diff] [blame] | 2551 | ret |
| 2552 | } |
| 2553 | |
| 2554 | fn main() { |
| 2555 | std::process::exit(if crosvm_main().is_ok() { 0 } else { 1 }); |
Zach Reizner | 639d967 | 2017-05-01 17:57:18 -0700 | [diff] [blame] | 2556 | } |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 2557 | |
| 2558 | #[cfg(test)] |
| 2559 | mod tests { |
| 2560 | use super::*; |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 2561 | use crosvm::{DEFAULT_TOUCH_DEVICE_HEIGHT, DEFAULT_TOUCH_DEVICE_WIDTH}; |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 2562 | |
| 2563 | #[test] |
| 2564 | fn parse_cpu_set_single() { |
| 2565 | assert_eq!(parse_cpu_set("123").expect("parse failed"), vec![123]); |
| 2566 | } |
| 2567 | |
| 2568 | #[test] |
| 2569 | fn parse_cpu_set_list() { |
| 2570 | assert_eq!( |
| 2571 | parse_cpu_set("0,1,2,3").expect("parse failed"), |
| 2572 | vec![0, 1, 2, 3] |
| 2573 | ); |
| 2574 | } |
| 2575 | |
| 2576 | #[test] |
| 2577 | fn parse_cpu_set_range() { |
| 2578 | assert_eq!( |
| 2579 | parse_cpu_set("0-3").expect("parse failed"), |
| 2580 | vec![0, 1, 2, 3] |
| 2581 | ); |
| 2582 | } |
| 2583 | |
| 2584 | #[test] |
| 2585 | fn parse_cpu_set_list_of_ranges() { |
| 2586 | assert_eq!( |
| 2587 | parse_cpu_set("3-4,7-9,18").expect("parse failed"), |
| 2588 | vec![3, 4, 7, 8, 9, 18] |
| 2589 | ); |
| 2590 | } |
| 2591 | |
| 2592 | #[test] |
| 2593 | fn parse_cpu_set_repeated() { |
| 2594 | // For now, allow duplicates - they will be handled gracefully by the vec to cpu_set_t conversion. |
| 2595 | assert_eq!(parse_cpu_set("1,1,1").expect("parse failed"), vec![1, 1, 1]); |
| 2596 | } |
| 2597 | |
| 2598 | #[test] |
| 2599 | fn parse_cpu_set_negative() { |
| 2600 | // Negative CPU numbers are not allowed. |
| 2601 | parse_cpu_set("-3").expect_err("parse should have failed"); |
| 2602 | } |
| 2603 | |
| 2604 | #[test] |
| 2605 | fn parse_cpu_set_reverse_range() { |
| 2606 | // Ranges must be from low to high. |
| 2607 | parse_cpu_set("5-2").expect_err("parse should have failed"); |
| 2608 | } |
| 2609 | |
| 2610 | #[test] |
| 2611 | fn parse_cpu_set_open_range() { |
| 2612 | parse_cpu_set("3-").expect_err("parse should have failed"); |
| 2613 | } |
| 2614 | |
| 2615 | #[test] |
| 2616 | fn parse_cpu_set_extra_comma() { |
| 2617 | parse_cpu_set("0,1,2,").expect_err("parse should have failed"); |
| 2618 | } |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 2619 | |
Daniel Verkamp | c677fb4 | 2020-09-08 13:47:49 -0700 | [diff] [blame] | 2620 | #[test] |
| 2621 | fn parse_cpu_affinity_global() { |
| 2622 | assert_eq!( |
| 2623 | parse_cpu_affinity("0,5-7,9").expect("parse failed"), |
| 2624 | VcpuAffinity::Global(vec![0, 5, 6, 7, 9]), |
| 2625 | ); |
| 2626 | } |
| 2627 | |
| 2628 | #[test] |
| 2629 | fn parse_cpu_affinity_per_vcpu_one_to_one() { |
| 2630 | let mut expected_map = BTreeMap::new(); |
| 2631 | expected_map.insert(0, vec![0]); |
| 2632 | expected_map.insert(1, vec![1]); |
| 2633 | expected_map.insert(2, vec![2]); |
| 2634 | expected_map.insert(3, vec![3]); |
| 2635 | assert_eq!( |
| 2636 | parse_cpu_affinity("0=0:1=1:2=2:3=3").expect("parse failed"), |
| 2637 | VcpuAffinity::PerVcpu(expected_map), |
| 2638 | ); |
| 2639 | } |
| 2640 | |
| 2641 | #[test] |
| 2642 | fn parse_cpu_affinity_per_vcpu_sets() { |
| 2643 | let mut expected_map = BTreeMap::new(); |
| 2644 | expected_map.insert(0, vec![0, 1, 2]); |
| 2645 | expected_map.insert(1, vec![3, 4, 5]); |
| 2646 | expected_map.insert(2, vec![6, 7, 8]); |
| 2647 | assert_eq!( |
| 2648 | parse_cpu_affinity("0=0,1,2:1=3-5:2=6,7-8").expect("parse failed"), |
| 2649 | VcpuAffinity::PerVcpu(expected_map), |
| 2650 | ); |
| 2651 | } |
| 2652 | |
Andrew Scull | 1590e6f | 2020-03-18 18:00:47 +0000 | [diff] [blame] | 2653 | #[cfg(feature = "audio")] |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 2654 | #[test] |
Judy Hsiao | d5c1e96 | 2020-02-04 12:30:01 +0800 | [diff] [blame] | 2655 | fn parse_ac97_vaild() { |
| 2656 | parse_ac97_options("backend=cras").expect("parse should have succeded"); |
| 2657 | } |
| 2658 | |
Andrew Scull | 1590e6f | 2020-03-18 18:00:47 +0000 | [diff] [blame] | 2659 | #[cfg(feature = "audio")] |
Judy Hsiao | d5c1e96 | 2020-02-04 12:30:01 +0800 | [diff] [blame] | 2660 | #[test] |
| 2661 | fn parse_ac97_null_vaild() { |
| 2662 | parse_ac97_options("backend=null").expect("parse should have succeded"); |
| 2663 | } |
| 2664 | |
Andrew Scull | 1590e6f | 2020-03-18 18:00:47 +0000 | [diff] [blame] | 2665 | #[cfg(feature = "audio")] |
Judy Hsiao | d5c1e96 | 2020-02-04 12:30:01 +0800 | [diff] [blame] | 2666 | #[test] |
Judy Hsiao | b4b94c7 | 2020-09-07 15:56:00 +0800 | [diff] [blame] | 2667 | fn parse_ac97_capture_vaild() { |
| 2668 | parse_ac97_options("backend=cras,capture=true").expect("parse should have succeded"); |
Judy Hsiao | d5c1e96 | 2020-02-04 12:30:01 +0800 | [diff] [blame] | 2669 | } |
| 2670 | |
Jorge E. Moreira | 359e7de | 2020-12-02 18:25:53 -0800 | [diff] [blame] | 2671 | #[cfg(feature = "audio")] |
| 2672 | #[test] |
paulhsia | 83d5160 | 2021-03-09 17:13:14 +0800 | [diff] [blame] | 2673 | fn parse_ac97_client_type() { |
| 2674 | parse_ac97_options("backend=cras,capture=true,client_type=crosvm") |
| 2675 | .expect("parse should have succeded"); |
| 2676 | parse_ac97_options("backend=cras,capture=true,client_type=arcvm") |
| 2677 | .expect("parse should have succeded"); |
| 2678 | parse_ac97_options("backend=cras,capture=true,client_type=none") |
| 2679 | .expect_err("parse should have failed"); |
| 2680 | } |
| 2681 | |
| 2682 | #[cfg(feature = "audio")] |
| 2683 | #[test] |
Jorge E. Moreira | 359e7de | 2020-12-02 18:25:53 -0800 | [diff] [blame] | 2684 | fn parse_ac97_vios_valid() { |
| 2685 | parse_ac97_options("backend=vios,server=/path/to/server") |
| 2686 | .expect("parse should have succeded"); |
| 2687 | } |
| 2688 | |
Judy Hsiao | d5c1e96 | 2020-02-04 12:30:01 +0800 | [diff] [blame] | 2689 | #[test] |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 2690 | fn parse_serial_vaild() { |
Jorge E. Moreira | 1e26230 | 2019-08-01 14:40:03 -0700 | [diff] [blame] | 2691 | parse_serial_options("type=syslog,num=1,console=true,stdin=true") |
| 2692 | .expect("parse should have succeded"); |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 2693 | } |
| 2694 | |
| 2695 | #[test] |
A. Cody Schuffelen | 3faab5a | 2020-10-05 17:29:16 -0700 | [diff] [blame] | 2696 | fn parse_serial_virtio_console_vaild() { |
| 2697 | parse_serial_options("type=syslog,num=5,console=true,stdin=true,hardware=virtio-console") |
| 2698 | .expect("parse should have succeded"); |
| 2699 | } |
| 2700 | |
| 2701 | #[test] |
Trent Begin | 923bab0 | 2019-06-17 13:48:06 -0600 | [diff] [blame] | 2702 | fn parse_serial_valid_no_num() { |
| 2703 | parse_serial_options("type=syslog").expect("parse should have succeded"); |
| 2704 | } |
| 2705 | |
| 2706 | #[test] |
Nicholas Hollingum | c76c2da | 2020-09-18 15:53:16 +1000 | [diff] [blame] | 2707 | fn parse_serial_equals_in_value() { |
| 2708 | let parsed = parse_serial_options("type=syslog,path=foo=bar==.log") |
| 2709 | .expect("parse should have succeded"); |
| 2710 | assert_eq!(parsed.path, Some(PathBuf::from("foo=bar==.log"))); |
| 2711 | } |
| 2712 | |
| 2713 | #[test] |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 2714 | fn parse_serial_invalid_type() { |
| 2715 | parse_serial_options("type=wormhole,num=1").expect_err("parse should have failed"); |
| 2716 | } |
| 2717 | |
| 2718 | #[test] |
| 2719 | fn parse_serial_invalid_num_upper() { |
| 2720 | parse_serial_options("type=syslog,num=5").expect_err("parse should have failed"); |
| 2721 | } |
| 2722 | |
| 2723 | #[test] |
| 2724 | fn parse_serial_invalid_num_lower() { |
| 2725 | parse_serial_options("type=syslog,num=0").expect_err("parse should have failed"); |
| 2726 | } |
| 2727 | |
| 2728 | #[test] |
A. Cody Schuffelen | 3faab5a | 2020-10-05 17:29:16 -0700 | [diff] [blame] | 2729 | fn parse_serial_virtio_console_invalid_num_lower() { |
| 2730 | parse_serial_options("type=syslog,hardware=virtio-console,num=0") |
| 2731 | .expect_err("parse should have failed"); |
| 2732 | } |
| 2733 | |
| 2734 | #[test] |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 2735 | fn parse_serial_invalid_num_string() { |
| 2736 | parse_serial_options("type=syslog,num=number3").expect_err("parse should have failed"); |
| 2737 | } |
| 2738 | |
| 2739 | #[test] |
| 2740 | fn parse_serial_invalid_option() { |
| 2741 | parse_serial_options("type=syslog,speed=lightspeed").expect_err("parse should have failed"); |
| 2742 | } |
Jorge E. Moreira | 1e26230 | 2019-08-01 14:40:03 -0700 | [diff] [blame] | 2743 | |
| 2744 | #[test] |
| 2745 | fn parse_serial_invalid_two_stdin() { |
| 2746 | let mut config = Config::default(); |
| 2747 | set_argument(&mut config, "serial", Some("num=1,type=stdout,stdin=true")) |
| 2748 | .expect("should parse the first serial argument"); |
| 2749 | set_argument(&mut config, "serial", Some("num=2,type=stdout,stdin=true")) |
| 2750 | .expect_err("should fail to parse a second serial port connected to stdin"); |
| 2751 | } |
Dmitry Torokhov | 458bb64 | 2019-12-13 11:47:52 -0800 | [diff] [blame] | 2752 | |
| 2753 | #[test] |
| 2754 | fn parse_plugin_mount_valid() { |
| 2755 | let mut config = Config::default(); |
| 2756 | set_argument( |
| 2757 | &mut config, |
| 2758 | "plugin-mount", |
| 2759 | Some("/dev/null:/dev/zero:true"), |
| 2760 | ) |
| 2761 | .expect("parse should succeed"); |
| 2762 | assert_eq!(config.plugin_mounts[0].src, PathBuf::from("/dev/null")); |
| 2763 | assert_eq!(config.plugin_mounts[0].dst, PathBuf::from("/dev/zero")); |
| 2764 | assert_eq!(config.plugin_mounts[0].writable, true); |
| 2765 | } |
| 2766 | |
| 2767 | #[test] |
| 2768 | fn parse_plugin_mount_valid_shorthand() { |
| 2769 | let mut config = Config::default(); |
| 2770 | set_argument(&mut config, "plugin-mount", Some("/dev/null")).expect("parse should succeed"); |
| 2771 | assert_eq!(config.plugin_mounts[0].dst, PathBuf::from("/dev/null")); |
| 2772 | assert_eq!(config.plugin_mounts[0].writable, false); |
| 2773 | set_argument(&mut config, "plugin-mount", Some("/dev/null:/dev/zero")) |
| 2774 | .expect("parse should succeed"); |
| 2775 | assert_eq!(config.plugin_mounts[1].dst, PathBuf::from("/dev/zero")); |
| 2776 | assert_eq!(config.plugin_mounts[1].writable, false); |
| 2777 | set_argument(&mut config, "plugin-mount", Some("/dev/null::true")) |
| 2778 | .expect("parse should succeed"); |
| 2779 | assert_eq!(config.plugin_mounts[2].dst, PathBuf::from("/dev/null")); |
| 2780 | assert_eq!(config.plugin_mounts[2].writable, true); |
| 2781 | } |
| 2782 | |
| 2783 | #[test] |
| 2784 | fn parse_plugin_mount_invalid() { |
| 2785 | let mut config = Config::default(); |
| 2786 | set_argument(&mut config, "plugin-mount", Some("")).expect_err("parse should fail"); |
| 2787 | set_argument( |
| 2788 | &mut config, |
| 2789 | "plugin-mount", |
| 2790 | Some("/dev/null:/dev/null:true:false"), |
| 2791 | ) |
| 2792 | .expect_err("parse should fail because too many arguments"); |
| 2793 | set_argument(&mut config, "plugin-mount", Some("null:/dev/null:true")) |
| 2794 | .expect_err("parse should fail because source is not absolute"); |
| 2795 | set_argument(&mut config, "plugin-mount", Some("/dev/null:null:true")) |
| 2796 | .expect_err("parse should fail because source is not absolute"); |
| 2797 | set_argument(&mut config, "plugin-mount", Some("/dev/null:null:blah")) |
| 2798 | .expect_err("parse should fail because flag is not boolean"); |
| 2799 | } |
| 2800 | |
| 2801 | #[test] |
| 2802 | fn parse_plugin_gid_map_valid() { |
| 2803 | let mut config = Config::default(); |
| 2804 | set_argument(&mut config, "plugin-gid-map", Some("1:2:3")).expect("parse should succeed"); |
| 2805 | assert_eq!(config.plugin_gid_maps[0].inner, 1); |
| 2806 | assert_eq!(config.plugin_gid_maps[0].outer, 2); |
| 2807 | assert_eq!(config.plugin_gid_maps[0].count, 3); |
| 2808 | } |
| 2809 | |
| 2810 | #[test] |
| 2811 | fn parse_plugin_gid_map_valid_shorthand() { |
| 2812 | let mut config = Config::default(); |
| 2813 | set_argument(&mut config, "plugin-gid-map", Some("1")).expect("parse should succeed"); |
| 2814 | assert_eq!(config.plugin_gid_maps[0].inner, 1); |
| 2815 | assert_eq!(config.plugin_gid_maps[0].outer, 1); |
| 2816 | assert_eq!(config.plugin_gid_maps[0].count, 1); |
| 2817 | set_argument(&mut config, "plugin-gid-map", Some("1:2")).expect("parse should succeed"); |
| 2818 | assert_eq!(config.plugin_gid_maps[1].inner, 1); |
| 2819 | assert_eq!(config.plugin_gid_maps[1].outer, 2); |
| 2820 | assert_eq!(config.plugin_gid_maps[1].count, 1); |
| 2821 | set_argument(&mut config, "plugin-gid-map", Some("1::3")).expect("parse should succeed"); |
| 2822 | assert_eq!(config.plugin_gid_maps[2].inner, 1); |
| 2823 | assert_eq!(config.plugin_gid_maps[2].outer, 1); |
| 2824 | assert_eq!(config.plugin_gid_maps[2].count, 3); |
| 2825 | } |
| 2826 | |
| 2827 | #[test] |
| 2828 | fn parse_plugin_gid_map_invalid() { |
| 2829 | let mut config = Config::default(); |
| 2830 | set_argument(&mut config, "plugin-gid-map", Some("")).expect_err("parse should fail"); |
| 2831 | set_argument(&mut config, "plugin-gid-map", Some("1:2:3:4")) |
| 2832 | .expect_err("parse should fail because too many arguments"); |
| 2833 | set_argument(&mut config, "plugin-gid-map", Some("blah:2:3")) |
| 2834 | .expect_err("parse should fail because inner is not a number"); |
| 2835 | set_argument(&mut config, "plugin-gid-map", Some("1:blah:3")) |
| 2836 | .expect_err("parse should fail because outer is not a number"); |
| 2837 | set_argument(&mut config, "plugin-gid-map", Some("1:2:blah")) |
| 2838 | .expect_err("parse should fail because count is not a number"); |
| 2839 | } |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 2840 | |
| 2841 | #[test] |
| 2842 | fn single_touch_spec_and_track_pad_spec_default_size() { |
| 2843 | let mut config = Config::default(); |
| 2844 | config |
| 2845 | .executable_path |
| 2846 | .replace(Executable::Kernel(PathBuf::from("kernel"))); |
| 2847 | set_argument(&mut config, "single-touch", Some("/dev/single-touch-test")).unwrap(); |
| 2848 | set_argument(&mut config, "trackpad", Some("/dev/single-touch-test")).unwrap(); |
| 2849 | validate_arguments(&mut config).unwrap(); |
| 2850 | assert_eq!( |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 2851 | config.virtio_single_touch.first().unwrap().get_size(), |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 2852 | (DEFAULT_TOUCH_DEVICE_WIDTH, DEFAULT_TOUCH_DEVICE_HEIGHT) |
| 2853 | ); |
| 2854 | assert_eq!( |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 2855 | config.virtio_trackpad.first().unwrap().get_size(), |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 2856 | (DEFAULT_TOUCH_DEVICE_WIDTH, DEFAULT_TOUCH_DEVICE_HEIGHT) |
| 2857 | ); |
| 2858 | } |
| 2859 | |
| 2860 | #[cfg(feature = "gpu")] |
| 2861 | #[test] |
| 2862 | fn single_touch_spec_default_size_from_gpu() { |
| 2863 | let width = 12345u32; |
| 2864 | let height = 54321u32; |
| 2865 | let mut config = Config::default(); |
| 2866 | config |
| 2867 | .executable_path |
| 2868 | .replace(Executable::Kernel(PathBuf::from("kernel"))); |
| 2869 | set_argument(&mut config, "single-touch", Some("/dev/single-touch-test")).unwrap(); |
| 2870 | set_argument( |
| 2871 | &mut config, |
| 2872 | "gpu", |
| 2873 | Some(&format!("width={},height={}", width, height)), |
| 2874 | ) |
| 2875 | .unwrap(); |
| 2876 | validate_arguments(&mut config).unwrap(); |
| 2877 | assert_eq!( |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 2878 | config.virtio_single_touch.first().unwrap().get_size(), |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 2879 | (width, height) |
| 2880 | ); |
| 2881 | } |
| 2882 | |
| 2883 | #[test] |
| 2884 | fn single_touch_spec_and_track_pad_spec_with_size() { |
| 2885 | let width = 12345u32; |
| 2886 | let height = 54321u32; |
| 2887 | let mut config = Config::default(); |
| 2888 | config |
| 2889 | .executable_path |
| 2890 | .replace(Executable::Kernel(PathBuf::from("kernel"))); |
| 2891 | set_argument( |
| 2892 | &mut config, |
| 2893 | "single-touch", |
| 2894 | Some(&format!("/dev/single-touch-test:{}:{}", width, height)), |
| 2895 | ) |
| 2896 | .unwrap(); |
| 2897 | set_argument( |
| 2898 | &mut config, |
| 2899 | "trackpad", |
| 2900 | Some(&format!("/dev/single-touch-test:{}:{}", width, height)), |
| 2901 | ) |
| 2902 | .unwrap(); |
| 2903 | validate_arguments(&mut config).unwrap(); |
| 2904 | assert_eq!( |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 2905 | config.virtio_single_touch.first().unwrap().get_size(), |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 2906 | (width, height) |
| 2907 | ); |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 2908 | assert_eq!( |
| 2909 | config.virtio_trackpad.first().unwrap().get_size(), |
| 2910 | (width, height) |
| 2911 | ); |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 2912 | } |
| 2913 | |
| 2914 | #[cfg(feature = "gpu")] |
| 2915 | #[test] |
| 2916 | fn single_touch_spec_with_size_independent_from_gpu() { |
| 2917 | let touch_width = 12345u32; |
| 2918 | let touch_height = 54321u32; |
| 2919 | let display_width = 1234u32; |
| 2920 | let display_height = 5432u32; |
| 2921 | let mut config = Config::default(); |
| 2922 | config |
| 2923 | .executable_path |
| 2924 | .replace(Executable::Kernel(PathBuf::from("kernel"))); |
| 2925 | set_argument( |
| 2926 | &mut config, |
| 2927 | "single-touch", |
| 2928 | Some(&format!( |
| 2929 | "/dev/single-touch-test:{}:{}", |
| 2930 | touch_width, touch_height |
| 2931 | )), |
| 2932 | ) |
| 2933 | .unwrap(); |
| 2934 | set_argument( |
| 2935 | &mut config, |
| 2936 | "gpu", |
| 2937 | Some(&format!( |
| 2938 | "width={},height={}", |
| 2939 | display_width, display_height |
| 2940 | )), |
| 2941 | ) |
| 2942 | .unwrap(); |
| 2943 | validate_arguments(&mut config).unwrap(); |
| 2944 | assert_eq!( |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 2945 | config.virtio_single_touch.first().unwrap().get_size(), |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 2946 | (touch_width, touch_height) |
| 2947 | ); |
| 2948 | } |
Kaiyi Li | dd348a4 | 2020-07-13 11:49:46 -0700 | [diff] [blame] | 2949 | |
Daniel Norman | 5e23df7 | 2021-03-11 10:11:02 -0800 | [diff] [blame] | 2950 | #[test] |
| 2951 | fn virtio_switches() { |
| 2952 | let mut config = Config::default(); |
| 2953 | config |
| 2954 | .executable_path |
| 2955 | .replace(Executable::Kernel(PathBuf::from("kernel"))); |
| 2956 | set_argument(&mut config, "switches", Some("/dev/switches-test")).unwrap(); |
| 2957 | validate_arguments(&mut config).unwrap(); |
| 2958 | assert_eq!( |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 2959 | config.virtio_switches.pop().unwrap(), |
Keiichi Watanabe | d56a2f4 | 2021-03-18 20:16:23 +0900 | [diff] [blame] | 2960 | PathBuf::from("/dev/switches-test") |
| 2961 | ); |
Daniel Norman | 5e23df7 | 2021-03-11 10:11:02 -0800 | [diff] [blame] | 2962 | } |
| 2963 | |
Chia-I Wu | 6d473b3 | 2021-04-12 10:14:24 -0700 | [diff] [blame] | 2964 | #[cfg(feature = "gpu")] |
Kaiyi Li | dd348a4 | 2020-07-13 11:49:46 -0700 | [diff] [blame] | 2965 | #[test] |
Chia-I Wu | 91df656 | 2021-04-12 09:47:38 -0700 | [diff] [blame] | 2966 | fn parse_gpu_options_default_vulkan_support() { |
Jason Macnak | d659a0d | 2021-03-15 15:33:01 -0700 | [diff] [blame^] | 2967 | { |
| 2968 | let mut gpu_params: GpuParameters = Default::default(); |
| 2969 | assert!(parse_gpu_options(Some("backend=virglrenderer"), &mut gpu_params).is_ok()); |
| 2970 | assert!(!gpu_params.use_vulkan); |
| 2971 | } |
Chia-I Wu | 6d473b3 | 2021-04-12 10:14:24 -0700 | [diff] [blame] | 2972 | |
| 2973 | #[cfg(feature = "gfxstream")] |
Jason Macnak | d659a0d | 2021-03-15 15:33:01 -0700 | [diff] [blame^] | 2974 | { |
| 2975 | let mut gpu_params: GpuParameters = Default::default(); |
| 2976 | assert!(parse_gpu_options(Some("backend=gfxstream"), &mut gpu_params).is_ok()); |
| 2977 | assert!(gpu_params.use_vulkan); |
| 2978 | } |
Chia-I Wu | 91df656 | 2021-04-12 09:47:38 -0700 | [diff] [blame] | 2979 | } |
| 2980 | |
Chia-I Wu | 6d473b3 | 2021-04-12 10:14:24 -0700 | [diff] [blame] | 2981 | #[cfg(feature = "gpu")] |
Chia-I Wu | 91df656 | 2021-04-12 09:47:38 -0700 | [diff] [blame] | 2982 | #[test] |
Chia-I Wu | 6d473b3 | 2021-04-12 10:14:24 -0700 | [diff] [blame] | 2983 | fn parse_gpu_options_with_vulkan_specified() { |
Jason Macnak | d659a0d | 2021-03-15 15:33:01 -0700 | [diff] [blame^] | 2984 | { |
| 2985 | let mut gpu_params: GpuParameters = Default::default(); |
| 2986 | assert!(parse_gpu_options(Some("vulkan=true"), &mut gpu_params).is_ok()); |
| 2987 | assert!(gpu_params.use_vulkan); |
| 2988 | } |
| 2989 | { |
| 2990 | let mut gpu_params: GpuParameters = Default::default(); |
| 2991 | assert!( |
| 2992 | parse_gpu_options(Some("backend=virglrenderer,vulkan=true"), &mut gpu_params) |
| 2993 | .is_ok() |
| 2994 | ); |
| 2995 | assert!(gpu_params.use_vulkan); |
| 2996 | } |
| 2997 | { |
| 2998 | let mut gpu_params: GpuParameters = Default::default(); |
| 2999 | assert!( |
| 3000 | parse_gpu_options(Some("vulkan=true,backend=virglrenderer"), &mut gpu_params) |
| 3001 | .is_ok() |
| 3002 | ); |
| 3003 | assert!(gpu_params.use_vulkan); |
| 3004 | } |
| 3005 | { |
| 3006 | let mut gpu_params: GpuParameters = Default::default(); |
| 3007 | assert!(parse_gpu_options(Some("vulkan=false"), &mut gpu_params).is_ok()); |
| 3008 | assert!(!gpu_params.use_vulkan); |
| 3009 | } |
| 3010 | { |
| 3011 | let mut gpu_params: GpuParameters = Default::default(); |
| 3012 | assert!( |
| 3013 | parse_gpu_options(Some("backend=virglrenderer,vulkan=false"), &mut gpu_params) |
| 3014 | .is_ok() |
| 3015 | ); |
| 3016 | assert!(!gpu_params.use_vulkan); |
| 3017 | } |
| 3018 | { |
| 3019 | let mut gpu_params: GpuParameters = Default::default(); |
| 3020 | assert!( |
| 3021 | parse_gpu_options(Some("vulkan=false,backend=virglrenderer"), &mut gpu_params) |
| 3022 | .is_ok() |
| 3023 | ); |
| 3024 | assert!(!gpu_params.use_vulkan); |
| 3025 | } |
| 3026 | { |
| 3027 | let mut gpu_params: GpuParameters = Default::default(); |
| 3028 | assert!(parse_gpu_options( |
| 3029 | Some("backend=virglrenderer,vulkan=invalid_value"), |
| 3030 | &mut gpu_params |
| 3031 | ) |
| 3032 | .is_err()); |
| 3033 | } |
| 3034 | { |
| 3035 | let mut gpu_params: GpuParameters = Default::default(); |
| 3036 | assert!(parse_gpu_options( |
| 3037 | Some("vulkan=invalid_value,backend=virglrenderer"), |
| 3038 | &mut gpu_params |
| 3039 | ) |
| 3040 | .is_err()); |
| 3041 | } |
Kaiyi Li | dd348a4 | 2020-07-13 11:49:46 -0700 | [diff] [blame] | 3042 | } |
| 3043 | |
| 3044 | #[cfg(all(feature = "gpu", feature = "gfxstream"))] |
| 3045 | #[test] |
| 3046 | fn parse_gpu_options_gfxstream_with_syncfd_specified() { |
Jason Macnak | d659a0d | 2021-03-15 15:33:01 -0700 | [diff] [blame^] | 3047 | { |
| 3048 | let mut gpu_params: GpuParameters = Default::default(); |
| 3049 | assert!( |
| 3050 | parse_gpu_options(Some("backend=gfxstream,syncfd=true"), &mut gpu_params).is_ok() |
| 3051 | ); |
| 3052 | assert!(gpu_params.gfxstream_use_syncfd); |
| 3053 | } |
| 3054 | { |
| 3055 | let mut gpu_params: GpuParameters = Default::default(); |
| 3056 | assert!( |
| 3057 | parse_gpu_options(Some("syncfd=true,backend=gfxstream"), &mut gpu_params).is_ok() |
| 3058 | ); |
| 3059 | assert!(gpu_params.gfxstream_use_syncfd); |
| 3060 | } |
| 3061 | { |
| 3062 | let mut gpu_params: GpuParameters = Default::default(); |
| 3063 | assert!( |
| 3064 | parse_gpu_options(Some("backend=gfxstream,syncfd=false"), &mut gpu_params).is_ok() |
| 3065 | ); |
| 3066 | assert!(!gpu_params.gfxstream_use_syncfd); |
| 3067 | } |
| 3068 | { |
| 3069 | let mut gpu_params: GpuParameters = Default::default(); |
| 3070 | assert!( |
| 3071 | parse_gpu_options(Some("syncfd=false,backend=gfxstream"), &mut gpu_params).is_ok() |
| 3072 | ); |
| 3073 | assert!(!gpu_params.gfxstream_use_syncfd); |
| 3074 | } |
| 3075 | { |
| 3076 | let mut gpu_params: GpuParameters = Default::default(); |
| 3077 | assert!(parse_gpu_options( |
| 3078 | Some("backend=gfxstream,syncfd=invalid_value"), |
| 3079 | &mut gpu_params |
| 3080 | ) |
| 3081 | .is_err()); |
| 3082 | } |
| 3083 | { |
| 3084 | let mut gpu_params: GpuParameters = Default::default(); |
| 3085 | assert!(parse_gpu_options( |
| 3086 | Some("syncfd=invalid_value,backend=gfxstream"), |
| 3087 | &mut gpu_params |
| 3088 | ) |
| 3089 | .is_err()); |
| 3090 | } |
Kaiyi Li | dd348a4 | 2020-07-13 11:49:46 -0700 | [diff] [blame] | 3091 | } |
| 3092 | |
| 3093 | #[cfg(all(feature = "gpu", feature = "gfxstream"))] |
| 3094 | #[test] |
| 3095 | fn parse_gpu_options_not_gfxstream_with_syncfd_specified() { |
Jason Macnak | d659a0d | 2021-03-15 15:33:01 -0700 | [diff] [blame^] | 3096 | { |
| 3097 | let mut gpu_params: GpuParameters = Default::default(); |
| 3098 | assert!( |
| 3099 | parse_gpu_options(Some("backend=virglrenderer,syncfd=true"), &mut gpu_params) |
| 3100 | .is_err() |
| 3101 | ); |
| 3102 | } |
| 3103 | { |
| 3104 | let mut gpu_params: GpuParameters = Default::default(); |
| 3105 | assert!( |
| 3106 | parse_gpu_options(Some("syncfd=true,backend=virglrenderer"), &mut gpu_params) |
| 3107 | .is_err() |
| 3108 | ); |
| 3109 | } |
| 3110 | } |
| 3111 | |
| 3112 | #[cfg(feature = "gpu")] |
| 3113 | #[test] |
| 3114 | fn parse_gpu_display_options_valid() { |
| 3115 | { |
| 3116 | let mut gpu_params: GpuParameters = Default::default(); |
| 3117 | assert!( |
| 3118 | parse_gpu_display_options(Some("width=500,height=600"), &mut gpu_params).is_ok() |
| 3119 | ); |
| 3120 | assert_eq!(gpu_params.displays.len(), 1); |
| 3121 | assert_eq!(gpu_params.displays[0].width, 500); |
| 3122 | assert_eq!(gpu_params.displays[0].height, 600); |
| 3123 | } |
| 3124 | } |
| 3125 | |
| 3126 | #[cfg(feature = "gpu")] |
| 3127 | #[test] |
| 3128 | fn parse_gpu_display_options_invalid() { |
| 3129 | { |
| 3130 | let mut gpu_params: GpuParameters = Default::default(); |
| 3131 | assert!(parse_gpu_display_options(Some("width=500"), &mut gpu_params).is_err()); |
| 3132 | } |
| 3133 | { |
| 3134 | let mut gpu_params: GpuParameters = Default::default(); |
| 3135 | assert!(parse_gpu_display_options(Some("height=500"), &mut gpu_params).is_err()); |
| 3136 | } |
| 3137 | { |
| 3138 | let mut gpu_params: GpuParameters = Default::default(); |
| 3139 | assert!(parse_gpu_display_options(Some("width"), &mut gpu_params).is_err()); |
| 3140 | } |
| 3141 | { |
| 3142 | let mut gpu_params: GpuParameters = Default::default(); |
| 3143 | assert!(parse_gpu_display_options(Some("blah"), &mut gpu_params).is_err()); |
| 3144 | } |
| 3145 | } |
| 3146 | |
| 3147 | #[cfg(feature = "gpu")] |
| 3148 | #[test] |
| 3149 | fn parse_gpu_options_and_gpu_display_options_valid() { |
| 3150 | { |
| 3151 | let mut gpu_params: GpuParameters = Default::default(); |
| 3152 | assert!(parse_gpu_options(Some("2D,width=500,height=600"), &mut gpu_params).is_ok()); |
| 3153 | assert!( |
| 3154 | parse_gpu_display_options(Some("width=700,height=800"), &mut gpu_params).is_ok() |
| 3155 | ); |
| 3156 | assert_eq!(gpu_params.displays.len(), 2); |
| 3157 | assert_eq!(gpu_params.displays[0].width, 500); |
| 3158 | assert_eq!(gpu_params.displays[0].height, 600); |
| 3159 | assert_eq!(gpu_params.displays[1].width, 700); |
| 3160 | assert_eq!(gpu_params.displays[1].height, 800); |
| 3161 | } |
| 3162 | { |
| 3163 | let mut gpu_params: GpuParameters = Default::default(); |
| 3164 | assert!(parse_gpu_options(Some("2D"), &mut gpu_params).is_ok()); |
| 3165 | assert!( |
| 3166 | parse_gpu_display_options(Some("width=700,height=800"), &mut gpu_params).is_ok() |
| 3167 | ); |
| 3168 | assert_eq!(gpu_params.displays.len(), 1); |
| 3169 | assert_eq!(gpu_params.displays[0].width, 700); |
| 3170 | assert_eq!(gpu_params.displays[0].height, 800); |
| 3171 | } |
Kaiyi Li | dd348a4 | 2020-07-13 11:49:46 -0700 | [diff] [blame] | 3172 | } |
Chuanxiao Dong | fd5626c | 2020-04-27 16:35:33 +0800 | [diff] [blame] | 3173 | |
| 3174 | #[test] |
| 3175 | fn parse_battery_vaild() { |
| 3176 | parse_battery_options(Some("type=goldfish")).expect("parse should have succeded"); |
| 3177 | } |
| 3178 | |
| 3179 | #[test] |
| 3180 | fn parse_battery_vaild_no_type() { |
| 3181 | parse_battery_options(None).expect("parse should have succeded"); |
| 3182 | } |
| 3183 | |
| 3184 | #[test] |
| 3185 | fn parse_battery_invaild_parameter() { |
| 3186 | parse_battery_options(Some("tyep=goldfish")).expect_err("parse should have failed"); |
| 3187 | } |
| 3188 | |
| 3189 | #[test] |
| 3190 | fn parse_battery_invaild_type_value() { |
| 3191 | parse_battery_options(Some("type=xxx")).expect_err("parse should have failed"); |
| 3192 | } |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 3193 | } |