blob: 60e063c26f6274cf7d0215ed1863dfe9848b7308 [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::config::VmConfig;
19use crate::Cid;
20use anyhow::Error;
Andrew Walbrandae07162021-03-12 17:05:20 +000021use log::{error, info};
22use shared_child::SharedChild;
Andrew Walbrana89fc132021-03-17 17:08:36 +000023use std::fs::File;
Andrew Walbrandae07162021-03-12 17:05:20 +000024use std::process::Command;
25use std::sync::atomic::{AtomicBool, Ordering};
26use std::sync::Arc;
27use std::thread;
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000028
29const 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)]
33pub struct VmInstance {
34 /// The crosvm child process.
Andrew Walbrandae07162021-03-12 17:05:20 +000035 child: SharedChild,
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000036 /// The CID assigned to the VM for vsock communication.
37 pub cid: Cid,
Andrew Walbranf6a1eb92021-04-01 11:16:02 +000038 /// The UID of the process which requested the VM.
39 pub requester_uid: u32,
40 /// The SID of the process which requested the VM.
Andrew Walbran02034492021-04-13 15:05:07 +000041 pub requester_sid: String,
Andrew Walbranf6a1eb92021-04-01 11:16:02 +000042 /// 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.
Andrew Walbran02034492021-04-13 15:05:07 +000044 pub requester_debug_pid: i32,
Andrew Walbrandae07162021-03-12 17:05:20 +000045 /// Whether the VM is still running.
46 running: AtomicBool,
47 /// Callbacks to clients of the VM.
48 pub callbacks: VirtualMachineCallbacks,
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000049}
50
51impl VmInstance {
52 /// Create a new `VmInstance` for the given process.
Andrew Walbranf6a1eb92021-04-01 11:16:02 +000053 fn new(
Andrew Walbrandae07162021-03-12 17:05:20 +000054 child: SharedChild,
Andrew Walbranf6a1eb92021-04-01 11:16:02 +000055 cid: Cid,
56 requester_uid: u32,
Andrew Walbran02034492021-04-13 15:05:07 +000057 requester_sid: String,
58 requester_debug_pid: i32,
Andrew Walbranf6a1eb92021-04-01 11:16:02 +000059 ) -> VmInstance {
Andrew Walbrandae07162021-03-12 17:05:20 +000060 VmInstance {
61 child,
62 cid,
63 requester_uid,
64 requester_sid,
Andrew Walbran02034492021-04-13 15:05:07 +000065 requester_debug_pid,
Andrew Walbrandae07162021-03-12 17:05:20 +000066 running: AtomicBool::new(true),
67 callbacks: Default::default(),
68 }
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000069 }
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 Walbranf6a1eb92021-04-01 11:16:02 +000073 pub fn start(
74 config: &VmConfig,
75 cid: Cid,
76 log_fd: Option<File>,
77 requester_uid: u32,
Andrew Walbran02034492021-04-13 15:05:07 +000078 requester_sid: String,
79 requester_debug_pid: i32,
Andrew Walbrandae07162021-03-12 17:05:20 +000080 ) -> Result<Arc<VmInstance>, Error> {
Andrew Walbrana89fc132021-03-17 17:08:36 +000081 let child = run_vm(config, cid, log_fd)?;
Andrew Walbran02034492021-04-13 15:05:07 +000082 let instance = Arc::new(VmInstance::new(
83 child,
84 cid,
85 requester_uid,
86 requester_sid,
87 requester_debug_pid,
88 ));
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000089
Andrew Walbrandae07162021-03-12 17:05:20 +000090 let instance_clone = instance.clone();
91 thread::spawn(move || {
92 instance_clone.monitor();
93 });
94
95 Ok(instance)
96 }
97
98 /// Wait for the crosvm child process to finish, then mark the VM as no longer running and call
99 /// any callbacks.
100 fn monitor(&self) {
101 match self.child.wait() {
102 Err(e) => error!("Error waiting for crosvm instance to die: {}", e),
103 Ok(status) => info!("crosvm exited with status {}", status),
104 }
105 self.running.store(false, Ordering::Release);
106 self.callbacks.callback_on_died(self.cid);
107 }
108
109 /// Return whether `crosvm` is still running the VM.
110 pub fn running(&self) -> bool {
111 self.running.load(Ordering::Acquire)
112 }
113
114 /// Kill the crosvm instance.
115 pub fn kill(&self) {
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000116 // TODO: Talk to crosvm to shutdown cleanly.
117 if let Err(e) = self.child.kill() {
118 error!("Error killing crosvm instance: {}", e);
119 }
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000120 }
121}
122
123/// Start an instance of `crosvm` to manage a new VM.
Andrew Walbrandae07162021-03-12 17:05:20 +0000124fn run_vm(config: &VmConfig, cid: Cid, log_fd: Option<File>) -> Result<SharedChild, Error> {
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000125 config.validate()?;
126
127 let mut command = Command::new(CROSVM_PATH);
128 // TODO(qwandor): Remove --disable-sandbox.
129 command.arg("run").arg("--disable-sandbox").arg("--cid").arg(cid.to_string());
Andrew Walbrana89fc132021-03-17 17:08:36 +0000130 if let Some(log_fd) = log_fd {
131 command.stdout(log_fd);
132 } else {
133 // Ignore console output.
134 command.arg("--serial=type=sink");
135 }
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000136 if let Some(bootloader) = &config.bootloader {
137 command.arg("--bios").arg(bootloader);
138 }
139 if let Some(initrd) = &config.initrd {
140 command.arg("--initrd").arg(initrd);
141 }
142 if let Some(params) = &config.params {
143 command.arg("--params").arg(params);
144 }
145 for disk in &config.disks {
146 command.arg(if disk.writable { "--rwdisk" } else { "--disk" }).arg(&disk.image);
147 }
148 if let Some(kernel) = &config.kernel {
149 command.arg(kernel);
150 }
151 info!("Running {:?}", command);
Andrew Walbrandae07162021-03-12 17:05:20 +0000152 Ok(SharedChild::spawn(&mut command)?)
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000153}