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::config::VmConfig; |
| 19 | use crate::Cid; |
| 20 | use anyhow::Error; |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame^] | 21 | use log::{error, info}; |
| 22 | use shared_child::SharedChild; |
Andrew Walbran | a89fc13 | 2021-03-17 17:08:36 +0000 | [diff] [blame] | 23 | use std::fs::File; |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame^] | 24 | use std::process::Command; |
| 25 | use std::sync::atomic::{AtomicBool, Ordering}; |
| 26 | use std::sync::Arc; |
| 27 | use std::thread; |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 28 | |
| 29 | const CROSVM_PATH: &str = "/apex/com.android.virt/bin/crosvm"; |
| 30 | |
| 31 | /// Information about a particular instance of a VM which is running. |
| 32 | #[derive(Debug)] |
| 33 | pub struct VmInstance { |
| 34 | /// The crosvm child process. |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame^] | 35 | child: SharedChild, |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 36 | /// The CID assigned to the VM for vsock communication. |
| 37 | pub cid: Cid, |
Andrew Walbran | f6a1eb9 | 2021-04-01 11:16:02 +0000 | [diff] [blame] | 38 | /// The UID of the process which requested the VM. |
| 39 | pub requester_uid: u32, |
| 40 | /// The SID of the process which requested the VM. |
| 41 | pub requester_sid: Option<String>, |
| 42 | /// The PID of the process which requested the VM. Note that this process may no longer exist |
| 43 | /// and the PID may have been reused for a different process, so this should not be trusted. |
| 44 | pub requester_pid: i32, |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame^] | 45 | /// Whether the VM is still running. |
| 46 | running: AtomicBool, |
| 47 | /// Callbacks to clients of the VM. |
| 48 | pub callbacks: VirtualMachineCallbacks, |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | impl VmInstance { |
| 52 | /// Create a new `VmInstance` for the given process. |
Andrew Walbran | f6a1eb9 | 2021-04-01 11:16:02 +0000 | [diff] [blame] | 53 | fn new( |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame^] | 54 | child: SharedChild, |
Andrew Walbran | f6a1eb9 | 2021-04-01 11:16:02 +0000 | [diff] [blame] | 55 | cid: Cid, |
| 56 | requester_uid: u32, |
| 57 | requester_sid: Option<String>, |
| 58 | requester_pid: i32, |
| 59 | ) -> VmInstance { |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame^] | 60 | VmInstance { |
| 61 | child, |
| 62 | cid, |
| 63 | requester_uid, |
| 64 | requester_sid, |
| 65 | requester_pid, |
| 66 | running: AtomicBool::new(true), |
| 67 | callbacks: Default::default(), |
| 68 | } |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /// Start an instance of `crosvm` to manage a new VM. The `crosvm` instance will be killed when |
| 72 | /// the `VmInstance` is dropped. |
Andrew Walbran | f6a1eb9 | 2021-04-01 11:16:02 +0000 | [diff] [blame] | 73 | pub fn start( |
| 74 | config: &VmConfig, |
| 75 | cid: Cid, |
| 76 | log_fd: Option<File>, |
| 77 | requester_uid: u32, |
| 78 | requester_sid: Option<String>, |
| 79 | requester_pid: i32, |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame^] | 80 | ) -> Result<Arc<VmInstance>, Error> { |
Andrew Walbran | a89fc13 | 2021-03-17 17:08:36 +0000 | [diff] [blame] | 81 | let child = run_vm(config, cid, log_fd)?; |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame^] | 82 | let instance = |
| 83 | Arc::new(VmInstance::new(child, cid, requester_uid, requester_sid, requester_pid)); |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 84 | |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame^] | 85 | let instance_clone = instance.clone(); |
| 86 | thread::spawn(move || { |
| 87 | instance_clone.monitor(); |
| 88 | }); |
| 89 | |
| 90 | Ok(instance) |
| 91 | } |
| 92 | |
| 93 | /// Wait for the crosvm child process to finish, then mark the VM as no longer running and call |
| 94 | /// any callbacks. |
| 95 | fn monitor(&self) { |
| 96 | match self.child.wait() { |
| 97 | Err(e) => error!("Error waiting for crosvm instance to die: {}", e), |
| 98 | Ok(status) => info!("crosvm exited with status {}", status), |
| 99 | } |
| 100 | self.running.store(false, Ordering::Release); |
| 101 | self.callbacks.callback_on_died(self.cid); |
| 102 | } |
| 103 | |
| 104 | /// Return whether `crosvm` is still running the VM. |
| 105 | pub fn running(&self) -> bool { |
| 106 | self.running.load(Ordering::Acquire) |
| 107 | } |
| 108 | |
| 109 | /// Kill the crosvm instance. |
| 110 | pub fn kill(&self) { |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 111 | // TODO: Talk to crosvm to shutdown cleanly. |
| 112 | if let Err(e) = self.child.kill() { |
| 113 | error!("Error killing crosvm instance: {}", e); |
| 114 | } |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
| 118 | /// Start an instance of `crosvm` to manage a new VM. |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame^] | 119 | fn run_vm(config: &VmConfig, cid: Cid, log_fd: Option<File>) -> Result<SharedChild, Error> { |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 120 | config.validate()?; |
| 121 | |
| 122 | let mut command = Command::new(CROSVM_PATH); |
| 123 | // TODO(qwandor): Remove --disable-sandbox. |
| 124 | command.arg("run").arg("--disable-sandbox").arg("--cid").arg(cid.to_string()); |
Andrew Walbran | a89fc13 | 2021-03-17 17:08:36 +0000 | [diff] [blame] | 125 | if let Some(log_fd) = log_fd { |
| 126 | command.stdout(log_fd); |
| 127 | } else { |
| 128 | // Ignore console output. |
| 129 | command.arg("--serial=type=sink"); |
| 130 | } |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 131 | if let Some(bootloader) = &config.bootloader { |
| 132 | command.arg("--bios").arg(bootloader); |
| 133 | } |
| 134 | if let Some(initrd) = &config.initrd { |
| 135 | command.arg("--initrd").arg(initrd); |
| 136 | } |
| 137 | if let Some(params) = &config.params { |
| 138 | command.arg("--params").arg(params); |
| 139 | } |
| 140 | for disk in &config.disks { |
| 141 | command.arg(if disk.writable { "--rwdisk" } else { "--disk" }).arg(&disk.image); |
| 142 | } |
| 143 | if let Some(kernel) = &config.kernel { |
| 144 | command.arg(kernel); |
| 145 | } |
| 146 | info!("Running {:?}", command); |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame^] | 147 | Ok(SharedChild::spawn(&mut command)?) |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 148 | } |