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