blob: 057b79199881a1f4aa5ee19415d31de94749c10f [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};
21use std::process::{Child, Command};
22
23const CROSVM_PATH: &str = "/apex/com.android.virt/bin/crosvm";
24
25/// Information about a particular instance of a VM which is running.
26#[derive(Debug)]
27pub struct VmInstance {
28 /// The crosvm child process.
29 child: Child,
30 /// The CID assigned to the VM for vsock communication.
31 pub cid: Cid,
32}
33
34impl VmInstance {
35 /// Create a new `VmInstance` for the given process.
36 fn new(child: Child, cid: Cid) -> VmInstance {
37 VmInstance { child, cid }
38 }
39
40 /// Start an instance of `crosvm` to manage a new VM. The `crosvm` instance will be killed when
41 /// the `VmInstance` is dropped.
42 pub fn start(config: &VmConfig, cid: Cid) -> Result<VmInstance, Error> {
43 let child = run_vm(config, cid)?;
44 Ok(VmInstance::new(child, cid))
45 }
46}
47
48impl Drop for VmInstance {
49 fn drop(&mut self) {
50 debug!("Dropping {:?}", self);
51 // TODO: Talk to crosvm to shutdown cleanly.
52 if let Err(e) = self.child.kill() {
53 error!("Error killing crosvm instance: {}", e);
54 }
55 // We need to wait on the process after killing it to avoid zombies.
56 match self.child.wait() {
57 Err(e) => error!("Error waiting for crosvm instance to die: {}", e),
58 Ok(status) => info!("Crosvm exited with status {}", status),
59 }
60 }
61}
62
63/// Start an instance of `crosvm` to manage a new VM.
64fn run_vm(config: &VmConfig, cid: Cid) -> Result<Child, Error> {
65 config.validate()?;
66
67 let mut command = Command::new(CROSVM_PATH);
68 // TODO(qwandor): Remove --disable-sandbox.
69 command.arg("run").arg("--disable-sandbox").arg("--cid").arg(cid.to_string());
70 // TODO(jiyong): Don't redirect console to the host syslog
71 command.arg("--serial=type=syslog");
72 if let Some(bootloader) = &config.bootloader {
73 command.arg("--bios").arg(bootloader);
74 }
75 if let Some(initrd) = &config.initrd {
76 command.arg("--initrd").arg(initrd);
77 }
78 if let Some(params) = &config.params {
79 command.arg("--params").arg(params);
80 }
81 for disk in &config.disks {
82 command.arg(if disk.writable { "--rwdisk" } else { "--disk" }).arg(&disk.image);
83 }
84 if let Some(kernel) = &config.kernel {
85 command.arg(kernel);
86 }
87 info!("Running {:?}", command);
88 // TODO: Monitor child process, and remove from VM map if it dies.
89 Ok(command.spawn()?)
90}