blob: f2c2fa4776ee79827e371744bd689e3b581d8cf8 [file] [log] [blame]
Andrew Walbranea9fa482021-03-04 16:11:12 +00001// 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 Hanc221c052022-02-22 05:20:15 +090017mod create_idsig;
Jiyong Park48b354d2021-07-15 15:04:38 +090018mod create_partition;
Andrew Walbranf395b822021-05-05 10:38:59 +000019mod run;
Andrew Walbranea9fa482021-03-04 16:11:12 +000020
Jiyong Parkc2a49cc2021-10-15 00:02:12 +090021use android_system_virtualizationservice::aidl::android::system::virtualizationservice::{
David Brazdil7d1e5ec2023-02-06 17:56:29 +000022 CpuTopology::CpuTopology, IVirtualizationService::IVirtualizationService,
23 PartitionType::PartitionType, VirtualMachineAppConfig::DebugLevel::DebugLevel,
Jiyong Parkc2a49cc2021-10-15 00:02:12 +090024};
Shikha Panwar61a74b52024-02-16 13:17:01 +000025#[cfg(not(llpvm_changes))]
26use anyhow::anyhow;
Yi-Yo Chiang2fbf0da2024-06-14 22:56:56 +080027use anyhow::{bail, Context, Error};
Alan Stokesc4d5def2023-02-14 17:01:59 +000028use binder::{ProcessState, Strong};
Jiyong Parkb1935ef2023-08-10 17:22:39 +090029use clap::{Args, Parser};
Jooyung Hanc221c052022-02-22 05:20:15 +090030use create_idsig::command_create_idsig;
Jiyong Park48b354d2021-07-15 15:04:38 +090031use create_partition::command_create_partition;
Nikita Ioffeb0b67562022-11-22 15:48:06 +000032use run::{command_run, command_run_app, command_run_microdroid};
Jaewan Kim0c99c612024-03-23 00:44:14 +090033use serde::Serialize;
Yi-Yo Chiang2fbf0da2024-06-14 22:56:56 +080034use std::io::{self, IsTerminal};
Nikita Ioffe5776f082023-02-10 21:38:26 +000035use std::num::NonZeroU16;
Yi-Yo Chiang2fbf0da2024-06-14 22:56:56 +080036use std::os::unix::process::CommandExt;
Andrew Walbranc4b1bde2022-02-03 15:26:02 +000037use std::path::{Path, PathBuf};
Yi-Yo Chiang2fbf0da2024-06-14 22:56:56 +080038use std::process::Command;
Andrew Walbranea9fa482021-03-04 16:11:12 +000039
Alan Stokesfda70842023-12-20 17:50:14 +000040#[derive(Args, Default)]
Jiyong Parkb1935ef2023-08-10 17:22:39 +090041/// Collection of flags that are at VM level and therefore applicable to all subcommands
42pub 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 Parkb1935ef2023-08-10 17:22:39 +090051 /// 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 Donnefort538a2c62024-03-20 16:01:10 +000059
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 Yoo13af0b62024-05-20 14:15:13 +090066
67 /// Run VM with network feature.
68 #[cfg(network)]
69 #[arg(short, long)]
70 network_supported: bool,
David Dai23cff712024-06-13 19:23:45 +000071
72 /// Boost uclamp to stablise results for benchmarks.
73 #[arg(short, long)]
74 boost_uclamp: bool,
Seungjae Yoo13af0b62024-05-20 14:15:13 +090075}
76
77impl CommonConfig {
Pierre-Clément Tosi6a1090d2024-09-03 17:27:53 +000078 #[cfg(network)]
Seungjae Yoo13af0b62024-05-20 14:15:13 +090079 fn network_supported(&self) -> bool {
Pierre-Clément Tosi6a1090d2024-09-03 17:27:53 +000080 self.network_supported
81 }
82
83 #[cfg(not(network))]
84 fn network_supported(&self) -> bool {
85 false
Seungjae Yoo13af0b62024-05-20 14:15:13 +090086 }
Jiyong Parkb1935ef2023-08-10 17:22:39 +090087}
88
Alan Stokesfda70842023-12-20 17:50:14 +000089#[derive(Args, Default)]
Jiyong Parkb1935ef2023-08-10 17:22:39 +090090/// Collection of flags for debugging
91pub 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 Ioffeb4268b32024-09-03 10:23:14 +0000112
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
119impl DebugConfig {
Pierre-Clément Tosi6a1090d2024-09-03 17:27:53 +0000120 #[cfg(debuggable_vms_improvements)]
Nikita Ioffeb4268b32024-09-03 10:23:14 +0000121 fn enable_earlycon(&self) -> bool {
Pierre-Clément Tosi6a1090d2024-09-03 17:27:53 +0000122 self.enable_earlycon
123 }
124
125 #[cfg(not(debuggable_vms_improvements))]
126 fn enable_earlycon(&self) -> bool {
127 false
Nikita Ioffeb4268b32024-09-03 10:23:14 +0000128 }
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900129}
130
Alan Stokesfda70842023-12-20 17:50:14 +0000131#[derive(Args, Default)]
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900132/// Collection of flags that are Microdroid specific
133pub 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 Parkb1935ef2023-08-10 17:22:39 +0900144 /// Path to disk image containing vendor-specific modules.
Nikita Ioffe631717e2023-09-05 13:38:07 +0100145 #[cfg(vendor_modules)]
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900146 #[arg(long)]
147 vendor: Option<PathBuf>,
148
149 /// SysFS nodes of devices to assign to VM
Nikita Ioffe94a8a182023-11-16 16:37:48 +0000150 #[cfg(device_assignment)]
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900151 #[arg(long)]
152 devices: Vec<PathBuf>,
Inseob Kim172f9eb2023-11-06 17:02:08 +0900153
Inseob Kim7a1fc8f2023-11-22 18:45:28 +0900154 /// Version of GKI to use. If set, use instead of microdroid kernel
Inseob Kim172f9eb2023-11-06 17:02:08 +0900155 #[cfg(vendor_modules)]
156 #[arg(long)]
Inseob Kim7a1fc8f2023-11-22 18:45:28 +0900157 gki: Option<String>,
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900158}
159
Nikita Ioffe631717e2023-09-05 13:38:07 +0100160impl MicrodroidConfig {
Pierre-Clément Tosi6a1090d2024-09-03 17:27:53 +0000161 #[cfg(vendor_modules)]
Pierre-Clément Tosif1feafb2024-09-03 14:05:19 +0100162 fn vendor(&self) -> Option<&PathBuf> {
Pierre-Clément Tosi6a1090d2024-09-03 17:27:53 +0000163 self.vendor.as_ref()
Nikita Ioffe631717e2023-09-05 13:38:07 +0100164 }
165
Pierre-Clément Tosi6a1090d2024-09-03 17:27:53 +0000166 #[cfg(not(vendor_modules))]
167 fn vendor(&self) -> Option<&PathBuf> {
168 None
169 }
170
171 #[cfg(vendor_modules)]
Inseob Kim7a1fc8f2023-11-22 18:45:28 +0900172 fn gki(&self) -> Option<&str> {
Pierre-Clément Tosi6a1090d2024-09-03 17:27:53 +0000173 self.gki.as_deref()
Inseob Kim172f9eb2023-11-06 17:02:08 +0900174 }
175
Pierre-Clément Tosi6a1090d2024-09-03 17:27:53 +0000176 #[cfg(not(vendor_modules))]
177 fn gki(&self) -> Option<&str> {
178 None
179 }
180
181 #[cfg(device_assignment)]
Pierre-Clément Tosif1feafb2024-09-03 14:05:19 +0100182 fn devices(&self) -> &[PathBuf] {
Pierre-Clément Tosi6a1090d2024-09-03 17:27:53 +0000183 &self.devices
184 }
185
186 #[cfg(not(device_assignment))]
187 fn devices(&self) -> &[PathBuf] {
188 &[]
Nikita Ioffe94a8a182023-11-16 16:37:48 +0000189 }
Nikita Ioffe631717e2023-09-05 13:38:07 +0100190}
191
Alan Stokesfda70842023-12-20 17:50:14 +0000192#[derive(Args, Default)]
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900193/// Flags for the run_app subcommand
194pub 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 Panwar61a74b52024-02-16 13:17:01 +0000213 /// 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 Parkb1935ef2023-08-10 17:22:39 +0900218 /// 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 Stokesfda70842023-12-20 17:50:14 +0000227 /// 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 Parkb1935ef2023-08-10 17:22:39 +0900233 /// Paths to extra idsig files.
234 #[arg(long = "extra-idsig")]
235 extra_idsigs: Vec<PathBuf>,
236}
237
Alan Stokesfda70842023-12-20 17:50:14 +0000238impl RunAppConfig {
Pierre-Clément Tosi6a1090d2024-09-03 17:27:53 +0000239 #[cfg(multi_tenant)]
Alan Stokesfda70842023-12-20 17:50:14 +0000240 fn extra_apks(&self) -> &[PathBuf] {
Pierre-Clément Tosi6a1090d2024-09-03 17:27:53 +0000241 &self.extra_apks
Alan Stokesfda70842023-12-20 17:50:14 +0000242 }
243
Pierre-Clément Tosi6a1090d2024-09-03 17:27:53 +0000244 #[cfg(not(multi_tenant))]
245 fn extra_apks(&self) -> &[PathBuf] {
246 &[]
247 }
248
249 #[cfg(llpvm_changes)]
Shikha Panwar61a74b52024-02-16 13:17:01 +0000250 fn instance_id(&self) -> Result<PathBuf, Error> {
Pierre-Clément Tosi6a1090d2024-09-03 17:27:53 +0000251 Ok(self.instance_id.clone())
Shikha Panwar61a74b52024-02-16 13:17:01 +0000252 }
253
Pierre-Clément Tosi6a1090d2024-09-03 17:27:53 +0000254 #[cfg(not(llpvm_changes))]
255 fn instance_id(&self) -> Result<PathBuf, Error> {
256 Err(anyhow!("LLPVM feature is disabled, --instance_id flag not supported"))
257 }
258
259 #[cfg(llpvm_changes)]
Shikha Panwar61a74b52024-02-16 13:17:01 +0000260 fn set_instance_id(&mut self, instance_id_file: PathBuf) -> Result<(), Error> {
Pierre-Clément Tosi6a1090d2024-09-03 17:27:53 +0000261 self.instance_id = instance_id_file;
262 Ok(())
263 }
264
265 #[cfg(not(llpvm_changes))]
266 fn set_instance_id(&mut self, _: PathBuf) -> Result<(), Error> {
267 Err(anyhow!("LLPVM feature is disabled, --instance_id flag not supported"))
Shikha Panwar61a74b52024-02-16 13:17:01 +0000268 }
Alan Stokesfda70842023-12-20 17:50:14 +0000269}
270
271#[derive(Args, Default)]
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900272/// Flags for the run_microdroid subcommand
273pub struct RunMicrodroidConfig {
274 #[command(flatten)]
275 common: CommonConfig,
276
277 #[command(flatten)]
278 debug: DebugConfig,
279
280 #[command(flatten)]
281 microdroid: MicrodroidConfig,
282
283 /// Path to the directory where VM-related files (e.g. instance.img, apk.idsig, etc.) will
284 /// be stored. If not specified a random directory under /data/local/tmp/microdroid will be
285 /// created and used.
286 #[arg(long)]
287 work_dir: Option<PathBuf>,
288}
289
Alan Stokesfda70842023-12-20 17:50:14 +0000290#[derive(Args, Default)]
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900291/// Flags for the run subcommand
292pub struct RunCustomVmConfig {
293 #[command(flatten)]
294 common: CommonConfig,
295
296 #[command(flatten)]
297 debug: DebugConfig,
298
299 /// Path to VM config JSON
300 config: PathBuf,
301}
302
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700303#[derive(Parser)]
David Brazdil20412d92021-03-18 10:53:06 +0000304enum Opt {
Shikha Panwar6d306412024-02-17 21:37:49 +0000305 /// Check if the feature is enabled on device.
306 CheckFeatureEnabled { feature: String },
Jooyung Han21e9b922021-06-26 04:14:16 +0900307 /// Run a virtual machine with a config in APK
308 RunApp {
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900309 #[command(flatten)]
310 config: RunAppConfig,
Jooyung Han21e9b922021-06-26 04:14:16 +0900311 },
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000312 /// Run a virtual machine with Microdroid inside
313 RunMicrodroid {
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900314 #[command(flatten)]
315 config: RunMicrodroidConfig,
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000316 },
David Brazdil20412d92021-03-18 10:53:06 +0000317 /// Run a virtual machine
318 Run {
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900319 #[command(flatten)]
320 config: RunCustomVmConfig,
David Brazdil3c2ddef2021-03-18 13:09:57 +0000321 },
David Brazdil20412d92021-03-18 10:53:06 +0000322 /// List running virtual machines
323 List,
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000324 /// Print information about virtual machine support
325 Info,
Andrew Walbrandff3b942021-06-09 15:20:36 +0000326 /// Create a new empty partition to be used as a writable partition for a VM
327 CreatePartition {
328 /// Path at which to create the image file
Andrew Walbrandff3b942021-06-09 15:20:36 +0000329 path: PathBuf,
330
331 /// The desired size of the partition, in bytes.
332 size: u64,
Jiyong Park9dd389e2021-08-23 20:42:59 +0900333
334 /// Type of the partition
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900335 #[arg(short = 't', long = "type", default_value = "raw",
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700336 value_parser = parse_partition_type)]
Jiyong Park9dd389e2021-08-23 20:42:59 +0900337 partition_type: PartitionType,
Andrew Walbrandff3b942021-06-09 15:20:36 +0000338 },
Jooyung Hanc221c052022-02-22 05:20:15 +0900339 /// Creates or update the idsig file by digesting the input APK file.
340 CreateIdsig {
341 /// Path to VM Payload APK
Jooyung Hanc221c052022-02-22 05:20:15 +0900342 apk: PathBuf,
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700343
Jooyung Hanc221c052022-02-22 05:20:15 +0900344 /// Path to idsig of the APK
Jooyung Hanc221c052022-02-22 05:20:15 +0900345 path: PathBuf,
346 },
Yi-Yo Chiang2fbf0da2024-06-14 22:56:56 +0800347 /// Connect to the serial console of a VM
348 Console {
349 /// CID of the VM
350 cid: Option<i32>,
351 },
David Brazdil20412d92021-03-18 10:53:06 +0000352}
353
Jiyong Parkc2a49cc2021-10-15 00:02:12 +0900354fn parse_debug_level(s: &str) -> Result<DebugLevel, String> {
355 match s {
356 "none" => Ok(DebugLevel::NONE),
Jiyong Parkc2a49cc2021-10-15 00:02:12 +0900357 "full" => Ok(DebugLevel::FULL),
358 _ => Err(format!("Invalid debug level {}", s)),
359 }
360}
361
Jiyong Park9dd389e2021-08-23 20:42:59 +0900362fn parse_partition_type(s: &str) -> Result<PartitionType, String> {
363 match s {
364 "raw" => Ok(PartitionType::RAW),
365 "instance" => Ok(PartitionType::ANDROID_VM_INSTANCE),
366 _ => Err(format!("Invalid partition type {}", s)),
367 }
368}
369
David Brazdil7d1e5ec2023-02-06 17:56:29 +0000370fn parse_cpu_topology(s: &str) -> Result<CpuTopology, String> {
371 match s {
372 "one_cpu" => Ok(CpuTopology::ONE_CPU),
373 "match_host" => Ok(CpuTopology::MATCH_HOST),
374 _ => Err(format!("Invalid cpu topology {}", s)),
375 }
376}
377
Alan Stokesc4d5def2023-02-14 17:01:59 +0000378fn get_service() -> Result<Strong<dyn IVirtualizationService>, Error> {
379 let virtmgr =
380 vmclient::VirtualizationService::new().context("Failed to spawn VirtualizationService")?;
381 virtmgr.connect().context("Failed to connect to VirtualizationService")
382}
383
Shikha Panwar6d306412024-02-17 21:37:49 +0000384fn command_check_feature_enabled(feature: &str) {
385 println!(
386 "Feature {feature} is {}",
387 if avf_features::is_feature_enabled(feature) { "enabled" } else { "disabled" }
388 );
389}
390
Andrew Walbranea9fa482021-03-04 16:11:12 +0000391fn main() -> Result<(), Error> {
392 env_logger::init();
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700393 let opt = Opt::parse();
Andrew Walbranea9fa482021-03-04 16:11:12 +0000394
395 // We need to start the thread pool for Binder to work properly, especially link_to_death.
396 ProcessState::start_thread_pool();
397
David Brazdil20412d92021-03-18 10:53:06 +0000398 match opt {
Shikha Panwar6d306412024-02-17 21:37:49 +0000399 Opt::CheckFeatureEnabled { feature } => {
400 command_check_feature_enabled(&feature);
401 Ok(())
402 }
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900403 Opt::RunApp { config } => command_run_app(config),
404 Opt::RunMicrodroid { config } => command_run_microdroid(config),
405 Opt::Run { config } => command_run(config),
Alan Stokesc4d5def2023-02-14 17:01:59 +0000406 Opt::List => command_list(get_service()?.as_ref()),
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000407 Opt::Info => command_info(),
Jiyong Park9dd389e2021-08-23 20:42:59 +0900408 Opt::CreatePartition { path, size, partition_type } => {
Alan Stokesc4d5def2023-02-14 17:01:59 +0000409 command_create_partition(get_service()?.as_ref(), &path, size, partition_type)
Jiyong Park9dd389e2021-08-23 20:42:59 +0900410 }
Alan Stokesc4d5def2023-02-14 17:01:59 +0000411 Opt::CreateIdsig { apk, path } => {
412 command_create_idsig(get_service()?.as_ref(), &apk, &path)
413 }
Yi-Yo Chiang2fbf0da2024-06-14 22:56:56 +0800414 Opt::Console { cid } => command_console(cid),
Andrew Walbranea9fa482021-03-04 16:11:12 +0000415 }
416}
417
Andrew Walbran320b5602021-03-04 16:11:12 +0000418/// List the VMs currently running.
Andrew Walbran616d13f2022-05-12 18:35:55 +0000419fn command_list(service: &dyn IVirtualizationService) -> Result<(), Error> {
Andrew Walbran17de24f2021-05-27 13:27:30 +0000420 let vms = service.debugListVms().context("Failed to get list of VMs")?;
Andrew Walbran320b5602021-03-04 16:11:12 +0000421 println!("Running VMs: {:#?}", vms);
422 Ok(())
423}
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000424
425/// Print information about supported VM types.
426fn command_info() -> Result<(), Error> {
Alan Stokesc4d5def2023-02-14 17:01:59 +0000427 let non_protected_vm_supported = hypervisor_props::is_vm_supported()?;
428 let protected_vm_supported = hypervisor_props::is_protected_vm_supported()?;
Alan Stokes8d39a9b2023-01-10 15:01:00 +0000429 match (non_protected_vm_supported, protected_vm_supported) {
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000430 (false, false) => println!("VMs are not supported."),
431 (false, true) => println!("Only protected VMs are supported."),
Alan Stokes8d39a9b2023-01-10 15:01:00 +0000432 (true, false) => println!("Only non-protected VMs are supported."),
433 (true, true) => println!("Both protected and non-protected VMs are supported."),
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000434 }
435
Alan Stokesc4d5def2023-02-14 17:01:59 +0000436 if let Some(version) = hypervisor_props::version()? {
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000437 println!("Hypervisor version: {}", version);
438 } else {
439 println!("Hypervisor version not set.");
440 }
441
442 if Path::new("/dev/kvm").exists() {
443 println!("/dev/kvm exists.");
444 } else {
445 println!("/dev/kvm does not exist.");
446 }
447
Inseob Kim6ef80972023-07-20 17:23:36 +0900448 if Path::new("/dev/vfio/vfio").exists() {
449 println!("/dev/vfio/vfio exists.");
450 } else {
451 println!("/dev/vfio/vfio does not exist.");
452 }
453
454 if Path::new("/sys/bus/platform/drivers/vfio-platform").exists() {
455 println!("VFIO-platform is supported.");
456 } else {
457 println!("VFIO-platform is not supported.");
458 }
459
Jaewan Kim0c99c612024-03-23 00:44:14 +0900460 #[derive(Serialize)]
461 struct AssignableDevice {
462 node: String,
463 dtbo_label: String,
464 }
465
Inseob Kim75460b32023-08-09 13:41:31 +0900466 let devices = get_service()?.getAssignableDevices()?;
Jaewan Kim0c99c612024-03-23 00:44:14 +0900467 let devices: Vec<_> = devices
468 .into_iter()
469 .map(|device| AssignableDevice { node: device.node, dtbo_label: device.dtbo_label })
470 .collect();
Inseob Kim75460b32023-08-09 13:41:31 +0900471 println!("Assignable devices: {}", serde_json::to_string(&devices)?);
472
Inseob Kim46257382024-01-03 15:41:22 +0900473 let os_list = get_service()?.getSupportedOSList()?;
474 println!("Available OS list: {}", serde_json::to_string(&os_list)?);
Inseob Kim7a1fc8f2023-11-22 18:45:28 +0900475
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000476 Ok(())
477}
Andrew Walbran1f810b62022-08-10 13:33:57 +0000478
Yi-Yo Chiang2fbf0da2024-06-14 22:56:56 +0800479fn command_console(cid: Option<i32>) -> Result<(), Error> {
480 if !io::stdin().is_terminal() {
481 bail!("Stdin must be a terminal (tty). Use 'adb shell -t' to force allocate tty.");
482 }
483 let mut vms = get_service()?.debugListVms().context("Failed to get list of VMs")?;
484 if let Some(cid) = cid {
485 vms.retain(|vm_info| vm_info.cid == cid);
486 }
487 let host_console_name = vms
488 .into_iter()
489 .find_map(|vm_info| vm_info.hostConsoleName)
490 .context("Failed to get VM with console")?;
491 Err(Command::new("microcom").arg(host_console_name).exec().into())
492}
493
Andrew Walbran1f810b62022-08-10 13:33:57 +0000494#[cfg(test)]
495mod tests {
496 use super::*;
Andrew Walbranaa1efc42022-08-10 13:33:57 +0000497 use clap::CommandFactory;
Andrew Walbran1f810b62022-08-10 13:33:57 +0000498
499 #[test]
500 fn verify_app() {
Andrew Walbranaa1efc42022-08-10 13:33:57 +0000501 // Check that the command parsing has been configured in a valid way.
502 Opt::command().debug_assert();
Andrew Walbran1f810b62022-08-10 13:33:57 +0000503 }
504}