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