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