| // Copyright 2021, The Android Open Source Project | |
| // | |
| // Licensed under the Apache License, Version 2.0 (the "License"); | |
| // you may not use this file except in compliance with the License. | |
| // You may obtain a copy of the License at | |
| // | |
| // http://www.apache.org/licenses/LICENSE-2.0 | |
| // | |
| // Unless required by applicable law or agreed to in writing, software | |
| // distributed under the License is distributed on an "AS IS" BASIS, | |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| // See the License for the specific language governing permissions and | |
| // limitations under the License. | |
| //! Command to run a VM. | |
| use crate::aidl as I; | |
| use crate::create_partition::command_create_partition; | |
| use crate::{RunAppConfig, RunCustomVmConfig, RunMicrodroidConfig}; | |
| use anyhow::{Context, Error, anyhow, bail}; | |
| use binder::ParcelFileDescriptor; | |
| use glob::glob; | |
| use microdroid_payload_config::VmPayloadConfig; | |
| use rand::{distr::Alphanumeric, prelude::*}; | |
| use std::fs; | |
| use std::fs::File; | |
| use std::fs::OpenOptions; | |
| use std::io; | |
| use std::io::{Read, Write}; | |
| use std::os::fd::AsFd; | |
| use std::path::{Path, PathBuf}; | |
| use vmclient::{ErrorCode, VmInstance}; | |
| use vmconfig::{VmConfig, get_debug_level, open_parcel_file}; | |
| use zip::ZipArchive; | |
| /// Run a VM from the given APK, idsig, and config. | |
| #[allow(clippy::needless_update)] | |
| pub fn command_run_app( | |
| service: &dyn I::IVirtualizationService, | |
| config: RunAppConfig, | |
| ) -> Result<(), Error> { | |
| let apk = File::open(&config.apk).context("Failed to open APK file")?; | |
| let extra_apks = match config.config_path.as_deref() { | |
| Some(path) => parse_extra_apk_list(&config.apk, path)?, | |
| None => config.extra_apks.to_vec(), | |
| }; | |
| if extra_apks.len() != config.extra_idsigs.len() { | |
| bail!( | |
| "Found {} extra apks, but there are {} extra idsigs", | |
| extra_apks.len(), | |
| config.extra_idsigs.len() | |
| ) | |
| } | |
| for (i, extra_apk) in extra_apks.iter().enumerate() { | |
| let extra_apk_fd = ParcelFileDescriptor::new(File::open(extra_apk)?); | |
| let extra_idsig_fd = ParcelFileDescriptor::new(File::create(&config.extra_idsigs[i])?); | |
| service.createOrUpdateIdsigFile(&extra_apk_fd, &extra_idsig_fd)?; | |
| } | |
| let idsig = File::create(&config.idsig).context("Failed to create idsig file")?; | |
| let apk_fd = ParcelFileDescriptor::new(apk); | |
| let idsig_fd = ParcelFileDescriptor::new(idsig); | |
| service.createOrUpdateIdsigFile(&apk_fd, &idsig_fd)?; | |
| let idsig = File::open(&config.idsig).context("Failed to open idsig file")?; | |
| let idsig_fd = ParcelFileDescriptor::new(idsig); | |
| if !config.instance.exists() { | |
| const INSTANCE_FILE_SIZE: u64 = 10 * 1024 * 1024; | |
| command_create_partition( | |
| service, | |
| &config.instance, | |
| INSTANCE_FILE_SIZE, | |
| I::PartitionType::ANDROID_VM_INSTANCE, | |
| )?; | |
| } | |
| let instance_id = { | |
| let id_file = config.instance_id; | |
| if id_file.exists() { | |
| let mut id = [0u8; 64]; | |
| let mut instance_id_file = File::open(id_file)?; | |
| instance_id_file.read_exact(&mut id)?; | |
| id | |
| } else { | |
| let id = service.allocateInstanceId().context("Failed to allocate instance_id")?; | |
| let mut instance_id_file = File::create(id_file)?; | |
| instance_id_file.write_all(&id)?; | |
| id | |
| } | |
| }; | |
| let storage = if let Some(ref path) = config.storage { | |
| if !path.exists() { | |
| command_create_partition( | |
| service, | |
| path, | |
| config.microdroid.storage_size.unwrap_or(10 * 1024 * 1024), | |
| I::PartitionType::ENCRYPTEDSTORE, | |
| )?; | |
| } else if let Some(storage_size) = config.microdroid.storage_size { | |
| set_encrypted_storage(service, path, storage_size)?; | |
| } | |
| Some(open_parcel_file(path, true)?) | |
| } else { | |
| None | |
| }; | |
| let vendor = | |
| config.microdroid.vendor().as_ref().map(|p| open_parcel_file(p, false)).transpose()?; | |
| let extra_idsig_files: Result<Vec<_>, _> = config.extra_idsigs.iter().map(File::open).collect(); | |
| let extra_idsig_fds = extra_idsig_files?.into_iter().map(ParcelFileDescriptor::new).collect(); | |
| let payload = if let Some(config_path) = config.config_path { | |
| if config.payload_binary_name.is_some() { | |
| bail!("Only one of --config-path or --payload-binary-name can be defined") | |
| } | |
| I::Payload::ConfigPath(config_path) | |
| } else if let Some(payload_binary_name) = config.payload_binary_name { | |
| let extra_apk_files: Result<Vec<_>, _> = extra_apks.iter().map(File::open).collect(); | |
| let extra_apk_fds = extra_apk_files?.into_iter().map(ParcelFileDescriptor::new).collect(); | |
| I::Payload::PayloadConfig(I::VirtualMachinePayloadConfig { | |
| payloadBinaryName: payload_binary_name, | |
| extraApks: extra_apk_fds, | |
| }) | |
| } else { | |
| bail!("Either --config-path or --payload-binary-name must be defined") | |
| }; | |
| let os_name = if let Some(ref os) = config.microdroid.os { os } else { "microdroid" }; | |
| let payload_config_str = format!("{:?}!{:?}", config.apk, payload); | |
| let profile_name = config.common.profile().cloned(); | |
| let mut custom_config = I::CustomConfig { | |
| gdbPort: config.debug.gdb.map(u16::from).unwrap_or(0) as i32, // 0 means no gdb | |
| vendorImage: vendor, | |
| devices: config | |
| .microdroid | |
| .devices() | |
| .iter() | |
| .map(|d| d.to_device_selector()) | |
| .collect::<Result<Vec<_>, _>>()?, | |
| networkSupported: config.common.network_supported(), | |
| teeServices: config.common.tee_services, | |
| ..Default::default() | |
| }; | |
| let cpu_options = I::CpuOptions { | |
| cpuTopology: config.common.cpu_topology, | |
| boostUclamp: config.common.boost_uclamp, | |
| ..Default::default() | |
| }; | |
| if config.debug.enable_earlycon() { | |
| if config.debug.debug != I::DebugLevel::FULL { | |
| bail!("earlycon is only supported for debuggable VMs") | |
| } | |
| if cfg!(target_arch = "aarch64") { | |
| custom_config | |
| .extraKernelCmdlineParams | |
| .push(String::from("earlycon=uart8250,mmio,0x3f8")); | |
| } else if cfg!(target_arch = "x86_64") { | |
| custom_config.extraKernelCmdlineParams.push(String::from("earlycon=uart8250,io,0x3f8")); | |
| } else { | |
| bail!("unexpected architecture!"); | |
| } | |
| custom_config.extraKernelCmdlineParams.push(String::from("keep_bootcon")); | |
| } | |
| if config.microdroid.ephemeral() { | |
| custom_config.wantUpdatable = false; | |
| } | |
| #[cfg(vm_to_host_services)] | |
| let host_services = config.common.host_services.clone(); | |
| #[cfg(not(vm_to_host_services))] | |
| let host_services: Vec<String> = Vec::new(); | |
| let vm_config = I::VirtualMachineConfig::AppConfig(I::VirtualMachineAppConfig { | |
| name: config.common.name.unwrap_or_else(|| String::from("VmRunApp")), | |
| apk: apk_fd.into(), | |
| idsig: idsig_fd.into(), | |
| extraIdsigs: extra_idsig_fds, | |
| instanceImage: open_parcel_file(&config.instance, true /* writable */)?.into(), | |
| instanceId: instance_id, | |
| encryptedStorageImage: storage, | |
| payload, | |
| debugLevel: config.debug.debug, | |
| protectedVm: config.common.protected, | |
| memoryMib: config.common.mem.unwrap_or(0) as i32, // 0 means use the VM default | |
| cpuOptions: cpu_options, | |
| customConfig: Some(custom_config), | |
| osName: os_name.to_string(), | |
| hugePages: config.common.hugepages, | |
| hostServices: host_services, | |
| // If a Microdroid VM was started via vm CLI, then most likely a developer would want to | |
| // adb shell into it, hence automatically start the adbd service. | |
| autoStartAdbd: true, | |
| ramdump: config.debug.ramdump, | |
| profileName: profile_name, | |
| ..Default::default() | |
| }); | |
| run( | |
| service, | |
| &vm_config, | |
| &payload_config_str, | |
| config.debug.console.as_ref().map(|p| p.as_ref()), | |
| config.debug.console_in.as_ref().map(|p| p.as_ref()), | |
| config.debug.log.as_ref().map(|p| p.as_ref()), | |
| config.debug.dump_device_tree.as_ref().map(|p| p.as_ref()), | |
| ) | |
| } | |
| fn find_empty_payload_apk_path() -> Result<PathBuf, Error> { | |
| const GLOB_PATTERN: &str = "/apex/com.android.virt/app/**/EmptyPayloadApp*.apk"; | |
| let mut entries: Vec<PathBuf> = | |
| glob(GLOB_PATTERN).context("failed to glob")?.filter_map(|e| e.ok()).collect(); | |
| if entries.len() > 1 { | |
| return Err(anyhow!("Found more than one apk matching {}", GLOB_PATTERN)); | |
| } | |
| match entries.pop() { | |
| Some(path) => Ok(path), | |
| None => Err(anyhow!("No apks match {}", GLOB_PATTERN)), | |
| } | |
| } | |
| fn create_work_dir() -> Result<PathBuf, Error> { | |
| let s: String = rand::rng().sample_iter(&Alphanumeric).take(17).map(char::from).collect(); | |
| let work_dir = PathBuf::from("/data/local/tmp/microdroid").join(s); | |
| println!("creating work dir {}", work_dir.display()); | |
| fs::create_dir_all(&work_dir).context("failed to mkdir")?; | |
| Ok(work_dir) | |
| } | |
| /// Run a VM with Microdroid | |
| pub fn command_run_microdroid( | |
| service: &dyn I::IVirtualizationService, | |
| config: RunMicrodroidConfig, | |
| ) -> Result<(), Error> { | |
| let apk = find_empty_payload_apk_path()?; | |
| println!("found path {}", apk.display()); | |
| let work_dir = config.work_dir.unwrap_or(create_work_dir()?); | |
| let idsig = work_dir.join("apk.idsig"); | |
| println!("apk.idsig path: {}", idsig.display()); | |
| let instance_img = work_dir.join("instance.img"); | |
| println!("instance.img path: {}", instance_img.display()); | |
| let storage_img = work_dir.join("storage.img"); | |
| println!("encryptedstore backing disk: {}", storage_img.display()); | |
| let mut app_config = RunAppConfig { | |
| common: config.common, | |
| debug: config.debug, | |
| microdroid: config.microdroid, | |
| apk, | |
| idsig, | |
| instance: instance_img, | |
| storage: Some(storage_img), | |
| payload_binary_name: Some("MicrodroidEmptyPayloadJniLib.so".to_owned()), | |
| ..Default::default() | |
| }; | |
| app_config.set_instance_id(work_dir.join("instance_id")); | |
| println!("instance_id file path: {}", app_config.instance_id.display()); | |
| command_run_app(service, app_config) | |
| } | |
| /// Run a VM from the given configuration file. | |
| #[allow(clippy::needless_update)] | |
| pub fn command_run( | |
| service: &dyn I::IVirtualizationService, | |
| config: RunCustomVmConfig, | |
| ) -> Result<(), Error> { | |
| let config_file = File::open(&config.config).context("Failed to open config file")?; | |
| let mut vm_config = | |
| VmConfig::load(&config_file).context("Failed to parse config file")?.to_parcelable()?; | |
| if let Some(mem) = config.common.mem { | |
| vm_config.memoryMib = mem as i32; | |
| } | |
| if let Some(ref name) = config.common.name { | |
| vm_config.name = name.to_string(); | |
| } else { | |
| vm_config.name = String::from("VmRun"); | |
| } | |
| // protected can be from either config or commandline | |
| vm_config.protectedVm |= config.common.protected; | |
| if let Some(gdb) = config.debug.gdb { | |
| vm_config.gdbPort = gdb.get() as i32; | |
| } | |
| vm_config.cpuOptions = I::CpuOptions { | |
| cpuTopology: config.common.cpu_topology.clone(), | |
| boostUclamp: config.common.boost_uclamp, | |
| ..Default::default() | |
| }; | |
| vm_config.hugePages = config.common.hugepages; | |
| vm_config.teeServices = config.common.tee_services; | |
| vm_config.ramdump = config.debug.ramdump; | |
| run( | |
| service, | |
| &I::VirtualMachineConfig::RawConfig(vm_config), | |
| &format!("{:?}", &config.config), | |
| config.debug.console.as_ref().map(|p| p.as_ref()), | |
| config.debug.console_in.as_ref().map(|p| p.as_ref()), | |
| config.debug.log.as_ref().map(|p| p.as_ref()), | |
| config.debug.dump_device_tree.as_ref().map(|p| p.as_ref()), | |
| ) | |
| } | |
| fn state_to_str(vm_state: I::VirtualMachineState) -> &'static str { | |
| match vm_state { | |
| I::VirtualMachineState::NOT_STARTED => "NOT_STARTED", | |
| I::VirtualMachineState::STARTING => "STARTING", | |
| I::VirtualMachineState::STARTED => "STARTED", | |
| I::VirtualMachineState::READY => "READY", | |
| I::VirtualMachineState::FINISHED => "FINISHED", | |
| I::VirtualMachineState::DEAD => "DEAD", | |
| _ => "UNKNOWN", | |
| } | |
| } | |
| fn run( | |
| service: &dyn I::IVirtualizationService, | |
| config: &I::VirtualMachineConfig, | |
| payload_config: &str, | |
| console_out_path: Option<&Path>, | |
| console_in_path: Option<&Path>, | |
| log_path: Option<&Path>, | |
| dump_device_tree: Option<&Path>, | |
| ) -> Result<(), Error> { | |
| let console_out = | |
| if let Some(console_out_path) = console_out_path { | |
| Some(File::create(console_out_path).with_context(|| { | |
| format!("Failed to open console output file {console_out_path:?}") | |
| })?) | |
| } else { | |
| Some(duplicate_fd(io::stdout())?) | |
| }; | |
| let console_in = | |
| if let Some(console_in_path) = console_in_path { | |
| Some(File::open(console_in_path).with_context(|| { | |
| format!("Failed to open console input file {console_in_path:?}") | |
| })?) | |
| } else { | |
| Some(duplicate_fd(io::stdin())?) | |
| }; | |
| let log = if let Some(log_path) = log_path { | |
| Some( | |
| File::create(log_path) | |
| .with_context(|| format!("Failed to open log file {log_path:?}"))?, | |
| ) | |
| } else { | |
| Some(duplicate_fd(io::stdout())?) | |
| }; | |
| let dump_dt = if let Some(dump_device_tree) = dump_device_tree { | |
| Some(File::create(dump_device_tree).with_context(|| { | |
| format!("Failed to open file to dump device tree: {dump_device_tree:?}") | |
| })?) | |
| } else { | |
| None | |
| }; | |
| let vm = VmInstance::create(service, config, console_out, console_in, log, dump_dt) | |
| .context("Failed to create VM")?; | |
| let callback = Box::new(Callback {}); | |
| vm.start(Some(callback)).context("Failed to start VM")?; | |
| let debug_level = get_debug_level(config).unwrap_or(I::DebugLevel::NONE); | |
| println!( | |
| "Created {} from {} with CID {}, state is {}.", | |
| if debug_level == I::DebugLevel::FULL { "debuggable VM" } else { "VM" }, | |
| payload_config, | |
| vm.cid(), | |
| state_to_str(vm.state()?) | |
| ); | |
| // Wait until the VM or VirtualizationService dies. If we just returned immediately then the | |
| // IVirtualMachine Binder object would be dropped and the VM would be killed. | |
| let death_reason = vm.wait_for_death(); | |
| println!("VM ended: {death_reason:?}"); | |
| Ok(()) | |
| } | |
| fn parse_extra_apk_list(apk: &Path, config_path: &str) -> Result<Vec<PathBuf>, Error> { | |
| let mut archive = ZipArchive::new(File::open(apk)?)?; | |
| let config_file = archive.by_name(config_path)?; | |
| let config: VmPayloadConfig = serde_json::from_reader(config_file)?; | |
| Ok(config.extra_apks.into_iter().map(|x| x.path.into()).collect()) | |
| } | |
| fn set_encrypted_storage( | |
| service: &dyn I::IVirtualizationService, | |
| image_path: &Path, | |
| size: u64, | |
| ) -> Result<(), Error> { | |
| let image = OpenOptions::new() | |
| .create_new(false) | |
| .read(true) | |
| .write(true) | |
| .open(image_path) | |
| .with_context(|| format!("Failed to open {image_path:?}"))?; | |
| service.setEncryptedStorageSize(&ParcelFileDescriptor::new(image), size.try_into()?)?; | |
| Ok(()) | |
| } | |
| struct Callback {} | |
| impl vmclient::VmCallback for Callback { | |
| fn on_payload_started(&self, _cid: i32) { | |
| eprintln!("payload started"); | |
| } | |
| fn on_payload_ready(&self, _cid: i32) { | |
| eprintln!("payload is ready"); | |
| } | |
| fn on_payload_finished(&self, _cid: i32, exit_code: i32) { | |
| eprintln!("payload finished with exit code {exit_code}"); | |
| } | |
| fn on_error(&self, _cid: i32, error_code: ErrorCode, message: &str) { | |
| eprintln!("VM encountered an error: code={error_code:?}, message={message}"); | |
| } | |
| } | |
| /// Safely duplicate the file descriptor. | |
| fn duplicate_fd<T: AsFd>(file: T) -> io::Result<File> { | |
| Ok(file.as_fd().try_clone_to_owned()?.into()) | |
| } |