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