blob: 3f25bba923f1bc7865de08be274933aa249349ae [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::{
Alan Stokes0d1ef782022-09-27 13:46:35 +010019 IVirtualizationService::IVirtualizationService,
20 PartitionType::PartitionType,
21 VirtualMachineAppConfig::{DebugLevel::DebugLevel, Payload::Payload, VirtualMachineAppConfig},
22 VirtualMachineConfig::VirtualMachineConfig,
Inseob Kim7b5f65c2022-11-15 14:27:04 +090023 VirtualMachinePayloadConfig::VirtualMachinePayloadConfig,
Andrew Walbranf8d94112021-09-07 11:45:36 +000024 VirtualMachineState::VirtualMachineState,
Jooyung Han21e9b922021-06-26 04:14:16 +090025};
Nikita Ioffeb0b67562022-11-22 15:48:06 +000026use anyhow::{anyhow, bail, Context, Error};
Alan Stokes0e82b502022-08-08 14:44:48 +010027use binder::ParcelFileDescriptor;
Inseob Kima5a262f2021-11-17 19:41:03 +090028use microdroid_payload_config::VmPayloadConfig;
Nikita Ioffeb0b67562022-11-22 15:48:06 +000029use rand::{distributions::Alphanumeric, Rng};
30use std::fs;
Andrew Walbranf395b822021-05-05 10:38:59 +000031use std::fs::File;
Alan Stokesf30982b2022-11-18 11:50:32 +000032use std::io;
Andrew Walbranf395b822021-05-05 10:38:59 +000033use std::os::unix::io::{AsRawFd, FromRawFd};
Inseob Kima5a262f2021-11-17 19:41:03 +090034use std::path::{Path, PathBuf};
Nikita Ioffeb0b67562022-11-22 15:48:06 +000035use std::process::Command;
Alan Stokes2bead0d2022-09-05 16:58:34 +010036use vmclient::{ErrorCode, VmInstance};
Jiyong Park48b354d2021-07-15 15:04:38 +090037use vmconfig::{open_parcel_file, VmConfig};
Inseob Kima5a262f2021-11-17 19:41:03 +090038use zip::ZipArchive;
Andrew Walbranf395b822021-05-05 10:38:59 +000039
Jooyung Han21e9b922021-06-26 04:14:16 +090040/// Run a VM from the given APK, idsig, and config.
Jiyong Park48b354d2021-07-15 15:04:38 +090041#[allow(clippy::too_many_arguments)]
Jooyung Han21e9b922021-06-26 04:14:16 +090042pub fn command_run_app(
Seungjae Yoo62085c02022-08-12 04:44:52 +000043 name: Option<String>,
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,
Shikha Panwar22e70452022-10-10 18:32:55 +000048 storage: Option<&Path>,
49 storage_size: Option<u64>,
Inseob Kim7b5f65c2022-11-15 14:27:04 +090050 config_path: Option<String>,
51 payload_path: Option<String>,
Jooyung Han21e9b922021-06-26 04:14:16 +090052 daemonize: bool,
Jiyong Parkb8182bb2021-10-26 22:53:08 +090053 console_path: Option<&Path>,
Jooyung Han21e9b922021-06-26 04:14:16 +090054 log_path: Option<&Path>,
Jiyong Parke558ab12022-07-07 20:18:55 +090055 ramdump_path: Option<&Path>,
Jiyong Parkc2a49cc2021-10-15 00:02:12 +090056 debug_level: DebugLevel,
Andrew Walbran3994f002022-01-27 17:33:45 +000057 protected: bool,
Jiyong Parkd63cfff2021-09-27 20:10:17 +090058 mem: Option<u32>,
Jiyong Park032615f2022-01-10 13:55:34 +090059 cpus: Option<u32>,
Jiyong Parkdfe16d62022-04-20 17:32:12 +090060 task_profiles: Vec<String>,
Inseob Kima5a262f2021-11-17 19:41:03 +090061 extra_idsigs: &[PathBuf],
Jooyung Han21e9b922021-06-26 04:14:16 +090062) -> Result<(), Error> {
Steven Moreland6a55e2e2022-10-22 00:30:42 +000063 let apk_file = File::open(apk).context("Failed to open APK file")?;
64
Inseob Kim7b5f65c2022-11-15 14:27:04 +090065 let extra_apks = match config_path.as_deref() {
66 Some(path) => parse_extra_apk_list(apk, path)?,
67 None => vec![],
68 };
69
Inseob Kima5a262f2021-11-17 19:41:03 +090070 if extra_apks.len() != extra_idsigs.len() {
71 bail!(
72 "Found {} extra apks, but there are {} extra idsigs",
73 extra_apks.len(),
74 extra_idsigs.len()
75 )
76 }
77
78 for i in 0..extra_apks.len() {
79 let extra_apk_fd = ParcelFileDescriptor::new(File::open(&extra_apks[i])?);
80 let extra_idsig_fd = ParcelFileDescriptor::new(File::create(&extra_idsigs[i])?);
81 service.createOrUpdateIdsigFile(&extra_apk_fd, &extra_idsig_fd)?;
82 }
83
Jiyong Park0a248432021-08-20 23:32:39 +090084 let idsig_file = File::create(idsig).context("Failed to create idsig file")?;
85
86 let apk_fd = ParcelFileDescriptor::new(apk_file);
87 let idsig_fd = ParcelFileDescriptor::new(idsig_file);
88 service.createOrUpdateIdsigFile(&apk_fd, &idsig_fd)?;
89
Jooyung Han21e9b922021-06-26 04:14:16 +090090 let idsig_file = File::open(idsig).context("Failed to open idsig file")?;
Jiyong Park0a248432021-08-20 23:32:39 +090091 let idsig_fd = ParcelFileDescriptor::new(idsig_file);
Jiyong Park48b354d2021-07-15 15:04:38 +090092
93 if !instance.exists() {
94 const INSTANCE_FILE_SIZE: u64 = 10 * 1024 * 1024;
Jiyong Park9dd389e2021-08-23 20:42:59 +090095 command_create_partition(
Andrew Walbran616d13f2022-05-12 18:35:55 +000096 service,
Jiyong Park9dd389e2021-08-23 20:42:59 +090097 instance,
98 INSTANCE_FILE_SIZE,
99 PartitionType::ANDROID_VM_INSTANCE,
100 )?;
Jiyong Park48b354d2021-07-15 15:04:38 +0900101 }
102
Shikha Panwar22e70452022-10-10 18:32:55 +0000103 let storage = if let Some(path) = storage {
104 if !path.exists() {
105 command_create_partition(
106 service,
107 path,
108 storage_size.unwrap_or(10 * 1024 * 1024),
Shikha Panwar9fd198f2022-11-18 17:43:43 +0000109 PartitionType::ENCRYPTEDSTORE,
Shikha Panwar22e70452022-10-10 18:32:55 +0000110 )?;
111 }
112 Some(open_parcel_file(path, true)?)
113 } else {
114 None
115 };
116
Inseob Kima5a262f2021-11-17 19:41:03 +0900117 let extra_idsig_files: Result<Vec<File>, _> = extra_idsigs.iter().map(File::open).collect();
118 let extra_idsig_fds = extra_idsig_files?.into_iter().map(ParcelFileDescriptor::new).collect();
119
Inseob Kim7b5f65c2022-11-15 14:27:04 +0900120 let payload = if let Some(config_path) = config_path {
121 if payload_path.is_some() {
122 bail!("Only one of --config-path or --payload-path can be defined")
123 }
124 Payload::ConfigPath(config_path)
125 } else if let Some(payload_path) = payload_path {
126 Payload::PayloadConfig(VirtualMachinePayloadConfig { payloadPath: payload_path })
127 } else {
128 bail!("Either --config-path or --payload-path must be defined")
129 };
130
131 let payload_config_str = format!("{:?}!{:?}", apk, payload);
132
Jooyung Han21e9b922021-06-26 04:14:16 +0900133 let config = VirtualMachineConfig::AppConfig(VirtualMachineAppConfig {
Seungjae Yoo62085c02022-08-12 04:44:52 +0000134 name: name.unwrap_or_else(|| String::from("VmRunApp")),
Jiyong Park0a248432021-08-20 23:32:39 +0900135 apk: apk_fd.into(),
136 idsig: idsig_fd.into(),
Inseob Kima5a262f2021-11-17 19:41:03 +0900137 extraIdsigs: extra_idsig_fds,
Jiyong Park48b354d2021-07-15 15:04:38 +0900138 instanceImage: open_parcel_file(instance, true /* writable */)?.into(),
Shikha Panwar22e70452022-10-10 18:32:55 +0000139 encryptedStorageImage: storage,
Inseob Kim7b5f65c2022-11-15 14:27:04 +0900140 payload,
Jiyong Parkc2a49cc2021-10-15 00:02:12 +0900141 debugLevel: debug_level,
Andrew Walbran3994f002022-01-27 17:33:45 +0000142 protectedVm: protected,
Jiyong Parkd63cfff2021-09-27 20:10:17 +0900143 memoryMib: mem.unwrap_or(0) as i32, // 0 means use the VM default
Jiyong Park032615f2022-01-10 13:55:34 +0900144 numCpus: cpus.unwrap_or(1) as i32,
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900145 taskProfiles: task_profiles,
Jooyung Han21e9b922021-06-26 04:14:16 +0900146 });
Inseob Kim7b5f65c2022-11-15 14:27:04 +0900147 run(service, &config, &payload_config_str, daemonize, console_path, log_path, ramdump_path)
Jooyung Han21e9b922021-06-26 04:14:16 +0900148}
149
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000150const EMPTY_PAYLOAD_APK: &str = "com.android.microdroid.empty_payload";
151
152fn find_empty_payload_apk_path() -> Result<PathBuf, Error> {
153 let output = Command::new("/system/bin/pm")
154 .arg("path")
155 .arg(EMPTY_PAYLOAD_APK)
156 .output()
157 .context("failed to execute pm path")?;
158 let output_str = String::from_utf8(output.stdout).context("failed to parse output")?;
159 match output_str.strip_prefix("package:") {
160 None => Err(anyhow!("Unexpected output {}", output_str)),
161 Some(apk_path) => Ok(PathBuf::from(apk_path.trim())),
162 }
163}
164
165fn create_work_dir() -> Result<PathBuf, Error> {
166 let s: String =
167 rand::thread_rng().sample_iter(&Alphanumeric).take(17).map(char::from).collect();
168 let work_dir = PathBuf::from("/data/local/tmp/microdroid").join(s);
169 println!("creating work dir {}", work_dir.display());
170 fs::create_dir_all(&work_dir).context("failed to mkdir")?;
171 Ok(work_dir)
172}
173
174/// Run a VM with Microdroid
175#[allow(clippy::too_many_arguments)]
176pub fn command_run_microdroid(
177 name: Option<String>,
178 service: &dyn IVirtualizationService,
179 work_dir: Option<PathBuf>,
180 storage: Option<&Path>,
181 storage_size: Option<u64>,
182 daemonize: bool,
183 console_path: Option<&Path>,
184 log_path: Option<&Path>,
185 ramdump_path: Option<&Path>,
186 debug_level: DebugLevel,
187 protected: bool,
188 mem: Option<u32>,
189 cpus: Option<u32>,
190 task_profiles: Vec<String>,
191) -> Result<(), Error> {
192 let apk = find_empty_payload_apk_path()
193 .context(anyhow!("failed to find path for {} apk", EMPTY_PAYLOAD_APK))?;
194 println!("found path for {} apk: {}", EMPTY_PAYLOAD_APK, apk.display());
195
196 let work_dir = work_dir.unwrap_or(create_work_dir()?);
197 let idsig = work_dir.join("apk.idsig");
198 println!("apk.idsig path: {}", idsig.display());
199 let instance_img = work_dir.join("instance.img");
200 println!("instance.img path: {}", instance_img.display());
201
202 let payload_path = "MicrodroidEmptyPayloadJniLib.so";
203 let extra_sig = [];
204 command_run_app(
205 name,
206 service,
207 &apk,
208 &idsig,
209 &instance_img,
210 storage,
211 storage_size,
212 /* config_path= */ None,
213 Some(payload_path.to_owned()),
214 daemonize,
215 console_path,
216 log_path,
217 ramdump_path,
218 debug_level,
219 protected,
220 mem,
221 cpus,
222 task_profiles,
223 &extra_sig,
224 )
225}
226
Andrew Walbranf395b822021-05-05 10:38:59 +0000227/// Run a VM from the given configuration file.
Jooyung Hanb7983a22022-02-22 05:21:27 +0900228#[allow(clippy::too_many_arguments)]
Andrew Walbranf395b822021-05-05 10:38:59 +0000229pub fn command_run(
Seungjae Yoo62085c02022-08-12 04:44:52 +0000230 name: Option<String>,
Andrew Walbran616d13f2022-05-12 18:35:55 +0000231 service: &dyn IVirtualizationService,
Andrew Walbranf395b822021-05-05 10:38:59 +0000232 config_path: &Path,
233 daemonize: bool,
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900234 console_path: Option<&Path>,
Jooyung Hanb7983a22022-02-22 05:21:27 +0900235 log_path: Option<&Path>,
Jiyong Parkd63cfff2021-09-27 20:10:17 +0900236 mem: Option<u32>,
Jiyong Park032615f2022-01-10 13:55:34 +0900237 cpus: Option<u32>,
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900238 task_profiles: Vec<String>,
Andrew Walbranf395b822021-05-05 10:38:59 +0000239) -> Result<(), Error> {
Andrew Walbran3a5a9212021-05-04 17:09:08 +0000240 let config_file = File::open(config_path).context("Failed to open config file")?;
Jiyong Parkd63cfff2021-09-27 20:10:17 +0900241 let mut config =
Andrew Walbran3a5a9212021-05-04 17:09:08 +0000242 VmConfig::load(&config_file).context("Failed to parse config file")?.to_parcelable()?;
Jiyong Parkd63cfff2021-09-27 20:10:17 +0900243 if let Some(mem) = mem {
244 config.memoryMib = mem as i32;
245 }
Jiyong Park032615f2022-01-10 13:55:34 +0900246 if let Some(cpus) = cpus {
247 config.numCpus = cpus as i32;
248 }
Seungjae Yoo62085c02022-08-12 04:44:52 +0000249 if let Some(name) = name {
250 config.name = name;
251 } else {
252 config.name = String::from("VmRun");
253 }
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900254 config.taskProfiles = task_profiles;
Jooyung Han21e9b922021-06-26 04:14:16 +0900255 run(
256 service,
257 &VirtualMachineConfig::RawConfig(config),
258 &format!("{:?}", config_path),
259 daemonize,
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900260 console_path,
Jooyung Hanb7983a22022-02-22 05:21:27 +0900261 log_path,
Jiyong Parke558ab12022-07-07 20:18:55 +0900262 /* ramdump_path */ None,
Jooyung Han21e9b922021-06-26 04:14:16 +0900263 )
264}
265
Andrew Walbranf8d94112021-09-07 11:45:36 +0000266fn state_to_str(vm_state: VirtualMachineState) -> &'static str {
267 match vm_state {
268 VirtualMachineState::NOT_STARTED => "NOT_STARTED",
269 VirtualMachineState::STARTING => "STARTING",
270 VirtualMachineState::STARTED => "STARTED",
271 VirtualMachineState::READY => "READY",
272 VirtualMachineState::FINISHED => "FINISHED",
273 VirtualMachineState::DEAD => "DEAD",
274 _ => "(invalid state)",
275 }
276}
277
Jooyung Han21e9b922021-06-26 04:14:16 +0900278fn run(
Andrew Walbran616d13f2022-05-12 18:35:55 +0000279 service: &dyn IVirtualizationService,
Jooyung Han21e9b922021-06-26 04:14:16 +0900280 config: &VirtualMachineConfig,
Inseob Kim7b5f65c2022-11-15 14:27:04 +0900281 payload_config: &str,
Jooyung Han21e9b922021-06-26 04:14:16 +0900282 daemonize: bool,
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900283 console_path: Option<&Path>,
Jooyung Han21e9b922021-06-26 04:14:16 +0900284 log_path: Option<&Path>,
Jiyong Parke558ab12022-07-07 20:18:55 +0900285 ramdump_path: Option<&Path>,
Jooyung Han21e9b922021-06-26 04:14:16 +0900286) -> Result<(), Error> {
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900287 let console = if let Some(console_path) = console_path {
Andrew Walbrand0ef4002022-05-16 16:14:10 +0000288 Some(
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900289 File::create(console_path)
290 .with_context(|| format!("Failed to open console file {:?}", console_path))?,
Andrew Walbrand0ef4002022-05-16 16:14:10 +0000291 )
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900292 } else if daemonize {
293 None
294 } else {
Andrew Walbrand0ef4002022-05-16 16:14:10 +0000295 Some(duplicate_stdout()?)
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900296 };
297 let log = if let Some(log_path) = log_path {
Andrew Walbrand0ef4002022-05-16 16:14:10 +0000298 Some(
Andrew Walbranbe429242021-06-28 12:22:54 +0000299 File::create(log_path)
300 .with_context(|| format!("Failed to open log file {:?}", log_path))?,
Andrew Walbrand0ef4002022-05-16 16:14:10 +0000301 )
Andrew Walbranbe429242021-06-28 12:22:54 +0000302 } else if daemonize {
303 None
304 } else {
Andrew Walbrand0ef4002022-05-16 16:14:10 +0000305 Some(duplicate_stdout()?)
Andrew Walbranbe429242021-06-28 12:22:54 +0000306 };
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900307
Alan Stokes0e82b502022-08-08 14:44:48 +0100308 let callback = Box::new(Callback {});
309 let vm = VmInstance::create(service, config, console, log, Some(callback))
310 .context("Failed to create VM")?;
Andrew Walbrand0ef4002022-05-16 16:14:10 +0000311 vm.start().context("Failed to start VM")?;
Andrew Walbranf395b822021-05-05 10:38:59 +0000312
Andrew Walbranf8d94112021-09-07 11:45:36 +0000313 println!(
314 "Created VM from {} with CID {}, state is {}.",
Inseob Kim7b5f65c2022-11-15 14:27:04 +0900315 payload_config,
Andrew Walbrand0ef4002022-05-16 16:14:10 +0000316 vm.cid(),
317 state_to_str(vm.state()?)
Andrew Walbranf8d94112021-09-07 11:45:36 +0000318 );
Andrew Walbranf395b822021-05-05 10:38:59 +0000319
320 if daemonize {
Andrew Walbranf6bf6862021-05-21 12:41:13 +0000321 // Pass the VM reference back to VirtualizationService and have it hold it in the
322 // background.
Andrew Walbrand0ef4002022-05-16 16:14:10 +0000323 service.debugHoldVmRef(&vm.vm).context("Failed to pass VM to VirtualizationService")?;
Andrew Walbranf395b822021-05-05 10:38:59 +0000324 } else {
Andrew Walbranf6bf6862021-05-21 12:41:13 +0000325 // Wait until the VM or VirtualizationService dies. If we just returned immediately then the
Andrew Walbranf395b822021-05-05 10:38:59 +0000326 // IVirtualMachine Binder object would be dropped and the VM would be killed.
Andrew Walbrand0ef4002022-05-16 16:14:10 +0000327 let death_reason = vm.wait_for_death();
Jiyong Parke558ab12022-07-07 20:18:55 +0900328
329 if let Some(path) = ramdump_path {
330 save_ramdump_if_available(path, &vm)?;
331 }
Alan Stokes2bead0d2022-09-05 16:58:34 +0100332 println!("VM ended: {:?}", death_reason);
Andrew Walbranf395b822021-05-05 10:38:59 +0000333 }
Andrew Walbranf395b822021-05-05 10:38:59 +0000334
Andrew Walbranf395b822021-05-05 10:38:59 +0000335 Ok(())
336}
337
Jiyong Parke558ab12022-07-07 20:18:55 +0900338fn save_ramdump_if_available(path: &Path, vm: &VmInstance) -> Result<(), Error> {
339 if let Some(mut ramdump) = vm.get_ramdump() {
340 let mut file =
341 File::create(path).context(format!("Failed to create ramdump file {:?}", path))?;
342 let size = std::io::copy(&mut ramdump, &mut file)
343 .context(format!("Failed to save ramdump to file {:?}", path))?;
344 eprintln!("Ramdump ({} bytes) saved to {:?}", size, path);
345 }
346 Ok(())
347}
348
Inseob Kima5a262f2021-11-17 19:41:03 +0900349fn parse_extra_apk_list(apk: &Path, config_path: &str) -> Result<Vec<String>, Error> {
350 let mut archive = ZipArchive::new(File::open(apk)?)?;
351 let config_file = archive.by_name(config_path)?;
352 let config: VmPayloadConfig = serde_json::from_reader(config_file)?;
353 Ok(config.extra_apks.into_iter().map(|x| x.path).collect())
354}
355
Alan Stokes0e82b502022-08-08 14:44:48 +0100356struct Callback {}
Andrew Walbranf395b822021-05-05 10:38:59 +0000357
Alan Stokes0e82b502022-08-08 14:44:48 +0100358impl vmclient::VmCallback for Callback {
David Brazdil451cc962022-10-14 14:08:12 +0100359 fn on_payload_started(&self, _cid: i32) {
360 eprintln!("payload started");
361 }
362
Alan Stokes0e82b502022-08-08 14:44:48 +0100363 fn on_payload_ready(&self, _cid: i32) {
Inseob Kim8dbc3222021-09-01 21:50:23 +0900364 eprintln!("payload is ready");
Inseob Kim14cb8692021-08-31 21:50:39 +0900365 }
366
Alan Stokes0e82b502022-08-08 14:44:48 +0100367 fn on_payload_finished(&self, _cid: i32, exit_code: i32) {
Inseob Kim8dbc3222021-09-01 21:50:23 +0900368 eprintln!("payload finished with exit code {}", exit_code);
Inseob Kim2444af92021-08-31 01:22:50 +0900369 }
370
Alan Stokes2bead0d2022-09-05 16:58:34 +0100371 fn on_error(&self, _cid: i32, error_code: ErrorCode, message: &str) {
372 eprintln!("VM encountered an error: code={:?}, message={}", error_code, message);
Andrew Walbranf395b822021-05-05 10:38:59 +0000373 }
374}
375
376/// Safely duplicate the standard output file descriptor.
377fn duplicate_stdout() -> io::Result<File> {
378 let stdout_fd = io::stdout().as_raw_fd();
379 // Safe because this just duplicates a file descriptor which we know to be valid, and we check
380 // for an error.
381 let dup_fd = unsafe { libc::dup(stdout_fd) };
382 if dup_fd < 0 {
383 Err(io::Error::last_os_error())
384 } else {
385 // Safe because we have just duplicated the file descriptor so we own it, and `from_raw_fd`
386 // takes ownership of it.
387 Ok(unsafe { File::from_raw_fd(dup_fd) })
388 }
389}