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