blob: 814a1a7d534ecdeac9fc9b6344fe14ca69a881a4 [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
17use crate::config::VmConfig;
18use crate::Cid;
19use anyhow::Error;
20use log::{debug, error, info};
Andrew Walbrana89fc132021-03-17 17:08:36 +000021use std::fs::File;
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000022use std::process::{Child, Command};
23
24const 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)]
28pub 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 Walbran320b5602021-03-04 16:11:12 +000033 /// 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 Walbrand6dce6f2021-03-05 16:39:08 +000036}
37
38impl VmInstance {
39 /// Create a new `VmInstance` for the given process.
Andrew Walbran320b5602021-03-04 16:11:12 +000040 fn new(child: Child, cid: Cid, config_path: &str) -> VmInstance {
41 VmInstance { child, cid, config_path: config_path.to_owned() }
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000042 }
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 Walbrana89fc132021-03-17 17:08:36 +000046 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 Walbran320b5602021-03-04 16:11:12 +000053 Ok(VmInstance::new(child, cid, config_path))
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000054 }
55}
56
57impl 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 Walbrana89fc132021-03-17 17:08:36 +000073fn run_vm(config: &VmConfig, cid: Cid, log_fd: Option<File>) -> Result<Child, Error> {
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000074 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 Walbrana89fc132021-03-17 17:08:36 +000079 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 Walbrand6dce6f2021-03-05 16:39:08 +000085 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}