Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 1 | // 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 | //! Payload disk image |
| 16 | |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 17 | use android_system_virtualizationservice::aidl::android::system::virtualizationservice::{ |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame] | 18 | DiskImage::DiskImage, Partition::Partition, VirtualMachineAppConfig::DebugLevel::DebugLevel, |
| 19 | VirtualMachineAppConfig::VirtualMachineAppConfig, |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 20 | VirtualMachineRawConfig::VirtualMachineRawConfig, |
| 21 | }; |
| 22 | use android_system_virtualizationservice::binder::ParcelFileDescriptor; |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 23 | use anyhow::{anyhow, bail, Context, Result}; |
Jooyung Han | 53cf799 | 2021-10-18 19:38:41 +0900 | [diff] [blame] | 24 | use binder::wait_for_interface; |
Alan Stokes | bf20c6a | 2022-01-04 12:30:50 +0000 | [diff] [blame^] | 25 | use log::{info, warn}; |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 26 | use microdroid_metadata::{ApexPayload, ApkPayload, Metadata}; |
Jooyung Han | 5dc4217 | 2021-10-05 16:43:47 +0900 | [diff] [blame] | 27 | use microdroid_payload_config::{ApexConfig, VmPayloadConfig}; |
Jooyung Han | 9900f3d | 2021-07-06 10:27:54 +0900 | [diff] [blame] | 28 | use once_cell::sync::OnceCell; |
Jooyung Han | 5dc4217 | 2021-10-05 16:43:47 +0900 | [diff] [blame] | 29 | use packagemanager_aidl::aidl::android::content::pm::IPackageManagerNative::IPackageManagerNative; |
Alan Stokes | bf20c6a | 2022-01-04 12:30:50 +0000 | [diff] [blame^] | 30 | use regex::Regex; |
Jooyung Han | 44b02ab | 2021-07-16 03:19:13 +0900 | [diff] [blame] | 31 | use serde::Deserialize; |
| 32 | use serde_xml_rs::from_reader; |
Alan Stokes | bf20c6a | 2022-01-04 12:30:50 +0000 | [diff] [blame^] | 33 | use std::collections::HashSet; |
Jooyung Han | 44b02ab | 2021-07-16 03:19:13 +0900 | [diff] [blame] | 34 | use std::fs::{File, OpenOptions}; |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 35 | use std::path::{Path, PathBuf}; |
Alan Stokes | bf20c6a | 2022-01-04 12:30:50 +0000 | [diff] [blame^] | 36 | use std::process::Command; |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 37 | use vmconfig::open_parcel_file; |
| 38 | |
| 39 | /// The list of APEXes which microdroid requires. |
| 40 | // TODO(b/192200378) move this to microdroid.json? |
Inseob Kim | b4868e6 | 2021-11-09 17:28:23 +0900 | [diff] [blame] | 41 | const MICRODROID_REQUIRED_APEXES: [&str; 1] = ["com.android.os.statsd"]; |
| 42 | const MICRODROID_REQUIRED_APEXES_DEBUG: [&str; 1] = ["com.android.adbd"]; |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 43 | |
Jooyung Han | 44b02ab | 2021-07-16 03:19:13 +0900 | [diff] [blame] | 44 | const APEX_INFO_LIST_PATH: &str = "/apex/apex-info-list.xml"; |
| 45 | |
Jooyung Han | 5dc4217 | 2021-10-05 16:43:47 +0900 | [diff] [blame] | 46 | const PACKAGE_MANAGER_NATIVE_SERVICE: &str = "package_native"; |
| 47 | |
Jooyung Han | 73bac24 | 2021-07-02 10:25:49 +0900 | [diff] [blame] | 48 | /// Represents the list of APEXes |
Jooyung Han | 9d7cd7d | 2021-10-12 17:44:14 +0900 | [diff] [blame] | 49 | #[derive(Clone, Debug, Deserialize)] |
Jooyung Han | 9900f3d | 2021-07-06 10:27:54 +0900 | [diff] [blame] | 50 | struct ApexInfoList { |
Jooyung Han | 44b02ab | 2021-07-16 03:19:13 +0900 | [diff] [blame] | 51 | #[serde(rename = "apex-info")] |
Jooyung Han | 73bac24 | 2021-07-02 10:25:49 +0900 | [diff] [blame] | 52 | list: Vec<ApexInfo>, |
| 53 | } |
| 54 | |
Jooyung Han | 9d7cd7d | 2021-10-12 17:44:14 +0900 | [diff] [blame] | 55 | #[derive(Clone, Debug, Deserialize)] |
Jooyung Han | 73bac24 | 2021-07-02 10:25:49 +0900 | [diff] [blame] | 56 | struct ApexInfo { |
Jooyung Han | 44b02ab | 2021-07-16 03:19:13 +0900 | [diff] [blame] | 57 | #[serde(rename = "moduleName")] |
Jooyung Han | 73bac24 | 2021-07-02 10:25:49 +0900 | [diff] [blame] | 58 | name: String, |
Jooyung Han | 44b02ab | 2021-07-16 03:19:13 +0900 | [diff] [blame] | 59 | #[serde(rename = "modulePath")] |
Jooyung Han | 73bac24 | 2021-07-02 10:25:49 +0900 | [diff] [blame] | 60 | path: PathBuf, |
Jooyung Han | 9d7cd7d | 2021-10-12 17:44:14 +0900 | [diff] [blame] | 61 | |
| 62 | #[serde(default)] |
Alan Stokes | 46ac386 | 2021-12-21 15:31:47 +0000 | [diff] [blame] | 63 | has_classpath_jar: bool, |
Jooyung Han | 73bac24 | 2021-07-02 10:25:49 +0900 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | impl ApexInfoList { |
| 67 | /// Loads ApexInfoList |
Jooyung Han | 9900f3d | 2021-07-06 10:27:54 +0900 | [diff] [blame] | 68 | fn load() -> Result<&'static ApexInfoList> { |
| 69 | static INSTANCE: OnceCell<ApexInfoList> = OnceCell::new(); |
| 70 | INSTANCE.get_or_try_init(|| { |
Jooyung Han | 44b02ab | 2021-07-16 03:19:13 +0900 | [diff] [blame] | 71 | let apex_info_list = File::open(APEX_INFO_LIST_PATH) |
| 72 | .context(format!("Failed to open {}", APEX_INFO_LIST_PATH))?; |
Jooyung Han | 9d7cd7d | 2021-10-12 17:44:14 +0900 | [diff] [blame] | 73 | let mut apex_info_list: ApexInfoList = from_reader(apex_info_list) |
Jooyung Han | 44b02ab | 2021-07-16 03:19:13 +0900 | [diff] [blame] | 74 | .context(format!("Failed to parse {}", APEX_INFO_LIST_PATH))?; |
Jooyung Han | 9d7cd7d | 2021-10-12 17:44:14 +0900 | [diff] [blame] | 75 | |
Alan Stokes | bf20c6a | 2022-01-04 12:30:50 +0000 | [diff] [blame^] | 76 | // For active APEXes, we run derive_classpath and parse its output to see if it |
| 77 | // contributes to the classpath(s). (This allows us to handle any new classpath env |
| 78 | // vars seamlessly.) |
| 79 | let classpath_vars = run_derive_classpath()?; |
| 80 | let classpath_apexes = find_apex_names_in_classpath(&classpath_vars)?; |
| 81 | |
Jooyung Han | 9d7cd7d | 2021-10-12 17:44:14 +0900 | [diff] [blame] | 82 | for apex_info in apex_info_list.list.iter_mut() { |
Alan Stokes | bf20c6a | 2022-01-04 12:30:50 +0000 | [diff] [blame^] | 83 | apex_info.has_classpath_jar = classpath_apexes.contains(&apex_info.name); |
Jooyung Han | 9d7cd7d | 2021-10-12 17:44:14 +0900 | [diff] [blame] | 84 | } |
Alan Stokes | bf20c6a | 2022-01-04 12:30:50 +0000 | [diff] [blame^] | 85 | |
Jooyung Han | 44b02ab | 2021-07-16 03:19:13 +0900 | [diff] [blame] | 86 | Ok(apex_info_list) |
Jooyung Han | 9900f3d | 2021-07-06 10:27:54 +0900 | [diff] [blame] | 87 | }) |
Jooyung Han | 73bac24 | 2021-07-02 10:25:49 +0900 | [diff] [blame] | 88 | } |
| 89 | |
Jooyung Han | 9d7cd7d | 2021-10-12 17:44:14 +0900 | [diff] [blame] | 90 | /// Returns the list of apex names matching with the predicate |
| 91 | fn get_matching(&self, predicate: fn(&ApexInfo) -> bool) -> Vec<String> { |
| 92 | self.list.iter().filter(|info| predicate(info)).map(|info| info.name.clone()).collect() |
| 93 | } |
| 94 | |
Jooyung Han | 73bac24 | 2021-07-02 10:25:49 +0900 | [diff] [blame] | 95 | fn get_path_for(&self, apex_name: &str) -> Result<PathBuf> { |
| 96 | Ok(self |
| 97 | .list |
| 98 | .iter() |
| 99 | .find(|apex| apex.name == apex_name) |
| 100 | .ok_or_else(|| anyhow!("{} not found.", apex_name))? |
| 101 | .path |
| 102 | .clone()) |
| 103 | } |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 104 | } |
| 105 | |
Jooyung Han | 5dc4217 | 2021-10-05 16:43:47 +0900 | [diff] [blame] | 106 | struct PackageManager { |
Jooyung Han | 5dc4217 | 2021-10-05 16:43:47 +0900 | [diff] [blame] | 107 | // TODO(b/199146189) use IPackageManagerNative |
| 108 | apex_info_list: &'static ApexInfoList, |
| 109 | } |
| 110 | |
| 111 | impl PackageManager { |
| 112 | fn new() -> Result<Self> { |
Jooyung Han | 5dc4217 | 2021-10-05 16:43:47 +0900 | [diff] [blame] | 113 | let apex_info_list = ApexInfoList::load()?; |
Jooyung Han | 53cf799 | 2021-10-18 19:38:41 +0900 | [diff] [blame] | 114 | Ok(Self { apex_info_list }) |
Jooyung Han | 5dc4217 | 2021-10-05 16:43:47 +0900 | [diff] [blame] | 115 | } |
| 116 | |
Jooyung Han | 9d7cd7d | 2021-10-12 17:44:14 +0900 | [diff] [blame] | 117 | fn get_apex_list(&self, prefer_staged: bool) -> Result<ApexInfoList> { |
Jooyung Han | 53cf799 | 2021-10-18 19:38:41 +0900 | [diff] [blame] | 118 | // get the list of active apexes |
Jooyung Han | 9d7cd7d | 2021-10-12 17:44:14 +0900 | [diff] [blame] | 119 | let mut list = self.apex_info_list.clone(); |
Jooyung Han | 53cf799 | 2021-10-18 19:38:41 +0900 | [diff] [blame] | 120 | // When prefer_staged, we override ApexInfo by consulting "package_native" |
Jooyung Han | 5dc4217 | 2021-10-05 16:43:47 +0900 | [diff] [blame] | 121 | if prefer_staged { |
Jooyung Han | 53cf799 | 2021-10-18 19:38:41 +0900 | [diff] [blame] | 122 | let pm = |
| 123 | wait_for_interface::<dyn IPackageManagerNative>(PACKAGE_MANAGER_NATIVE_SERVICE) |
| 124 | .context("Failed to get service when prefer_staged is set.")?; |
| 125 | let staged = pm.getStagedApexModuleNames()?; |
Jooyung Han | 9d7cd7d | 2021-10-12 17:44:14 +0900 | [diff] [blame] | 126 | for apex_info in list.list.iter_mut() { |
| 127 | if staged.contains(&apex_info.name) { |
Jooyung Han | 53cf799 | 2021-10-18 19:38:41 +0900 | [diff] [blame] | 128 | let staged_apex_info = pm.getStagedApexInfo(&apex_info.name)?; |
Jooyung Han | 9d7cd7d | 2021-10-12 17:44:14 +0900 | [diff] [blame] | 129 | if let Some(staged_apex_info) = staged_apex_info { |
| 130 | apex_info.path = PathBuf::from(staged_apex_info.diskImagePath); |
Alan Stokes | 46ac386 | 2021-12-21 15:31:47 +0000 | [diff] [blame] | 131 | apex_info.has_classpath_jar = staged_apex_info.hasClassPathJars; |
Jooyung Han | 9d7cd7d | 2021-10-12 17:44:14 +0900 | [diff] [blame] | 132 | } |
| 133 | } |
Jooyung Han | 5dc4217 | 2021-10-05 16:43:47 +0900 | [diff] [blame] | 134 | } |
| 135 | } |
Jooyung Han | 9d7cd7d | 2021-10-12 17:44:14 +0900 | [diff] [blame] | 136 | Ok(list) |
Jooyung Han | 5dc4217 | 2021-10-05 16:43:47 +0900 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 140 | fn make_metadata_file( |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 141 | config_path: &str, |
Jooyung Han | 5e0f206 | 2021-10-12 14:00:46 +0900 | [diff] [blame] | 142 | apex_names: &[String], |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 143 | temporary_directory: &Path, |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 144 | ) -> Result<ParcelFileDescriptor> { |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 145 | let metadata_path = temporary_directory.join("metadata"); |
| 146 | let metadata = Metadata { |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 147 | version: 1, |
Jooyung Han | 5e0f206 | 2021-10-12 14:00:46 +0900 | [diff] [blame] | 148 | apexes: apex_names |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 149 | .iter() |
Jooyung Han | 19c1d6c | 2021-08-06 14:08:16 +0900 | [diff] [blame] | 150 | .enumerate() |
Jooyung Han | 5e0f206 | 2021-10-12 14:00:46 +0900 | [diff] [blame] | 151 | .map(|(i, apex_name)| ApexPayload { |
| 152 | name: apex_name.clone(), |
Jooyung Han | 19c1d6c | 2021-08-06 14:08:16 +0900 | [diff] [blame] | 153 | partition_name: format!("microdroid-apex-{}", i), |
| 154 | ..Default::default() |
| 155 | }) |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 156 | .collect(), |
| 157 | apk: Some(ApkPayload { |
Jooyung Han | 35edb8f | 2021-07-01 16:17:16 +0900 | [diff] [blame] | 158 | name: "apk".to_owned(), |
| 159 | payload_partition_name: "microdroid-apk".to_owned(), |
| 160 | idsig_partition_name: "microdroid-apk-idsig".to_owned(), |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 161 | ..Default::default() |
| 162 | }) |
| 163 | .into(), |
| 164 | payload_config_path: format!("/mnt/apk/{}", config_path), |
| 165 | ..Default::default() |
| 166 | }; |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 167 | |
| 168 | // Write metadata to file. |
| 169 | let mut metadata_file = OpenOptions::new() |
| 170 | .create_new(true) |
| 171 | .read(true) |
| 172 | .write(true) |
| 173 | .open(&metadata_path) |
| 174 | .with_context(|| format!("Failed to open metadata file {:?}", metadata_path))?; |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 175 | microdroid_metadata::write_metadata(&metadata, &mut metadata_file)?; |
| 176 | |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 177 | // Re-open the metadata file as read-only. |
| 178 | open_parcel_file(&metadata_path, false) |
| 179 | } |
| 180 | |
| 181 | /// Creates a DiskImage with partitions: |
| 182 | /// metadata: metadata |
| 183 | /// microdroid-apex-0: apex 0 |
| 184 | /// microdroid-apex-1: apex 1 |
| 185 | /// .. |
| 186 | /// microdroid-apk: apk |
| 187 | /// microdroid-apk-idsig: idsig |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 188 | /// extra-apk-0: additional apk 0 |
| 189 | /// extra-idsig-0: additional idsig 0 |
| 190 | /// extra-apk-1: additional apk 1 |
| 191 | /// extra-idsig-1: additional idsig 1 |
| 192 | /// .. |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 193 | fn make_payload_disk( |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 194 | app_config: &VirtualMachineAppConfig, |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 195 | apk_file: File, |
| 196 | idsig_file: File, |
Jooyung Han | 9d7cd7d | 2021-10-12 17:44:14 +0900 | [diff] [blame] | 197 | vm_payload_config: &VmPayloadConfig, |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 198 | temporary_directory: &Path, |
| 199 | ) -> Result<DiskImage> { |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 200 | if vm_payload_config.extra_apks.len() != app_config.extraIdsigs.len() { |
| 201 | bail!( |
| 202 | "payload config has {} apks, but app config has {} idsigs", |
| 203 | vm_payload_config.extra_apks.len(), |
| 204 | app_config.extraIdsigs.len() |
| 205 | ); |
| 206 | } |
| 207 | |
Jooyung Han | 9d7cd7d | 2021-10-12 17:44:14 +0900 | [diff] [blame] | 208 | let pm = PackageManager::new()?; |
| 209 | let apex_list = pm.get_apex_list(vm_payload_config.prefer_staged)?; |
| 210 | |
| 211 | // collect APEX names from config |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 212 | let apexes = collect_apex_names(&apex_list, &vm_payload_config.apexes, app_config.debugLevel); |
Jooyung Han | 9d7cd7d | 2021-10-12 17:44:14 +0900 | [diff] [blame] | 213 | info!("Microdroid payload APEXes: {:?}", apexes); |
| 214 | |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 215 | let metadata_file = make_metadata_file(&app_config.configPath, &apexes, temporary_directory)?; |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 216 | // put metadata at the first partition |
| 217 | let mut partitions = vec![Partition { |
Jooyung Han | 14e5a8e | 2021-07-06 20:48:38 +0900 | [diff] [blame] | 218 | label: "payload-metadata".to_owned(), |
Jooyung Han | 631d588 | 2021-07-29 06:34:05 +0900 | [diff] [blame] | 219 | image: Some(metadata_file), |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 220 | writable: false, |
| 221 | }]; |
| 222 | |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 223 | for (i, apex) in apexes.iter().enumerate() { |
Jooyung Han | 9d7cd7d | 2021-10-12 17:44:14 +0900 | [diff] [blame] | 224 | let apex_path = apex_list.get_path_for(apex)?; |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 225 | let apex_file = open_parcel_file(&apex_path, false)?; |
Jooyung Han | 9588463 | 2021-07-06 22:27:54 +0900 | [diff] [blame] | 226 | partitions.push(Partition { |
| 227 | label: format!("microdroid-apex-{}", i), |
Jooyung Han | 631d588 | 2021-07-29 06:34:05 +0900 | [diff] [blame] | 228 | image: Some(apex_file), |
Jooyung Han | 9588463 | 2021-07-06 22:27:54 +0900 | [diff] [blame] | 229 | writable: false, |
| 230 | }); |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 231 | } |
Jooyung Han | 9588463 | 2021-07-06 22:27:54 +0900 | [diff] [blame] | 232 | partitions.push(Partition { |
| 233 | label: "microdroid-apk".to_owned(), |
Jooyung Han | 631d588 | 2021-07-29 06:34:05 +0900 | [diff] [blame] | 234 | image: Some(ParcelFileDescriptor::new(apk_file)), |
Jooyung Han | 9588463 | 2021-07-06 22:27:54 +0900 | [diff] [blame] | 235 | writable: false, |
| 236 | }); |
| 237 | partitions.push(Partition { |
| 238 | label: "microdroid-apk-idsig".to_owned(), |
Jooyung Han | 631d588 | 2021-07-29 06:34:05 +0900 | [diff] [blame] | 239 | image: Some(ParcelFileDescriptor::new(idsig_file)), |
Jooyung Han | 9588463 | 2021-07-06 22:27:54 +0900 | [diff] [blame] | 240 | writable: false, |
| 241 | }); |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 242 | |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 243 | // we've already checked that extra_apks and extraIdsigs are in the same size. |
| 244 | let extra_apks = &vm_payload_config.extra_apks; |
| 245 | let extra_idsigs = &app_config.extraIdsigs; |
| 246 | for (i, (extra_apk, extra_idsig)) in extra_apks.iter().zip(extra_idsigs.iter()).enumerate() { |
| 247 | partitions.push(Partition { |
| 248 | label: format!("extra-apk-{}", i), |
| 249 | image: Some(ParcelFileDescriptor::new(File::open(PathBuf::from(&extra_apk.path))?)), |
| 250 | writable: false, |
| 251 | }); |
| 252 | |
| 253 | partitions.push(Partition { |
| 254 | label: format!("extra-idsig-{}", i), |
| 255 | image: Some(ParcelFileDescriptor::new(extra_idsig.as_ref().try_clone()?)), |
| 256 | writable: false, |
| 257 | }); |
| 258 | } |
| 259 | |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 260 | Ok(DiskImage { image: None, partitions, writable: false }) |
| 261 | } |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 262 | |
Alan Stokes | bf20c6a | 2022-01-04 12:30:50 +0000 | [diff] [blame^] | 263 | fn run_derive_classpath() -> Result<String> { |
| 264 | let result = Command::new("/apex/com.android.sdkext/bin/derive_classpath") |
| 265 | .arg("/proc/self/fd/1") |
| 266 | .output() |
| 267 | .context("Failed to run derive_classpath")?; |
| 268 | |
| 269 | if !result.status.success() { |
| 270 | bail!("derive_classpath returned {}", result.status); |
| 271 | } |
| 272 | |
| 273 | String::from_utf8(result.stdout).context("Converting derive_classpath output") |
| 274 | } |
| 275 | |
| 276 | fn find_apex_names_in_classpath(classpath_vars: &str) -> Result<HashSet<String>> { |
| 277 | // Each line should be in the format "export <var name> <paths>", where <paths> is a |
| 278 | // colon-separated list of paths to JARs. We don't care about the var names, and we're only |
| 279 | // interested in paths that look like "/apex/<apex name>/<anything>" so we know which APEXes |
| 280 | // contribute to at least one var. |
| 281 | let mut apexes = HashSet::new(); |
| 282 | |
| 283 | let pattern = Regex::new(r"^export [^ ]+ ([^ ]+)$").context("Failed to construct Regex")?; |
| 284 | for line in classpath_vars.lines() { |
| 285 | if let Some(captures) = pattern.captures(line) { |
| 286 | if let Some(paths) = captures.get(1) { |
| 287 | apexes.extend(paths.as_str().split(':').filter_map(|path| { |
| 288 | let path = path.strip_prefix("/apex/")?; |
| 289 | Some(path[..path.find('/')?].to_owned()) |
| 290 | })); |
| 291 | continue; |
| 292 | } |
| 293 | } |
| 294 | warn!("Malformed line from derive_classpath: {}", line); |
| 295 | } |
| 296 | |
| 297 | Ok(apexes) |
Jooyung Han | 5e0f206 | 2021-10-12 14:00:46 +0900 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | // Collect APEX names from config |
Inseob Kim | b4868e6 | 2021-11-09 17:28:23 +0900 | [diff] [blame] | 301 | fn collect_apex_names( |
| 302 | apex_list: &ApexInfoList, |
| 303 | apexes: &[ApexConfig], |
| 304 | debug_level: DebugLevel, |
| 305 | ) -> Vec<String> { |
Alan Stokes | 46ac386 | 2021-12-21 15:31:47 +0000 | [diff] [blame] | 306 | // Process pseudo names like "{CLASSPATH}". |
Jooyung Han | 5e0f206 | 2021-10-12 14:00:46 +0900 | [diff] [blame] | 307 | // For now we have following pseudo APEX names: |
Alan Stokes | 46ac386 | 2021-12-21 15:31:47 +0000 | [diff] [blame] | 308 | // - {CLASSPATH}: represents APEXes contributing to any derive_classpath environment variable |
Jooyung Han | 5e0f206 | 2021-10-12 14:00:46 +0900 | [diff] [blame] | 309 | let mut apex_names: Vec<String> = apexes |
| 310 | .iter() |
| 311 | .flat_map(|apex| match apex.name.as_str() { |
Alan Stokes | 46ac386 | 2021-12-21 15:31:47 +0000 | [diff] [blame] | 312 | "{CLASSPATH}" => apex_list.get_matching(|apex| apex.has_classpath_jar), |
Jooyung Han | 5e0f206 | 2021-10-12 14:00:46 +0900 | [diff] [blame] | 313 | _ => vec![apex.name.clone()], |
| 314 | }) |
| 315 | .collect(); |
| 316 | // Add required APEXes |
| 317 | apex_names.extend(MICRODROID_REQUIRED_APEXES.iter().map(|name| name.to_string())); |
Inseob Kim | b4868e6 | 2021-11-09 17:28:23 +0900 | [diff] [blame] | 318 | if debug_level != DebugLevel::NONE { |
| 319 | apex_names.extend(MICRODROID_REQUIRED_APEXES_DEBUG.iter().map(|name| name.to_string())); |
| 320 | } |
Jooyung Han | 5e0f206 | 2021-10-12 14:00:46 +0900 | [diff] [blame] | 321 | apex_names.sort(); |
| 322 | apex_names.dedup(); |
| 323 | apex_names |
| 324 | } |
| 325 | |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 326 | pub fn add_microdroid_images( |
| 327 | config: &VirtualMachineAppConfig, |
| 328 | temporary_directory: &Path, |
| 329 | apk_file: File, |
| 330 | idsig_file: File, |
Jiyong Park | 8d08181 | 2021-07-23 17:45:04 +0900 | [diff] [blame] | 331 | instance_file: File, |
Jooyung Han | 5dc4217 | 2021-10-05 16:43:47 +0900 | [diff] [blame] | 332 | vm_payload_config: &VmPayloadConfig, |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 333 | vm_config: &mut VirtualMachineRawConfig, |
| 334 | ) -> Result<()> { |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 335 | vm_config.disks.push(make_payload_disk( |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 336 | config, |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 337 | apk_file, |
| 338 | idsig_file, |
Jooyung Han | 9d7cd7d | 2021-10-12 17:44:14 +0900 | [diff] [blame] | 339 | vm_payload_config, |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 340 | temporary_directory, |
| 341 | )?); |
| 342 | |
Jiyong Park | acf31b0 | 2021-11-04 20:45:14 +0900 | [diff] [blame] | 343 | vm_config.disks[1].partitions.push(Partition { |
| 344 | label: "vbmeta".to_owned(), |
| 345 | image: Some(open_parcel_file( |
| 346 | Path::new("/apex/com.android.virt/etc/fs/microdroid_vbmeta_bootconfig.img"), |
| 347 | false, |
| 348 | )?), |
| 349 | writable: false, |
| 350 | }); |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame] | 351 | let bootconfig_image = "/apex/com.android.virt/etc/microdroid_bootconfig.".to_owned() |
| 352 | + match config.debugLevel { |
| 353 | DebugLevel::NONE => "normal", |
| 354 | DebugLevel::APP_ONLY => "app_debuggable", |
| 355 | DebugLevel::FULL => "full_debuggable", |
| 356 | _ => return Err(anyhow!("unsupported debug level: {:?}", config.debugLevel)), |
| 357 | }; |
| 358 | vm_config.disks[1].partitions.push(Partition { |
| 359 | label: "bootconfig".to_owned(), |
| 360 | image: Some(open_parcel_file(Path::new(&bootconfig_image), false)?), |
| 361 | writable: false, |
| 362 | }); |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 363 | |
Jiyong Park | 8d08181 | 2021-07-23 17:45:04 +0900 | [diff] [blame] | 364 | // instance image is at the second partition in the second disk. |
| 365 | vm_config.disks[1].partitions.push(Partition { |
| 366 | label: "vm-instance".to_owned(), |
Jooyung Han | 631d588 | 2021-07-29 06:34:05 +0900 | [diff] [blame] | 367 | image: Some(ParcelFileDescriptor::new(instance_file)), |
Jiyong Park | 8d08181 | 2021-07-23 17:45:04 +0900 | [diff] [blame] | 368 | writable: true, |
| 369 | }); |
| 370 | |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 371 | Ok(()) |
| 372 | } |
Jooyung Han | 5e0f206 | 2021-10-12 14:00:46 +0900 | [diff] [blame] | 373 | |
| 374 | #[cfg(test)] |
| 375 | mod tests { |
| 376 | use super::*; |
| 377 | #[test] |
Alan Stokes | bf20c6a | 2022-01-04 12:30:50 +0000 | [diff] [blame^] | 378 | fn test_find_apex_names_in_classpath() { |
| 379 | let vars = r#" |
| 380 | export FOO /apex/unterminated |
| 381 | export BAR /apex/valid.apex/something |
| 382 | wrong |
| 383 | export EMPTY |
| 384 | export OTHER /foo/bar:/baz:/apex/second.valid.apex/:gibberish:"#; |
| 385 | let expected = vec!["valid.apex", "second.valid.apex"]; |
| 386 | let expected: HashSet<_> = expected.into_iter().map(ToString::to_string).collect(); |
| 387 | |
| 388 | assert_eq!(find_apex_names_in_classpath(vars).unwrap(), expected); |
Jooyung Han | 5e0f206 | 2021-10-12 14:00:46 +0900 | [diff] [blame] | 389 | } |
| 390 | } |