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 | f6a1eb9 | 2021-04-01 11:16:02 +0000 | [diff] [blame^] | 33 | /// The UID of the process which requested the VM. |
| 34 | pub requester_uid: u32, |
| 35 | /// The SID of the process which requested the VM. |
| 36 | pub requester_sid: Option<String>, |
| 37 | /// The PID of the process which requested the VM. Note that this process may no longer exist |
| 38 | /// and the PID may have been reused for a different process, so this should not be trusted. |
| 39 | pub requester_pid: i32, |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | impl VmInstance { |
| 43 | /// Create a new `VmInstance` for the given process. |
Andrew Walbran | f6a1eb9 | 2021-04-01 11:16:02 +0000 | [diff] [blame^] | 44 | fn new( |
| 45 | child: Child, |
| 46 | cid: Cid, |
| 47 | requester_uid: u32, |
| 48 | requester_sid: Option<String>, |
| 49 | requester_pid: i32, |
| 50 | ) -> VmInstance { |
| 51 | VmInstance { child, cid, requester_uid, requester_sid, requester_pid } |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /// Start an instance of `crosvm` to manage a new VM. The `crosvm` instance will be killed when |
| 55 | /// the `VmInstance` is dropped. |
Andrew Walbran | f6a1eb9 | 2021-04-01 11:16:02 +0000 | [diff] [blame^] | 56 | pub fn start( |
| 57 | config: &VmConfig, |
| 58 | cid: Cid, |
| 59 | log_fd: Option<File>, |
| 60 | requester_uid: u32, |
| 61 | requester_sid: Option<String>, |
| 62 | requester_pid: i32, |
| 63 | ) -> Result<VmInstance, Error> { |
Andrew Walbran | a89fc13 | 2021-03-17 17:08:36 +0000 | [diff] [blame] | 64 | let child = run_vm(config, cid, log_fd)?; |
Andrew Walbran | f6a1eb9 | 2021-04-01 11:16:02 +0000 | [diff] [blame^] | 65 | Ok(VmInstance::new(child, cid, requester_uid, requester_sid, requester_pid)) |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
| 69 | impl Drop for VmInstance { |
| 70 | fn drop(&mut self) { |
| 71 | debug!("Dropping {:?}", self); |
| 72 | // TODO: Talk to crosvm to shutdown cleanly. |
| 73 | if let Err(e) = self.child.kill() { |
| 74 | error!("Error killing crosvm instance: {}", e); |
| 75 | } |
| 76 | // We need to wait on the process after killing it to avoid zombies. |
| 77 | match self.child.wait() { |
| 78 | Err(e) => error!("Error waiting for crosvm instance to die: {}", e), |
| 79 | Ok(status) => info!("Crosvm exited with status {}", status), |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /// Start an instance of `crosvm` to manage a new VM. |
Andrew Walbran | a89fc13 | 2021-03-17 17:08:36 +0000 | [diff] [blame] | 85 | 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] | 86 | config.validate()?; |
| 87 | |
| 88 | let mut command = Command::new(CROSVM_PATH); |
| 89 | // TODO(qwandor): Remove --disable-sandbox. |
| 90 | command.arg("run").arg("--disable-sandbox").arg("--cid").arg(cid.to_string()); |
Andrew Walbran | a89fc13 | 2021-03-17 17:08:36 +0000 | [diff] [blame] | 91 | if let Some(log_fd) = log_fd { |
| 92 | command.stdout(log_fd); |
| 93 | } else { |
| 94 | // Ignore console output. |
| 95 | command.arg("--serial=type=sink"); |
| 96 | } |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 97 | if let Some(bootloader) = &config.bootloader { |
| 98 | command.arg("--bios").arg(bootloader); |
| 99 | } |
| 100 | if let Some(initrd) = &config.initrd { |
| 101 | command.arg("--initrd").arg(initrd); |
| 102 | } |
| 103 | if let Some(params) = &config.params { |
| 104 | command.arg("--params").arg(params); |
| 105 | } |
| 106 | for disk in &config.disks { |
| 107 | command.arg(if disk.writable { "--rwdisk" } else { "--disk" }).arg(&disk.image); |
| 108 | } |
| 109 | if let Some(kernel) = &config.kernel { |
| 110 | command.arg(kernel); |
| 111 | } |
| 112 | info!("Running {:?}", command); |
| 113 | // TODO: Monitor child process, and remove from VM map if it dies. |
| 114 | Ok(command.spawn()?) |
| 115 | } |