blob: 44eb27a6f61bb2e03eab2506654d928666543801 [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;
Jooyung Han21e9b922021-06-26 04:14:16 +090018use android_system_virtualizationservice::aidl::android::system::virtualizationservice::{
Andrew Walbrand0ef4002022-05-16 16:14:10 +000019 DeathReason::DeathReason,
20 IVirtualMachineCallback::{BnVirtualMachineCallback, IVirtualMachineCallback},
21 IVirtualizationService::IVirtualizationService,
22 PartitionType::PartitionType,
Jiyong Parkc2a49cc2021-10-15 00:02:12 +090023 VirtualMachineAppConfig::DebugLevel::DebugLevel,
Andrew Walbrand0ef4002022-05-16 16:14:10 +000024 VirtualMachineAppConfig::VirtualMachineAppConfig,
25 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 Walbrand0ef4002022-05-16 16:14:10 +000029 BinderFeatures, Interface, ParcelFileDescriptor, Result as BinderResult,
Andrew Walbranf395b822021-05-05 10:38:59 +000030};
Inseob Kima5a262f2021-11-17 19:41:03 +090031use anyhow::{bail, Context, Error};
32use microdroid_payload_config::VmPayloadConfig;
Andrew Walbranf395b822021-05-05 10:38:59 +000033use std::fs::File;
Jiyong Park8611a6c2021-07-09 18:17:44 +090034use std::io::{self, BufRead, BufReader};
Andrew Walbranf395b822021-05-05 10:38:59 +000035use std::os::unix::io::{AsRawFd, FromRawFd};
Inseob Kima5a262f2021-11-17 19:41:03 +090036use std::path::{Path, PathBuf};
Andrew Walbrand0ef4002022-05-16 16:14:10 +000037use vmclient::VmInstance;
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 Parke558ab12022-07-07 20:18:55 +090052 ramdump_path: Option<&Path>,
Jiyong Parkc2a49cc2021-10-15 00:02:12 +090053 debug_level: DebugLevel,
Andrew Walbran3994f002022-01-27 17:33:45 +000054 protected: bool,
Jiyong Parkd63cfff2021-09-27 20:10:17 +090055 mem: Option<u32>,
Jiyong Park032615f2022-01-10 13:55:34 +090056 cpus: Option<u32>,
57 cpu_affinity: Option<String>,
Jiyong Parkdfe16d62022-04-20 17:32:12 +090058 task_profiles: Vec<String>,
Inseob Kima5a262f2021-11-17 19:41:03 +090059 extra_idsigs: &[PathBuf],
Jooyung Han21e9b922021-06-26 04:14:16 +090060) -> Result<(), Error> {
Inseob Kima5a262f2021-11-17 19:41:03 +090061 let extra_apks = parse_extra_apk_list(apk, config_path)?;
62 if extra_apks.len() != extra_idsigs.len() {
63 bail!(
64 "Found {} extra apks, but there are {} extra idsigs",
65 extra_apks.len(),
66 extra_idsigs.len()
67 )
68 }
69
70 for i in 0..extra_apks.len() {
71 let extra_apk_fd = ParcelFileDescriptor::new(File::open(&extra_apks[i])?);
72 let extra_idsig_fd = ParcelFileDescriptor::new(File::create(&extra_idsigs[i])?);
73 service.createOrUpdateIdsigFile(&extra_apk_fd, &extra_idsig_fd)?;
74 }
75
Jooyung Han21e9b922021-06-26 04:14:16 +090076 let apk_file = File::open(apk).context("Failed to open APK file")?;
Jiyong Park0a248432021-08-20 23:32:39 +090077 let idsig_file = File::create(idsig).context("Failed to create idsig file")?;
78
79 let apk_fd = ParcelFileDescriptor::new(apk_file);
80 let idsig_fd = ParcelFileDescriptor::new(idsig_file);
81 service.createOrUpdateIdsigFile(&apk_fd, &idsig_fd)?;
82
Jooyung Han21e9b922021-06-26 04:14:16 +090083 let idsig_file = File::open(idsig).context("Failed to open idsig file")?;
Jiyong Park0a248432021-08-20 23:32:39 +090084 let idsig_fd = ParcelFileDescriptor::new(idsig_file);
Jiyong Park48b354d2021-07-15 15:04:38 +090085
86 if !instance.exists() {
87 const INSTANCE_FILE_SIZE: u64 = 10 * 1024 * 1024;
Jiyong Park9dd389e2021-08-23 20:42:59 +090088 command_create_partition(
Andrew Walbran616d13f2022-05-12 18:35:55 +000089 service,
Jiyong Park9dd389e2021-08-23 20:42:59 +090090 instance,
91 INSTANCE_FILE_SIZE,
92 PartitionType::ANDROID_VM_INSTANCE,
93 )?;
Jiyong Park48b354d2021-07-15 15:04:38 +090094 }
95
Inseob Kima5a262f2021-11-17 19:41:03 +090096 let extra_idsig_files: Result<Vec<File>, _> = extra_idsigs.iter().map(File::open).collect();
97 let extra_idsig_fds = extra_idsig_files?.into_iter().map(ParcelFileDescriptor::new).collect();
98
Jooyung Han21e9b922021-06-26 04:14:16 +090099 let config = VirtualMachineConfig::AppConfig(VirtualMachineAppConfig {
Jiyong Park0a248432021-08-20 23:32:39 +0900100 apk: apk_fd.into(),
101 idsig: idsig_fd.into(),
Inseob Kima5a262f2021-11-17 19:41:03 +0900102 extraIdsigs: extra_idsig_fds,
Jiyong Park48b354d2021-07-15 15:04:38 +0900103 instanceImage: open_parcel_file(instance, true /* writable */)?.into(),
Jooyung Han21e9b922021-06-26 04:14:16 +0900104 configPath: config_path.to_owned(),
Jiyong Parkc2a49cc2021-10-15 00:02:12 +0900105 debugLevel: debug_level,
Andrew Walbran3994f002022-01-27 17:33:45 +0000106 protectedVm: protected,
Jiyong Parkd63cfff2021-09-27 20:10:17 +0900107 memoryMib: mem.unwrap_or(0) as i32, // 0 means use the VM default
Jiyong Park032615f2022-01-10 13:55:34 +0900108 numCpus: cpus.unwrap_or(1) as i32,
109 cpuAffinity: cpu_affinity,
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900110 taskProfiles: task_profiles,
Jooyung Han21e9b922021-06-26 04:14:16 +0900111 });
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900112 run(
113 service,
114 &config,
115 &format!("{:?}!{:?}", apk, config_path),
116 daemonize,
117 console_path,
118 log_path,
Jiyong Parke558ab12022-07-07 20:18:55 +0900119 ramdump_path,
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900120 )
Jooyung Han21e9b922021-06-26 04:14:16 +0900121}
122
Andrew Walbranf395b822021-05-05 10:38:59 +0000123/// Run a VM from the given configuration file.
Jooyung Hanb7983a22022-02-22 05:21:27 +0900124#[allow(clippy::too_many_arguments)]
Andrew Walbranf395b822021-05-05 10:38:59 +0000125pub fn command_run(
Andrew Walbran616d13f2022-05-12 18:35:55 +0000126 service: &dyn IVirtualizationService,
Andrew Walbranf395b822021-05-05 10:38:59 +0000127 config_path: &Path,
128 daemonize: bool,
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900129 console_path: Option<&Path>,
Jooyung Hanb7983a22022-02-22 05:21:27 +0900130 log_path: Option<&Path>,
Jiyong Parkd63cfff2021-09-27 20:10:17 +0900131 mem: Option<u32>,
Jiyong Park032615f2022-01-10 13:55:34 +0900132 cpus: Option<u32>,
133 cpu_affinity: Option<String>,
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900134 task_profiles: Vec<String>,
Andrew Walbranf395b822021-05-05 10:38:59 +0000135) -> Result<(), Error> {
Andrew Walbran3a5a9212021-05-04 17:09:08 +0000136 let config_file = File::open(config_path).context("Failed to open config file")?;
Jiyong Parkd63cfff2021-09-27 20:10:17 +0900137 let mut config =
Andrew Walbran3a5a9212021-05-04 17:09:08 +0000138 VmConfig::load(&config_file).context("Failed to parse config file")?.to_parcelable()?;
Jiyong Parkd63cfff2021-09-27 20:10:17 +0900139 if let Some(mem) = mem {
140 config.memoryMib = mem as i32;
141 }
Jiyong Park032615f2022-01-10 13:55:34 +0900142 if let Some(cpus) = cpus {
143 config.numCpus = cpus as i32;
144 }
145 config.cpuAffinity = cpu_affinity;
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900146 config.taskProfiles = task_profiles;
Jooyung Han21e9b922021-06-26 04:14:16 +0900147 run(
148 service,
149 &VirtualMachineConfig::RawConfig(config),
150 &format!("{:?}", config_path),
151 daemonize,
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900152 console_path,
Jooyung Hanb7983a22022-02-22 05:21:27 +0900153 log_path,
Jiyong Parke558ab12022-07-07 20:18:55 +0900154 /* ramdump_path */ None,
Jooyung Han21e9b922021-06-26 04:14:16 +0900155 )
156}
157
Andrew Walbranf8d94112021-09-07 11:45:36 +0000158fn state_to_str(vm_state: VirtualMachineState) -> &'static str {
159 match vm_state {
160 VirtualMachineState::NOT_STARTED => "NOT_STARTED",
161 VirtualMachineState::STARTING => "STARTING",
162 VirtualMachineState::STARTED => "STARTED",
163 VirtualMachineState::READY => "READY",
164 VirtualMachineState::FINISHED => "FINISHED",
165 VirtualMachineState::DEAD => "DEAD",
166 _ => "(invalid state)",
167 }
168}
169
Jooyung Han21e9b922021-06-26 04:14:16 +0900170fn run(
Andrew Walbran616d13f2022-05-12 18:35:55 +0000171 service: &dyn IVirtualizationService,
Jooyung Han21e9b922021-06-26 04:14:16 +0900172 config: &VirtualMachineConfig,
173 config_path: &str,
174 daemonize: bool,
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900175 console_path: Option<&Path>,
Jooyung Han21e9b922021-06-26 04:14:16 +0900176 log_path: Option<&Path>,
Jiyong Parke558ab12022-07-07 20:18:55 +0900177 ramdump_path: Option<&Path>,
Jooyung Han21e9b922021-06-26 04:14:16 +0900178) -> Result<(), Error> {
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900179 let console = if let Some(console_path) = console_path {
Andrew Walbrand0ef4002022-05-16 16:14:10 +0000180 Some(
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900181 File::create(console_path)
182 .with_context(|| format!("Failed to open console file {:?}", console_path))?,
Andrew Walbrand0ef4002022-05-16 16:14:10 +0000183 )
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900184 } else if daemonize {
185 None
186 } else {
Andrew Walbrand0ef4002022-05-16 16:14:10 +0000187 Some(duplicate_stdout()?)
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900188 };
189 let log = if let Some(log_path) = log_path {
Andrew Walbrand0ef4002022-05-16 16:14:10 +0000190 Some(
Andrew Walbranbe429242021-06-28 12:22:54 +0000191 File::create(log_path)
192 .with_context(|| format!("Failed to open log file {:?}", log_path))?,
Andrew Walbrand0ef4002022-05-16 16:14:10 +0000193 )
Andrew Walbranbe429242021-06-28 12:22:54 +0000194 } else if daemonize {
195 None
196 } else {
Andrew Walbrand0ef4002022-05-16 16:14:10 +0000197 Some(duplicate_stdout()?)
Andrew Walbranbe429242021-06-28 12:22:54 +0000198 };
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900199
Andrew Walbrand0ef4002022-05-16 16:14:10 +0000200 let vm = VmInstance::create(service, config, console, log).context("Failed to create VM")?;
201 let callback =
202 BnVirtualMachineCallback::new_binder(VirtualMachineCallback {}, BinderFeatures::default());
203 vm.vm.registerCallback(&callback)?;
204 vm.start().context("Failed to start VM")?;
Andrew Walbranf395b822021-05-05 10:38:59 +0000205
Andrew Walbranf8d94112021-09-07 11:45:36 +0000206 println!(
207 "Created VM from {} with CID {}, state is {}.",
208 config_path,
Andrew Walbrand0ef4002022-05-16 16:14:10 +0000209 vm.cid(),
210 state_to_str(vm.state()?)
Andrew Walbranf8d94112021-09-07 11:45:36 +0000211 );
Andrew Walbranf395b822021-05-05 10:38:59 +0000212
213 if daemonize {
Andrew Walbranf6bf6862021-05-21 12:41:13 +0000214 // Pass the VM reference back to VirtualizationService and have it hold it in the
215 // background.
Andrew Walbrand0ef4002022-05-16 16:14:10 +0000216 service.debugHoldVmRef(&vm.vm).context("Failed to pass VM to VirtualizationService")?;
Andrew Walbranf395b822021-05-05 10:38:59 +0000217 } else {
Andrew Walbranf6bf6862021-05-21 12:41:13 +0000218 // Wait until the VM or VirtualizationService dies. If we just returned immediately then the
Andrew Walbranf395b822021-05-05 10:38:59 +0000219 // IVirtualMachine Binder object would be dropped and the VM would be killed.
Andrew Walbrand0ef4002022-05-16 16:14:10 +0000220 let death_reason = vm.wait_for_death();
Jiyong Parke558ab12022-07-07 20:18:55 +0900221
222 if let Some(path) = ramdump_path {
223 save_ramdump_if_available(path, &vm)?;
224 }
Andrew Walbrand0ef4002022-05-16 16:14:10 +0000225 println!("{}", death_reason);
Andrew Walbranf395b822021-05-05 10:38:59 +0000226 }
Andrew Walbranf395b822021-05-05 10:38:59 +0000227
Andrew Walbranf395b822021-05-05 10:38:59 +0000228 Ok(())
229}
230
Jiyong Parke558ab12022-07-07 20:18:55 +0900231fn save_ramdump_if_available(path: &Path, vm: &VmInstance) -> Result<(), Error> {
232 if let Some(mut ramdump) = vm.get_ramdump() {
233 let mut file =
234 File::create(path).context(format!("Failed to create ramdump file {:?}", path))?;
235 let size = std::io::copy(&mut ramdump, &mut file)
236 .context(format!("Failed to save ramdump to file {:?}", path))?;
237 eprintln!("Ramdump ({} bytes) saved to {:?}", size, path);
238 }
239 Ok(())
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)]
Andrew Walbrand0ef4002022-05-16 16:14:10 +0000250struct VirtualMachineCallback {}
Andrew Walbranf395b822021-05-05 10:38:59 +0000251
252impl Interface for VirtualMachineCallback {}
253
254impl IVirtualMachineCallback for VirtualMachineCallback {
Inseob Kim7f61fe72021-08-20 20:50:47 +0900255 fn onPayloadStarted(
256 &self,
257 _cid: i32,
258 stream: Option<&ParcelFileDescriptor>,
259 ) -> BinderResult<()> {
260 // Show the output of the payload
261 if let Some(stream) = stream {
262 let mut reader = BufReader::new(stream.as_ref());
263 loop {
264 let mut s = String::new();
265 match reader.read_line(&mut s) {
266 Ok(0) => break,
267 Ok(_) => print!("{}", s),
268 Err(e) => eprintln!("error reading from virtual machine: {}", e),
269 };
270 }
Jiyong Park8611a6c2021-07-09 18:17:44 +0900271 }
272 Ok(())
273 }
274
Inseob Kim14cb8692021-08-31 21:50:39 +0900275 fn onPayloadReady(&self, _cid: i32) -> BinderResult<()> {
Inseob Kim8dbc3222021-09-01 21:50:23 +0900276 eprintln!("payload is ready");
Inseob Kim14cb8692021-08-31 21:50:39 +0900277 Ok(())
278 }
279
Inseob Kim8dbc3222021-09-01 21:50:23 +0900280 fn onPayloadFinished(&self, _cid: i32, exit_code: i32) -> BinderResult<()> {
281 eprintln!("payload finished with exit code {}", exit_code);
Inseob Kim2444af92021-08-31 01:22:50 +0900282 Ok(())
283 }
284
Jooyung Handd0a1732021-11-23 15:26:20 +0900285 fn onError(&self, _cid: i32, error_code: i32, message: &str) -> BinderResult<()> {
286 eprintln!("VM encountered an error: code={}, message={}", error_code, message);
287 Ok(())
288 }
289
Jiyong Parke558ab12022-07-07 20:18:55 +0900290 fn onRamdump(&self, _cid: i32, _stream: &ParcelFileDescriptor) -> BinderResult<()> {
291 // Do nothing. We get ramdump from the vmclient library.
292 Ok(())
293 }
294
Andrew Walbrand0ef4002022-05-16 16:14:10 +0000295 fn onDied(&self, _cid: i32, _reason: DeathReason) -> BinderResult<()> {
Andrew Walbranf395b822021-05-05 10:38:59 +0000296 Ok(())
297 }
298}
299
300/// Safely duplicate the standard output file descriptor.
301fn duplicate_stdout() -> io::Result<File> {
302 let stdout_fd = io::stdout().as_raw_fd();
303 // Safe because this just duplicates a file descriptor which we know to be valid, and we check
304 // for an error.
305 let dup_fd = unsafe { libc::dup(stdout_fd) };
306 if dup_fd < 0 {
307 Err(io::Error::last_os_error())
308 } else {
309 // Safe because we have just duplicated the file descriptor so we own it, and `from_raw_fd`
310 // takes ownership of it.
311 Ok(unsafe { File::from_raw_fd(dup_fd) })
312 }
313}