blob: 5984ff0cc68e10dec66146a67b9a4be2ba00eada [file] [log] [blame]
Andrew Walbrand6dce6f2021-03-05 16:39:08 +00001// 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 Walbrandae07162021-03-12 17:05:20 +000017use crate::aidl::VirtualMachineCallbacks;
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000018use crate::Cid;
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +000019use anyhow::{bail, Error};
Andrew Walbran02b8ec02021-06-22 13:07:02 +000020use command_fds::CommandFdExt;
Andrew Walbran3a5a9212021-05-04 17:09:08 +000021use log::{debug, error, info};
Andrew Walbrandae07162021-03-12 17:05:20 +000022use shared_child::SharedChild;
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +000023use std::fs::{remove_dir_all, File};
Andrew Walbranb15cd6e2021-07-05 16:38:07 +000024use std::num::NonZeroU32;
Andrew Walbran02b8ec02021-06-22 13:07:02 +000025use std::os::unix::io::{AsRawFd, RawFd};
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +000026use std::path::PathBuf;
Andrew Walbrandae07162021-03-12 17:05:20 +000027use std::process::Command;
28use std::sync::atomic::{AtomicBool, Ordering};
Inseob Kim7f61fe72021-08-20 20:50:47 +090029use std::sync::{Arc, Mutex};
Andrew Walbrandae07162021-03-12 17:05:20 +000030use std::thread;
Inseob Kim7f61fe72021-08-20 20:50:47 +090031use vsock::VsockStream;
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000032
33const CROSVM_PATH: &str = "/apex/com.android.virt/bin/crosvm";
34
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +000035/// Configuration for a VM to run with crosvm.
36#[derive(Debug)]
37pub struct CrosvmConfig<'a> {
38 pub cid: Cid,
39 pub bootloader: Option<&'a File>,
40 pub kernel: Option<&'a File>,
41 pub initrd: Option<&'a File>,
42 pub disks: Vec<DiskFile>,
43 pub params: Option<String>,
Andrew Walbranf8650422021-06-09 15:54:09 +000044 pub protected: bool,
Andrew Walbranb15cd6e2021-07-05 16:38:07 +000045 pub memory_mib: Option<NonZeroU32>,
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +000046}
47
48/// A disk image to pass to crosvm for a VM.
49#[derive(Debug)]
50pub struct DiskFile {
51 pub image: File,
52 pub writable: bool,
53}
54
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000055/// Information about a particular instance of a VM which is running.
56#[derive(Debug)]
57pub struct VmInstance {
58 /// The crosvm child process.
Andrew Walbrandae07162021-03-12 17:05:20 +000059 child: SharedChild,
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000060 /// The CID assigned to the VM for vsock communication.
61 pub cid: Cid,
Andrew Walbranf8650422021-06-09 15:54:09 +000062 /// Whether the VM is a protected VM.
63 pub protected: bool,
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +000064 /// Directory of temporary files used by the VM while it is running.
65 pub temporary_directory: PathBuf,
Andrew Walbranf6a1eb92021-04-01 11:16:02 +000066 /// The UID of the process which requested the VM.
67 pub requester_uid: u32,
68 /// The SID of the process which requested the VM.
Andrew Walbran02034492021-04-13 15:05:07 +000069 pub requester_sid: String,
Andrew Walbranf6a1eb92021-04-01 11:16:02 +000070 /// The PID of the process which requested the VM. Note that this process may no longer exist
71 /// and the PID may have been reused for a different process, so this should not be trusted.
Andrew Walbran02034492021-04-13 15:05:07 +000072 pub requester_debug_pid: i32,
Andrew Walbrandae07162021-03-12 17:05:20 +000073 /// Whether the VM is still running.
74 running: AtomicBool,
75 /// Callbacks to clients of the VM.
76 pub callbacks: VirtualMachineCallbacks,
Inseob Kim7f61fe72021-08-20 20:50:47 +090077 /// Input/output stream of the payload run in the VM.
78 pub stream: Mutex<Option<VsockStream>>,
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000079}
80
81impl VmInstance {
82 /// Create a new `VmInstance` for the given process.
Andrew Walbranf6a1eb92021-04-01 11:16:02 +000083 fn new(
Andrew Walbrandae07162021-03-12 17:05:20 +000084 child: SharedChild,
Andrew Walbranf6a1eb92021-04-01 11:16:02 +000085 cid: Cid,
Andrew Walbranf8650422021-06-09 15:54:09 +000086 protected: bool,
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +000087 temporary_directory: PathBuf,
Andrew Walbranf6a1eb92021-04-01 11:16:02 +000088 requester_uid: u32,
Andrew Walbran02034492021-04-13 15:05:07 +000089 requester_sid: String,
90 requester_debug_pid: i32,
Andrew Walbranf6a1eb92021-04-01 11:16:02 +000091 ) -> VmInstance {
Andrew Walbrandae07162021-03-12 17:05:20 +000092 VmInstance {
93 child,
94 cid,
Andrew Walbranf8650422021-06-09 15:54:09 +000095 protected,
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +000096 temporary_directory,
Andrew Walbrandae07162021-03-12 17:05:20 +000097 requester_uid,
98 requester_sid,
Andrew Walbran02034492021-04-13 15:05:07 +000099 requester_debug_pid,
Andrew Walbrandae07162021-03-12 17:05:20 +0000100 running: AtomicBool::new(true),
101 callbacks: Default::default(),
Inseob Kim7f61fe72021-08-20 20:50:47 +0900102 stream: Mutex::new(None),
Andrew Walbrandae07162021-03-12 17:05:20 +0000103 }
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000104 }
105
106 /// Start an instance of `crosvm` to manage a new VM. The `crosvm` instance will be killed when
107 /// the `VmInstance` is dropped.
Andrew Walbranf6a1eb92021-04-01 11:16:02 +0000108 pub fn start(
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000109 config: &CrosvmConfig,
Andrew Walbranf6a1eb92021-04-01 11:16:02 +0000110 log_fd: Option<File>,
Andrew Walbran02b8ec02021-06-22 13:07:02 +0000111 composite_disk_fds: &[RawFd],
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000112 temporary_directory: PathBuf,
Andrew Walbranf6a1eb92021-04-01 11:16:02 +0000113 requester_uid: u32,
Andrew Walbran02034492021-04-13 15:05:07 +0000114 requester_sid: String,
115 requester_debug_pid: i32,
Andrew Walbrandae07162021-03-12 17:05:20 +0000116 ) -> Result<Arc<VmInstance>, Error> {
Andrew Walbran02b8ec02021-06-22 13:07:02 +0000117 let child = run_vm(config, log_fd, composite_disk_fds)?;
Andrew Walbran02034492021-04-13 15:05:07 +0000118 let instance = Arc::new(VmInstance::new(
119 child,
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000120 config.cid,
Andrew Walbranf8650422021-06-09 15:54:09 +0000121 config.protected,
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000122 temporary_directory,
Andrew Walbran02034492021-04-13 15:05:07 +0000123 requester_uid,
124 requester_sid,
125 requester_debug_pid,
126 ));
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000127
Andrew Walbrandae07162021-03-12 17:05:20 +0000128 let instance_clone = instance.clone();
129 thread::spawn(move || {
130 instance_clone.monitor();
131 });
132
133 Ok(instance)
134 }
135
136 /// Wait for the crosvm child process to finish, then mark the VM as no longer running and call
137 /// any callbacks.
138 fn monitor(&self) {
139 match self.child.wait() {
140 Err(e) => error!("Error waiting for crosvm instance to die: {}", e),
141 Ok(status) => info!("crosvm exited with status {}", status),
142 }
143 self.running.store(false, Ordering::Release);
144 self.callbacks.callback_on_died(self.cid);
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000145
146 // Delete temporary files.
147 if let Err(e) = remove_dir_all(&self.temporary_directory) {
Andrew Walbran806f1542021-06-10 14:07:12 +0000148 error!("Error removing temporary directory {:?}: {}", self.temporary_directory, e);
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000149 }
Andrew Walbrandae07162021-03-12 17:05:20 +0000150 }
151
152 /// Return whether `crosvm` is still running the VM.
153 pub fn running(&self) -> bool {
154 self.running.load(Ordering::Acquire)
155 }
156
157 /// Kill the crosvm instance.
158 pub fn kill(&self) {
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000159 // TODO: Talk to crosvm to shutdown cleanly.
160 if let Err(e) = self.child.kill() {
161 error!("Error killing crosvm instance: {}", e);
162 }
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000163 }
164}
165
166/// Start an instance of `crosvm` to manage a new VM.
Andrew Walbran3a5a9212021-05-04 17:09:08 +0000167fn run_vm(
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000168 config: &CrosvmConfig,
Andrew Walbran3a5a9212021-05-04 17:09:08 +0000169 log_fd: Option<File>,
Andrew Walbran02b8ec02021-06-22 13:07:02 +0000170 composite_disk_fds: &[RawFd],
Andrew Walbran3a5a9212021-05-04 17:09:08 +0000171) -> Result<SharedChild, Error> {
172 validate_config(config)?;
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000173
174 let mut command = Command::new(CROSVM_PATH);
175 // TODO(qwandor): Remove --disable-sandbox.
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000176 command.arg("run").arg("--disable-sandbox").arg("--cid").arg(config.cid.to_string());
Andrew Walbran3a5a9212021-05-04 17:09:08 +0000177
Andrew Walbranf8650422021-06-09 15:54:09 +0000178 if config.protected {
179 command.arg("--protected-vm");
180 }
181
Andrew Walbranb15cd6e2021-07-05 16:38:07 +0000182 if let Some(memory_mib) = config.memory_mib {
183 command.arg("--mem").arg(memory_mib.to_string());
184 }
185
Andrew Walbrana89fc132021-03-17 17:08:36 +0000186 if let Some(log_fd) = log_fd {
187 command.stdout(log_fd);
188 } else {
189 // Ignore console output.
190 command.arg("--serial=type=sink");
191 }
Andrew Walbran3a5a9212021-05-04 17:09:08 +0000192
193 // Keep track of what file descriptors should be mapped to the crosvm process.
Andrew Walbran02b8ec02021-06-22 13:07:02 +0000194 let mut preserved_fds = composite_disk_fds.to_vec();
Andrew Walbran3a5a9212021-05-04 17:09:08 +0000195
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000196 if let Some(bootloader) = &config.bootloader {
Andrew Walbran02b8ec02021-06-22 13:07:02 +0000197 command.arg("--bios").arg(add_preserved_fd(&mut preserved_fds, bootloader));
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000198 }
Andrew Walbran3a5a9212021-05-04 17:09:08 +0000199
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000200 if let Some(initrd) = &config.initrd {
Andrew Walbran02b8ec02021-06-22 13:07:02 +0000201 command.arg("--initrd").arg(add_preserved_fd(&mut preserved_fds, initrd));
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000202 }
Andrew Walbran3a5a9212021-05-04 17:09:08 +0000203
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000204 if let Some(params) = &config.params {
205 command.arg("--params").arg(params);
206 }
Andrew Walbran3a5a9212021-05-04 17:09:08 +0000207
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000208 for disk in &config.disks {
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000209 command
210 .arg(if disk.writable { "--rwdisk" } else { "--disk" })
Andrew Walbran02b8ec02021-06-22 13:07:02 +0000211 .arg(add_preserved_fd(&mut preserved_fds, &disk.image));
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000212 }
Andrew Walbran3a5a9212021-05-04 17:09:08 +0000213
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000214 if let Some(kernel) = &config.kernel {
Andrew Walbran02b8ec02021-06-22 13:07:02 +0000215 command.arg(add_preserved_fd(&mut preserved_fds, kernel));
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000216 }
Andrew Walbran3a5a9212021-05-04 17:09:08 +0000217
Andrew Walbran02b8ec02021-06-22 13:07:02 +0000218 debug!("Preserving FDs {:?}", preserved_fds);
219 command.preserved_fds(preserved_fds);
Andrew Walbran3a5a9212021-05-04 17:09:08 +0000220
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000221 info!("Running {:?}", command);
Andrew Walbran3a5a9212021-05-04 17:09:08 +0000222 let result = SharedChild::spawn(&mut command)?;
223 Ok(result)
224}
225
226/// Ensure that the configuration has a valid combination of fields set, or return an error if not.
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000227fn validate_config(config: &CrosvmConfig) -> Result<(), Error> {
Andrew Walbran3a5a9212021-05-04 17:09:08 +0000228 if config.bootloader.is_none() && config.kernel.is_none() {
229 bail!("VM must have either a bootloader or a kernel image.");
230 }
231 if config.bootloader.is_some() && (config.kernel.is_some() || config.initrd.is_some()) {
232 bail!("Can't have both bootloader and kernel/initrd image.");
233 }
234 Ok(())
235}
236
Andrew Walbran02b8ec02021-06-22 13:07:02 +0000237/// Adds the file descriptor for `file` to `preserved_fds`, and returns a string of the form
238/// "/proc/self/fd/N" where N is the file descriptor.
239fn add_preserved_fd(preserved_fds: &mut Vec<RawFd>, file: &File) -> String {
Andrew Walbran3a5a9212021-05-04 17:09:08 +0000240 let fd = file.as_raw_fd();
Andrew Walbran02b8ec02021-06-22 13:07:02 +0000241 preserved_fds.push(fd);
Andrew Walbran3a5a9212021-05-04 17:09:08 +0000242 format!("/proc/self/fd/{}", fd)
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000243}