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