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}; |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 22 | use shared_child::SharedChild; |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 23 | use std::fs::{remove_dir_all, File}; |
Andrew Walbran | b15cd6e | 2021-07-05 16:38:07 +0000 | [diff] [blame] | 24 | use std::num::NonZeroU32; |
Andrew Walbran | 02b8ec0 | 2021-06-22 13:07:02 +0000 | [diff] [blame] | 25 | use std::os::unix::io::{AsRawFd, RawFd}; |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 26 | use std::path::PathBuf; |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 27 | use std::process::Command; |
| 28 | use std::sync::atomic::{AtomicBool, Ordering}; |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame^] | 29 | use std::sync::{Arc, Mutex}; |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 30 | use std::thread; |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame^] | 31 | use vsock::VsockStream; |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 32 | |
| 33 | const CROSVM_PATH: &str = "/apex/com.android.virt/bin/crosvm"; |
| 34 | |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 35 | /// Configuration for a VM to run with crosvm. |
| 36 | #[derive(Debug)] |
| 37 | pub struct CrosvmConfig<'a> { |
| 38 | pub cid: Cid, |
| 39 | pub bootloader: Option<&'a File>, |
| 40 | pub kernel: Option<&'a File>, |
| 41 | pub initrd: Option<&'a File>, |
| 42 | pub disks: Vec<DiskFile>, |
| 43 | pub params: Option<String>, |
Andrew Walbran | f865042 | 2021-06-09 15:54:09 +0000 | [diff] [blame] | 44 | pub protected: bool, |
Andrew Walbran | b15cd6e | 2021-07-05 16:38:07 +0000 | [diff] [blame] | 45 | pub memory_mib: Option<NonZeroU32>, |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | /// A disk image to pass to crosvm for a VM. |
| 49 | #[derive(Debug)] |
| 50 | pub struct DiskFile { |
| 51 | pub image: File, |
| 52 | pub writable: bool, |
| 53 | } |
| 54 | |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 55 | /// Information about a particular instance of a VM which is running. |
| 56 | #[derive(Debug)] |
| 57 | pub struct VmInstance { |
| 58 | /// The crosvm child process. |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 59 | child: SharedChild, |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 60 | /// The CID assigned to the VM for vsock communication. |
| 61 | pub cid: Cid, |
Andrew Walbran | f865042 | 2021-06-09 15:54:09 +0000 | [diff] [blame] | 62 | /// Whether the VM is a protected VM. |
| 63 | pub protected: bool, |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 64 | /// Directory of temporary files used by the VM while it is running. |
| 65 | pub temporary_directory: PathBuf, |
Andrew Walbran | f6a1eb9 | 2021-04-01 11:16:02 +0000 | [diff] [blame] | 66 | /// The UID of the process which requested the VM. |
| 67 | pub requester_uid: u32, |
| 68 | /// The SID of the process which requested the VM. |
Andrew Walbran | 0203449 | 2021-04-13 15:05:07 +0000 | [diff] [blame] | 69 | pub requester_sid: String, |
Andrew Walbran | f6a1eb9 | 2021-04-01 11:16:02 +0000 | [diff] [blame] | 70 | /// The PID of the process which requested the VM. Note that this process may no longer exist |
| 71 | /// 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] | 72 | pub requester_debug_pid: i32, |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 73 | /// Whether the VM is still running. |
| 74 | running: AtomicBool, |
| 75 | /// Callbacks to clients of the VM. |
| 76 | pub callbacks: VirtualMachineCallbacks, |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame^] | 77 | /// Input/output stream of the payload run in the VM. |
| 78 | pub stream: Mutex<Option<VsockStream>>, |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | impl VmInstance { |
| 82 | /// Create a new `VmInstance` for the given process. |
Andrew Walbran | f6a1eb9 | 2021-04-01 11:16:02 +0000 | [diff] [blame] | 83 | fn new( |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 84 | child: SharedChild, |
Andrew Walbran | f6a1eb9 | 2021-04-01 11:16:02 +0000 | [diff] [blame] | 85 | cid: Cid, |
Andrew Walbran | f865042 | 2021-06-09 15:54:09 +0000 | [diff] [blame] | 86 | protected: bool, |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 87 | temporary_directory: PathBuf, |
Andrew Walbran | f6a1eb9 | 2021-04-01 11:16:02 +0000 | [diff] [blame] | 88 | requester_uid: u32, |
Andrew Walbran | 0203449 | 2021-04-13 15:05:07 +0000 | [diff] [blame] | 89 | requester_sid: String, |
| 90 | requester_debug_pid: i32, |
Andrew Walbran | f6a1eb9 | 2021-04-01 11:16:02 +0000 | [diff] [blame] | 91 | ) -> VmInstance { |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 92 | VmInstance { |
| 93 | child, |
| 94 | cid, |
Andrew Walbran | f865042 | 2021-06-09 15:54:09 +0000 | [diff] [blame] | 95 | protected, |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 96 | temporary_directory, |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 97 | requester_uid, |
| 98 | requester_sid, |
Andrew Walbran | 0203449 | 2021-04-13 15:05:07 +0000 | [diff] [blame] | 99 | requester_debug_pid, |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 100 | running: AtomicBool::new(true), |
| 101 | callbacks: Default::default(), |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame^] | 102 | stream: Mutex::new(None), |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 103 | } |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /// Start an instance of `crosvm` to manage a new VM. The `crosvm` instance will be killed when |
| 107 | /// the `VmInstance` is dropped. |
Andrew Walbran | f6a1eb9 | 2021-04-01 11:16:02 +0000 | [diff] [blame] | 108 | pub fn start( |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 109 | config: &CrosvmConfig, |
Andrew Walbran | f6a1eb9 | 2021-04-01 11:16:02 +0000 | [diff] [blame] | 110 | log_fd: Option<File>, |
Andrew Walbran | 02b8ec0 | 2021-06-22 13:07:02 +0000 | [diff] [blame] | 111 | composite_disk_fds: &[RawFd], |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 112 | temporary_directory: PathBuf, |
Andrew Walbran | f6a1eb9 | 2021-04-01 11:16:02 +0000 | [diff] [blame] | 113 | requester_uid: u32, |
Andrew Walbran | 0203449 | 2021-04-13 15:05:07 +0000 | [diff] [blame] | 114 | requester_sid: String, |
| 115 | requester_debug_pid: i32, |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 116 | ) -> Result<Arc<VmInstance>, Error> { |
Andrew Walbran | 02b8ec0 | 2021-06-22 13:07:02 +0000 | [diff] [blame] | 117 | let child = run_vm(config, log_fd, composite_disk_fds)?; |
Andrew Walbran | 0203449 | 2021-04-13 15:05:07 +0000 | [diff] [blame] | 118 | let instance = Arc::new(VmInstance::new( |
| 119 | child, |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 120 | config.cid, |
Andrew Walbran | f865042 | 2021-06-09 15:54:09 +0000 | [diff] [blame] | 121 | config.protected, |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 122 | temporary_directory, |
Andrew Walbran | 0203449 | 2021-04-13 15:05:07 +0000 | [diff] [blame] | 123 | requester_uid, |
| 124 | requester_sid, |
| 125 | requester_debug_pid, |
| 126 | )); |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 127 | |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 128 | let instance_clone = instance.clone(); |
| 129 | thread::spawn(move || { |
| 130 | instance_clone.monitor(); |
| 131 | }); |
| 132 | |
| 133 | Ok(instance) |
| 134 | } |
| 135 | |
| 136 | /// Wait for the crosvm child process to finish, then mark the VM as no longer running and call |
| 137 | /// any callbacks. |
| 138 | fn monitor(&self) { |
| 139 | match self.child.wait() { |
| 140 | Err(e) => error!("Error waiting for crosvm instance to die: {}", e), |
| 141 | Ok(status) => info!("crosvm exited with status {}", status), |
| 142 | } |
| 143 | self.running.store(false, Ordering::Release); |
| 144 | self.callbacks.callback_on_died(self.cid); |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 145 | |
| 146 | // Delete temporary files. |
| 147 | if let Err(e) = remove_dir_all(&self.temporary_directory) { |
Andrew Walbran | 806f154 | 2021-06-10 14:07:12 +0000 | [diff] [blame] | 148 | error!("Error removing temporary directory {:?}: {}", self.temporary_directory, e); |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 149 | } |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /// Return whether `crosvm` is still running the VM. |
| 153 | pub fn running(&self) -> bool { |
| 154 | self.running.load(Ordering::Acquire) |
| 155 | } |
| 156 | |
| 157 | /// Kill the crosvm instance. |
| 158 | pub fn kill(&self) { |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 159 | // TODO: Talk to crosvm to shutdown cleanly. |
| 160 | if let Err(e) = self.child.kill() { |
| 161 | error!("Error killing crosvm instance: {}", e); |
| 162 | } |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
| 166 | /// Start an instance of `crosvm` to manage a new VM. |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 167 | fn run_vm( |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 168 | config: &CrosvmConfig, |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 169 | log_fd: Option<File>, |
Andrew Walbran | 02b8ec0 | 2021-06-22 13:07:02 +0000 | [diff] [blame] | 170 | composite_disk_fds: &[RawFd], |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 171 | ) -> Result<SharedChild, Error> { |
| 172 | validate_config(config)?; |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 173 | |
| 174 | let mut command = Command::new(CROSVM_PATH); |
| 175 | // TODO(qwandor): Remove --disable-sandbox. |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 176 | command.arg("run").arg("--disable-sandbox").arg("--cid").arg(config.cid.to_string()); |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 177 | |
Andrew Walbran | f865042 | 2021-06-09 15:54:09 +0000 | [diff] [blame] | 178 | if config.protected { |
| 179 | command.arg("--protected-vm"); |
| 180 | } |
| 181 | |
Andrew Walbran | b15cd6e | 2021-07-05 16:38:07 +0000 | [diff] [blame] | 182 | if let Some(memory_mib) = config.memory_mib { |
| 183 | command.arg("--mem").arg(memory_mib.to_string()); |
| 184 | } |
| 185 | |
Andrew Walbran | a89fc13 | 2021-03-17 17:08:36 +0000 | [diff] [blame] | 186 | if let Some(log_fd) = log_fd { |
| 187 | command.stdout(log_fd); |
| 188 | } else { |
| 189 | // Ignore console output. |
| 190 | command.arg("--serial=type=sink"); |
| 191 | } |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 192 | |
| 193 | // Keep track of what file descriptors should be mapped to the crosvm process. |
Andrew Walbran | 02b8ec0 | 2021-06-22 13:07:02 +0000 | [diff] [blame] | 194 | let mut preserved_fds = composite_disk_fds.to_vec(); |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 195 | |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 196 | if let Some(bootloader) = &config.bootloader { |
Andrew Walbran | 02b8ec0 | 2021-06-22 13:07:02 +0000 | [diff] [blame] | 197 | command.arg("--bios").arg(add_preserved_fd(&mut preserved_fds, bootloader)); |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 198 | } |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 199 | |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 200 | if let Some(initrd) = &config.initrd { |
Andrew Walbran | 02b8ec0 | 2021-06-22 13:07:02 +0000 | [diff] [blame] | 201 | command.arg("--initrd").arg(add_preserved_fd(&mut preserved_fds, initrd)); |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 202 | } |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 203 | |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 204 | if let Some(params) = &config.params { |
| 205 | command.arg("--params").arg(params); |
| 206 | } |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 207 | |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 208 | for disk in &config.disks { |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 209 | command |
| 210 | .arg(if disk.writable { "--rwdisk" } else { "--disk" }) |
Andrew Walbran | 02b8ec0 | 2021-06-22 13:07:02 +0000 | [diff] [blame] | 211 | .arg(add_preserved_fd(&mut preserved_fds, &disk.image)); |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 212 | } |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 213 | |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 214 | if let Some(kernel) = &config.kernel { |
Andrew Walbran | 02b8ec0 | 2021-06-22 13:07:02 +0000 | [diff] [blame] | 215 | command.arg(add_preserved_fd(&mut preserved_fds, kernel)); |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 216 | } |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 217 | |
Andrew Walbran | 02b8ec0 | 2021-06-22 13:07:02 +0000 | [diff] [blame] | 218 | debug!("Preserving FDs {:?}", preserved_fds); |
| 219 | command.preserved_fds(preserved_fds); |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 220 | |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 221 | info!("Running {:?}", command); |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 222 | let result = SharedChild::spawn(&mut command)?; |
| 223 | Ok(result) |
| 224 | } |
| 225 | |
| 226 | /// 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] | 227 | fn validate_config(config: &CrosvmConfig) -> Result<(), Error> { |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 228 | if config.bootloader.is_none() && config.kernel.is_none() { |
| 229 | bail!("VM must have either a bootloader or a kernel image."); |
| 230 | } |
| 231 | if config.bootloader.is_some() && (config.kernel.is_some() || config.initrd.is_some()) { |
| 232 | bail!("Can't have both bootloader and kernel/initrd image."); |
| 233 | } |
| 234 | Ok(()) |
| 235 | } |
| 236 | |
Andrew Walbran | 02b8ec0 | 2021-06-22 13:07:02 +0000 | [diff] [blame] | 237 | /// Adds the file descriptor for `file` to `preserved_fds`, and returns a string of the form |
| 238 | /// "/proc/self/fd/N" where N is the file descriptor. |
| 239 | fn add_preserved_fd(preserved_fds: &mut Vec<RawFd>, file: &File) -> String { |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 240 | let fd = file.as_raw_fd(); |
Andrew Walbran | 02b8ec0 | 2021-06-22 13:07:02 +0000 | [diff] [blame] | 241 | preserved_fds.push(fd); |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 242 | format!("/proc/self/fd/{}", fd) |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 243 | } |