blob: bef998259f35f12abaa1c71757610c62a70477fd [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 Walbranf6a1eb92021-04-01 11:16:02 +000033 /// The UID of the process which requested the VM.
34 pub requester_uid: u32,
35 /// The SID of the process which requested the VM.
36 pub requester_sid: Option<String>,
37 /// The PID of the process which requested the VM. Note that this process may no longer exist
38 /// and the PID may have been reused for a different process, so this should not be trusted.
39 pub requester_pid: i32,
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000040}
41
42impl VmInstance {
43 /// Create a new `VmInstance` for the given process.
Andrew Walbranf6a1eb92021-04-01 11:16:02 +000044 fn new(
45 child: Child,
46 cid: Cid,
47 requester_uid: u32,
48 requester_sid: Option<String>,
49 requester_pid: i32,
50 ) -> VmInstance {
51 VmInstance { child, cid, requester_uid, requester_sid, requester_pid }
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000052 }
53
54 /// Start an instance of `crosvm` to manage a new VM. The `crosvm` instance will be killed when
55 /// the `VmInstance` is dropped.
Andrew Walbranf6a1eb92021-04-01 11:16:02 +000056 pub fn start(
57 config: &VmConfig,
58 cid: Cid,
59 log_fd: Option<File>,
60 requester_uid: u32,
61 requester_sid: Option<String>,
62 requester_pid: i32,
63 ) -> Result<VmInstance, Error> {
Andrew Walbrana89fc132021-03-17 17:08:36 +000064 let child = run_vm(config, cid, log_fd)?;
Andrew Walbranf6a1eb92021-04-01 11:16:02 +000065 Ok(VmInstance::new(child, cid, requester_uid, requester_sid, requester_pid))
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000066 }
67}
68
69impl Drop for VmInstance {
70 fn drop(&mut self) {
71 debug!("Dropping {:?}", self);
72 // TODO: Talk to crosvm to shutdown cleanly.
73 if let Err(e) = self.child.kill() {
74 error!("Error killing crosvm instance: {}", e);
75 }
76 // We need to wait on the process after killing it to avoid zombies.
77 match self.child.wait() {
78 Err(e) => error!("Error waiting for crosvm instance to die: {}", e),
79 Ok(status) => info!("Crosvm exited with status {}", status),
80 }
81 }
82}
83
84/// Start an instance of `crosvm` to manage a new VM.
Andrew Walbrana89fc132021-03-17 17:08:36 +000085fn run_vm(config: &VmConfig, cid: Cid, log_fd: Option<File>) -> Result<Child, Error> {
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000086 config.validate()?;
87
88 let mut command = Command::new(CROSVM_PATH);
89 // TODO(qwandor): Remove --disable-sandbox.
90 command.arg("run").arg("--disable-sandbox").arg("--cid").arg(cid.to_string());
Andrew Walbrana89fc132021-03-17 17:08:36 +000091 if let Some(log_fd) = log_fd {
92 command.stdout(log_fd);
93 } else {
94 // Ignore console output.
95 command.arg("--serial=type=sink");
96 }
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000097 if let Some(bootloader) = &config.bootloader {
98 command.arg("--bios").arg(bootloader);
99 }
100 if let Some(initrd) = &config.initrd {
101 command.arg("--initrd").arg(initrd);
102 }
103 if let Some(params) = &config.params {
104 command.arg("--params").arg(params);
105 }
106 for disk in &config.disks {
107 command.arg(if disk.writable { "--rwdisk" } else { "--disk" }).arg(&disk.image);
108 }
109 if let Some(kernel) = &config.kernel {
110 command.arg(kernel);
111 }
112 info!("Running {:?}", command);
113 // TODO: Monitor child process, and remove from VM map if it dies.
114 Ok(command.spawn()?)
115}