blob: 2ae2c957cbda1206d0319782810cab6e33951cf3 [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 Walbran616d13f2022-05-12 18:35:55 +000029 BinderFeatures, DeathRecipient, IBinder, Interface, ParcelFileDescriptor,
30 Result as BinderResult,
Andrew Walbranf395b822021-05-05 10:38:59 +000031};
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(
Andrew Walbran616d13f2022-05-12 18:35:55 +000044 service: &dyn IVirtualizationService,
Jooyung Han21e9b922021-06-26 04:14:16 +090045 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>,
Jiyong Parkdfe16d62022-04-20 17:32:12 +090057 task_profiles: Vec<String>,
Inseob Kima5a262f2021-11-17 19:41:03 +090058 extra_idsigs: &[PathBuf],
Jooyung Han21e9b922021-06-26 04:14:16 +090059) -> Result<(), Error> {
Inseob Kima5a262f2021-11-17 19:41:03 +090060 let extra_apks = parse_extra_apk_list(apk, config_path)?;
61 if extra_apks.len() != extra_idsigs.len() {
62 bail!(
63 "Found {} extra apks, but there are {} extra idsigs",
64 extra_apks.len(),
65 extra_idsigs.len()
66 )
67 }
68
69 for i in 0..extra_apks.len() {
70 let extra_apk_fd = ParcelFileDescriptor::new(File::open(&extra_apks[i])?);
71 let extra_idsig_fd = ParcelFileDescriptor::new(File::create(&extra_idsigs[i])?);
72 service.createOrUpdateIdsigFile(&extra_apk_fd, &extra_idsig_fd)?;
73 }
74
Jooyung Han21e9b922021-06-26 04:14:16 +090075 let apk_file = File::open(apk).context("Failed to open APK file")?;
Jiyong Park0a248432021-08-20 23:32:39 +090076 let idsig_file = File::create(idsig).context("Failed to create idsig file")?;
77
78 let apk_fd = ParcelFileDescriptor::new(apk_file);
79 let idsig_fd = ParcelFileDescriptor::new(idsig_file);
80 service.createOrUpdateIdsigFile(&apk_fd, &idsig_fd)?;
81
Jooyung Han21e9b922021-06-26 04:14:16 +090082 let idsig_file = File::open(idsig).context("Failed to open idsig file")?;
Jiyong Park0a248432021-08-20 23:32:39 +090083 let idsig_fd = ParcelFileDescriptor::new(idsig_file);
Jiyong Park48b354d2021-07-15 15:04:38 +090084
85 if !instance.exists() {
86 const INSTANCE_FILE_SIZE: u64 = 10 * 1024 * 1024;
Jiyong Park9dd389e2021-08-23 20:42:59 +090087 command_create_partition(
Andrew Walbran616d13f2022-05-12 18:35:55 +000088 service,
Jiyong Park9dd389e2021-08-23 20:42:59 +090089 instance,
90 INSTANCE_FILE_SIZE,
91 PartitionType::ANDROID_VM_INSTANCE,
92 )?;
Jiyong Park48b354d2021-07-15 15:04:38 +090093 }
94
Inseob Kima5a262f2021-11-17 19:41:03 +090095 let extra_idsig_files: Result<Vec<File>, _> = extra_idsigs.iter().map(File::open).collect();
96 let extra_idsig_fds = extra_idsig_files?.into_iter().map(ParcelFileDescriptor::new).collect();
97
Jooyung Han21e9b922021-06-26 04:14:16 +090098 let config = VirtualMachineConfig::AppConfig(VirtualMachineAppConfig {
Jiyong Park0a248432021-08-20 23:32:39 +090099 apk: apk_fd.into(),
100 idsig: idsig_fd.into(),
Inseob Kima5a262f2021-11-17 19:41:03 +0900101 extraIdsigs: extra_idsig_fds,
Jiyong Park48b354d2021-07-15 15:04:38 +0900102 instanceImage: open_parcel_file(instance, true /* writable */)?.into(),
Jooyung Han21e9b922021-06-26 04:14:16 +0900103 configPath: config_path.to_owned(),
Jiyong Parkc2a49cc2021-10-15 00:02:12 +0900104 debugLevel: debug_level,
Andrew Walbran3994f002022-01-27 17:33:45 +0000105 protectedVm: protected,
Jiyong Parkd63cfff2021-09-27 20:10:17 +0900106 memoryMib: mem.unwrap_or(0) as i32, // 0 means use the VM default
Jiyong Park032615f2022-01-10 13:55:34 +0900107 numCpus: cpus.unwrap_or(1) as i32,
108 cpuAffinity: cpu_affinity,
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900109 taskProfiles: task_profiles,
Jooyung Han21e9b922021-06-26 04:14:16 +0900110 });
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900111 run(
112 service,
113 &config,
114 &format!("{:?}!{:?}", apk, config_path),
115 daemonize,
116 console_path,
117 log_path,
118 )
Jooyung Han21e9b922021-06-26 04:14:16 +0900119}
120
Andrew Walbranf395b822021-05-05 10:38:59 +0000121/// Run a VM from the given configuration file.
Jooyung Hanb7983a22022-02-22 05:21:27 +0900122#[allow(clippy::too_many_arguments)]
Andrew Walbranf395b822021-05-05 10:38:59 +0000123pub fn command_run(
Andrew Walbran616d13f2022-05-12 18:35:55 +0000124 service: &dyn IVirtualizationService,
Andrew Walbranf395b822021-05-05 10:38:59 +0000125 config_path: &Path,
126 daemonize: bool,
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900127 console_path: Option<&Path>,
Jooyung Hanb7983a22022-02-22 05:21:27 +0900128 log_path: Option<&Path>,
Jiyong Parkd63cfff2021-09-27 20:10:17 +0900129 mem: Option<u32>,
Jiyong Park032615f2022-01-10 13:55:34 +0900130 cpus: Option<u32>,
131 cpu_affinity: Option<String>,
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900132 task_profiles: Vec<String>,
Andrew Walbranf395b822021-05-05 10:38:59 +0000133) -> Result<(), Error> {
Andrew Walbran3a5a9212021-05-04 17:09:08 +0000134 let config_file = File::open(config_path).context("Failed to open config file")?;
Jiyong Parkd63cfff2021-09-27 20:10:17 +0900135 let mut config =
Andrew Walbran3a5a9212021-05-04 17:09:08 +0000136 VmConfig::load(&config_file).context("Failed to parse config file")?.to_parcelable()?;
Jiyong Parkd63cfff2021-09-27 20:10:17 +0900137 if let Some(mem) = mem {
138 config.memoryMib = mem as i32;
139 }
Jiyong Park032615f2022-01-10 13:55:34 +0900140 if let Some(cpus) = cpus {
141 config.numCpus = cpus as i32;
142 }
143 config.cpuAffinity = cpu_affinity;
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900144 config.taskProfiles = task_profiles;
Jooyung Han21e9b922021-06-26 04:14:16 +0900145 run(
146 service,
147 &VirtualMachineConfig::RawConfig(config),
148 &format!("{:?}", config_path),
149 daemonize,
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900150 console_path,
Jooyung Hanb7983a22022-02-22 05:21:27 +0900151 log_path,
Jooyung Han21e9b922021-06-26 04:14:16 +0900152 )
153}
154
Andrew Walbranf8d94112021-09-07 11:45:36 +0000155fn state_to_str(vm_state: VirtualMachineState) -> &'static str {
156 match vm_state {
157 VirtualMachineState::NOT_STARTED => "NOT_STARTED",
158 VirtualMachineState::STARTING => "STARTING",
159 VirtualMachineState::STARTED => "STARTED",
160 VirtualMachineState::READY => "READY",
161 VirtualMachineState::FINISHED => "FINISHED",
162 VirtualMachineState::DEAD => "DEAD",
163 _ => "(invalid state)",
164 }
165}
166
Jooyung Han21e9b922021-06-26 04:14:16 +0900167fn run(
Andrew Walbran616d13f2022-05-12 18:35:55 +0000168 service: &dyn IVirtualizationService,
Jooyung Han21e9b922021-06-26 04:14:16 +0900169 config: &VirtualMachineConfig,
170 config_path: &str,
171 daemonize: bool,
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900172 console_path: Option<&Path>,
Jooyung Han21e9b922021-06-26 04:14:16 +0900173 log_path: Option<&Path>,
174) -> Result<(), Error> {
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900175 let console = if let Some(console_path) = console_path {
176 Some(ParcelFileDescriptor::new(
177 File::create(console_path)
178 .with_context(|| format!("Failed to open console file {:?}", console_path))?,
179 ))
180 } else if daemonize {
181 None
182 } else {
183 Some(ParcelFileDescriptor::new(duplicate_stdout()?))
184 };
185 let log = if let Some(log_path) = log_path {
Andrew Walbranbe429242021-06-28 12:22:54 +0000186 Some(ParcelFileDescriptor::new(
187 File::create(log_path)
188 .with_context(|| format!("Failed to open log file {:?}", log_path))?,
189 ))
190 } else if daemonize {
191 None
192 } else {
193 Some(ParcelFileDescriptor::new(duplicate_stdout()?))
194 };
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900195
196 let vm =
197 service.createVm(config, console.as_ref(), log.as_ref()).context("Failed to create VM")?;
Andrew Walbranf395b822021-05-05 10:38:59 +0000198
199 let cid = vm.getCid().context("Failed to get CID")?;
Andrew Walbranf8d94112021-09-07 11:45:36 +0000200 println!(
201 "Created VM from {} with CID {}, state is {}.",
202 config_path,
203 cid,
204 state_to_str(vm.getState()?)
205 );
206 vm.start()?;
207 println!("Started VM, state now {}.", state_to_str(vm.getState()?));
Andrew Walbranf395b822021-05-05 10:38:59 +0000208
209 if daemonize {
Andrew Walbranf6bf6862021-05-21 12:41:13 +0000210 // Pass the VM reference back to VirtualizationService and have it hold it in the
211 // background.
Andrew Walbran17de24f2021-05-27 13:27:30 +0000212 service.debugHoldVmRef(&vm).context("Failed to pass VM to VirtualizationService")
Andrew Walbranf395b822021-05-05 10:38:59 +0000213 } else {
Andrew Walbranf6bf6862021-05-21 12:41:13 +0000214 // Wait until the VM or VirtualizationService dies. If we just returned immediately then the
Andrew Walbranf395b822021-05-05 10:38:59 +0000215 // IVirtualMachine Binder object would be dropped and the VM would be killed.
Andrew Walbran616d13f2022-05-12 18:35:55 +0000216 wait_for_vm(vm.as_ref())
Andrew Walbranf395b822021-05-05 10:38:59 +0000217 }
218}
219
Andrew Walbranf6bf6862021-05-21 12:41:13 +0000220/// Wait until the given VM or the VirtualizationService itself dies.
Andrew Walbran616d13f2022-05-12 18:35:55 +0000221fn wait_for_vm(vm: &dyn IVirtualMachine) -> Result<(), Error> {
Andrew Walbranf395b822021-05-05 10:38:59 +0000222 let dead = AtomicFlag::default();
223 let callback = BnVirtualMachineCallback::new_binder(
224 VirtualMachineCallback { dead: dead.clone() },
225 BinderFeatures::default(),
226 );
227 vm.registerCallback(&callback)?;
228 let death_recipient = wait_for_death(&mut vm.as_binder(), dead.clone())?;
229 dead.wait();
230 // Ensure that death_recipient isn't dropped before we wait on the flag, as it is removed
231 // from the Binder when it's dropped.
232 drop(death_recipient);
233 Ok(())
234}
235
236/// Raise the given flag when the given Binder object dies.
237///
238/// If the returned DeathRecipient is dropped then this will no longer do anything.
239fn wait_for_death(binder: &mut impl IBinder, dead: AtomicFlag) -> Result<DeathRecipient, Error> {
240 let mut death_recipient = DeathRecipient::new(move || {
Jiyong Park8611a6c2021-07-09 18:17:44 +0900241 eprintln!("VirtualizationService unexpectedly died");
Andrew Walbranf395b822021-05-05 10:38:59 +0000242 dead.raise();
243 });
244 binder.link_to_death(&mut death_recipient)?;
245 Ok(death_recipient)
246}
247
Inseob Kima5a262f2021-11-17 19:41:03 +0900248fn parse_extra_apk_list(apk: &Path, config_path: &str) -> Result<Vec<String>, Error> {
249 let mut archive = ZipArchive::new(File::open(apk)?)?;
250 let config_file = archive.by_name(config_path)?;
251 let config: VmPayloadConfig = serde_json::from_reader(config_file)?;
252 Ok(config.extra_apks.into_iter().map(|x| x.path).collect())
253}
254
Andrew Walbranf395b822021-05-05 10:38:59 +0000255#[derive(Debug)]
256struct VirtualMachineCallback {
257 dead: AtomicFlag,
258}
259
260impl Interface for VirtualMachineCallback {}
261
262impl IVirtualMachineCallback for VirtualMachineCallback {
Inseob Kim7f61fe72021-08-20 20:50:47 +0900263 fn onPayloadStarted(
264 &self,
265 _cid: i32,
266 stream: Option<&ParcelFileDescriptor>,
267 ) -> BinderResult<()> {
268 // Show the output of the payload
269 if let Some(stream) = stream {
270 let mut reader = BufReader::new(stream.as_ref());
271 loop {
272 let mut s = String::new();
273 match reader.read_line(&mut s) {
274 Ok(0) => break,
275 Ok(_) => print!("{}", s),
276 Err(e) => eprintln!("error reading from virtual machine: {}", e),
277 };
278 }
Jiyong Park8611a6c2021-07-09 18:17:44 +0900279 }
280 Ok(())
281 }
282
Inseob Kim14cb8692021-08-31 21:50:39 +0900283 fn onPayloadReady(&self, _cid: i32) -> BinderResult<()> {
Inseob Kim8dbc3222021-09-01 21:50:23 +0900284 eprintln!("payload is ready");
Inseob Kim14cb8692021-08-31 21:50:39 +0900285 Ok(())
286 }
287
Inseob Kim8dbc3222021-09-01 21:50:23 +0900288 fn onPayloadFinished(&self, _cid: i32, exit_code: i32) -> BinderResult<()> {
289 eprintln!("payload finished with exit code {}", exit_code);
Inseob Kim2444af92021-08-31 01:22:50 +0900290 Ok(())
291 }
292
Jooyung Handd0a1732021-11-23 15:26:20 +0900293 fn onError(&self, _cid: i32, error_code: i32, message: &str) -> BinderResult<()> {
294 eprintln!("VM encountered an error: code={}, message={}", error_code, message);
295 Ok(())
296 }
297
Andrew Walbranc92d35f2022-01-12 12:45:19 +0000298 fn onDied(&self, _cid: i32, reason: DeathReason) -> BinderResult<()> {
Andrew Walbranf395b822021-05-05 10:38:59 +0000299 self.dead.raise();
Andrew Walbranc92d35f2022-01-12 12:45:19 +0000300
301 match reason {
Andrew Walbrand15c5632022-02-03 13:38:31 +0000302 DeathReason::INFRASTRUCTURE_ERROR => println!("Error waiting for VM to finish."),
Andrew Walbranc92d35f2022-01-12 12:45:19 +0000303 DeathReason::KILLED => println!("VM was killed."),
304 DeathReason::UNKNOWN => println!("VM died for an unknown reason."),
Andrew Walbrand15c5632022-02-03 13:38:31 +0000305 DeathReason::SHUTDOWN => println!("VM shutdown cleanly."),
306 DeathReason::ERROR => println!("Error starting VM."),
307 DeathReason::REBOOT => println!("VM tried to reboot, possibly due to a kernel panic."),
308 DeathReason::CRASH => println!("VM crashed."),
Andrew Walbrane405ba02022-05-16 11:05:34 +0000309 DeathReason::PVM_FIRMWARE_PUBLIC_KEY_MISMATCH => println!(
310 "pVM firmware failed to verify the VM because the public key doesn't match."
311 ),
312 DeathReason::PVM_FIRMWARE_INSTANCE_IMAGE_CHANGED => {
313 println!("pVM firmware failed to verify the VM because the instance image changed.")
314 }
315 DeathReason::BOOTLOADER_PUBLIC_KEY_MISMATCH => {
316 println!("Bootloader failed to verify the VM because the public key doesn't match.")
317 }
318 DeathReason::BOOTLOADER_INSTANCE_IMAGE_CHANGED => {
319 println!("Bootloader failed to verify the VM because the instance image changed.")
320 }
Andrew Walbranc92d35f2022-01-12 12:45:19 +0000321 _ => println!("VM died for an unrecognised reason."),
322 }
Andrew Walbranf395b822021-05-05 10:38:59 +0000323 Ok(())
324 }
325}
326
327/// Safely duplicate the standard output file descriptor.
328fn duplicate_stdout() -> io::Result<File> {
329 let stdout_fd = io::stdout().as_raw_fd();
330 // Safe because this just duplicates a file descriptor which we know to be valid, and we check
331 // for an error.
332 let dup_fd = unsafe { libc::dup(stdout_fd) };
333 if dup_fd < 0 {
334 Err(io::Error::last_os_error())
335 } else {
336 // Safe because we have just duplicated the file descriptor so we own it, and `from_raw_fd`
337 // takes ownership of it.
338 Ok(unsafe { File::from_raw_fd(dup_fd) })
339 }
340}