blob: 4ae1fcd593e715f50934376a2ffc586dddcc41c4 [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,
Andrew Walbran320b5602021-03-04 16:11:12 +000032 /// The filename of the config file that was used to start the VM. This may have changed since
33 /// it was read so it shouldn't be trusted; it is only stored for debugging purposes.
34 pub config_path: String,
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000035}
36
37impl VmInstance {
38 /// Create a new `VmInstance` for the given process.
Andrew Walbran320b5602021-03-04 16:11:12 +000039 fn new(child: Child, cid: Cid, config_path: &str) -> VmInstance {
40 VmInstance { child, cid, config_path: config_path.to_owned() }
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000041 }
42
43 /// Start an instance of `crosvm` to manage a new VM. The `crosvm` instance will be killed when
44 /// the `VmInstance` is dropped.
Andrew Walbran320b5602021-03-04 16:11:12 +000045 pub fn start(config: &VmConfig, cid: Cid, config_path: &str) -> Result<VmInstance, Error> {
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000046 let child = run_vm(config, cid)?;
Andrew Walbran320b5602021-03-04 16:11:12 +000047 Ok(VmInstance::new(child, cid, config_path))
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000048 }
49}
50
51impl Drop for VmInstance {
52 fn drop(&mut self) {
53 debug!("Dropping {:?}", self);
54 // TODO: Talk to crosvm to shutdown cleanly.
55 if let Err(e) = self.child.kill() {
56 error!("Error killing crosvm instance: {}", e);
57 }
58 // We need to wait on the process after killing it to avoid zombies.
59 match self.child.wait() {
60 Err(e) => error!("Error waiting for crosvm instance to die: {}", e),
61 Ok(status) => info!("Crosvm exited with status {}", status),
62 }
63 }
64}
65
66/// Start an instance of `crosvm` to manage a new VM.
67fn run_vm(config: &VmConfig, cid: Cid) -> Result<Child, Error> {
68 config.validate()?;
69
70 let mut command = Command::new(CROSVM_PATH);
71 // TODO(qwandor): Remove --disable-sandbox.
72 command.arg("run").arg("--disable-sandbox").arg("--cid").arg(cid.to_string());
73 // TODO(jiyong): Don't redirect console to the host syslog
74 command.arg("--serial=type=syslog");
75 if let Some(bootloader) = &config.bootloader {
76 command.arg("--bios").arg(bootloader);
77 }
78 if let Some(initrd) = &config.initrd {
79 command.arg("--initrd").arg(initrd);
80 }
81 if let Some(params) = &config.params {
82 command.arg("--params").arg(params);
83 }
84 for disk in &config.disks {
85 command.arg(if disk.writable { "--rwdisk" } else { "--disk" }).arg(&disk.image);
86 }
87 if let Some(kernel) = &config.kernel {
88 command.arg(kernel);
89 }
90 info!("Running {:?}", command);
91 // TODO: Monitor child process, and remove from VM map if it dies.
92 Ok(command.spawn()?)
93}