blob: c8653573e6d9b1a23fedef64c2e4de900e148f98 [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,
33}
34
35impl VmInstance {
36 /// Create a new `VmInstance` for the given process.
Andrew Walbran06b5f5c2021-03-31 12:34:13 +000037 fn new(child: Child, cid: Cid) -> VmInstance {
38 VmInstance { child, cid }
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000039 }
40
41 /// Start an instance of `crosvm` to manage a new VM. The `crosvm` instance will be killed when
42 /// the `VmInstance` is dropped.
Andrew Walbran06b5f5c2021-03-31 12:34:13 +000043 pub fn start(config: &VmConfig, cid: Cid, log_fd: Option<File>) -> Result<VmInstance, Error> {
Andrew Walbrana89fc132021-03-17 17:08:36 +000044 let child = run_vm(config, cid, log_fd)?;
Andrew Walbran06b5f5c2021-03-31 12:34:13 +000045 Ok(VmInstance::new(child, cid))
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000046 }
47}
48
49impl Drop for VmInstance {
50 fn drop(&mut self) {
51 debug!("Dropping {:?}", self);
52 // TODO: Talk to crosvm to shutdown cleanly.
53 if let Err(e) = self.child.kill() {
54 error!("Error killing crosvm instance: {}", e);
55 }
56 // We need to wait on the process after killing it to avoid zombies.
57 match self.child.wait() {
58 Err(e) => error!("Error waiting for crosvm instance to die: {}", e),
59 Ok(status) => info!("Crosvm exited with status {}", status),
60 }
61 }
62}
63
64/// Start an instance of `crosvm` to manage a new VM.
Andrew Walbrana89fc132021-03-17 17:08:36 +000065fn run_vm(config: &VmConfig, cid: Cid, log_fd: Option<File>) -> Result<Child, Error> {
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000066 config.validate()?;
67
68 let mut command = Command::new(CROSVM_PATH);
69 // TODO(qwandor): Remove --disable-sandbox.
70 command.arg("run").arg("--disable-sandbox").arg("--cid").arg(cid.to_string());
Andrew Walbrana89fc132021-03-17 17:08:36 +000071 if let Some(log_fd) = log_fd {
72 command.stdout(log_fd);
73 } else {
74 // Ignore console output.
75 command.arg("--serial=type=sink");
76 }
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000077 if let Some(bootloader) = &config.bootloader {
78 command.arg("--bios").arg(bootloader);
79 }
80 if let Some(initrd) = &config.initrd {
81 command.arg("--initrd").arg(initrd);
82 }
83 if let Some(params) = &config.params {
84 command.arg("--params").arg(params);
85 }
86 for disk in &config.disks {
87 command.arg(if disk.writable { "--rwdisk" } else { "--disk" }).arg(&disk.image);
88 }
89 if let Some(kernel) = &config.kernel {
90 command.arg(kernel);
91 }
92 info!("Running {:?}", command);
93 // TODO: Monitor child process, and remove from VM map if it dies.
94 Ok(command.spawn()?)
95}