blob: 64bcd02ab6513113fbcce8c44afcb1b4ac062cd6 [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};
David Brazdil20412d92021-03-18 10:53:06 +000025use anyhow::{Context, Error};
Alan Stokesc4d5def2023-02-14 17:01:59 +000026use binder::{ProcessState, Strong};
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070027use clap::Parser;
Jooyung Hanc221c052022-02-22 05:20:15 +090028use create_idsig::command_create_idsig;
Jiyong Park48b354d2021-07-15 15:04:38 +090029use create_partition::command_create_partition;
Nikita Ioffeb0b67562022-11-22 15:48:06 +000030use run::{command_run, command_run_app, command_run_microdroid};
Nikita Ioffe5776f082023-02-10 21:38:26 +000031use std::num::NonZeroU16;
Andrew Walbranc4b1bde2022-02-03 15:26:02 +000032use std::path::{Path, PathBuf};
Andrew Walbranea9fa482021-03-04 16:11:12 +000033
Inseob Kima5a262f2021-11-17 19:41:03 +090034#[derive(Debug)]
35struct Idsigs(Vec<PathBuf>);
36
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070037#[derive(Parser)]
David Brazdil20412d92021-03-18 10:53:06 +000038enum Opt {
Jooyung Han21e9b922021-06-26 04:14:16 +090039 /// Run a virtual machine with a config in APK
40 RunApp {
41 /// Path to VM Payload APK
Jooyung Han21e9b922021-06-26 04:14:16 +090042 apk: PathBuf,
43
44 /// Path to idsig of the APK
Jooyung Han21e9b922021-06-26 04:14:16 +090045 idsig: PathBuf,
46
Jiyong Park48b354d2021-07-15 15:04:38 +090047 /// Path to the instance image. Created if not exists.
Jiyong Park48b354d2021-07-15 15:04:38 +090048 instance: PathBuf,
49
Jooyung Han21e9b922021-06-26 04:14:16 +090050 /// Path to VM config JSON within APK (e.g. assets/vm_config.json)
Inseob Kim7b5f65c2022-11-15 14:27:04 +090051 #[clap(long)]
Alan Stokes0d1ef782022-09-27 13:46:35 +010052 config_path: Option<String>,
Jooyung Han21e9b922021-06-26 04:14:16 +090053
Alan Stokes8f12f2b2023-01-09 09:19:20 +000054 /// Name of VM payload binary within APK (e.g. MicrodroidTestNativeLib.so)
Inseob Kim7b5f65c2022-11-15 14:27:04 +090055 #[clap(long)]
Alan Stokes8f12f2b2023-01-09 09:19:20 +000056 #[clap(alias = "payload_path")]
57 payload_binary_name: Option<String>,
Inseob Kim7b5f65c2022-11-15 14:27:04 +090058
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070059 /// Name of VM
60 #[clap(long)]
61 name: Option<String>,
62
Shikha Panwar22e70452022-10-10 18:32:55 +000063 /// Path to the file backing the storage.
64 /// Created if the option is used but the path does not exist in the device.
65 #[clap(long)]
66 storage: Option<PathBuf>,
67
68 /// Size of the storage. Used only if --storage is supplied but path does not exist
69 /// Default size is 10*1024*1024
70 #[clap(long)]
71 storage_size: Option<u64>,
72
Jiyong Parkb8182bb2021-10-26 22:53:08 +090073 /// Path to file for VM console output.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070074 #[clap(long)]
Jiyong Parkb8182bb2021-10-26 22:53:08 +090075 console: Option<PathBuf>,
76
Jiyong Parke6fb1672023-06-26 16:45:55 +090077 /// Path to file for VM console input.
78 #[clap(long)]
79 console_in: Option<PathBuf>,
80
Jooyung Han21e9b922021-06-26 04:14:16 +090081 /// Path to file for VM log output.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070082 #[clap(long)]
Jooyung Han21e9b922021-06-26 04:14:16 +090083 log: Option<PathBuf>,
Jiyong Park23601142021-07-05 13:15:32 +090084
Jiyong Parke9d49662023-07-14 15:46:44 +090085 /// Debug level of the VM. Supported values: "full" (default), and "none".
86 #[clap(long, default_value = "full", value_parser = parse_debug_level)]
Jiyong Parkc2a49cc2021-10-15 00:02:12 +090087 debug: DebugLevel,
Jiyong Parkd63cfff2021-09-27 20:10:17 +090088
Andrew Walbran3994f002022-01-27 17:33:45 +000089 /// Run VM in protected mode.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070090 #[clap(short, long)]
Andrew Walbran3994f002022-01-27 17:33:45 +000091 protected: bool,
92
Jiyong Parkd63cfff2021-09-27 20:10:17 +090093 /// Memory size (in MiB) of the VM. If unspecified, defaults to the value of `memory_mib`
94 /// in the VM config file.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070095 #[clap(short, long)]
Jiyong Parkd63cfff2021-09-27 20:10:17 +090096 mem: Option<u32>,
Inseob Kima5a262f2021-11-17 19:41:03 +090097
David Brazdil7d1e5ec2023-02-06 17:56:29 +000098 /// Run VM with vCPU topology matching that of the host. If unspecified, defaults to 1 vCPU.
99 #[clap(long, default_value = "one_cpu", value_parser = parse_cpu_topology)]
100 cpu_topology: CpuTopology,
Jiyong Park032615f2022-01-10 13:55:34 +0900101
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900102 /// Comma separated list of task profile names to apply to the VM
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700103 #[clap(long)]
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900104 task_profiles: Vec<String>,
105
Inseob Kima5a262f2021-11-17 19:41:03 +0900106 /// Paths to extra idsig files.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700107 #[clap(long = "extra-idsig")]
Inseob Kima5a262f2021-11-17 19:41:03 +0900108 extra_idsigs: Vec<PathBuf>,
Nikita Ioffe5776f082023-02-10 21:38:26 +0000109
110 /// Port at which crosvm will start a gdb server to debug guest kernel.
111 /// Note: this is only supported on Android kernels android14-5.15 and higher.
112 #[clap(long)]
113 gdb: Option<NonZeroU16>,
Nikita Ioffe26c35ed2023-06-05 17:49:08 +0100114
115 /// Path to custom kernel image to use when booting Microdroid.
116 #[clap(long)]
117 kernel: Option<PathBuf>,
Nikita Ioffe5dfddf22023-06-29 16:11:26 +0100118
119 /// Path to disk image containing vendor-specific modules.
120 #[clap(long)]
121 vendor: Option<PathBuf>,
Inseob Kim6ef80972023-07-20 17:23:36 +0900122
123 /// SysFS nodes of devices to assign to VM
124 #[clap(long)]
125 devices: Vec<PathBuf>,
Jooyung Han21e9b922021-06-26 04:14:16 +0900126 },
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000127 /// Run a virtual machine with Microdroid inside
128 RunMicrodroid {
129 /// Path to the directory where VM-related files (e.g. instance.img, apk.idsig, etc.) will
130 /// be stored. If not specified a random directory under /data/local/tmp/microdroid will be
131 /// created and used.
132 #[clap(long)]
133 work_dir: Option<PathBuf>,
134
135 /// Name of VM
136 #[clap(long)]
137 name: Option<String>,
138
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000139 /// Path to the file backing the storage.
140 /// Created if the option is used but the path does not exist in the device.
141 #[clap(long)]
142 storage: Option<PathBuf>,
143
144 /// Size of the storage. Used only if --storage is supplied but path does not exist
145 /// Default size is 10*1024*1024
146 #[clap(long)]
147 storage_size: Option<u64>,
148
149 /// Path to file for VM console output.
150 #[clap(long)]
151 console: Option<PathBuf>,
152
Jiyong Parke6fb1672023-06-26 16:45:55 +0900153 /// Path to file for VM console input.
154 #[clap(long)]
155 console_in: Option<PathBuf>,
156
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000157 /// Path to file for VM log output.
158 #[clap(long)]
159 log: Option<PathBuf>,
160
Jiyong Parke9d49662023-07-14 15:46:44 +0900161 /// Debug level of the VM. Supported values: "full" (default), and "none".
Nikita Ioffe0a15fcf2022-12-01 21:12:50 +0000162 #[clap(long, default_value = "full", value_parser = parse_debug_level)]
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000163 debug: DebugLevel,
164
165 /// Run VM in protected mode.
166 #[clap(short, long)]
167 protected: bool,
168
169 /// Memory size (in MiB) of the VM. If unspecified, defaults to the value of `memory_mib`
170 /// in the VM config file.
171 #[clap(short, long)]
172 mem: Option<u32>,
173
David Brazdil7d1e5ec2023-02-06 17:56:29 +0000174 /// Run VM with vCPU topology matching that of the host. If unspecified, defaults to 1 vCPU.
175 #[clap(long, default_value = "one_cpu", value_parser = parse_cpu_topology)]
176 cpu_topology: CpuTopology,
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000177
178 /// Comma separated list of task profile names to apply to the VM
179 #[clap(long)]
180 task_profiles: Vec<String>,
Nikita Ioffe5776f082023-02-10 21:38:26 +0000181
182 /// Port at which crosvm will start a gdb server to debug guest kernel.
183 /// Note: this is only supported on Android kernels android14-5.15 and higher.
184 #[clap(long)]
185 gdb: Option<NonZeroU16>,
Nikita Ioffe26c35ed2023-06-05 17:49:08 +0100186
187 /// Path to custom kernel image to use when booting Microdroid.
188 #[clap(long)]
189 kernel: Option<PathBuf>,
Nikita Ioffe5dfddf22023-06-29 16:11:26 +0100190
191 /// Path to disk image containing vendor-specific modules.
192 #[clap(long)]
193 vendor: Option<PathBuf>,
Inseob Kim6ef80972023-07-20 17:23:36 +0900194
195 /// SysFS nodes of devices to assign to VM
196 #[clap(long)]
197 devices: Vec<PathBuf>,
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000198 },
David Brazdil20412d92021-03-18 10:53:06 +0000199 /// Run a virtual machine
200 Run {
201 /// Path to VM config JSON
David Brazdil20412d92021-03-18 10:53:06 +0000202 config: PathBuf,
David Brazdil3c2ddef2021-03-18 13:09:57 +0000203
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700204 /// Name of VM
205 #[clap(long)]
206 name: Option<String>,
207
David Brazdil7d1e5ec2023-02-06 17:56:29 +0000208 /// Run VM with vCPU topology matching that of the host. If unspecified, defaults to 1 vCPU.
209 #[clap(long, default_value = "one_cpu", value_parser = parse_cpu_topology)]
210 cpu_topology: CpuTopology,
Jiyong Park032615f2022-01-10 13:55:34 +0900211
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900212 /// Comma separated list of task profile names to apply to the VM
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700213 #[clap(long)]
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900214 task_profiles: Vec<String>,
215
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900216 /// Path to file for VM console output.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700217 #[clap(long)]
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900218 console: Option<PathBuf>,
Jooyung Hanb7983a22022-02-22 05:21:27 +0900219
Jiyong Parke6fb1672023-06-26 16:45:55 +0900220 /// Path to file for VM console input.
221 #[clap(long)]
222 console_in: Option<PathBuf>,
223
Jooyung Hanb7983a22022-02-22 05:21:27 +0900224 /// Path to file for VM log output.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700225 #[clap(long)]
Jooyung Hanb7983a22022-02-22 05:21:27 +0900226 log: Option<PathBuf>,
Nikita Ioffe5776f082023-02-10 21:38:26 +0000227
228 /// Port at which crosvm will start a gdb server to debug guest kernel.
229 /// Note: this is only supported on Android kernels android14-5.15 and higher.
230 #[clap(long)]
231 gdb: Option<NonZeroU16>,
David Brazdil3c2ddef2021-03-18 13:09:57 +0000232 },
David Brazdil20412d92021-03-18 10:53:06 +0000233 /// List running virtual machines
234 List,
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000235 /// Print information about virtual machine support
236 Info,
Andrew Walbrandff3b942021-06-09 15:20:36 +0000237 /// Create a new empty partition to be used as a writable partition for a VM
238 CreatePartition {
239 /// Path at which to create the image file
Andrew Walbrandff3b942021-06-09 15:20:36 +0000240 path: PathBuf,
241
242 /// The desired size of the partition, in bytes.
243 size: u64,
Jiyong Park9dd389e2021-08-23 20:42:59 +0900244
245 /// Type of the partition
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700246 #[clap(short = 't', long = "type", default_value = "raw",
247 value_parser = parse_partition_type)]
Jiyong Park9dd389e2021-08-23 20:42:59 +0900248 partition_type: PartitionType,
Andrew Walbrandff3b942021-06-09 15:20:36 +0000249 },
Jooyung Hanc221c052022-02-22 05:20:15 +0900250 /// Creates or update the idsig file by digesting the input APK file.
251 CreateIdsig {
252 /// Path to VM Payload APK
Jooyung Hanc221c052022-02-22 05:20:15 +0900253 apk: PathBuf,
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700254
Jooyung Hanc221c052022-02-22 05:20:15 +0900255 /// Path to idsig of the APK
Jooyung Hanc221c052022-02-22 05:20:15 +0900256 path: PathBuf,
257 },
David Brazdil20412d92021-03-18 10:53:06 +0000258}
259
Jiyong Parkc2a49cc2021-10-15 00:02:12 +0900260fn parse_debug_level(s: &str) -> Result<DebugLevel, String> {
261 match s {
262 "none" => Ok(DebugLevel::NONE),
Jiyong Parkc2a49cc2021-10-15 00:02:12 +0900263 "full" => Ok(DebugLevel::FULL),
264 _ => Err(format!("Invalid debug level {}", s)),
265 }
266}
267
Jiyong Park9dd389e2021-08-23 20:42:59 +0900268fn parse_partition_type(s: &str) -> Result<PartitionType, String> {
269 match s {
270 "raw" => Ok(PartitionType::RAW),
271 "instance" => Ok(PartitionType::ANDROID_VM_INSTANCE),
272 _ => Err(format!("Invalid partition type {}", s)),
273 }
274}
275
David Brazdil7d1e5ec2023-02-06 17:56:29 +0000276fn parse_cpu_topology(s: &str) -> Result<CpuTopology, String> {
277 match s {
278 "one_cpu" => Ok(CpuTopology::ONE_CPU),
279 "match_host" => Ok(CpuTopology::MATCH_HOST),
280 _ => Err(format!("Invalid cpu topology {}", s)),
281 }
282}
283
Alan Stokesc4d5def2023-02-14 17:01:59 +0000284fn get_service() -> Result<Strong<dyn IVirtualizationService>, Error> {
285 let virtmgr =
286 vmclient::VirtualizationService::new().context("Failed to spawn VirtualizationService")?;
287 virtmgr.connect().context("Failed to connect to VirtualizationService")
288}
289
Andrew Walbranea9fa482021-03-04 16:11:12 +0000290fn main() -> Result<(), Error> {
291 env_logger::init();
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700292 let opt = Opt::parse();
Andrew Walbranea9fa482021-03-04 16:11:12 +0000293
294 // We need to start the thread pool for Binder to work properly, especially link_to_death.
295 ProcessState::start_thread_pool();
296
David Brazdil20412d92021-03-18 10:53:06 +0000297 match opt {
Inseob Kima5a262f2021-11-17 19:41:03 +0900298 Opt::RunApp {
Seungjae Yoo62085c02022-08-12 04:44:52 +0000299 name,
Inseob Kima5a262f2021-11-17 19:41:03 +0900300 apk,
301 idsig,
302 instance,
Shikha Panwar22e70452022-10-10 18:32:55 +0000303 storage,
304 storage_size,
Inseob Kima5a262f2021-11-17 19:41:03 +0900305 config_path,
Alan Stokes8f12f2b2023-01-09 09:19:20 +0000306 payload_binary_name,
Inseob Kima5a262f2021-11-17 19:41:03 +0900307 console,
Jiyong Parke6fb1672023-06-26 16:45:55 +0900308 console_in,
Inseob Kima5a262f2021-11-17 19:41:03 +0900309 log,
310 debug,
Andrew Walbran3994f002022-01-27 17:33:45 +0000311 protected,
Inseob Kima5a262f2021-11-17 19:41:03 +0900312 mem,
David Brazdil7d1e5ec2023-02-06 17:56:29 +0000313 cpu_topology,
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900314 task_profiles,
Inseob Kima5a262f2021-11-17 19:41:03 +0900315 extra_idsigs,
Nikita Ioffe5776f082023-02-10 21:38:26 +0000316 gdb,
Nikita Ioffe26c35ed2023-06-05 17:49:08 +0100317 kernel,
Nikita Ioffe5dfddf22023-06-29 16:11:26 +0100318 vendor,
Inseob Kim6ef80972023-07-20 17:23:36 +0900319 devices,
Inseob Kima5a262f2021-11-17 19:41:03 +0900320 } => command_run_app(
Seungjae Yoo62085c02022-08-12 04:44:52 +0000321 name,
Alan Stokesc4d5def2023-02-14 17:01:59 +0000322 get_service()?.as_ref(),
Inseob Kima5a262f2021-11-17 19:41:03 +0900323 &apk,
324 &idsig,
325 &instance,
Shikha Panwar22e70452022-10-10 18:32:55 +0000326 storage.as_deref(),
327 storage_size,
Inseob Kim7b5f65c2022-11-15 14:27:04 +0900328 config_path,
Alan Stokes8f12f2b2023-01-09 09:19:20 +0000329 payload_binary_name,
Inseob Kima5a262f2021-11-17 19:41:03 +0900330 console.as_deref(),
Jiyong Parke6fb1672023-06-26 16:45:55 +0900331 console_in.as_deref(),
Inseob Kima5a262f2021-11-17 19:41:03 +0900332 log.as_deref(),
333 debug,
Andrew Walbran3994f002022-01-27 17:33:45 +0000334 protected,
Inseob Kima5a262f2021-11-17 19:41:03 +0900335 mem,
David Brazdil7d1e5ec2023-02-06 17:56:29 +0000336 cpu_topology,
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900337 task_profiles,
Inseob Kima5a262f2021-11-17 19:41:03 +0900338 &extra_idsigs,
Nikita Ioffe5776f082023-02-10 21:38:26 +0000339 gdb,
Nikita Ioffe26c35ed2023-06-05 17:49:08 +0100340 kernel.as_deref(),
Nikita Ioffe5dfddf22023-06-29 16:11:26 +0100341 vendor.as_deref(),
Inseob Kim6ef80972023-07-20 17:23:36 +0900342 devices,
Inseob Kima5a262f2021-11-17 19:41:03 +0900343 ),
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000344 Opt::RunMicrodroid {
345 name,
346 work_dir,
347 storage,
348 storage_size,
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000349 console,
Jiyong Parke6fb1672023-06-26 16:45:55 +0900350 console_in,
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000351 log,
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000352 debug,
353 protected,
354 mem,
David Brazdil7d1e5ec2023-02-06 17:56:29 +0000355 cpu_topology,
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000356 task_profiles,
Nikita Ioffe5776f082023-02-10 21:38:26 +0000357 gdb,
Nikita Ioffe26c35ed2023-06-05 17:49:08 +0100358 kernel,
Nikita Ioffe5dfddf22023-06-29 16:11:26 +0100359 vendor,
Inseob Kim6ef80972023-07-20 17:23:36 +0900360 devices,
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000361 } => command_run_microdroid(
362 name,
Alan Stokesc4d5def2023-02-14 17:01:59 +0000363 get_service()?.as_ref(),
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000364 work_dir,
365 storage.as_deref(),
366 storage_size,
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000367 console.as_deref(),
Jiyong Parke6fb1672023-06-26 16:45:55 +0900368 console_in.as_deref(),
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000369 log.as_deref(),
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000370 debug,
371 protected,
372 mem,
David Brazdil7d1e5ec2023-02-06 17:56:29 +0000373 cpu_topology,
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000374 task_profiles,
Nikita Ioffe5776f082023-02-10 21:38:26 +0000375 gdb,
Nikita Ioffe26c35ed2023-06-05 17:49:08 +0100376 kernel.as_deref(),
Nikita Ioffe5dfddf22023-06-29 16:11:26 +0100377 vendor.as_deref(),
Inseob Kim6ef80972023-07-20 17:23:36 +0900378 devices,
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000379 ),
Jiyong Parke6fb1672023-06-26 16:45:55 +0900380 Opt::Run { name, config, cpu_topology, task_profiles, console, console_in, log, gdb } => {
Jiyong Park032615f2022-01-10 13:55:34 +0900381 command_run(
Seungjae Yoo62085c02022-08-12 04:44:52 +0000382 name,
Alan Stokesc4d5def2023-02-14 17:01:59 +0000383 get_service()?.as_ref(),
Jiyong Park032615f2022-01-10 13:55:34 +0900384 &config,
Jiyong Park032615f2022-01-10 13:55:34 +0900385 console.as_deref(),
Jiyong Parke6fb1672023-06-26 16:45:55 +0900386 console_in.as_deref(),
Jooyung Hanb7983a22022-02-22 05:21:27 +0900387 log.as_deref(),
Jiyong Park032615f2022-01-10 13:55:34 +0900388 /* mem */ None,
David Brazdil7d1e5ec2023-02-06 17:56:29 +0000389 cpu_topology,
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900390 task_profiles,
Nikita Ioffe5776f082023-02-10 21:38:26 +0000391 gdb,
Jiyong Park032615f2022-01-10 13:55:34 +0900392 )
Andrew Walbranbe429242021-06-28 12:22:54 +0000393 }
Alan Stokesc4d5def2023-02-14 17:01:59 +0000394 Opt::List => command_list(get_service()?.as_ref()),
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000395 Opt::Info => command_info(),
Jiyong Park9dd389e2021-08-23 20:42:59 +0900396 Opt::CreatePartition { path, size, partition_type } => {
Alan Stokesc4d5def2023-02-14 17:01:59 +0000397 command_create_partition(get_service()?.as_ref(), &path, size, partition_type)
Jiyong Park9dd389e2021-08-23 20:42:59 +0900398 }
Alan Stokesc4d5def2023-02-14 17:01:59 +0000399 Opt::CreateIdsig { apk, path } => {
400 command_create_idsig(get_service()?.as_ref(), &apk, &path)
401 }
Andrew Walbranea9fa482021-03-04 16:11:12 +0000402 }
403}
404
Andrew Walbran320b5602021-03-04 16:11:12 +0000405/// List the VMs currently running.
Andrew Walbran616d13f2022-05-12 18:35:55 +0000406fn command_list(service: &dyn IVirtualizationService) -> Result<(), Error> {
Andrew Walbran17de24f2021-05-27 13:27:30 +0000407 let vms = service.debugListVms().context("Failed to get list of VMs")?;
Andrew Walbran320b5602021-03-04 16:11:12 +0000408 println!("Running VMs: {:#?}", vms);
409 Ok(())
410}
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000411
412/// Print information about supported VM types.
413fn command_info() -> Result<(), Error> {
Alan Stokesc4d5def2023-02-14 17:01:59 +0000414 let non_protected_vm_supported = hypervisor_props::is_vm_supported()?;
415 let protected_vm_supported = hypervisor_props::is_protected_vm_supported()?;
Alan Stokes8d39a9b2023-01-10 15:01:00 +0000416 match (non_protected_vm_supported, protected_vm_supported) {
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000417 (false, false) => println!("VMs are not supported."),
418 (false, true) => println!("Only protected VMs are supported."),
Alan Stokes8d39a9b2023-01-10 15:01:00 +0000419 (true, false) => println!("Only non-protected VMs are supported."),
420 (true, true) => println!("Both protected and non-protected VMs are supported."),
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000421 }
422
Alan Stokesc4d5def2023-02-14 17:01:59 +0000423 if let Some(version) = hypervisor_props::version()? {
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000424 println!("Hypervisor version: {}", version);
425 } else {
426 println!("Hypervisor version not set.");
427 }
428
429 if Path::new("/dev/kvm").exists() {
430 println!("/dev/kvm exists.");
431 } else {
432 println!("/dev/kvm does not exist.");
433 }
434
Inseob Kim6ef80972023-07-20 17:23:36 +0900435 if Path::new("/dev/vfio/vfio").exists() {
436 println!("/dev/vfio/vfio exists.");
437 } else {
438 println!("/dev/vfio/vfio does not exist.");
439 }
440
441 if Path::new("/sys/bus/platform/drivers/vfio-platform").exists() {
442 println!("VFIO-platform is supported.");
443 } else {
444 println!("VFIO-platform is not supported.");
445 }
446
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000447 Ok(())
448}
Andrew Walbran1f810b62022-08-10 13:33:57 +0000449
450#[cfg(test)]
451mod tests {
452 use super::*;
Andrew Walbranaa1efc42022-08-10 13:33:57 +0000453 use clap::CommandFactory;
Andrew Walbran1f810b62022-08-10 13:33:57 +0000454
455 #[test]
456 fn verify_app() {
Andrew Walbranaa1efc42022-08-10 13:33:57 +0000457 // Check that the command parsing has been configured in a valid way.
458 Opt::command().debug_assert();
Andrew Walbran1f810b62022-08-10 13:33:57 +0000459 }
460}