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