blob: 6a0fc1597d9db27eab6bb58287b7592d9f14544e [file] [log] [blame]
Andrew Walbranf395b822021-05-05 10:38:59 +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//! Command to run a VM.
16
Jiyong Park48b354d2021-07-15 15:04:38 +090017use crate::create_partition::command_create_partition;
Andrew Walbranf395b822021-05-05 10:38:59 +000018use crate::sync::AtomicFlag;
Jooyung Han21e9b922021-06-26 04:14:16 +090019use android_system_virtualizationservice::aidl::android::system::virtualizationservice::{
Andrew Walbranc92d35f2022-01-12 12:45:19 +000020 DeathReason::DeathReason, IVirtualMachine::IVirtualMachine,
21 IVirtualMachineCallback::BnVirtualMachineCallback,
Jiyong Parkc2a49cc2021-10-15 00:02:12 +090022 IVirtualMachineCallback::IVirtualMachineCallback,
23 IVirtualizationService::IVirtualizationService, PartitionType::PartitionType,
24 VirtualMachineAppConfig::DebugLevel::DebugLevel,
25 VirtualMachineAppConfig::VirtualMachineAppConfig, VirtualMachineConfig::VirtualMachineConfig,
Andrew Walbranf8d94112021-09-07 11:45:36 +000026 VirtualMachineState::VirtualMachineState,
Jooyung Han21e9b922021-06-26 04:14:16 +090027};
Andrew Walbranf6bf6862021-05-21 12:41:13 +000028use android_system_virtualizationservice::binder::{
Andrew Walbranf395b822021-05-05 10:38:59 +000029 BinderFeatures, DeathRecipient, IBinder, ParcelFileDescriptor, Strong,
30};
Andrew Walbranf6bf6862021-05-21 12:41:13 +000031use android_system_virtualizationservice::binder::{Interface, Result as BinderResult};
Inseob Kima5a262f2021-11-17 19:41:03 +090032use anyhow::{bail, Context, Error};
33use microdroid_payload_config::VmPayloadConfig;
Andrew Walbranf395b822021-05-05 10:38:59 +000034use std::fs::File;
Jiyong Park8611a6c2021-07-09 18:17:44 +090035use std::io::{self, BufRead, BufReader};
Andrew Walbranf395b822021-05-05 10:38:59 +000036use std::os::unix::io::{AsRawFd, FromRawFd};
Inseob Kima5a262f2021-11-17 19:41:03 +090037use std::path::{Path, PathBuf};
Jiyong Park48b354d2021-07-15 15:04:38 +090038use vmconfig::{open_parcel_file, VmConfig};
Inseob Kima5a262f2021-11-17 19:41:03 +090039use zip::ZipArchive;
Andrew Walbranf395b822021-05-05 10:38:59 +000040
Jooyung Han21e9b922021-06-26 04:14:16 +090041/// Run a VM from the given APK, idsig, and config.
Jiyong Park48b354d2021-07-15 15:04:38 +090042#[allow(clippy::too_many_arguments)]
Jooyung Han21e9b922021-06-26 04:14:16 +090043pub fn command_run_app(
44 service: Strong<dyn IVirtualizationService>,
45 apk: &Path,
46 idsig: &Path,
Jiyong Park48b354d2021-07-15 15:04:38 +090047 instance: &Path,
Jooyung Han21e9b922021-06-26 04:14:16 +090048 config_path: &str,
49 daemonize: bool,
Jiyong Parkb8182bb2021-10-26 22:53:08 +090050 console_path: Option<&Path>,
Jooyung Han21e9b922021-06-26 04:14:16 +090051 log_path: Option<&Path>,
Jiyong Parkc2a49cc2021-10-15 00:02:12 +090052 debug_level: DebugLevel,
Andrew Walbran3994f002022-01-27 17:33:45 +000053 protected: bool,
Jiyong Parkd63cfff2021-09-27 20:10:17 +090054 mem: Option<u32>,
Jiyong Park032615f2022-01-10 13:55:34 +090055 cpus: Option<u32>,
56 cpu_affinity: Option<String>,
Inseob Kima5a262f2021-11-17 19:41:03 +090057 extra_idsigs: &[PathBuf],
Jooyung Han21e9b922021-06-26 04:14:16 +090058) -> Result<(), Error> {
Inseob Kima5a262f2021-11-17 19:41:03 +090059 let extra_apks = parse_extra_apk_list(apk, config_path)?;
60 if extra_apks.len() != extra_idsigs.len() {
61 bail!(
62 "Found {} extra apks, but there are {} extra idsigs",
63 extra_apks.len(),
64 extra_idsigs.len()
65 )
66 }
67
68 for i in 0..extra_apks.len() {
69 let extra_apk_fd = ParcelFileDescriptor::new(File::open(&extra_apks[i])?);
70 let extra_idsig_fd = ParcelFileDescriptor::new(File::create(&extra_idsigs[i])?);
71 service.createOrUpdateIdsigFile(&extra_apk_fd, &extra_idsig_fd)?;
72 }
73
Jooyung Han21e9b922021-06-26 04:14:16 +090074 let apk_file = File::open(apk).context("Failed to open APK file")?;
Jiyong Park0a248432021-08-20 23:32:39 +090075 let idsig_file = File::create(idsig).context("Failed to create idsig file")?;
76
77 let apk_fd = ParcelFileDescriptor::new(apk_file);
78 let idsig_fd = ParcelFileDescriptor::new(idsig_file);
79 service.createOrUpdateIdsigFile(&apk_fd, &idsig_fd)?;
80
Jooyung Han21e9b922021-06-26 04:14:16 +090081 let idsig_file = File::open(idsig).context("Failed to open idsig file")?;
Jiyong Park0a248432021-08-20 23:32:39 +090082 let idsig_fd = ParcelFileDescriptor::new(idsig_file);
Jiyong Park48b354d2021-07-15 15:04:38 +090083
84 if !instance.exists() {
85 const INSTANCE_FILE_SIZE: u64 = 10 * 1024 * 1024;
Jiyong Park9dd389e2021-08-23 20:42:59 +090086 command_create_partition(
87 service.clone(),
88 instance,
89 INSTANCE_FILE_SIZE,
90 PartitionType::ANDROID_VM_INSTANCE,
91 )?;
Jiyong Park48b354d2021-07-15 15:04:38 +090092 }
93
Inseob Kima5a262f2021-11-17 19:41:03 +090094 let extra_idsig_files: Result<Vec<File>, _> = extra_idsigs.iter().map(File::open).collect();
95 let extra_idsig_fds = extra_idsig_files?.into_iter().map(ParcelFileDescriptor::new).collect();
96
Jooyung Han21e9b922021-06-26 04:14:16 +090097 let config = VirtualMachineConfig::AppConfig(VirtualMachineAppConfig {
Jiyong Park0a248432021-08-20 23:32:39 +090098 apk: apk_fd.into(),
99 idsig: idsig_fd.into(),
Inseob Kima5a262f2021-11-17 19:41:03 +0900100 extraIdsigs: extra_idsig_fds,
Jiyong Park48b354d2021-07-15 15:04:38 +0900101 instanceImage: open_parcel_file(instance, true /* writable */)?.into(),
Jooyung Han21e9b922021-06-26 04:14:16 +0900102 configPath: config_path.to_owned(),
Jiyong Parkc2a49cc2021-10-15 00:02:12 +0900103 debugLevel: debug_level,
Andrew Walbran3994f002022-01-27 17:33:45 +0000104 protectedVm: protected,
Jiyong Parkd63cfff2021-09-27 20:10:17 +0900105 memoryMib: mem.unwrap_or(0) as i32, // 0 means use the VM default
Jiyong Park032615f2022-01-10 13:55:34 +0900106 numCpus: cpus.unwrap_or(1) as i32,
107 cpuAffinity: cpu_affinity,
Jooyung Han21e9b922021-06-26 04:14:16 +0900108 });
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900109 run(
110 service,
111 &config,
112 &format!("{:?}!{:?}", apk, config_path),
113 daemonize,
114 console_path,
115 log_path,
116 )
Jooyung Han21e9b922021-06-26 04:14:16 +0900117}
118
Andrew Walbranf395b822021-05-05 10:38:59 +0000119/// Run a VM from the given configuration file.
120pub fn command_run(
Andrew Walbran17de24f2021-05-27 13:27:30 +0000121 service: Strong<dyn IVirtualizationService>,
Andrew Walbranf395b822021-05-05 10:38:59 +0000122 config_path: &Path,
123 daemonize: bool,
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900124 console_path: Option<&Path>,
Jiyong Parkd63cfff2021-09-27 20:10:17 +0900125 mem: Option<u32>,
Jiyong Park032615f2022-01-10 13:55:34 +0900126 cpus: Option<u32>,
127 cpu_affinity: Option<String>,
Andrew Walbranf395b822021-05-05 10:38:59 +0000128) -> Result<(), Error> {
Andrew Walbran3a5a9212021-05-04 17:09:08 +0000129 let config_file = File::open(config_path).context("Failed to open config file")?;
Jiyong Parkd63cfff2021-09-27 20:10:17 +0900130 let mut config =
Andrew Walbran3a5a9212021-05-04 17:09:08 +0000131 VmConfig::load(&config_file).context("Failed to parse config file")?.to_parcelable()?;
Jiyong Parkd63cfff2021-09-27 20:10:17 +0900132 if let Some(mem) = mem {
133 config.memoryMib = mem as i32;
134 }
Jiyong Park032615f2022-01-10 13:55:34 +0900135 if let Some(cpus) = cpus {
136 config.numCpus = cpus as i32;
137 }
138 config.cpuAffinity = cpu_affinity;
Jooyung Han21e9b922021-06-26 04:14:16 +0900139 run(
140 service,
141 &VirtualMachineConfig::RawConfig(config),
142 &format!("{:?}", config_path),
143 daemonize,
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900144 console_path,
145 None,
Jooyung Han21e9b922021-06-26 04:14:16 +0900146 )
147}
148
Andrew Walbranf8d94112021-09-07 11:45:36 +0000149fn state_to_str(vm_state: VirtualMachineState) -> &'static str {
150 match vm_state {
151 VirtualMachineState::NOT_STARTED => "NOT_STARTED",
152 VirtualMachineState::STARTING => "STARTING",
153 VirtualMachineState::STARTED => "STARTED",
154 VirtualMachineState::READY => "READY",
155 VirtualMachineState::FINISHED => "FINISHED",
156 VirtualMachineState::DEAD => "DEAD",
157 _ => "(invalid state)",
158 }
159}
160
Jooyung Han21e9b922021-06-26 04:14:16 +0900161fn run(
162 service: Strong<dyn IVirtualizationService>,
163 config: &VirtualMachineConfig,
164 config_path: &str,
165 daemonize: bool,
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900166 console_path: Option<&Path>,
Jooyung Han21e9b922021-06-26 04:14:16 +0900167 log_path: Option<&Path>,
168) -> Result<(), Error> {
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900169 let console = if let Some(console_path) = console_path {
170 Some(ParcelFileDescriptor::new(
171 File::create(console_path)
172 .with_context(|| format!("Failed to open console file {:?}", console_path))?,
173 ))
174 } else if daemonize {
175 None
176 } else {
177 Some(ParcelFileDescriptor::new(duplicate_stdout()?))
178 };
179 let log = if let Some(log_path) = log_path {
Andrew Walbranbe429242021-06-28 12:22:54 +0000180 Some(ParcelFileDescriptor::new(
181 File::create(log_path)
182 .with_context(|| format!("Failed to open log file {:?}", log_path))?,
183 ))
184 } else if daemonize {
185 None
186 } else {
187 Some(ParcelFileDescriptor::new(duplicate_stdout()?))
188 };
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900189
190 let vm =
191 service.createVm(config, console.as_ref(), log.as_ref()).context("Failed to create VM")?;
Andrew Walbranf395b822021-05-05 10:38:59 +0000192
193 let cid = vm.getCid().context("Failed to get CID")?;
Andrew Walbranf8d94112021-09-07 11:45:36 +0000194 println!(
195 "Created VM from {} with CID {}, state is {}.",
196 config_path,
197 cid,
198 state_to_str(vm.getState()?)
199 );
200 vm.start()?;
201 println!("Started VM, state now {}.", state_to_str(vm.getState()?));
Andrew Walbranf395b822021-05-05 10:38:59 +0000202
203 if daemonize {
Andrew Walbranf6bf6862021-05-21 12:41:13 +0000204 // Pass the VM reference back to VirtualizationService and have it hold it in the
205 // background.
Andrew Walbran17de24f2021-05-27 13:27:30 +0000206 service.debugHoldVmRef(&vm).context("Failed to pass VM to VirtualizationService")
Andrew Walbranf395b822021-05-05 10:38:59 +0000207 } else {
Andrew Walbranf6bf6862021-05-21 12:41:13 +0000208 // Wait until the VM or VirtualizationService dies. If we just returned immediately then the
Andrew Walbranf395b822021-05-05 10:38:59 +0000209 // IVirtualMachine Binder object would be dropped and the VM would be killed.
210 wait_for_vm(vm)
211 }
212}
213
Andrew Walbranf6bf6862021-05-21 12:41:13 +0000214/// Wait until the given VM or the VirtualizationService itself dies.
Andrew Walbranf395b822021-05-05 10:38:59 +0000215fn wait_for_vm(vm: Strong<dyn IVirtualMachine>) -> Result<(), Error> {
216 let dead = AtomicFlag::default();
217 let callback = BnVirtualMachineCallback::new_binder(
218 VirtualMachineCallback { dead: dead.clone() },
219 BinderFeatures::default(),
220 );
221 vm.registerCallback(&callback)?;
222 let death_recipient = wait_for_death(&mut vm.as_binder(), dead.clone())?;
223 dead.wait();
224 // Ensure that death_recipient isn't dropped before we wait on the flag, as it is removed
225 // from the Binder when it's dropped.
226 drop(death_recipient);
227 Ok(())
228}
229
230/// Raise the given flag when the given Binder object dies.
231///
232/// If the returned DeathRecipient is dropped then this will no longer do anything.
233fn wait_for_death(binder: &mut impl IBinder, dead: AtomicFlag) -> Result<DeathRecipient, Error> {
234 let mut death_recipient = DeathRecipient::new(move || {
Jiyong Park8611a6c2021-07-09 18:17:44 +0900235 eprintln!("VirtualizationService unexpectedly died");
Andrew Walbranf395b822021-05-05 10:38:59 +0000236 dead.raise();
237 });
238 binder.link_to_death(&mut death_recipient)?;
239 Ok(death_recipient)
240}
241
Inseob Kima5a262f2021-11-17 19:41:03 +0900242fn parse_extra_apk_list(apk: &Path, config_path: &str) -> Result<Vec<String>, Error> {
243 let mut archive = ZipArchive::new(File::open(apk)?)?;
244 let config_file = archive.by_name(config_path)?;
245 let config: VmPayloadConfig = serde_json::from_reader(config_file)?;
246 Ok(config.extra_apks.into_iter().map(|x| x.path).collect())
247}
248
Andrew Walbranf395b822021-05-05 10:38:59 +0000249#[derive(Debug)]
250struct VirtualMachineCallback {
251 dead: AtomicFlag,
252}
253
254impl Interface for VirtualMachineCallback {}
255
256impl IVirtualMachineCallback for VirtualMachineCallback {
Inseob Kim7f61fe72021-08-20 20:50:47 +0900257 fn onPayloadStarted(
258 &self,
259 _cid: i32,
260 stream: Option<&ParcelFileDescriptor>,
261 ) -> BinderResult<()> {
262 // Show the output of the payload
263 if let Some(stream) = stream {
264 let mut reader = BufReader::new(stream.as_ref());
265 loop {
266 let mut s = String::new();
267 match reader.read_line(&mut s) {
268 Ok(0) => break,
269 Ok(_) => print!("{}", s),
270 Err(e) => eprintln!("error reading from virtual machine: {}", e),
271 };
272 }
Jiyong Park8611a6c2021-07-09 18:17:44 +0900273 }
274 Ok(())
275 }
276
Inseob Kim14cb8692021-08-31 21:50:39 +0900277 fn onPayloadReady(&self, _cid: i32) -> BinderResult<()> {
Inseob Kim8dbc3222021-09-01 21:50:23 +0900278 eprintln!("payload is ready");
Inseob Kim14cb8692021-08-31 21:50:39 +0900279 Ok(())
280 }
281
Inseob Kim8dbc3222021-09-01 21:50:23 +0900282 fn onPayloadFinished(&self, _cid: i32, exit_code: i32) -> BinderResult<()> {
283 eprintln!("payload finished with exit code {}", exit_code);
Inseob Kim2444af92021-08-31 01:22:50 +0900284 Ok(())
285 }
286
Jooyung Handd0a1732021-11-23 15:26:20 +0900287 fn onError(&self, _cid: i32, error_code: i32, message: &str) -> BinderResult<()> {
288 eprintln!("VM encountered an error: code={}, message={}", error_code, message);
289 Ok(())
290 }
291
Andrew Walbranc92d35f2022-01-12 12:45:19 +0000292 fn onDied(&self, _cid: i32, reason: DeathReason) -> BinderResult<()> {
Andrew Walbranf395b822021-05-05 10:38:59 +0000293 self.dead.raise();
Andrew Walbranc92d35f2022-01-12 12:45:19 +0000294
295 match reason {
Andrew Walbrand15c5632022-02-03 13:38:31 +0000296 DeathReason::INFRASTRUCTURE_ERROR => println!("Error waiting for VM to finish."),
Andrew Walbranc92d35f2022-01-12 12:45:19 +0000297 DeathReason::KILLED => println!("VM was killed."),
298 DeathReason::UNKNOWN => println!("VM died for an unknown reason."),
Andrew Walbrand15c5632022-02-03 13:38:31 +0000299 DeathReason::SHUTDOWN => println!("VM shutdown cleanly."),
300 DeathReason::ERROR => println!("Error starting VM."),
301 DeathReason::REBOOT => println!("VM tried to reboot, possibly due to a kernel panic."),
302 DeathReason::CRASH => println!("VM crashed."),
Andrew Walbranc92d35f2022-01-12 12:45:19 +0000303 _ => println!("VM died for an unrecognised reason."),
304 }
Andrew Walbranf395b822021-05-05 10:38:59 +0000305 Ok(())
306 }
307}
308
309/// Safely duplicate the standard output file descriptor.
310fn duplicate_stdout() -> io::Result<File> {
311 let stdout_fd = io::stdout().as_raw_fd();
312 // Safe because this just duplicates a file descriptor which we know to be valid, and we check
313 // for an error.
314 let dup_fd = unsafe { libc::dup(stdout_fd) };
315 if dup_fd < 0 {
316 Err(io::Error::last_os_error())
317 } else {
318 // Safe because we have just duplicated the file descriptor so we own it, and `from_raw_fd`
319 // takes ownership of it.
320 Ok(unsafe { File::from_raw_fd(dup_fd) })
321 }
322}