Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [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 | //! Command to run a VM. |
| 16 | |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 17 | use crate::create_partition::command_create_partition; |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 18 | use android_system_virtualizationservice::aidl::android::system::virtualizationservice::{ |
David Brazdil | 7d1e5ec | 2023-02-06 17:56:29 +0000 | [diff] [blame] | 19 | CpuTopology::CpuTopology, |
Alan Stokes | 0d1ef78 | 2022-09-27 13:46:35 +0100 | [diff] [blame] | 20 | IVirtualizationService::IVirtualizationService, |
| 21 | PartitionType::PartitionType, |
Nikita Ioffe | a0eb5ee | 2023-06-26 18:18:21 +0100 | [diff] [blame^] | 22 | VirtualMachineAppConfig::{ |
| 23 | CustomConfig::CustomConfig, DebugLevel::DebugLevel, Payload::Payload, |
| 24 | VirtualMachineAppConfig, |
| 25 | }, |
Alan Stokes | 0d1ef78 | 2022-09-27 13:46:35 +0100 | [diff] [blame] | 26 | VirtualMachineConfig::VirtualMachineConfig, |
Inseob Kim | 7b5f65c | 2022-11-15 14:27:04 +0900 | [diff] [blame] | 27 | VirtualMachinePayloadConfig::VirtualMachinePayloadConfig, |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 28 | VirtualMachineState::VirtualMachineState, |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 29 | }; |
Nikita Ioffe | b0b6756 | 2022-11-22 15:48:06 +0000 | [diff] [blame] | 30 | use anyhow::{anyhow, bail, Context, Error}; |
Alan Stokes | 0e82b50 | 2022-08-08 14:44:48 +0100 | [diff] [blame] | 31 | use binder::ParcelFileDescriptor; |
Nikita Ioffe | fc04196 | 2023-01-18 00:10:40 +0000 | [diff] [blame] | 32 | use glob::glob; |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 33 | use microdroid_payload_config::VmPayloadConfig; |
Nikita Ioffe | b0b6756 | 2022-11-22 15:48:06 +0000 | [diff] [blame] | 34 | use rand::{distributions::Alphanumeric, Rng}; |
| 35 | use std::fs; |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 36 | use std::fs::File; |
Alan Stokes | f30982b | 2022-11-18 11:50:32 +0000 | [diff] [blame] | 37 | use std::io; |
Nikita Ioffe | 5776f08 | 2023-02-10 21:38:26 +0000 | [diff] [blame] | 38 | use std::num::NonZeroU16; |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 39 | use std::os::unix::io::{AsRawFd, FromRawFd}; |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 40 | use std::path::{Path, PathBuf}; |
Alan Stokes | 2bead0d | 2022-09-05 16:58:34 +0100 | [diff] [blame] | 41 | use vmclient::{ErrorCode, VmInstance}; |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 42 | use vmconfig::{open_parcel_file, VmConfig}; |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 43 | use zip::ZipArchive; |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 44 | |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 45 | /// Run a VM from the given APK, idsig, and config. |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 46 | #[allow(clippy::too_many_arguments)] |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 47 | pub fn command_run_app( |
Seungjae Yoo | 62085c0 | 2022-08-12 04:44:52 +0000 | [diff] [blame] | 48 | name: Option<String>, |
Andrew Walbran | 616d13f | 2022-05-12 18:35:55 +0000 | [diff] [blame] | 49 | service: &dyn IVirtualizationService, |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 50 | apk: &Path, |
| 51 | idsig: &Path, |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 52 | instance: &Path, |
Shikha Panwar | 22e7045 | 2022-10-10 18:32:55 +0000 | [diff] [blame] | 53 | storage: Option<&Path>, |
| 54 | storage_size: Option<u64>, |
Inseob Kim | 7b5f65c | 2022-11-15 14:27:04 +0900 | [diff] [blame] | 55 | config_path: Option<String>, |
Alan Stokes | 8f12f2b | 2023-01-09 09:19:20 +0000 | [diff] [blame] | 56 | payload_binary_name: Option<String>, |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 57 | console_path: Option<&Path>, |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 58 | log_path: Option<&Path>, |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame] | 59 | debug_level: DebugLevel, |
Andrew Walbran | 3994f00 | 2022-01-27 17:33:45 +0000 | [diff] [blame] | 60 | protected: bool, |
Jiyong Park | d63cfff | 2021-09-27 20:10:17 +0900 | [diff] [blame] | 61 | mem: Option<u32>, |
David Brazdil | 7d1e5ec | 2023-02-06 17:56:29 +0000 | [diff] [blame] | 62 | cpu_topology: CpuTopology, |
Jiyong Park | dfe16d6 | 2022-04-20 17:32:12 +0900 | [diff] [blame] | 63 | task_profiles: Vec<String>, |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 64 | extra_idsigs: &[PathBuf], |
Nikita Ioffe | 5776f08 | 2023-02-10 21:38:26 +0000 | [diff] [blame] | 65 | gdb: Option<NonZeroU16>, |
Nikita Ioffe | 26c35ed | 2023-06-05 17:49:08 +0100 | [diff] [blame] | 66 | kernel: Option<&Path>, |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 67 | ) -> Result<(), Error> { |
Steven Moreland | 6a55e2e | 2022-10-22 00:30:42 +0000 | [diff] [blame] | 68 | let apk_file = File::open(apk).context("Failed to open APK file")?; |
| 69 | |
Inseob Kim | 7b5f65c | 2022-11-15 14:27:04 +0900 | [diff] [blame] | 70 | let extra_apks = match config_path.as_deref() { |
| 71 | Some(path) => parse_extra_apk_list(apk, path)?, |
| 72 | None => vec![], |
| 73 | }; |
| 74 | |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 75 | if extra_apks.len() != extra_idsigs.len() { |
| 76 | bail!( |
| 77 | "Found {} extra apks, but there are {} extra idsigs", |
| 78 | extra_apks.len(), |
| 79 | extra_idsigs.len() |
| 80 | ) |
| 81 | } |
| 82 | |
| 83 | for i in 0..extra_apks.len() { |
| 84 | let extra_apk_fd = ParcelFileDescriptor::new(File::open(&extra_apks[i])?); |
| 85 | let extra_idsig_fd = ParcelFileDescriptor::new(File::create(&extra_idsigs[i])?); |
| 86 | service.createOrUpdateIdsigFile(&extra_apk_fd, &extra_idsig_fd)?; |
| 87 | } |
| 88 | |
Jiyong Park | 0a24843 | 2021-08-20 23:32:39 +0900 | [diff] [blame] | 89 | let idsig_file = File::create(idsig).context("Failed to create idsig file")?; |
| 90 | |
| 91 | let apk_fd = ParcelFileDescriptor::new(apk_file); |
| 92 | let idsig_fd = ParcelFileDescriptor::new(idsig_file); |
| 93 | service.createOrUpdateIdsigFile(&apk_fd, &idsig_fd)?; |
| 94 | |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 95 | let idsig_file = File::open(idsig).context("Failed to open idsig file")?; |
Jiyong Park | 0a24843 | 2021-08-20 23:32:39 +0900 | [diff] [blame] | 96 | let idsig_fd = ParcelFileDescriptor::new(idsig_file); |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 97 | |
| 98 | if !instance.exists() { |
| 99 | const INSTANCE_FILE_SIZE: u64 = 10 * 1024 * 1024; |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 100 | command_create_partition( |
Andrew Walbran | 616d13f | 2022-05-12 18:35:55 +0000 | [diff] [blame] | 101 | service, |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 102 | instance, |
| 103 | INSTANCE_FILE_SIZE, |
| 104 | PartitionType::ANDROID_VM_INSTANCE, |
| 105 | )?; |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 106 | } |
| 107 | |
Shikha Panwar | 22e7045 | 2022-10-10 18:32:55 +0000 | [diff] [blame] | 108 | let storage = if let Some(path) = storage { |
| 109 | if !path.exists() { |
| 110 | command_create_partition( |
| 111 | service, |
| 112 | path, |
| 113 | storage_size.unwrap_or(10 * 1024 * 1024), |
Shikha Panwar | 9fd198f | 2022-11-18 17:43:43 +0000 | [diff] [blame] | 114 | PartitionType::ENCRYPTEDSTORE, |
Shikha Panwar | 22e7045 | 2022-10-10 18:32:55 +0000 | [diff] [blame] | 115 | )?; |
| 116 | } |
| 117 | Some(open_parcel_file(path, true)?) |
| 118 | } else { |
| 119 | None |
| 120 | }; |
| 121 | |
Nikita Ioffe | 26c35ed | 2023-06-05 17:49:08 +0100 | [diff] [blame] | 122 | let kernel = kernel.map(|p| open_parcel_file(p, false)).transpose()?; |
| 123 | |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 124 | let extra_idsig_files: Result<Vec<File>, _> = extra_idsigs.iter().map(File::open).collect(); |
| 125 | let extra_idsig_fds = extra_idsig_files?.into_iter().map(ParcelFileDescriptor::new).collect(); |
| 126 | |
Inseob Kim | 7b5f65c | 2022-11-15 14:27:04 +0900 | [diff] [blame] | 127 | let payload = if let Some(config_path) = config_path { |
Alan Stokes | 8f12f2b | 2023-01-09 09:19:20 +0000 | [diff] [blame] | 128 | if payload_binary_name.is_some() { |
| 129 | bail!("Only one of --config-path or --payload-binary-name can be defined") |
Inseob Kim | 7b5f65c | 2022-11-15 14:27:04 +0900 | [diff] [blame] | 130 | } |
| 131 | Payload::ConfigPath(config_path) |
Alan Stokes | 8f12f2b | 2023-01-09 09:19:20 +0000 | [diff] [blame] | 132 | } else if let Some(payload_binary_name) = payload_binary_name { |
| 133 | Payload::PayloadConfig(VirtualMachinePayloadConfig { |
| 134 | payloadBinaryName: payload_binary_name, |
| 135 | }) |
Inseob Kim | 7b5f65c | 2022-11-15 14:27:04 +0900 | [diff] [blame] | 136 | } else { |
Alan Stokes | 8f12f2b | 2023-01-09 09:19:20 +0000 | [diff] [blame] | 137 | bail!("Either --config-path or --payload-binary-name must be defined") |
Inseob Kim | 7b5f65c | 2022-11-15 14:27:04 +0900 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | let payload_config_str = format!("{:?}!{:?}", apk, payload); |
| 141 | |
Nikita Ioffe | a0eb5ee | 2023-06-26 18:18:21 +0100 | [diff] [blame^] | 142 | let custom_config = CustomConfig { |
| 143 | customKernelImage: kernel, |
| 144 | gdbPort: gdb.map(u16::from).unwrap_or(0) as i32, // 0 means no gdb |
| 145 | taskProfiles: task_profiles, |
| 146 | }; |
| 147 | |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 148 | let config = VirtualMachineConfig::AppConfig(VirtualMachineAppConfig { |
Seungjae Yoo | 62085c0 | 2022-08-12 04:44:52 +0000 | [diff] [blame] | 149 | name: name.unwrap_or_else(|| String::from("VmRunApp")), |
Jiyong Park | 0a24843 | 2021-08-20 23:32:39 +0900 | [diff] [blame] | 150 | apk: apk_fd.into(), |
| 151 | idsig: idsig_fd.into(), |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 152 | extraIdsigs: extra_idsig_fds, |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 153 | instanceImage: open_parcel_file(instance, true /* writable */)?.into(), |
Shikha Panwar | 22e7045 | 2022-10-10 18:32:55 +0000 | [diff] [blame] | 154 | encryptedStorageImage: storage, |
Inseob Kim | 7b5f65c | 2022-11-15 14:27:04 +0900 | [diff] [blame] | 155 | payload, |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame] | 156 | debugLevel: debug_level, |
Andrew Walbran | 3994f00 | 2022-01-27 17:33:45 +0000 | [diff] [blame] | 157 | protectedVm: protected, |
Jiyong Park | d63cfff | 2021-09-27 20:10:17 +0900 | [diff] [blame] | 158 | memoryMib: mem.unwrap_or(0) as i32, // 0 means use the VM default |
David Brazdil | 7d1e5ec | 2023-02-06 17:56:29 +0000 | [diff] [blame] | 159 | cpuTopology: cpu_topology, |
Nikita Ioffe | a0eb5ee | 2023-06-26 18:18:21 +0100 | [diff] [blame^] | 160 | customConfig: Some(custom_config), |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 161 | }); |
David Brazdil | 2b6352f | 2023-01-12 11:01:17 +0000 | [diff] [blame] | 162 | run(service, &config, &payload_config_str, console_path, log_path) |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 163 | } |
| 164 | |
Nikita Ioffe | b0b6756 | 2022-11-22 15:48:06 +0000 | [diff] [blame] | 165 | fn find_empty_payload_apk_path() -> Result<PathBuf, Error> { |
Cole Faust | 237ee3e | 2023-03-01 11:58:01 -0800 | [diff] [blame] | 166 | const GLOB_PATTERN: &str = "/apex/com.android.virt/app/**/EmptyPayloadApp*.apk"; |
Nikita Ioffe | fc04196 | 2023-01-18 00:10:40 +0000 | [diff] [blame] | 167 | let mut entries: Vec<PathBuf> = |
| 168 | glob(GLOB_PATTERN).context("failed to glob")?.filter_map(|e| e.ok()).collect(); |
| 169 | if entries.len() > 1 { |
| 170 | return Err(anyhow!("Found more than one apk matching {}", GLOB_PATTERN)); |
| 171 | } |
| 172 | match entries.pop() { |
| 173 | Some(path) => Ok(path), |
| 174 | None => Err(anyhow!("No apks match {}", GLOB_PATTERN)), |
Nikita Ioffe | b0b6756 | 2022-11-22 15:48:06 +0000 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
| 178 | fn create_work_dir() -> Result<PathBuf, Error> { |
| 179 | let s: String = |
| 180 | rand::thread_rng().sample_iter(&Alphanumeric).take(17).map(char::from).collect(); |
| 181 | let work_dir = PathBuf::from("/data/local/tmp/microdroid").join(s); |
| 182 | println!("creating work dir {}", work_dir.display()); |
| 183 | fs::create_dir_all(&work_dir).context("failed to mkdir")?; |
| 184 | Ok(work_dir) |
| 185 | } |
| 186 | |
| 187 | /// Run a VM with Microdroid |
| 188 | #[allow(clippy::too_many_arguments)] |
| 189 | pub fn command_run_microdroid( |
| 190 | name: Option<String>, |
| 191 | service: &dyn IVirtualizationService, |
| 192 | work_dir: Option<PathBuf>, |
| 193 | storage: Option<&Path>, |
| 194 | storage_size: Option<u64>, |
Nikita Ioffe | b0b6756 | 2022-11-22 15:48:06 +0000 | [diff] [blame] | 195 | console_path: Option<&Path>, |
| 196 | log_path: Option<&Path>, |
Nikita Ioffe | b0b6756 | 2022-11-22 15:48:06 +0000 | [diff] [blame] | 197 | debug_level: DebugLevel, |
| 198 | protected: bool, |
| 199 | mem: Option<u32>, |
David Brazdil | 7d1e5ec | 2023-02-06 17:56:29 +0000 | [diff] [blame] | 200 | cpu_topology: CpuTopology, |
Nikita Ioffe | b0b6756 | 2022-11-22 15:48:06 +0000 | [diff] [blame] | 201 | task_profiles: Vec<String>, |
Nikita Ioffe | 5776f08 | 2023-02-10 21:38:26 +0000 | [diff] [blame] | 202 | gdb: Option<NonZeroU16>, |
Nikita Ioffe | 26c35ed | 2023-06-05 17:49:08 +0100 | [diff] [blame] | 203 | kernel: Option<&Path>, |
Nikita Ioffe | b0b6756 | 2022-11-22 15:48:06 +0000 | [diff] [blame] | 204 | ) -> Result<(), Error> { |
Nikita Ioffe | fc04196 | 2023-01-18 00:10:40 +0000 | [diff] [blame] | 205 | let apk = find_empty_payload_apk_path()?; |
| 206 | println!("found path {}", apk.display()); |
Nikita Ioffe | b0b6756 | 2022-11-22 15:48:06 +0000 | [diff] [blame] | 207 | |
| 208 | let work_dir = work_dir.unwrap_or(create_work_dir()?); |
| 209 | let idsig = work_dir.join("apk.idsig"); |
| 210 | println!("apk.idsig path: {}", idsig.display()); |
| 211 | let instance_img = work_dir.join("instance.img"); |
| 212 | println!("instance.img path: {}", instance_img.display()); |
| 213 | |
Alan Stokes | 8f12f2b | 2023-01-09 09:19:20 +0000 | [diff] [blame] | 214 | let payload_binary_name = "MicrodroidEmptyPayloadJniLib.so"; |
Nikita Ioffe | b0b6756 | 2022-11-22 15:48:06 +0000 | [diff] [blame] | 215 | let extra_sig = []; |
| 216 | command_run_app( |
| 217 | name, |
| 218 | service, |
| 219 | &apk, |
| 220 | &idsig, |
| 221 | &instance_img, |
| 222 | storage, |
| 223 | storage_size, |
| 224 | /* config_path= */ None, |
Alan Stokes | 8f12f2b | 2023-01-09 09:19:20 +0000 | [diff] [blame] | 225 | Some(payload_binary_name.to_owned()), |
Nikita Ioffe | b0b6756 | 2022-11-22 15:48:06 +0000 | [diff] [blame] | 226 | console_path, |
| 227 | log_path, |
Nikita Ioffe | b0b6756 | 2022-11-22 15:48:06 +0000 | [diff] [blame] | 228 | debug_level, |
| 229 | protected, |
| 230 | mem, |
David Brazdil | 7d1e5ec | 2023-02-06 17:56:29 +0000 | [diff] [blame] | 231 | cpu_topology, |
Nikita Ioffe | b0b6756 | 2022-11-22 15:48:06 +0000 | [diff] [blame] | 232 | task_profiles, |
| 233 | &extra_sig, |
Nikita Ioffe | 5776f08 | 2023-02-10 21:38:26 +0000 | [diff] [blame] | 234 | gdb, |
Nikita Ioffe | 26c35ed | 2023-06-05 17:49:08 +0100 | [diff] [blame] | 235 | kernel, |
Nikita Ioffe | b0b6756 | 2022-11-22 15:48:06 +0000 | [diff] [blame] | 236 | ) |
| 237 | } |
| 238 | |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 239 | /// Run a VM from the given configuration file. |
Jooyung Han | b7983a2 | 2022-02-22 05:21:27 +0900 | [diff] [blame] | 240 | #[allow(clippy::too_many_arguments)] |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 241 | pub fn command_run( |
Seungjae Yoo | 62085c0 | 2022-08-12 04:44:52 +0000 | [diff] [blame] | 242 | name: Option<String>, |
Andrew Walbran | 616d13f | 2022-05-12 18:35:55 +0000 | [diff] [blame] | 243 | service: &dyn IVirtualizationService, |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 244 | config_path: &Path, |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 245 | console_path: Option<&Path>, |
Jooyung Han | b7983a2 | 2022-02-22 05:21:27 +0900 | [diff] [blame] | 246 | log_path: Option<&Path>, |
Jiyong Park | d63cfff | 2021-09-27 20:10:17 +0900 | [diff] [blame] | 247 | mem: Option<u32>, |
David Brazdil | 7d1e5ec | 2023-02-06 17:56:29 +0000 | [diff] [blame] | 248 | cpu_topology: CpuTopology, |
Jiyong Park | dfe16d6 | 2022-04-20 17:32:12 +0900 | [diff] [blame] | 249 | task_profiles: Vec<String>, |
Nikita Ioffe | 5776f08 | 2023-02-10 21:38:26 +0000 | [diff] [blame] | 250 | gdb: Option<NonZeroU16>, |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 251 | ) -> Result<(), Error> { |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 252 | let config_file = File::open(config_path).context("Failed to open config file")?; |
Jiyong Park | d63cfff | 2021-09-27 20:10:17 +0900 | [diff] [blame] | 253 | let mut config = |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 254 | VmConfig::load(&config_file).context("Failed to parse config file")?.to_parcelable()?; |
Jiyong Park | d63cfff | 2021-09-27 20:10:17 +0900 | [diff] [blame] | 255 | if let Some(mem) = mem { |
| 256 | config.memoryMib = mem as i32; |
| 257 | } |
Seungjae Yoo | 62085c0 | 2022-08-12 04:44:52 +0000 | [diff] [blame] | 258 | if let Some(name) = name { |
| 259 | config.name = name; |
| 260 | } else { |
| 261 | config.name = String::from("VmRun"); |
| 262 | } |
Nikita Ioffe | 5776f08 | 2023-02-10 21:38:26 +0000 | [diff] [blame] | 263 | if let Some(gdb) = gdb { |
| 264 | config.gdbPort = gdb.get() as i32; |
| 265 | } |
David Brazdil | 7d1e5ec | 2023-02-06 17:56:29 +0000 | [diff] [blame] | 266 | config.cpuTopology = cpu_topology; |
Jiyong Park | dfe16d6 | 2022-04-20 17:32:12 +0900 | [diff] [blame] | 267 | config.taskProfiles = task_profiles; |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 268 | run( |
| 269 | service, |
| 270 | &VirtualMachineConfig::RawConfig(config), |
| 271 | &format!("{:?}", config_path), |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 272 | console_path, |
Jooyung Han | b7983a2 | 2022-02-22 05:21:27 +0900 | [diff] [blame] | 273 | log_path, |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 274 | ) |
| 275 | } |
| 276 | |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 277 | fn state_to_str(vm_state: VirtualMachineState) -> &'static str { |
| 278 | match vm_state { |
| 279 | VirtualMachineState::NOT_STARTED => "NOT_STARTED", |
| 280 | VirtualMachineState::STARTING => "STARTING", |
| 281 | VirtualMachineState::STARTED => "STARTED", |
| 282 | VirtualMachineState::READY => "READY", |
| 283 | VirtualMachineState::FINISHED => "FINISHED", |
| 284 | VirtualMachineState::DEAD => "DEAD", |
| 285 | _ => "(invalid state)", |
| 286 | } |
| 287 | } |
| 288 | |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 289 | fn run( |
Andrew Walbran | 616d13f | 2022-05-12 18:35:55 +0000 | [diff] [blame] | 290 | service: &dyn IVirtualizationService, |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 291 | config: &VirtualMachineConfig, |
Inseob Kim | 7b5f65c | 2022-11-15 14:27:04 +0900 | [diff] [blame] | 292 | payload_config: &str, |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 293 | console_path: Option<&Path>, |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 294 | log_path: Option<&Path>, |
| 295 | ) -> Result<(), Error> { |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 296 | let console = if let Some(console_path) = console_path { |
Andrew Walbran | d0ef400 | 2022-05-16 16:14:10 +0000 | [diff] [blame] | 297 | Some( |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 298 | File::create(console_path) |
| 299 | .with_context(|| format!("Failed to open console file {:?}", console_path))?, |
Andrew Walbran | d0ef400 | 2022-05-16 16:14:10 +0000 | [diff] [blame] | 300 | ) |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 301 | } else { |
Andrew Walbran | d0ef400 | 2022-05-16 16:14:10 +0000 | [diff] [blame] | 302 | Some(duplicate_stdout()?) |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 303 | }; |
| 304 | let log = if let Some(log_path) = log_path { |
Andrew Walbran | d0ef400 | 2022-05-16 16:14:10 +0000 | [diff] [blame] | 305 | Some( |
Andrew Walbran | be42924 | 2021-06-28 12:22:54 +0000 | [diff] [blame] | 306 | File::create(log_path) |
| 307 | .with_context(|| format!("Failed to open log file {:?}", log_path))?, |
Andrew Walbran | d0ef400 | 2022-05-16 16:14:10 +0000 | [diff] [blame] | 308 | ) |
Andrew Walbran | be42924 | 2021-06-28 12:22:54 +0000 | [diff] [blame] | 309 | } else { |
Andrew Walbran | d0ef400 | 2022-05-16 16:14:10 +0000 | [diff] [blame] | 310 | Some(duplicate_stdout()?) |
Andrew Walbran | be42924 | 2021-06-28 12:22:54 +0000 | [diff] [blame] | 311 | }; |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 312 | |
Alan Stokes | 0e82b50 | 2022-08-08 14:44:48 +0100 | [diff] [blame] | 313 | let callback = Box::new(Callback {}); |
| 314 | let vm = VmInstance::create(service, config, console, log, Some(callback)) |
| 315 | .context("Failed to create VM")?; |
Andrew Walbran | d0ef400 | 2022-05-16 16:14:10 +0000 | [diff] [blame] | 316 | vm.start().context("Failed to start VM")?; |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 317 | |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 318 | println!( |
| 319 | "Created VM from {} with CID {}, state is {}.", |
Inseob Kim | 7b5f65c | 2022-11-15 14:27:04 +0900 | [diff] [blame] | 320 | payload_config, |
Andrew Walbran | d0ef400 | 2022-05-16 16:14:10 +0000 | [diff] [blame] | 321 | vm.cid(), |
| 322 | state_to_str(vm.state()?) |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 323 | ); |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 324 | |
David Brazdil | 2b6352f | 2023-01-12 11:01:17 +0000 | [diff] [blame] | 325 | // Wait until the VM or VirtualizationService dies. If we just returned immediately then the |
| 326 | // IVirtualMachine Binder object would be dropped and the VM would be killed. |
| 327 | let death_reason = vm.wait_for_death(); |
| 328 | println!("VM ended: {:?}", death_reason); |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 329 | Ok(()) |
| 330 | } |
| 331 | |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 332 | fn parse_extra_apk_list(apk: &Path, config_path: &str) -> Result<Vec<String>, Error> { |
| 333 | let mut archive = ZipArchive::new(File::open(apk)?)?; |
| 334 | let config_file = archive.by_name(config_path)?; |
| 335 | let config: VmPayloadConfig = serde_json::from_reader(config_file)?; |
| 336 | Ok(config.extra_apks.into_iter().map(|x| x.path).collect()) |
| 337 | } |
| 338 | |
Alan Stokes | 0e82b50 | 2022-08-08 14:44:48 +0100 | [diff] [blame] | 339 | struct Callback {} |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 340 | |
Alan Stokes | 0e82b50 | 2022-08-08 14:44:48 +0100 | [diff] [blame] | 341 | impl vmclient::VmCallback for Callback { |
David Brazdil | 451cc96 | 2022-10-14 14:08:12 +0100 | [diff] [blame] | 342 | fn on_payload_started(&self, _cid: i32) { |
| 343 | eprintln!("payload started"); |
| 344 | } |
| 345 | |
Alan Stokes | 0e82b50 | 2022-08-08 14:44:48 +0100 | [diff] [blame] | 346 | fn on_payload_ready(&self, _cid: i32) { |
Inseob Kim | 8dbc322 | 2021-09-01 21:50:23 +0900 | [diff] [blame] | 347 | eprintln!("payload is ready"); |
Inseob Kim | 14cb869 | 2021-08-31 21:50:39 +0900 | [diff] [blame] | 348 | } |
| 349 | |
Alan Stokes | 0e82b50 | 2022-08-08 14:44:48 +0100 | [diff] [blame] | 350 | fn on_payload_finished(&self, _cid: i32, exit_code: i32) { |
Inseob Kim | 8dbc322 | 2021-09-01 21:50:23 +0900 | [diff] [blame] | 351 | eprintln!("payload finished with exit code {}", exit_code); |
Inseob Kim | 2444af9 | 2021-08-31 01:22:50 +0900 | [diff] [blame] | 352 | } |
| 353 | |
Alan Stokes | 2bead0d | 2022-09-05 16:58:34 +0100 | [diff] [blame] | 354 | fn on_error(&self, _cid: i32, error_code: ErrorCode, message: &str) { |
| 355 | eprintln!("VM encountered an error: code={:?}, message={}", error_code, message); |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | |
| 359 | /// Safely duplicate the standard output file descriptor. |
| 360 | fn duplicate_stdout() -> io::Result<File> { |
| 361 | let stdout_fd = io::stdout().as_raw_fd(); |
| 362 | // Safe because this just duplicates a file descriptor which we know to be valid, and we check |
| 363 | // for an error. |
| 364 | let dup_fd = unsafe { libc::dup(stdout_fd) }; |
| 365 | if dup_fd < 0 { |
| 366 | Err(io::Error::last_os_error()) |
| 367 | } else { |
| 368 | // Safe because we have just duplicated the file descriptor so we own it, and `from_raw_fd` |
| 369 | // takes ownership of it. |
| 370 | Ok(unsafe { File::from_raw_fd(dup_fd) }) |
| 371 | } |
| 372 | } |