Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 1 | // Copyright 2021, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | //! Functions for running instances of `crosvm`. |
| 16 | |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 17 | use crate::aidl::VirtualMachineCallbacks; |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 18 | use crate::Cid; |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 19 | use anyhow::{bail, Error}; |
Andrew Walbran | 02b8ec0 | 2021-06-22 13:07:02 +0000 | [diff] [blame] | 20 | use command_fds::CommandFdExt; |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 21 | use log::{debug, error, info}; |
Jiyong Park | dcf1741 | 2022-02-08 15:07:23 +0900 | [diff] [blame^] | 22 | use semver::{Version, VersionReq}; |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 23 | use shared_child::SharedChild; |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 24 | use std::fs::{remove_dir_all, File}; |
Andrew Walbran | c92d35f | 2022-01-12 12:45:19 +0000 | [diff] [blame] | 25 | use std::io; |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 26 | use std::mem; |
Andrew Walbran | b15cd6e | 2021-07-05 16:38:07 +0000 | [diff] [blame] | 27 | use std::num::NonZeroU32; |
Andrew Walbran | 02b8ec0 | 2021-06-22 13:07:02 +0000 | [diff] [blame] | 28 | use std::os::unix::io::{AsRawFd, RawFd}; |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 29 | use std::path::PathBuf; |
Andrew Walbran | c92d35f | 2022-01-12 12:45:19 +0000 | [diff] [blame] | 30 | use std::process::{Command, ExitStatus}; |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 31 | use std::sync::{Arc, Mutex}; |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 32 | use std::thread; |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 33 | use vsock::VsockStream; |
Andrew Walbran | c92d35f | 2022-01-12 12:45:19 +0000 | [diff] [blame] | 34 | use android_system_virtualizationservice::aidl::android::system::virtualizationservice::DeathReason::DeathReason; |
Inseob Kim | c7d28c7 | 2021-10-25 14:28:10 +0000 | [diff] [blame] | 35 | use android_system_virtualmachineservice::binder::Strong; |
| 36 | use android_system_virtualmachineservice::aidl::android::system::virtualmachineservice::IVirtualMachineService::IVirtualMachineService; |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 37 | |
| 38 | const CROSVM_PATH: &str = "/apex/com.android.virt/bin/crosvm"; |
| 39 | |
Jiyong Park | dcf1741 | 2022-02-08 15:07:23 +0900 | [diff] [blame^] | 40 | /// Version of the platform that crosvm currently implements. The format follows SemVer. This |
| 41 | /// should be updated when there is a platform change in the crosvm side. Having this value here is |
| 42 | /// fine because virtualizationservice and crosvm are supposed to be updated together in the virt |
| 43 | /// APEX. |
| 44 | const CROSVM_PLATFORM_VERSION: &str = "1.0.0"; |
| 45 | |
Andrew Walbran | d15c563 | 2022-02-03 13:38:31 +0000 | [diff] [blame] | 46 | /// The exit status which crosvm returns when it has an error starting a VM. |
| 47 | const CROSVM_ERROR_STATUS: i32 = 1; |
Andrew Walbran | c92d35f | 2022-01-12 12:45:19 +0000 | [diff] [blame] | 48 | /// The exit status which crosvm returns when a VM requests a reboot. |
| 49 | const CROSVM_REBOOT_STATUS: i32 = 32; |
Andrew Walbran | d15c563 | 2022-02-03 13:38:31 +0000 | [diff] [blame] | 50 | /// The exit status which crosvm returns when it crashes due to an error. |
| 51 | const CROSVM_CRASH_STATUS: i32 = 33; |
Andrew Walbran | c92d35f | 2022-01-12 12:45:19 +0000 | [diff] [blame] | 52 | |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 53 | /// Configuration for a VM to run with crosvm. |
| 54 | #[derive(Debug)] |
Andrew Walbran | d3a8418 | 2021-09-07 14:48:52 +0000 | [diff] [blame] | 55 | pub struct CrosvmConfig { |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 56 | pub cid: Cid, |
Andrew Walbran | d3a8418 | 2021-09-07 14:48:52 +0000 | [diff] [blame] | 57 | pub bootloader: Option<File>, |
| 58 | pub kernel: Option<File>, |
| 59 | pub initrd: Option<File>, |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 60 | pub disks: Vec<DiskFile>, |
| 61 | pub params: Option<String>, |
Andrew Walbran | f865042 | 2021-06-09 15:54:09 +0000 | [diff] [blame] | 62 | pub protected: bool, |
Andrew Walbran | b15cd6e | 2021-07-05 16:38:07 +0000 | [diff] [blame] | 63 | pub memory_mib: Option<NonZeroU32>, |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 64 | pub cpus: Option<NonZeroU32>, |
| 65 | pub cpu_affinity: Option<String>, |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 66 | pub console_fd: Option<File>, |
Andrew Walbran | d3a8418 | 2021-09-07 14:48:52 +0000 | [diff] [blame] | 67 | pub log_fd: Option<File>, |
| 68 | pub indirect_files: Vec<File>, |
Jiyong Park | dcf1741 | 2022-02-08 15:07:23 +0900 | [diff] [blame^] | 69 | pub platform_version: VersionReq, |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /// A disk image to pass to crosvm for a VM. |
| 73 | #[derive(Debug)] |
| 74 | pub struct DiskFile { |
| 75 | pub image: File, |
| 76 | pub writable: bool, |
| 77 | } |
| 78 | |
Andrew Walbran | 6b65066 | 2021-09-07 13:13:23 +0000 | [diff] [blame] | 79 | /// The lifecycle state which the payload in the VM has reported itself to be in. |
| 80 | /// |
| 81 | /// Note that the order of enum variants is significant; only forward transitions are allowed by |
| 82 | /// [`VmInstance::update_payload_state`]. |
| 83 | #[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)] |
| 84 | pub enum PayloadState { |
| 85 | Starting, |
| 86 | Started, |
| 87 | Ready, |
| 88 | Finished, |
| 89 | } |
| 90 | |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 91 | /// The current state of the VM itself. |
| 92 | #[derive(Debug)] |
| 93 | pub enum VmState { |
| 94 | /// The VM has not yet tried to start. |
| 95 | NotStarted { |
| 96 | ///The configuration needed to start the VM, if it has not yet been started. |
| 97 | config: CrosvmConfig, |
| 98 | }, |
| 99 | /// The VM has been started. |
| 100 | Running { |
| 101 | /// The crosvm child process. |
| 102 | child: Arc<SharedChild>, |
| 103 | }, |
| 104 | /// The VM died or was killed. |
| 105 | Dead, |
| 106 | /// The VM failed to start. |
| 107 | Failed, |
| 108 | } |
| 109 | |
| 110 | impl VmState { |
| 111 | /// Tries to start the VM, if it is in the `NotStarted` state. |
| 112 | /// |
| 113 | /// Returns an error if the VM is in the wrong state, or fails to start. |
| 114 | fn start(&mut self, instance: Arc<VmInstance>) -> Result<(), Error> { |
| 115 | let state = mem::replace(self, VmState::Failed); |
| 116 | if let VmState::NotStarted { config } = state { |
| 117 | // If this fails and returns an error, `self` will be left in the `Failed` state. |
| 118 | let child = Arc::new(run_vm(config)?); |
| 119 | |
| 120 | let child_clone = child.clone(); |
| 121 | thread::spawn(move || { |
| 122 | instance.monitor(child_clone); |
| 123 | }); |
| 124 | |
| 125 | // If it started correctly, update the state. |
| 126 | *self = VmState::Running { child }; |
| 127 | Ok(()) |
| 128 | } else { |
| 129 | *self = state; |
| 130 | bail!("VM already started or failed") |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /// Information about a particular instance of a VM which may be running. |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 136 | #[derive(Debug)] |
| 137 | pub struct VmInstance { |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 138 | /// The current state of the VM. |
| 139 | pub vm_state: Mutex<VmState>, |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 140 | /// The CID assigned to the VM for vsock communication. |
| 141 | pub cid: Cid, |
Andrew Walbran | f865042 | 2021-06-09 15:54:09 +0000 | [diff] [blame] | 142 | /// Whether the VM is a protected VM. |
| 143 | pub protected: bool, |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 144 | /// Directory of temporary files used by the VM while it is running. |
| 145 | pub temporary_directory: PathBuf, |
Andrew Walbran | f6a1eb9 | 2021-04-01 11:16:02 +0000 | [diff] [blame] | 146 | /// The UID of the process which requested the VM. |
| 147 | pub requester_uid: u32, |
| 148 | /// The SID of the process which requested the VM. |
Andrew Walbran | 0203449 | 2021-04-13 15:05:07 +0000 | [diff] [blame] | 149 | pub requester_sid: String, |
Andrew Walbran | f6a1eb9 | 2021-04-01 11:16:02 +0000 | [diff] [blame] | 150 | /// The PID of the process which requested the VM. Note that this process may no longer exist |
| 151 | /// and the PID may have been reused for a different process, so this should not be trusted. |
Andrew Walbran | 0203449 | 2021-04-13 15:05:07 +0000 | [diff] [blame] | 152 | pub requester_debug_pid: i32, |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 153 | /// Callbacks to clients of the VM. |
| 154 | pub callbacks: VirtualMachineCallbacks, |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 155 | /// Input/output stream of the payload run in the VM. |
| 156 | pub stream: Mutex<Option<VsockStream>>, |
Inseob Kim | c7d28c7 | 2021-10-25 14:28:10 +0000 | [diff] [blame] | 157 | /// VirtualMachineService binder object for the VM. |
| 158 | pub vm_service: Mutex<Option<Strong<dyn IVirtualMachineService>>>, |
Andrew Walbran | 6b65066 | 2021-09-07 13:13:23 +0000 | [diff] [blame] | 159 | /// The latest lifecycle state which the payload reported itself to be in. |
| 160 | payload_state: Mutex<PayloadState>, |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | impl VmInstance { |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 164 | /// Validates the given config and creates a new `VmInstance` but doesn't start running it. |
| 165 | pub fn new( |
Andrew Walbran | d3a8418 | 2021-09-07 14:48:52 +0000 | [diff] [blame] | 166 | config: CrosvmConfig, |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 167 | temporary_directory: PathBuf, |
Andrew Walbran | f6a1eb9 | 2021-04-01 11:16:02 +0000 | [diff] [blame] | 168 | requester_uid: u32, |
Andrew Walbran | 0203449 | 2021-04-13 15:05:07 +0000 | [diff] [blame] | 169 | requester_sid: String, |
| 170 | requester_debug_pid: i32, |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 171 | ) -> Result<VmInstance, Error> { |
| 172 | validate_config(&config)?; |
Andrew Walbran | d3a8418 | 2021-09-07 14:48:52 +0000 | [diff] [blame] | 173 | let cid = config.cid; |
| 174 | let protected = config.protected; |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 175 | Ok(VmInstance { |
| 176 | vm_state: Mutex::new(VmState::NotStarted { config }), |
Andrew Walbran | d3a8418 | 2021-09-07 14:48:52 +0000 | [diff] [blame] | 177 | cid, |
| 178 | protected, |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 179 | temporary_directory, |
Andrew Walbran | 0203449 | 2021-04-13 15:05:07 +0000 | [diff] [blame] | 180 | requester_uid, |
| 181 | requester_sid, |
| 182 | requester_debug_pid, |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 183 | callbacks: Default::default(), |
| 184 | stream: Mutex::new(None), |
Inseob Kim | c7d28c7 | 2021-10-25 14:28:10 +0000 | [diff] [blame] | 185 | vm_service: Mutex::new(None), |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 186 | payload_state: Mutex::new(PayloadState::Starting), |
| 187 | }) |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 190 | /// Starts an instance of `crosvm` to manage the VM. The `crosvm` instance will be killed when |
| 191 | /// the `VmInstance` is dropped. |
| 192 | pub fn start(self: &Arc<Self>) -> Result<(), Error> { |
| 193 | self.vm_state.lock().unwrap().start(self.clone()) |
| 194 | } |
| 195 | |
| 196 | /// Waits for the crosvm child process to finish, then marks the VM as no longer running and |
| 197 | /// calls any callbacks. |
| 198 | /// |
| 199 | /// This takes a separate reference to the `SharedChild` rather than using the one in |
| 200 | /// `self.vm_state` to avoid holding the lock on `vm_state` while it is running. |
| 201 | fn monitor(&self, child: Arc<SharedChild>) { |
Andrew Walbran | c92d35f | 2022-01-12 12:45:19 +0000 | [diff] [blame] | 202 | let result = child.wait(); |
| 203 | match &result { |
Jooyung Han | bfe086f | 2021-10-28 10:15:45 +0900 | [diff] [blame] | 204 | Err(e) => error!("Error waiting for crosvm({}) instance to die: {}", child.id(), e), |
| 205 | Ok(status) => info!("crosvm({}) exited with status {}", child.id(), status), |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 206 | } |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 207 | |
| 208 | let mut vm_state = self.vm_state.lock().unwrap(); |
| 209 | *vm_state = VmState::Dead; |
| 210 | // Ensure that the mutex is released before calling the callbacks. |
| 211 | drop(vm_state); |
| 212 | |
Andrew Walbran | c92d35f | 2022-01-12 12:45:19 +0000 | [diff] [blame] | 213 | self.callbacks.callback_on_died(self.cid, death_reason(&result)); |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 214 | |
| 215 | // Delete temporary files. |
| 216 | if let Err(e) = remove_dir_all(&self.temporary_directory) { |
Andrew Walbran | 806f154 | 2021-06-10 14:07:12 +0000 | [diff] [blame] | 217 | error!("Error removing temporary directory {:?}: {}", self.temporary_directory, e); |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 218 | } |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Andrew Walbran | 6b65066 | 2021-09-07 13:13:23 +0000 | [diff] [blame] | 221 | /// Returns the last reported state of the VM payload. |
| 222 | pub fn payload_state(&self) -> PayloadState { |
| 223 | *self.payload_state.lock().unwrap() |
| 224 | } |
| 225 | |
| 226 | /// Updates the payload state to the given value, if it is a valid state transition. |
| 227 | pub fn update_payload_state(&self, new_state: PayloadState) -> Result<(), Error> { |
| 228 | let mut state_locked = self.payload_state.lock().unwrap(); |
| 229 | // Only allow forward transitions, e.g. from starting to started or finished, not back in |
| 230 | // the other direction. |
| 231 | if new_state > *state_locked { |
| 232 | *state_locked = new_state; |
| 233 | Ok(()) |
| 234 | } else { |
| 235 | bail!("Invalid payload state transition from {:?} to {:?}", *state_locked, new_state) |
| 236 | } |
| 237 | } |
| 238 | |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 239 | /// Kills the crosvm instance, if it is running. |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 240 | pub fn kill(&self) { |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 241 | let vm_state = &*self.vm_state.lock().unwrap(); |
| 242 | if let VmState::Running { child } = vm_state { |
Jooyung Han | bfe086f | 2021-10-28 10:15:45 +0900 | [diff] [blame] | 243 | let id = child.id(); |
| 244 | debug!("Killing crosvm({})", id); |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 245 | // TODO: Talk to crosvm to shutdown cleanly. |
| 246 | if let Err(e) = child.kill() { |
Jooyung Han | bfe086f | 2021-10-28 10:15:45 +0900 | [diff] [blame] | 247 | error!("Error killing crosvm({}) instance: {}", id, e); |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 248 | } |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 249 | } |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
Andrew Walbran | c92d35f | 2022-01-12 12:45:19 +0000 | [diff] [blame] | 253 | fn death_reason(result: &Result<ExitStatus, io::Error>) -> DeathReason { |
| 254 | if let Ok(status) = result { |
| 255 | match status.code() { |
| 256 | None => DeathReason::KILLED, |
| 257 | Some(0) => DeathReason::SHUTDOWN, |
Andrew Walbran | d15c563 | 2022-02-03 13:38:31 +0000 | [diff] [blame] | 258 | Some(CROSVM_ERROR_STATUS) => DeathReason::ERROR, |
Andrew Walbran | c92d35f | 2022-01-12 12:45:19 +0000 | [diff] [blame] | 259 | Some(CROSVM_REBOOT_STATUS) => DeathReason::REBOOT, |
Andrew Walbran | d15c563 | 2022-02-03 13:38:31 +0000 | [diff] [blame] | 260 | Some(CROSVM_CRASH_STATUS) => DeathReason::CRASH, |
Andrew Walbran | c92d35f | 2022-01-12 12:45:19 +0000 | [diff] [blame] | 261 | Some(_) => DeathReason::UNKNOWN, |
| 262 | } |
| 263 | } else { |
| 264 | DeathReason::INFRASTRUCTURE_ERROR |
| 265 | } |
| 266 | } |
| 267 | |
Andrew Walbran | d3a8418 | 2021-09-07 14:48:52 +0000 | [diff] [blame] | 268 | /// Starts an instance of `crosvm` to manage a new VM. |
| 269 | fn run_vm(config: CrosvmConfig) -> Result<SharedChild, Error> { |
| 270 | validate_config(&config)?; |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 271 | |
| 272 | let mut command = Command::new(CROSVM_PATH); |
| 273 | // TODO(qwandor): Remove --disable-sandbox. |
Andrew Walbran | c92d35f | 2022-01-12 12:45:19 +0000 | [diff] [blame] | 274 | command |
| 275 | .arg("--extended-status") |
| 276 | .arg("run") |
| 277 | .arg("--disable-sandbox") |
| 278 | .arg("--cid") |
| 279 | .arg(config.cid.to_string()); |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 280 | |
Andrew Walbran | f865042 | 2021-06-09 15:54:09 +0000 | [diff] [blame] | 281 | if config.protected { |
David Brazdil | 86c76fa | 2022-02-04 15:50:57 +0000 | [diff] [blame] | 282 | command.arg("--protected-vm"); |
Andrew Walbran | 0b5789f | 2022-02-04 13:57:57 +0000 | [diff] [blame] | 283 | |
| 284 | // 3 virtio-console devices + vsock = 4. |
| 285 | let virtio_pci_device_count = 4 + config.disks.len(); |
| 286 | // crosvm virtio queue has 256 entries, so 2 MiB per device (2 pages per entry) should be |
| 287 | // enough. |
| 288 | let swiotlb_size_mib = 2 * virtio_pci_device_count; |
| 289 | command.arg("--swiotlb").arg(swiotlb_size_mib.to_string()); |
Andrew Walbran | f865042 | 2021-06-09 15:54:09 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Andrew Walbran | b15cd6e | 2021-07-05 16:38:07 +0000 | [diff] [blame] | 292 | if let Some(memory_mib) = config.memory_mib { |
| 293 | command.arg("--mem").arg(memory_mib.to_string()); |
| 294 | } |
| 295 | |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 296 | if let Some(cpus) = config.cpus { |
| 297 | command.arg("--cpus").arg(cpus.to_string()); |
| 298 | } |
| 299 | |
| 300 | if let Some(cpu_affinity) = config.cpu_affinity { |
| 301 | command.arg("--cpu-affinity").arg(cpu_affinity); |
| 302 | } |
| 303 | |
Jiyong Park | fa91d70 | 2021-10-18 23:51:39 +0900 | [diff] [blame] | 304 | // Keep track of what file descriptors should be mapped to the crosvm process. |
| 305 | let mut preserved_fds = config.indirect_files.iter().map(|file| file.as_raw_fd()).collect(); |
| 306 | |
Jiyong Park | 747d636 | 2021-10-19 17:12:52 +0900 | [diff] [blame] | 307 | // Setup the serial devices. |
| 308 | // 1. uart device: used as the output device by bootloaders and as early console by linux |
Jiyong Park | ae5a4ed | 2021-11-01 18:36:28 +0900 | [diff] [blame] | 309 | // 2. virtio-console device: used as the console device where kmsg is redirected to |
| 310 | // 3. virtio-console device: used as the androidboot.console device (not used currently) |
| 311 | // 4. virtio-console device: used as the logcat output |
Jiyong Park | 747d636 | 2021-10-19 17:12:52 +0900 | [diff] [blame] | 312 | // |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 313 | // When [console|log]_fd is not specified, the devices are attached to sink, which means what's |
| 314 | // written there is discarded. |
| 315 | let mut format_serial_arg = |fd: &Option<File>| { |
| 316 | let path = fd.as_ref().map(|fd| add_preserved_fd(&mut preserved_fds, fd)); |
| 317 | let type_arg = path.as_ref().map_or("type=sink", |_| "type=file"); |
| 318 | let path_arg = path.as_ref().map_or(String::new(), |path| format!(",path={}", path)); |
| 319 | format!("{}{}", type_arg, path_arg) |
| 320 | }; |
| 321 | let console_arg = format_serial_arg(&config.console_fd); |
| 322 | let log_arg = format_serial_arg(&config.log_fd); |
Jiyong Park | fa91d70 | 2021-10-18 23:51:39 +0900 | [diff] [blame] | 323 | |
Jiyong Park | 747d636 | 2021-10-19 17:12:52 +0900 | [diff] [blame] | 324 | // Warning: Adding more serial devices requires you to shift the PCI device ID of the boot |
| 325 | // disks in bootconfig.x86_64. This is because x86 crosvm puts serial devices and the block |
| 326 | // devices in the same PCI bus and serial devices comes before the block devices. Arm crosvm |
| 327 | // doesn't have the issue. |
Jiyong Park | fa91d70 | 2021-10-18 23:51:39 +0900 | [diff] [blame] | 328 | // /dev/ttyS0 |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 329 | command.arg(format!("--serial={},hardware=serial", &console_arg)); |
Jiyong Park | fa91d70 | 2021-10-18 23:51:39 +0900 | [diff] [blame] | 330 | // /dev/hvc0 |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 331 | command.arg(format!("--serial={},hardware=virtio-console,num=1", &console_arg)); |
Jiyong Park | ae5a4ed | 2021-11-01 18:36:28 +0900 | [diff] [blame] | 332 | // /dev/hvc1 (not used currently) |
| 333 | command.arg("--serial=type=sink,hardware=virtio-console,num=2"); |
| 334 | // /dev/hvc2 |
| 335 | command.arg(format!("--serial={},hardware=virtio-console,num=3", &log_arg)); |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 336 | |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 337 | if let Some(bootloader) = &config.bootloader { |
Andrew Walbran | 02b8ec0 | 2021-06-22 13:07:02 +0000 | [diff] [blame] | 338 | command.arg("--bios").arg(add_preserved_fd(&mut preserved_fds, bootloader)); |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 339 | } |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 340 | |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 341 | if let Some(initrd) = &config.initrd { |
Andrew Walbran | 02b8ec0 | 2021-06-22 13:07:02 +0000 | [diff] [blame] | 342 | command.arg("--initrd").arg(add_preserved_fd(&mut preserved_fds, initrd)); |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 343 | } |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 344 | |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 345 | if let Some(params) = &config.params { |
| 346 | command.arg("--params").arg(params); |
| 347 | } |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 348 | |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 349 | for disk in &config.disks { |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 350 | command |
| 351 | .arg(if disk.writable { "--rwdisk" } else { "--disk" }) |
Andrew Walbran | 02b8ec0 | 2021-06-22 13:07:02 +0000 | [diff] [blame] | 352 | .arg(add_preserved_fd(&mut preserved_fds, &disk.image)); |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 353 | } |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 354 | |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 355 | if let Some(kernel) = &config.kernel { |
Andrew Walbran | 02b8ec0 | 2021-06-22 13:07:02 +0000 | [diff] [blame] | 356 | command.arg(add_preserved_fd(&mut preserved_fds, kernel)); |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 357 | } |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 358 | |
Andrew Walbran | 02b8ec0 | 2021-06-22 13:07:02 +0000 | [diff] [blame] | 359 | debug!("Preserving FDs {:?}", preserved_fds); |
| 360 | command.preserved_fds(preserved_fds); |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 361 | |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 362 | info!("Running {:?}", command); |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 363 | let result = SharedChild::spawn(&mut command)?; |
Jooyung Han | bfe086f | 2021-10-28 10:15:45 +0900 | [diff] [blame] | 364 | debug!("Spawned crosvm({}).", result.id()); |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 365 | Ok(result) |
| 366 | } |
| 367 | |
| 368 | /// Ensure that the configuration has a valid combination of fields set, or return an error if not. |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 369 | fn validate_config(config: &CrosvmConfig) -> Result<(), Error> { |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 370 | if config.bootloader.is_none() && config.kernel.is_none() { |
| 371 | bail!("VM must have either a bootloader or a kernel image."); |
| 372 | } |
| 373 | if config.bootloader.is_some() && (config.kernel.is_some() || config.initrd.is_some()) { |
| 374 | bail!("Can't have both bootloader and kernel/initrd image."); |
| 375 | } |
Jiyong Park | dcf1741 | 2022-02-08 15:07:23 +0900 | [diff] [blame^] | 376 | let version = Version::parse(CROSVM_PLATFORM_VERSION).unwrap(); |
| 377 | if !config.platform_version.matches(&version) { |
| 378 | bail!( |
| 379 | "Incompatible platform version. The config is compatible with platform version(s) \ |
| 380 | {}, but the actual platform version is {}", |
| 381 | config.platform_version, |
| 382 | version |
| 383 | ); |
| 384 | } |
| 385 | |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 386 | Ok(()) |
| 387 | } |
| 388 | |
Andrew Walbran | 02b8ec0 | 2021-06-22 13:07:02 +0000 | [diff] [blame] | 389 | /// Adds the file descriptor for `file` to `preserved_fds`, and returns a string of the form |
| 390 | /// "/proc/self/fd/N" where N is the file descriptor. |
| 391 | fn add_preserved_fd(preserved_fds: &mut Vec<RawFd>, file: &File) -> String { |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 392 | let fd = file.as_raw_fd(); |
Andrew Walbran | 02b8ec0 | 2021-06-22 13:07:02 +0000 | [diff] [blame] | 393 | preserved_fds.push(fd); |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 394 | format!("/proc/self/fd/{}", fd) |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 395 | } |