blob: bc05ec301dced5588ecbc2ecda976edb182f488b [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;
David Brazdil20412d92021-03-18 10:53:06 +000027use anyhow::{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};
Nikita Ioffe5776f082023-02-10 21:38:26 +000033use std::num::NonZeroU16;
Andrew Walbranc4b1bde2022-02-03 15:26:02 +000034use std::path::{Path, PathBuf};
Andrew Walbranea9fa482021-03-04 16:11:12 +000035
Alan Stokesfda70842023-12-20 17:50:14 +000036#[derive(Args, Default)]
Jiyong Parkb1935ef2023-08-10 17:22:39 +090037/// Collection of flags that are at VM level and therefore applicable to all subcommands
38pub struct CommonConfig {
39 /// Name of VM
40 #[arg(long)]
41 name: Option<String>,
42
43 /// Run VM with vCPU topology matching that of the host. If unspecified, defaults to 1 vCPU.
44 #[arg(long, default_value = "one_cpu", value_parser = parse_cpu_topology)]
45 cpu_topology: CpuTopology,
46
Jiyong Parkb1935ef2023-08-10 17:22:39 +090047 /// Memory size (in MiB) of the VM. If unspecified, defaults to the value of `memory_mib`
48 /// in the VM config file.
49 #[arg(short, long)]
50 mem: Option<u32>,
51
52 /// Run VM in protected mode.
53 #[arg(short, long)]
54 protected: bool,
55}
56
Alan Stokesfda70842023-12-20 17:50:14 +000057#[derive(Args, Default)]
Jiyong Parkb1935ef2023-08-10 17:22:39 +090058/// Collection of flags for debugging
59pub struct DebugConfig {
60 /// Debug level of the VM. Supported values: "full" (default), and "none".
61 #[arg(long, default_value = "full", value_parser = parse_debug_level)]
62 debug: DebugLevel,
63
64 /// Path to file for VM console output.
65 #[arg(long)]
66 console: Option<PathBuf>,
67
68 /// Path to file for VM console input.
69 #[arg(long)]
70 console_in: Option<PathBuf>,
71
72 /// Path to file for VM log output.
73 #[arg(long)]
74 log: Option<PathBuf>,
75
76 /// Port at which crosvm will start a gdb server to debug guest kernel.
77 /// Note: this is only supported on Android kernels android14-5.15 and higher.
78 #[arg(long)]
79 gdb: Option<NonZeroU16>,
80}
81
Alan Stokesfda70842023-12-20 17:50:14 +000082#[derive(Args, Default)]
Jiyong Parkb1935ef2023-08-10 17:22:39 +090083/// Collection of flags that are Microdroid specific
84pub struct MicrodroidConfig {
85 /// Path to the file backing the storage.
86 /// Created if the option is used but the path does not exist in the device.
87 #[arg(long)]
88 storage: Option<PathBuf>,
89
90 /// Size of the storage. Used only if --storage is supplied but path does not exist
91 /// Default size is 10*1024*1024
92 #[arg(long)]
93 storage_size: Option<u64>,
94
Jiyong Parkb1935ef2023-08-10 17:22:39 +090095 /// Path to disk image containing vendor-specific modules.
Nikita Ioffe631717e2023-09-05 13:38:07 +010096 #[cfg(vendor_modules)]
Jiyong Parkb1935ef2023-08-10 17:22:39 +090097 #[arg(long)]
98 vendor: Option<PathBuf>,
99
100 /// SysFS nodes of devices to assign to VM
Nikita Ioffe94a8a182023-11-16 16:37:48 +0000101 #[cfg(device_assignment)]
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900102 #[arg(long)]
103 devices: Vec<PathBuf>,
Inseob Kim172f9eb2023-11-06 17:02:08 +0900104
Inseob Kim7a1fc8f2023-11-22 18:45:28 +0900105 /// Version of GKI to use. If set, use instead of microdroid kernel
Inseob Kim172f9eb2023-11-06 17:02:08 +0900106 #[cfg(vendor_modules)]
107 #[arg(long)]
Inseob Kim7a1fc8f2023-11-22 18:45:28 +0900108 gki: Option<String>,
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900109}
110
Nikita Ioffe631717e2023-09-05 13:38:07 +0100111impl MicrodroidConfig {
112 #[cfg(vendor_modules)]
Nikita Ioffe631717e2023-09-05 13:38:07 +0100113 fn vendor(&self) -> &Option<PathBuf> {
114 &self.vendor
115 }
116
117 #[cfg(not(vendor_modules))]
Nikita Ioffe631717e2023-09-05 13:38:07 +0100118 fn vendor(&self) -> Option<PathBuf> {
119 None
120 }
Nikita Ioffe94a8a182023-11-16 16:37:48 +0000121
Inseob Kim172f9eb2023-11-06 17:02:08 +0900122 #[cfg(vendor_modules)]
Inseob Kim7a1fc8f2023-11-22 18:45:28 +0900123 fn gki(&self) -> Option<&str> {
124 self.gki.as_deref()
Inseob Kim172f9eb2023-11-06 17:02:08 +0900125 }
126
127 #[cfg(not(vendor_modules))]
Inseob Kim7a1fc8f2023-11-22 18:45:28 +0900128 fn gki(&self) -> Option<&str> {
129 None
Inseob Kim172f9eb2023-11-06 17:02:08 +0900130 }
131
Nikita Ioffe94a8a182023-11-16 16:37:48 +0000132 #[cfg(device_assignment)]
133 fn devices(&self) -> &Vec<PathBuf> {
134 &self.devices
135 }
136
137 #[cfg(not(device_assignment))]
138 fn devices(&self) -> Vec<PathBuf> {
139 Vec::new()
140 }
Nikita Ioffe631717e2023-09-05 13:38:07 +0100141}
142
Alan Stokesfda70842023-12-20 17:50:14 +0000143#[derive(Args, Default)]
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900144/// Flags for the run_app subcommand
145pub struct RunAppConfig {
146 #[command(flatten)]
147 common: CommonConfig,
148
149 #[command(flatten)]
150 debug: DebugConfig,
151
152 #[command(flatten)]
153 microdroid: MicrodroidConfig,
154
155 /// Path to VM Payload APK
156 apk: PathBuf,
157
158 /// Path to idsig of the APK
159 idsig: PathBuf,
160
161 /// Path to the instance image. Created if not exists.
162 instance: PathBuf,
163
Shikha Panwar61a74b52024-02-16 13:17:01 +0000164 /// Path to file containing instance_id. Required iff llpvm feature is enabled.
165 #[cfg(llpvm_changes)]
166 #[arg(long = "instance-id-file")]
167 instance_id: PathBuf,
168
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900169 /// Path to VM config JSON within APK (e.g. assets/vm_config.json)
170 #[arg(long)]
171 config_path: Option<String>,
172
173 /// Name of VM payload binary within APK (e.g. MicrodroidTestNativeLib.so)
174 #[arg(long)]
175 #[arg(alias = "payload_path")]
176 payload_binary_name: Option<String>,
177
Alan Stokesfda70842023-12-20 17:50:14 +0000178 /// Paths to extra apk files.
179 #[cfg(multi_tenant)]
180 #[arg(long = "extra-apk")]
181 #[clap(conflicts_with = "config_path")]
182 extra_apks: Vec<PathBuf>,
183
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900184 /// Paths to extra idsig files.
185 #[arg(long = "extra-idsig")]
186 extra_idsigs: Vec<PathBuf>,
187}
188
Alan Stokesfda70842023-12-20 17:50:14 +0000189impl RunAppConfig {
190 #[cfg(multi_tenant)]
191 fn extra_apks(&self) -> &[PathBuf] {
192 &self.extra_apks
193 }
194
195 #[cfg(not(multi_tenant))]
196 fn extra_apks(&self) -> &[PathBuf] {
197 &[]
198 }
Shikha Panwar61a74b52024-02-16 13:17:01 +0000199
200 #[cfg(llpvm_changes)]
201 fn instance_id(&self) -> Result<PathBuf, Error> {
202 Ok(self.instance_id.clone())
203 }
204
205 #[cfg(not(llpvm_changes))]
206 fn instance_id(&self) -> Result<PathBuf, Error> {
207 Err(anyhow!("LLPVM feature is disabled, --instance_id flag not supported"))
208 }
209
210 #[cfg(llpvm_changes)]
211 fn set_instance_id(&mut self, instance_id_file: PathBuf) -> Result<(), Error> {
212 self.instance_id = instance_id_file;
213 Ok(())
214 }
215
216 #[cfg(not(llpvm_changes))]
217 fn set_instance_id(&mut self, _: PathBuf) -> Result<(), Error> {
218 Err(anyhow!("LLPVM feature is disabled, --instance_id flag not supported"))
219 }
Alan Stokesfda70842023-12-20 17:50:14 +0000220}
221
222#[derive(Args, Default)]
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900223/// Flags for the run_microdroid subcommand
224pub struct RunMicrodroidConfig {
225 #[command(flatten)]
226 common: CommonConfig,
227
228 #[command(flatten)]
229 debug: DebugConfig,
230
231 #[command(flatten)]
232 microdroid: MicrodroidConfig,
233
234 /// Path to the directory where VM-related files (e.g. instance.img, apk.idsig, etc.) will
235 /// be stored. If not specified a random directory under /data/local/tmp/microdroid will be
236 /// created and used.
237 #[arg(long)]
238 work_dir: Option<PathBuf>,
239}
240
Alan Stokesfda70842023-12-20 17:50:14 +0000241#[derive(Args, Default)]
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900242/// Flags for the run subcommand
243pub struct RunCustomVmConfig {
244 #[command(flatten)]
245 common: CommonConfig,
246
247 #[command(flatten)]
248 debug: DebugConfig,
249
250 /// Path to VM config JSON
251 config: PathBuf,
252}
253
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700254#[derive(Parser)]
David Brazdil20412d92021-03-18 10:53:06 +0000255enum Opt {
Shikha Panwar6d306412024-02-17 21:37:49 +0000256 /// Check if the feature is enabled on device.
257 CheckFeatureEnabled { feature: String },
Jooyung Han21e9b922021-06-26 04:14:16 +0900258 /// Run a virtual machine with a config in APK
259 RunApp {
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900260 #[command(flatten)]
261 config: RunAppConfig,
Jooyung Han21e9b922021-06-26 04:14:16 +0900262 },
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000263 /// Run a virtual machine with Microdroid inside
264 RunMicrodroid {
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900265 #[command(flatten)]
266 config: RunMicrodroidConfig,
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000267 },
David Brazdil20412d92021-03-18 10:53:06 +0000268 /// Run a virtual machine
269 Run {
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900270 #[command(flatten)]
271 config: RunCustomVmConfig,
David Brazdil3c2ddef2021-03-18 13:09:57 +0000272 },
David Brazdil20412d92021-03-18 10:53:06 +0000273 /// List running virtual machines
274 List,
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000275 /// Print information about virtual machine support
276 Info,
Andrew Walbrandff3b942021-06-09 15:20:36 +0000277 /// Create a new empty partition to be used as a writable partition for a VM
278 CreatePartition {
279 /// Path at which to create the image file
Andrew Walbrandff3b942021-06-09 15:20:36 +0000280 path: PathBuf,
281
282 /// The desired size of the partition, in bytes.
283 size: u64,
Jiyong Park9dd389e2021-08-23 20:42:59 +0900284
285 /// Type of the partition
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900286 #[arg(short = 't', long = "type", default_value = "raw",
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700287 value_parser = parse_partition_type)]
Jiyong Park9dd389e2021-08-23 20:42:59 +0900288 partition_type: PartitionType,
Andrew Walbrandff3b942021-06-09 15:20:36 +0000289 },
Jooyung Hanc221c052022-02-22 05:20:15 +0900290 /// Creates or update the idsig file by digesting the input APK file.
291 CreateIdsig {
292 /// Path to VM Payload APK
Jooyung Hanc221c052022-02-22 05:20:15 +0900293 apk: PathBuf,
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700294
Jooyung Hanc221c052022-02-22 05:20:15 +0900295 /// Path to idsig of the APK
Jooyung Hanc221c052022-02-22 05:20:15 +0900296 path: PathBuf,
297 },
David Brazdil20412d92021-03-18 10:53:06 +0000298}
299
Jiyong Parkc2a49cc2021-10-15 00:02:12 +0900300fn parse_debug_level(s: &str) -> Result<DebugLevel, String> {
301 match s {
302 "none" => Ok(DebugLevel::NONE),
Jiyong Parkc2a49cc2021-10-15 00:02:12 +0900303 "full" => Ok(DebugLevel::FULL),
304 _ => Err(format!("Invalid debug level {}", s)),
305 }
306}
307
Jiyong Park9dd389e2021-08-23 20:42:59 +0900308fn parse_partition_type(s: &str) -> Result<PartitionType, String> {
309 match s {
310 "raw" => Ok(PartitionType::RAW),
311 "instance" => Ok(PartitionType::ANDROID_VM_INSTANCE),
312 _ => Err(format!("Invalid partition type {}", s)),
313 }
314}
315
David Brazdil7d1e5ec2023-02-06 17:56:29 +0000316fn parse_cpu_topology(s: &str) -> Result<CpuTopology, String> {
317 match s {
318 "one_cpu" => Ok(CpuTopology::ONE_CPU),
319 "match_host" => Ok(CpuTopology::MATCH_HOST),
320 _ => Err(format!("Invalid cpu topology {}", s)),
321 }
322}
323
Alan Stokesc4d5def2023-02-14 17:01:59 +0000324fn get_service() -> Result<Strong<dyn IVirtualizationService>, Error> {
325 let virtmgr =
326 vmclient::VirtualizationService::new().context("Failed to spawn VirtualizationService")?;
327 virtmgr.connect().context("Failed to connect to VirtualizationService")
328}
329
Shikha Panwar6d306412024-02-17 21:37:49 +0000330fn command_check_feature_enabled(feature: &str) {
331 println!(
332 "Feature {feature} is {}",
333 if avf_features::is_feature_enabled(feature) { "enabled" } else { "disabled" }
334 );
335}
336
Andrew Walbranea9fa482021-03-04 16:11:12 +0000337fn main() -> Result<(), Error> {
338 env_logger::init();
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700339 let opt = Opt::parse();
Andrew Walbranea9fa482021-03-04 16:11:12 +0000340
341 // We need to start the thread pool for Binder to work properly, especially link_to_death.
342 ProcessState::start_thread_pool();
343
David Brazdil20412d92021-03-18 10:53:06 +0000344 match opt {
Shikha Panwar6d306412024-02-17 21:37:49 +0000345 Opt::CheckFeatureEnabled { feature } => {
346 command_check_feature_enabled(&feature);
347 Ok(())
348 }
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900349 Opt::RunApp { config } => command_run_app(config),
350 Opt::RunMicrodroid { config } => command_run_microdroid(config),
351 Opt::Run { config } => command_run(config),
Alan Stokesc4d5def2023-02-14 17:01:59 +0000352 Opt::List => command_list(get_service()?.as_ref()),
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000353 Opt::Info => command_info(),
Jiyong Park9dd389e2021-08-23 20:42:59 +0900354 Opt::CreatePartition { path, size, partition_type } => {
Alan Stokesc4d5def2023-02-14 17:01:59 +0000355 command_create_partition(get_service()?.as_ref(), &path, size, partition_type)
Jiyong Park9dd389e2021-08-23 20:42:59 +0900356 }
Alan Stokesc4d5def2023-02-14 17:01:59 +0000357 Opt::CreateIdsig { apk, path } => {
358 command_create_idsig(get_service()?.as_ref(), &apk, &path)
359 }
Andrew Walbranea9fa482021-03-04 16:11:12 +0000360 }
361}
362
Andrew Walbran320b5602021-03-04 16:11:12 +0000363/// List the VMs currently running.
Andrew Walbran616d13f2022-05-12 18:35:55 +0000364fn command_list(service: &dyn IVirtualizationService) -> Result<(), Error> {
Andrew Walbran17de24f2021-05-27 13:27:30 +0000365 let vms = service.debugListVms().context("Failed to get list of VMs")?;
Andrew Walbran320b5602021-03-04 16:11:12 +0000366 println!("Running VMs: {:#?}", vms);
367 Ok(())
368}
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000369
370/// Print information about supported VM types.
371fn command_info() -> Result<(), Error> {
Alan Stokesc4d5def2023-02-14 17:01:59 +0000372 let non_protected_vm_supported = hypervisor_props::is_vm_supported()?;
373 let protected_vm_supported = hypervisor_props::is_protected_vm_supported()?;
Alan Stokes8d39a9b2023-01-10 15:01:00 +0000374 match (non_protected_vm_supported, protected_vm_supported) {
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000375 (false, false) => println!("VMs are not supported."),
376 (false, true) => println!("Only protected VMs are supported."),
Alan Stokes8d39a9b2023-01-10 15:01:00 +0000377 (true, false) => println!("Only non-protected VMs are supported."),
378 (true, true) => println!("Both protected and non-protected VMs are supported."),
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000379 }
380
Alan Stokesc4d5def2023-02-14 17:01:59 +0000381 if let Some(version) = hypervisor_props::version()? {
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000382 println!("Hypervisor version: {}", version);
383 } else {
384 println!("Hypervisor version not set.");
385 }
386
387 if Path::new("/dev/kvm").exists() {
388 println!("/dev/kvm exists.");
389 } else {
390 println!("/dev/kvm does not exist.");
391 }
392
Inseob Kim6ef80972023-07-20 17:23:36 +0900393 if Path::new("/dev/vfio/vfio").exists() {
394 println!("/dev/vfio/vfio exists.");
395 } else {
396 println!("/dev/vfio/vfio does not exist.");
397 }
398
399 if Path::new("/sys/bus/platform/drivers/vfio-platform").exists() {
400 println!("VFIO-platform is supported.");
401 } else {
402 println!("VFIO-platform is not supported.");
403 }
404
Inseob Kim75460b32023-08-09 13:41:31 +0900405 let devices = get_service()?.getAssignableDevices()?;
406 let devices = devices.into_iter().map(|x| x.node).collect::<Vec<_>>();
407 println!("Assignable devices: {}", serde_json::to_string(&devices)?);
408
Inseob Kim46257382024-01-03 15:41:22 +0900409 let os_list = get_service()?.getSupportedOSList()?;
410 println!("Available OS list: {}", serde_json::to_string(&os_list)?);
Inseob Kim7a1fc8f2023-11-22 18:45:28 +0900411
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000412 Ok(())
413}
Andrew Walbran1f810b62022-08-10 13:33:57 +0000414
415#[cfg(test)]
416mod tests {
417 use super::*;
Andrew Walbranaa1efc42022-08-10 13:33:57 +0000418 use clap::CommandFactory;
Andrew Walbran1f810b62022-08-10 13:33:57 +0000419
420 #[test]
421 fn verify_app() {
Andrew Walbranaa1efc42022-08-10 13:33:57 +0000422 // Check that the command parsing has been configured in a valid way.
423 Opt::command().debug_assert();
Andrew Walbran1f810b62022-08-10 13:33:57 +0000424 }
425}