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