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