Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +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 | //! Android VM control tool. |
| 16 | |
Jooyung Han | c221c05 | 2022-02-22 05:20:15 +0900 | [diff] [blame] | 17 | mod create_idsig; |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 18 | mod create_partition; |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 19 | mod run; |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 20 | |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame] | 21 | use android_system_virtualizationservice::aidl::android::system::virtualizationservice::{ |
David Brazdil | 7d1e5ec | 2023-02-06 17:56:29 +0000 | [diff] [blame] | 22 | CpuTopology::CpuTopology, IVirtualizationService::IVirtualizationService, |
| 23 | PartitionType::PartitionType, VirtualMachineAppConfig::DebugLevel::DebugLevel, |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame] | 24 | }; |
Shikha Panwar | 61a74b5 | 2024-02-16 13:17:01 +0000 | [diff] [blame] | 25 | #[cfg(not(llpvm_changes))] |
| 26 | use anyhow::anyhow; |
Yi-Yo Chiang | 2fbf0da | 2024-06-14 22:56:56 +0800 | [diff] [blame] | 27 | use anyhow::{bail, Context, Error}; |
Alan Stokes | c4d5def | 2023-02-14 17:01:59 +0000 | [diff] [blame] | 28 | use binder::{ProcessState, Strong}; |
Jiyong Park | b1935ef | 2023-08-10 17:22:39 +0900 | [diff] [blame] | 29 | use clap::{Args, Parser}; |
Jooyung Han | c221c05 | 2022-02-22 05:20:15 +0900 | [diff] [blame] | 30 | use create_idsig::command_create_idsig; |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 31 | use create_partition::command_create_partition; |
Nikita Ioffe | b0b6756 | 2022-11-22 15:48:06 +0000 | [diff] [blame] | 32 | use run::{command_run, command_run_app, command_run_microdroid}; |
Jaewan Kim | 0c99c61 | 2024-03-23 00:44:14 +0900 | [diff] [blame] | 33 | use serde::Serialize; |
Yi-Yo Chiang | 2fbf0da | 2024-06-14 22:56:56 +0800 | [diff] [blame] | 34 | use std::io::{self, IsTerminal}; |
Nikita Ioffe | 5776f08 | 2023-02-10 21:38:26 +0000 | [diff] [blame] | 35 | use std::num::NonZeroU16; |
Yi-Yo Chiang | 2fbf0da | 2024-06-14 22:56:56 +0800 | [diff] [blame] | 36 | use std::os::unix::process::CommandExt; |
Andrew Walbran | c4b1bde | 2022-02-03 15:26:02 +0000 | [diff] [blame] | 37 | use std::path::{Path, PathBuf}; |
Yi-Yo Chiang | 2fbf0da | 2024-06-14 22:56:56 +0800 | [diff] [blame] | 38 | use std::process::Command; |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 39 | |
Alan Stokes | fda7084 | 2023-12-20 17:50:14 +0000 | [diff] [blame] | 40 | #[derive(Args, Default)] |
Jiyong Park | b1935ef | 2023-08-10 17:22:39 +0900 | [diff] [blame] | 41 | /// Collection of flags that are at VM level and therefore applicable to all subcommands |
| 42 | pub struct CommonConfig { |
| 43 | /// Name of VM |
| 44 | #[arg(long)] |
| 45 | name: Option<String>, |
| 46 | |
| 47 | /// Run VM with vCPU topology matching that of the host. If unspecified, defaults to 1 vCPU. |
| 48 | #[arg(long, default_value = "one_cpu", value_parser = parse_cpu_topology)] |
| 49 | cpu_topology: CpuTopology, |
| 50 | |
Jiyong Park | b1935ef | 2023-08-10 17:22:39 +0900 | [diff] [blame] | 51 | /// Memory size (in MiB) of the VM. If unspecified, defaults to the value of `memory_mib` |
| 52 | /// in the VM config file. |
| 53 | #[arg(short, long)] |
| 54 | mem: Option<u32>, |
| 55 | |
| 56 | /// Run VM in protected mode. |
| 57 | #[arg(short, long)] |
| 58 | protected: bool, |
Vincent Donnefort | 538a2c6 | 2024-03-20 16:01:10 +0000 | [diff] [blame] | 59 | |
| 60 | /// Ask the kernel for transparent huge-pages (THP). This is only a hint and |
| 61 | /// the kernel will allocate THP-backed memory only if globally enabled by |
| 62 | /// the system and if any can be found. See |
| 63 | /// https://docs.kernel.org/admin-guide/mm/transhuge.html |
| 64 | #[arg(short, long)] |
| 65 | hugepages: bool, |
Seungjae Yoo | 13af0b6 | 2024-05-20 14:15:13 +0900 | [diff] [blame] | 66 | |
| 67 | /// Run VM with network feature. |
| 68 | #[cfg(network)] |
| 69 | #[arg(short, long)] |
| 70 | network_supported: bool, |
David Dai | 23cff71 | 2024-06-13 19:23:45 +0000 | [diff] [blame] | 71 | |
| 72 | /// Boost uclamp to stablise results for benchmarks. |
| 73 | #[arg(short, long)] |
| 74 | boost_uclamp: bool, |
Nikita Ioffe | 6952187 | 2024-10-22 14:46:07 +0000 | [diff] [blame] | 75 | |
| 76 | /// Secure services this VM wants to access. |
| 77 | #[cfg(tee_services_allowlist)] |
| 78 | #[arg(long)] |
| 79 | tee_services: Vec<String>, |
Seungjae Yoo | 13af0b6 | 2024-05-20 14:15:13 +0900 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | impl CommonConfig { |
Seungjae Yoo | 13af0b6 | 2024-05-20 14:15:13 +0900 | [diff] [blame] | 83 | fn network_supported(&self) -> bool { |
Pierre-Clément Tosi | fdd056f | 2024-09-03 14:15:02 +0100 | [diff] [blame] | 84 | cfg_if::cfg_if! { |
| 85 | if #[cfg(network)] { |
| 86 | self.network_supported |
| 87 | } else { |
| 88 | false |
| 89 | } |
| 90 | } |
Seungjae Yoo | 13af0b6 | 2024-05-20 14:15:13 +0900 | [diff] [blame] | 91 | } |
Nikita Ioffe | 6952187 | 2024-10-22 14:46:07 +0000 | [diff] [blame] | 92 | |
| 93 | fn tee_services(&self) -> &[String] { |
| 94 | cfg_if::cfg_if! { |
| 95 | if #[cfg(tee_services_allowlist)] { |
| 96 | &self.tee_services |
| 97 | } else { |
| 98 | &[] |
| 99 | } |
| 100 | } |
| 101 | } |
Jiyong Park | b1935ef | 2023-08-10 17:22:39 +0900 | [diff] [blame] | 102 | } |
| 103 | |
Alan Stokes | fda7084 | 2023-12-20 17:50:14 +0000 | [diff] [blame] | 104 | #[derive(Args, Default)] |
Jiyong Park | b1935ef | 2023-08-10 17:22:39 +0900 | [diff] [blame] | 105 | /// Collection of flags for debugging |
| 106 | pub struct DebugConfig { |
| 107 | /// Debug level of the VM. Supported values: "full" (default), and "none". |
| 108 | #[arg(long, default_value = "full", value_parser = parse_debug_level)] |
| 109 | debug: DebugLevel, |
| 110 | |
| 111 | /// Path to file for VM console output. |
| 112 | #[arg(long)] |
| 113 | console: Option<PathBuf>, |
| 114 | |
| 115 | /// Path to file for VM console input. |
| 116 | #[arg(long)] |
| 117 | console_in: Option<PathBuf>, |
| 118 | |
| 119 | /// Path to file for VM log output. |
| 120 | #[arg(long)] |
| 121 | log: Option<PathBuf>, |
| 122 | |
| 123 | /// Port at which crosvm will start a gdb server to debug guest kernel. |
| 124 | /// Note: this is only supported on Android kernels android14-5.15 and higher. |
| 125 | #[arg(long)] |
| 126 | gdb: Option<NonZeroU16>, |
Nikita Ioffe | b4268b3 | 2024-09-03 10:23:14 +0000 | [diff] [blame] | 127 | |
| 128 | /// Whether to enable earlycon. Only supported for debuggable Linux-based VMs. |
| 129 | #[cfg(debuggable_vms_improvements)] |
| 130 | #[arg(long)] |
| 131 | enable_earlycon: bool, |
Elie Kheirallah | 5c807a2 | 2024-09-23 20:40:42 +0000 | [diff] [blame] | 132 | |
| 133 | /// Path to file to dump VM device tree. |
| 134 | #[arg(long)] |
| 135 | dump_device_tree: Option<PathBuf>, |
Nikita Ioffe | b4268b3 | 2024-09-03 10:23:14 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | impl DebugConfig { |
Nikita Ioffe | b4268b3 | 2024-09-03 10:23:14 +0000 | [diff] [blame] | 139 | fn enable_earlycon(&self) -> bool { |
Pierre-Clément Tosi | fdd056f | 2024-09-03 14:15:02 +0100 | [diff] [blame] | 140 | cfg_if::cfg_if! { |
| 141 | if #[cfg(debuggable_vms_improvements)] { |
| 142 | self.enable_earlycon |
| 143 | } else { |
| 144 | false |
| 145 | } |
| 146 | } |
Nikita Ioffe | b4268b3 | 2024-09-03 10:23:14 +0000 | [diff] [blame] | 147 | } |
Jiyong Park | b1935ef | 2023-08-10 17:22:39 +0900 | [diff] [blame] | 148 | } |
| 149 | |
Alan Stokes | fda7084 | 2023-12-20 17:50:14 +0000 | [diff] [blame] | 150 | #[derive(Args, Default)] |
Jiyong Park | b1935ef | 2023-08-10 17:22:39 +0900 | [diff] [blame] | 151 | /// Collection of flags that are Microdroid specific |
| 152 | pub struct MicrodroidConfig { |
| 153 | /// Path to the file backing the storage. |
| 154 | /// Created if the option is used but the path does not exist in the device. |
| 155 | #[arg(long)] |
| 156 | storage: Option<PathBuf>, |
| 157 | |
| 158 | /// Size of the storage. Used only if --storage is supplied but path does not exist |
| 159 | /// Default size is 10*1024*1024 |
| 160 | #[arg(long)] |
| 161 | storage_size: Option<u64>, |
| 162 | |
Jiyong Park | b1935ef | 2023-08-10 17:22:39 +0900 | [diff] [blame] | 163 | /// Path to disk image containing vendor-specific modules. |
Nikita Ioffe | 631717e | 2023-09-05 13:38:07 +0100 | [diff] [blame] | 164 | #[cfg(vendor_modules)] |
Jiyong Park | b1935ef | 2023-08-10 17:22:39 +0900 | [diff] [blame] | 165 | #[arg(long)] |
| 166 | vendor: Option<PathBuf>, |
| 167 | |
| 168 | /// SysFS nodes of devices to assign to VM |
Nikita Ioffe | 94a8a18 | 2023-11-16 16:37:48 +0000 | [diff] [blame] | 169 | #[cfg(device_assignment)] |
Jiyong Park | b1935ef | 2023-08-10 17:22:39 +0900 | [diff] [blame] | 170 | #[arg(long)] |
| 171 | devices: Vec<PathBuf>, |
Inseob Kim | 172f9eb | 2023-11-06 17:02:08 +0900 | [diff] [blame] | 172 | |
Nikita Ioffe | 43c9362 | 2024-10-30 20:33:58 +0000 | [diff] [blame] | 173 | /// Version of OS to use. If not set, defaults to microdroid. |
| 174 | /// You can list all available OSes via `vm info` command. |
Inseob Kim | 172f9eb | 2023-11-06 17:02:08 +0900 | [diff] [blame] | 175 | #[arg(long)] |
Nikita Ioffe | 43c9362 | 2024-10-30 20:33:58 +0000 | [diff] [blame] | 176 | os: Option<String>, |
Jiyong Park | b1935ef | 2023-08-10 17:22:39 +0900 | [diff] [blame] | 177 | } |
| 178 | |
Nikita Ioffe | 631717e | 2023-09-05 13:38:07 +0100 | [diff] [blame] | 179 | impl MicrodroidConfig { |
Pierre-Clément Tosi | f1feafb | 2024-09-03 14:05:19 +0100 | [diff] [blame] | 180 | fn vendor(&self) -> Option<&PathBuf> { |
Pierre-Clément Tosi | fdd056f | 2024-09-03 14:15:02 +0100 | [diff] [blame] | 181 | cfg_if::cfg_if! { |
| 182 | if #[cfg(vendor_modules)] { |
| 183 | self.vendor.as_ref() |
| 184 | } else { |
| 185 | None |
| 186 | } |
| 187 | } |
Nikita Ioffe | 631717e | 2023-09-05 13:38:07 +0100 | [diff] [blame] | 188 | } |
| 189 | |
Pierre-Clément Tosi | f1feafb | 2024-09-03 14:05:19 +0100 | [diff] [blame] | 190 | fn devices(&self) -> &[PathBuf] { |
Pierre-Clément Tosi | fdd056f | 2024-09-03 14:15:02 +0100 | [diff] [blame] | 191 | cfg_if::cfg_if! { |
| 192 | if #[cfg(device_assignment)] { |
| 193 | &self.devices |
| 194 | } else { |
| 195 | &[] |
| 196 | } |
| 197 | } |
Nikita Ioffe | 94a8a18 | 2023-11-16 16:37:48 +0000 | [diff] [blame] | 198 | } |
Nikita Ioffe | 631717e | 2023-09-05 13:38:07 +0100 | [diff] [blame] | 199 | } |
| 200 | |
Alan Stokes | fda7084 | 2023-12-20 17:50:14 +0000 | [diff] [blame] | 201 | #[derive(Args, Default)] |
Jiyong Park | b1935ef | 2023-08-10 17:22:39 +0900 | [diff] [blame] | 202 | /// Flags for the run_app subcommand |
| 203 | pub struct RunAppConfig { |
| 204 | #[command(flatten)] |
| 205 | common: CommonConfig, |
| 206 | |
| 207 | #[command(flatten)] |
| 208 | debug: DebugConfig, |
| 209 | |
| 210 | #[command(flatten)] |
| 211 | microdroid: MicrodroidConfig, |
| 212 | |
| 213 | /// Path to VM Payload APK |
| 214 | apk: PathBuf, |
| 215 | |
| 216 | /// Path to idsig of the APK |
| 217 | idsig: PathBuf, |
| 218 | |
| 219 | /// Path to the instance image. Created if not exists. |
| 220 | instance: PathBuf, |
| 221 | |
Shikha Panwar | 61a74b5 | 2024-02-16 13:17:01 +0000 | [diff] [blame] | 222 | /// Path to file containing instance_id. Required iff llpvm feature is enabled. |
| 223 | #[cfg(llpvm_changes)] |
| 224 | #[arg(long = "instance-id-file")] |
| 225 | instance_id: PathBuf, |
| 226 | |
Jiyong Park | b1935ef | 2023-08-10 17:22:39 +0900 | [diff] [blame] | 227 | /// Path to VM config JSON within APK (e.g. assets/vm_config.json) |
| 228 | #[arg(long)] |
| 229 | config_path: Option<String>, |
| 230 | |
| 231 | /// Name of VM payload binary within APK (e.g. MicrodroidTestNativeLib.so) |
| 232 | #[arg(long)] |
| 233 | #[arg(alias = "payload_path")] |
| 234 | payload_binary_name: Option<String>, |
| 235 | |
Alan Stokes | fda7084 | 2023-12-20 17:50:14 +0000 | [diff] [blame] | 236 | /// Paths to extra apk files. |
| 237 | #[cfg(multi_tenant)] |
| 238 | #[arg(long = "extra-apk")] |
| 239 | #[clap(conflicts_with = "config_path")] |
| 240 | extra_apks: Vec<PathBuf>, |
| 241 | |
Jiyong Park | b1935ef | 2023-08-10 17:22:39 +0900 | [diff] [blame] | 242 | /// Paths to extra idsig files. |
| 243 | #[arg(long = "extra-idsig")] |
| 244 | extra_idsigs: Vec<PathBuf>, |
| 245 | } |
| 246 | |
Alan Stokes | fda7084 | 2023-12-20 17:50:14 +0000 | [diff] [blame] | 247 | impl RunAppConfig { |
Alan Stokes | fda7084 | 2023-12-20 17:50:14 +0000 | [diff] [blame] | 248 | fn extra_apks(&self) -> &[PathBuf] { |
Pierre-Clément Tosi | fdd056f | 2024-09-03 14:15:02 +0100 | [diff] [blame] | 249 | cfg_if::cfg_if! { |
| 250 | if #[cfg(multi_tenant)] { |
| 251 | &self.extra_apks |
| 252 | } else { |
| 253 | &[] |
| 254 | } |
| 255 | } |
Alan Stokes | fda7084 | 2023-12-20 17:50:14 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Shikha Panwar | 61a74b5 | 2024-02-16 13:17:01 +0000 | [diff] [blame] | 258 | fn instance_id(&self) -> Result<PathBuf, Error> { |
Pierre-Clément Tosi | fdd056f | 2024-09-03 14:15:02 +0100 | [diff] [blame] | 259 | cfg_if::cfg_if! { |
| 260 | if #[cfg(llpvm_changes)] { |
| 261 | Ok(self.instance_id.clone()) |
| 262 | } else { |
| 263 | Err(anyhow!("LLPVM feature is disabled, --instance_id flag not supported")) |
| 264 | } |
| 265 | } |
Shikha Panwar | 61a74b5 | 2024-02-16 13:17:01 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Shikha Panwar | 61a74b5 | 2024-02-16 13:17:01 +0000 | [diff] [blame] | 268 | fn set_instance_id(&mut self, instance_id_file: PathBuf) -> Result<(), Error> { |
Pierre-Clément Tosi | fdd056f | 2024-09-03 14:15:02 +0100 | [diff] [blame] | 269 | cfg_if::cfg_if! { |
| 270 | if #[cfg(llpvm_changes)] { |
| 271 | self.instance_id = instance_id_file; |
| 272 | Ok(()) |
| 273 | } else { |
| 274 | let _ = instance_id_file; |
| 275 | Err(anyhow!("LLPVM feature is disabled, --instance_id flag not supported")) |
| 276 | } |
| 277 | } |
Shikha Panwar | 61a74b5 | 2024-02-16 13:17:01 +0000 | [diff] [blame] | 278 | } |
Alan Stokes | fda7084 | 2023-12-20 17:50:14 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | #[derive(Args, Default)] |
Jiyong Park | b1935ef | 2023-08-10 17:22:39 +0900 | [diff] [blame] | 282 | /// Flags for the run_microdroid subcommand |
| 283 | pub struct RunMicrodroidConfig { |
| 284 | #[command(flatten)] |
| 285 | common: CommonConfig, |
| 286 | |
| 287 | #[command(flatten)] |
| 288 | debug: DebugConfig, |
| 289 | |
| 290 | #[command(flatten)] |
| 291 | microdroid: MicrodroidConfig, |
| 292 | |
| 293 | /// Path to the directory where VM-related files (e.g. instance.img, apk.idsig, etc.) will |
| 294 | /// be stored. If not specified a random directory under /data/local/tmp/microdroid will be |
| 295 | /// created and used. |
| 296 | #[arg(long)] |
| 297 | work_dir: Option<PathBuf>, |
| 298 | } |
| 299 | |
Alan Stokes | fda7084 | 2023-12-20 17:50:14 +0000 | [diff] [blame] | 300 | #[derive(Args, Default)] |
Jiyong Park | b1935ef | 2023-08-10 17:22:39 +0900 | [diff] [blame] | 301 | /// Flags for the run subcommand |
| 302 | pub struct RunCustomVmConfig { |
| 303 | #[command(flatten)] |
| 304 | common: CommonConfig, |
| 305 | |
| 306 | #[command(flatten)] |
| 307 | debug: DebugConfig, |
| 308 | |
| 309 | /// Path to VM config JSON |
| 310 | config: PathBuf, |
| 311 | } |
| 312 | |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 313 | #[derive(Parser)] |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 314 | enum Opt { |
Shikha Panwar | 6d30641 | 2024-02-17 21:37:49 +0000 | [diff] [blame] | 315 | /// Check if the feature is enabled on device. |
| 316 | CheckFeatureEnabled { feature: String }, |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 317 | /// Run a virtual machine with a config in APK |
| 318 | RunApp { |
Jiyong Park | b1935ef | 2023-08-10 17:22:39 +0900 | [diff] [blame] | 319 | #[command(flatten)] |
| 320 | config: RunAppConfig, |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 321 | }, |
Nikita Ioffe | b0b6756 | 2022-11-22 15:48:06 +0000 | [diff] [blame] | 322 | /// Run a virtual machine with Microdroid inside |
| 323 | RunMicrodroid { |
Jiyong Park | b1935ef | 2023-08-10 17:22:39 +0900 | [diff] [blame] | 324 | #[command(flatten)] |
| 325 | config: RunMicrodroidConfig, |
Nikita Ioffe | b0b6756 | 2022-11-22 15:48:06 +0000 | [diff] [blame] | 326 | }, |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 327 | /// Run a virtual machine |
| 328 | Run { |
Jiyong Park | b1935ef | 2023-08-10 17:22:39 +0900 | [diff] [blame] | 329 | #[command(flatten)] |
| 330 | config: RunCustomVmConfig, |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 331 | }, |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 332 | /// List running virtual machines |
| 333 | List, |
Andrew Walbran | c4b1bde | 2022-02-03 15:26:02 +0000 | [diff] [blame] | 334 | /// Print information about virtual machine support |
| 335 | Info, |
Andrew Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 336 | /// Create a new empty partition to be used as a writable partition for a VM |
| 337 | CreatePartition { |
| 338 | /// Path at which to create the image file |
Andrew Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 339 | path: PathBuf, |
| 340 | |
| 341 | /// The desired size of the partition, in bytes. |
| 342 | size: u64, |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 343 | |
| 344 | /// Type of the partition |
Jiyong Park | b1935ef | 2023-08-10 17:22:39 +0900 | [diff] [blame] | 345 | #[arg(short = 't', long = "type", default_value = "raw", |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 346 | value_parser = parse_partition_type)] |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 347 | partition_type: PartitionType, |
Andrew Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 348 | }, |
Jooyung Han | c221c05 | 2022-02-22 05:20:15 +0900 | [diff] [blame] | 349 | /// Creates or update the idsig file by digesting the input APK file. |
| 350 | CreateIdsig { |
| 351 | /// Path to VM Payload APK |
Jooyung Han | c221c05 | 2022-02-22 05:20:15 +0900 | [diff] [blame] | 352 | apk: PathBuf, |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 353 | |
Jooyung Han | c221c05 | 2022-02-22 05:20:15 +0900 | [diff] [blame] | 354 | /// Path to idsig of the APK |
Jooyung Han | c221c05 | 2022-02-22 05:20:15 +0900 | [diff] [blame] | 355 | path: PathBuf, |
| 356 | }, |
Yi-Yo Chiang | 2fbf0da | 2024-06-14 22:56:56 +0800 | [diff] [blame] | 357 | /// Connect to the serial console of a VM |
| 358 | Console { |
| 359 | /// CID of the VM |
| 360 | cid: Option<i32>, |
| 361 | }, |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame] | 364 | fn parse_debug_level(s: &str) -> Result<DebugLevel, String> { |
| 365 | match s { |
| 366 | "none" => Ok(DebugLevel::NONE), |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame] | 367 | "full" => Ok(DebugLevel::FULL), |
| 368 | _ => Err(format!("Invalid debug level {}", s)), |
| 369 | } |
| 370 | } |
| 371 | |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 372 | fn parse_partition_type(s: &str) -> Result<PartitionType, String> { |
| 373 | match s { |
| 374 | "raw" => Ok(PartitionType::RAW), |
| 375 | "instance" => Ok(PartitionType::ANDROID_VM_INSTANCE), |
| 376 | _ => Err(format!("Invalid partition type {}", s)), |
| 377 | } |
| 378 | } |
| 379 | |
David Brazdil | 7d1e5ec | 2023-02-06 17:56:29 +0000 | [diff] [blame] | 380 | fn parse_cpu_topology(s: &str) -> Result<CpuTopology, String> { |
| 381 | match s { |
| 382 | "one_cpu" => Ok(CpuTopology::ONE_CPU), |
| 383 | "match_host" => Ok(CpuTopology::MATCH_HOST), |
| 384 | _ => Err(format!("Invalid cpu topology {}", s)), |
| 385 | } |
| 386 | } |
| 387 | |
Alan Stokes | c4d5def | 2023-02-14 17:01:59 +0000 | [diff] [blame] | 388 | fn get_service() -> Result<Strong<dyn IVirtualizationService>, Error> { |
| 389 | let virtmgr = |
| 390 | vmclient::VirtualizationService::new().context("Failed to spawn VirtualizationService")?; |
| 391 | virtmgr.connect().context("Failed to connect to VirtualizationService") |
| 392 | } |
| 393 | |
Shikha Panwar | 6d30641 | 2024-02-17 21:37:49 +0000 | [diff] [blame] | 394 | fn command_check_feature_enabled(feature: &str) { |
| 395 | println!( |
| 396 | "Feature {feature} is {}", |
| 397 | if avf_features::is_feature_enabled(feature) { "enabled" } else { "disabled" } |
| 398 | ); |
| 399 | } |
| 400 | |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 401 | fn main() -> Result<(), Error> { |
| 402 | env_logger::init(); |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 403 | let opt = Opt::parse(); |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 404 | |
| 405 | // We need to start the thread pool for Binder to work properly, especially link_to_death. |
| 406 | ProcessState::start_thread_pool(); |
| 407 | |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 408 | match opt { |
Shikha Panwar | 6d30641 | 2024-02-17 21:37:49 +0000 | [diff] [blame] | 409 | Opt::CheckFeatureEnabled { feature } => { |
| 410 | command_check_feature_enabled(&feature); |
| 411 | Ok(()) |
| 412 | } |
Jiyong Park | b1935ef | 2023-08-10 17:22:39 +0900 | [diff] [blame] | 413 | Opt::RunApp { config } => command_run_app(config), |
| 414 | Opt::RunMicrodroid { config } => command_run_microdroid(config), |
| 415 | Opt::Run { config } => command_run(config), |
Alan Stokes | c4d5def | 2023-02-14 17:01:59 +0000 | [diff] [blame] | 416 | Opt::List => command_list(get_service()?.as_ref()), |
Andrew Walbran | c4b1bde | 2022-02-03 15:26:02 +0000 | [diff] [blame] | 417 | Opt::Info => command_info(), |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 418 | Opt::CreatePartition { path, size, partition_type } => { |
Alan Stokes | c4d5def | 2023-02-14 17:01:59 +0000 | [diff] [blame] | 419 | command_create_partition(get_service()?.as_ref(), &path, size, partition_type) |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 420 | } |
Alan Stokes | c4d5def | 2023-02-14 17:01:59 +0000 | [diff] [blame] | 421 | Opt::CreateIdsig { apk, path } => { |
| 422 | command_create_idsig(get_service()?.as_ref(), &apk, &path) |
| 423 | } |
Yi-Yo Chiang | 2fbf0da | 2024-06-14 22:56:56 +0800 | [diff] [blame] | 424 | Opt::Console { cid } => command_console(cid), |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 428 | /// List the VMs currently running. |
Andrew Walbran | 616d13f | 2022-05-12 18:35:55 +0000 | [diff] [blame] | 429 | fn command_list(service: &dyn IVirtualizationService) -> Result<(), Error> { |
Andrew Walbran | 17de24f | 2021-05-27 13:27:30 +0000 | [diff] [blame] | 430 | let vms = service.debugListVms().context("Failed to get list of VMs")?; |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 431 | println!("Running VMs: {:#?}", vms); |
| 432 | Ok(()) |
| 433 | } |
Andrew Walbran | c4b1bde | 2022-02-03 15:26:02 +0000 | [diff] [blame] | 434 | |
| 435 | /// Print information about supported VM types. |
| 436 | fn command_info() -> Result<(), Error> { |
Alan Stokes | c4d5def | 2023-02-14 17:01:59 +0000 | [diff] [blame] | 437 | let non_protected_vm_supported = hypervisor_props::is_vm_supported()?; |
| 438 | let protected_vm_supported = hypervisor_props::is_protected_vm_supported()?; |
Alan Stokes | 8d39a9b | 2023-01-10 15:01:00 +0000 | [diff] [blame] | 439 | match (non_protected_vm_supported, protected_vm_supported) { |
Andrew Walbran | c4b1bde | 2022-02-03 15:26:02 +0000 | [diff] [blame] | 440 | (false, false) => println!("VMs are not supported."), |
| 441 | (false, true) => println!("Only protected VMs are supported."), |
Alan Stokes | 8d39a9b | 2023-01-10 15:01:00 +0000 | [diff] [blame] | 442 | (true, false) => println!("Only non-protected VMs are supported."), |
| 443 | (true, true) => println!("Both protected and non-protected VMs are supported."), |
Andrew Walbran | c4b1bde | 2022-02-03 15:26:02 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Alan Stokes | c4d5def | 2023-02-14 17:01:59 +0000 | [diff] [blame] | 446 | if let Some(version) = hypervisor_props::version()? { |
Andrew Walbran | c4b1bde | 2022-02-03 15:26:02 +0000 | [diff] [blame] | 447 | println!("Hypervisor version: {}", version); |
| 448 | } else { |
| 449 | println!("Hypervisor version not set."); |
| 450 | } |
| 451 | |
| 452 | if Path::new("/dev/kvm").exists() { |
| 453 | println!("/dev/kvm exists."); |
| 454 | } else { |
| 455 | println!("/dev/kvm does not exist."); |
| 456 | } |
| 457 | |
Inseob Kim | 6ef8097 | 2023-07-20 17:23:36 +0900 | [diff] [blame] | 458 | if Path::new("/dev/vfio/vfio").exists() { |
| 459 | println!("/dev/vfio/vfio exists."); |
| 460 | } else { |
| 461 | println!("/dev/vfio/vfio does not exist."); |
| 462 | } |
| 463 | |
| 464 | if Path::new("/sys/bus/platform/drivers/vfio-platform").exists() { |
| 465 | println!("VFIO-platform is supported."); |
| 466 | } else { |
| 467 | println!("VFIO-platform is not supported."); |
| 468 | } |
| 469 | |
Jaewan Kim | 0c99c61 | 2024-03-23 00:44:14 +0900 | [diff] [blame] | 470 | #[derive(Serialize)] |
| 471 | struct AssignableDevice { |
| 472 | node: String, |
| 473 | dtbo_label: String, |
| 474 | } |
| 475 | |
Inseob Kim | 75460b3 | 2023-08-09 13:41:31 +0900 | [diff] [blame] | 476 | let devices = get_service()?.getAssignableDevices()?; |
Jaewan Kim | 0c99c61 | 2024-03-23 00:44:14 +0900 | [diff] [blame] | 477 | let devices: Vec<_> = devices |
| 478 | .into_iter() |
| 479 | .map(|device| AssignableDevice { node: device.node, dtbo_label: device.dtbo_label }) |
| 480 | .collect(); |
Inseob Kim | 75460b3 | 2023-08-09 13:41:31 +0900 | [diff] [blame] | 481 | println!("Assignable devices: {}", serde_json::to_string(&devices)?); |
| 482 | |
Inseob Kim | 4625738 | 2024-01-03 15:41:22 +0900 | [diff] [blame] | 483 | let os_list = get_service()?.getSupportedOSList()?; |
| 484 | println!("Available OS list: {}", serde_json::to_string(&os_list)?); |
Inseob Kim | 7a1fc8f | 2023-11-22 18:45:28 +0900 | [diff] [blame] | 485 | |
Jaewan Kim | abd328b | 2024-12-10 15:59:33 +0900 | [diff] [blame] | 486 | let debug_policy = get_service()?.getDebugPolicy()?; |
| 487 | println!("Debug policy: {}", debug_policy); |
| 488 | |
Andrew Walbran | c4b1bde | 2022-02-03 15:26:02 +0000 | [diff] [blame] | 489 | Ok(()) |
| 490 | } |
Andrew Walbran | 1f810b6 | 2022-08-10 13:33:57 +0000 | [diff] [blame] | 491 | |
Yi-Yo Chiang | 2fbf0da | 2024-06-14 22:56:56 +0800 | [diff] [blame] | 492 | fn command_console(cid: Option<i32>) -> Result<(), Error> { |
| 493 | if !io::stdin().is_terminal() { |
| 494 | bail!("Stdin must be a terminal (tty). Use 'adb shell -t' to force allocate tty."); |
| 495 | } |
| 496 | let mut vms = get_service()?.debugListVms().context("Failed to get list of VMs")?; |
| 497 | if let Some(cid) = cid { |
| 498 | vms.retain(|vm_info| vm_info.cid == cid); |
| 499 | } |
| 500 | let host_console_name = vms |
| 501 | .into_iter() |
| 502 | .find_map(|vm_info| vm_info.hostConsoleName) |
| 503 | .context("Failed to get VM with console")?; |
| 504 | Err(Command::new("microcom").arg(host_console_name).exec().into()) |
| 505 | } |
| 506 | |
Andrew Walbran | 1f810b6 | 2022-08-10 13:33:57 +0000 | [diff] [blame] | 507 | #[cfg(test)] |
| 508 | mod tests { |
| 509 | use super::*; |
Andrew Walbran | aa1efc4 | 2022-08-10 13:33:57 +0000 | [diff] [blame] | 510 | use clap::CommandFactory; |
Andrew Walbran | 1f810b6 | 2022-08-10 13:33:57 +0000 | [diff] [blame] | 511 | |
| 512 | #[test] |
| 513 | fn verify_app() { |
Andrew Walbran | aa1efc4 | 2022-08-10 13:33:57 +0000 | [diff] [blame] | 514 | // Check that the command parsing has been configured in a valid way. |
| 515 | Opt::command().debug_assert(); |
Andrew Walbran | 1f810b6 | 2022-08-10 13:33:57 +0000 | [diff] [blame] | 516 | } |
| 517 | } |