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 | |
| 17 | use crate::config::VmConfig; |
| 18 | use crate::Cid; |
| 19 | use anyhow::Error; |
| 20 | use log::{debug, error, info}; |
Andrew Walbran | a89fc13 | 2021-03-17 17:08:36 +0000 | [diff] [blame] | 21 | use std::fs::File; |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 22 | use std::process::{Child, Command}; |
| 23 | |
| 24 | const CROSVM_PATH: &str = "/apex/com.android.virt/bin/crosvm"; |
| 25 | |
| 26 | /// Information about a particular instance of a VM which is running. |
| 27 | #[derive(Debug)] |
| 28 | pub struct VmInstance { |
| 29 | /// The crosvm child process. |
| 30 | child: Child, |
| 31 | /// The CID assigned to the VM for vsock communication. |
| 32 | pub cid: Cid, |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 33 | /// The filename of the config file that was used to start the VM. This may have changed since |
| 34 | /// it was read so it shouldn't be trusted; it is only stored for debugging purposes. |
| 35 | pub config_path: String, |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | impl VmInstance { |
| 39 | /// Create a new `VmInstance` for the given process. |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 40 | fn new(child: Child, cid: Cid, config_path: &str) -> VmInstance { |
| 41 | VmInstance { child, cid, config_path: config_path.to_owned() } |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | /// Start an instance of `crosvm` to manage a new VM. The `crosvm` instance will be killed when |
| 45 | /// the `VmInstance` is dropped. |
Andrew Walbran | a89fc13 | 2021-03-17 17:08:36 +0000 | [diff] [blame] | 46 | pub fn start( |
| 47 | config: &VmConfig, |
| 48 | cid: Cid, |
| 49 | config_path: &str, |
| 50 | log_fd: Option<File>, |
| 51 | ) -> Result<VmInstance, Error> { |
| 52 | let child = run_vm(config, cid, log_fd)?; |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 53 | Ok(VmInstance::new(child, cid, config_path)) |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
| 57 | impl Drop for VmInstance { |
| 58 | fn drop(&mut self) { |
| 59 | debug!("Dropping {:?}", self); |
| 60 | // TODO: Talk to crosvm to shutdown cleanly. |
| 61 | if let Err(e) = self.child.kill() { |
| 62 | error!("Error killing crosvm instance: {}", e); |
| 63 | } |
| 64 | // We need to wait on the process after killing it to avoid zombies. |
| 65 | match self.child.wait() { |
| 66 | Err(e) => error!("Error waiting for crosvm instance to die: {}", e), |
| 67 | Ok(status) => info!("Crosvm exited with status {}", status), |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /// Start an instance of `crosvm` to manage a new VM. |
Andrew Walbran | a89fc13 | 2021-03-17 17:08:36 +0000 | [diff] [blame] | 73 | fn run_vm(config: &VmConfig, cid: Cid, log_fd: Option<File>) -> Result<Child, Error> { |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 74 | config.validate()?; |
| 75 | |
| 76 | let mut command = Command::new(CROSVM_PATH); |
| 77 | // TODO(qwandor): Remove --disable-sandbox. |
| 78 | command.arg("run").arg("--disable-sandbox").arg("--cid").arg(cid.to_string()); |
Andrew Walbran | a89fc13 | 2021-03-17 17:08:36 +0000 | [diff] [blame] | 79 | if let Some(log_fd) = log_fd { |
| 80 | command.stdout(log_fd); |
| 81 | } else { |
| 82 | // Ignore console output. |
| 83 | command.arg("--serial=type=sink"); |
| 84 | } |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 85 | if let Some(bootloader) = &config.bootloader { |
| 86 | command.arg("--bios").arg(bootloader); |
| 87 | } |
| 88 | if let Some(initrd) = &config.initrd { |
| 89 | command.arg("--initrd").arg(initrd); |
| 90 | } |
| 91 | if let Some(params) = &config.params { |
| 92 | command.arg("--params").arg(params); |
| 93 | } |
| 94 | for disk in &config.disks { |
| 95 | command.arg(if disk.writable { "--rwdisk" } else { "--disk" }).arg(&disk.image); |
| 96 | } |
| 97 | if let Some(kernel) = &config.kernel { |
| 98 | command.arg(kernel); |
| 99 | } |
| 100 | info!("Running {:?}", command); |
| 101 | // TODO: Monitor child process, and remove from VM map if it dies. |
| 102 | Ok(command.spawn()?) |
| 103 | } |