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