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