blob: 6c08a19d1287c2f7b0f0abe6f696f1cc4bcd2e7a [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};
Andrew Walbranc4b1bde2022-02-03 15:26:02 +000031use std::path::{Path, PathBuf};
Andrew Walbranea9fa482021-03-04 16:11:12 +000032
Inseob Kima5a262f2021-11-17 19:41:03 +090033#[derive(Debug)]
34struct Idsigs(Vec<PathBuf>);
35
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070036#[derive(Parser)]
David Brazdil20412d92021-03-18 10:53:06 +000037enum Opt {
Jooyung Han21e9b922021-06-26 04:14:16 +090038 /// Run a virtual machine with a config in APK
39 RunApp {
40 /// Path to VM Payload APK
Jooyung Han21e9b922021-06-26 04:14:16 +090041 apk: PathBuf,
42
43 /// Path to idsig of the APK
Jooyung Han21e9b922021-06-26 04:14:16 +090044 idsig: PathBuf,
45
Jiyong Park48b354d2021-07-15 15:04:38 +090046 /// Path to the instance image. Created if not exists.
Jiyong Park48b354d2021-07-15 15:04:38 +090047 instance: PathBuf,
48
Jooyung Han21e9b922021-06-26 04:14:16 +090049 /// Path to VM config JSON within APK (e.g. assets/vm_config.json)
Inseob Kim7b5f65c2022-11-15 14:27:04 +090050 #[clap(long)]
Alan Stokes0d1ef782022-09-27 13:46:35 +010051 config_path: Option<String>,
Jooyung Han21e9b922021-06-26 04:14:16 +090052
Alan Stokes8f12f2b2023-01-09 09:19:20 +000053 /// Name of VM payload binary within APK (e.g. MicrodroidTestNativeLib.so)
Inseob Kim7b5f65c2022-11-15 14:27:04 +090054 #[clap(long)]
Alan Stokes8f12f2b2023-01-09 09:19:20 +000055 #[clap(alias = "payload_path")]
56 payload_binary_name: Option<String>,
Inseob Kim7b5f65c2022-11-15 14:27:04 +090057
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070058 /// Name of VM
59 #[clap(long)]
60 name: Option<String>,
61
Shikha Panwar22e70452022-10-10 18:32:55 +000062 /// Path to the file backing the storage.
63 /// Created if the option is used but the path does not exist in the device.
64 #[clap(long)]
65 storage: Option<PathBuf>,
66
67 /// Size of the storage. Used only if --storage is supplied but path does not exist
68 /// Default size is 10*1024*1024
69 #[clap(long)]
70 storage_size: Option<u64>,
71
Jiyong Parkb8182bb2021-10-26 22:53:08 +090072 /// Path to file for VM console output.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070073 #[clap(long)]
Jiyong Parkb8182bb2021-10-26 22:53:08 +090074 console: Option<PathBuf>,
75
Jooyung Han21e9b922021-06-26 04:14:16 +090076 /// Path to file for VM log output.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070077 #[clap(long)]
Jooyung Han21e9b922021-06-26 04:14:16 +090078 log: Option<PathBuf>,
Jiyong Park23601142021-07-05 13:15:32 +090079
Seungjae Yoofa22bb02022-12-08 16:38:42 +090080 /// Debug level of the VM. Supported values: "none" (default), and "full".
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070081 #[clap(long, default_value = "none", value_parser = parse_debug_level)]
Jiyong Parkc2a49cc2021-10-15 00:02:12 +090082 debug: DebugLevel,
Jiyong Parkd63cfff2021-09-27 20:10:17 +090083
Andrew Walbran3994f002022-01-27 17:33:45 +000084 /// Run VM in protected mode.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070085 #[clap(short, long)]
Andrew Walbran3994f002022-01-27 17:33:45 +000086 protected: bool,
87
Jiyong Parkd63cfff2021-09-27 20:10:17 +090088 /// Memory size (in MiB) of the VM. If unspecified, defaults to the value of `memory_mib`
89 /// in the VM config file.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070090 #[clap(short, long)]
Jiyong Parkd63cfff2021-09-27 20:10:17 +090091 mem: Option<u32>,
Inseob Kima5a262f2021-11-17 19:41:03 +090092
David Brazdil7d1e5ec2023-02-06 17:56:29 +000093 /// Run VM with vCPU topology matching that of the host. If unspecified, defaults to 1 vCPU.
94 #[clap(long, default_value = "one_cpu", value_parser = parse_cpu_topology)]
95 cpu_topology: CpuTopology,
Jiyong Park032615f2022-01-10 13:55:34 +090096
Jiyong Parkdfe16d62022-04-20 17:32:12 +090097 /// Comma separated list of task profile names to apply to the VM
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070098 #[clap(long)]
Jiyong Parkdfe16d62022-04-20 17:32:12 +090099 task_profiles: Vec<String>,
100
Inseob Kima5a262f2021-11-17 19:41:03 +0900101 /// Paths to extra idsig files.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700102 #[clap(long = "extra-idsig")]
Inseob Kima5a262f2021-11-17 19:41:03 +0900103 extra_idsigs: Vec<PathBuf>,
Jooyung Han21e9b922021-06-26 04:14:16 +0900104 },
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000105 /// Run a virtual machine with Microdroid inside
106 RunMicrodroid {
107 /// Path to the directory where VM-related files (e.g. instance.img, apk.idsig, etc.) will
108 /// be stored. If not specified a random directory under /data/local/tmp/microdroid will be
109 /// created and used.
110 #[clap(long)]
111 work_dir: Option<PathBuf>,
112
113 /// Name of VM
114 #[clap(long)]
115 name: Option<String>,
116
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000117 /// Path to the file backing the storage.
118 /// Created if the option is used but the path does not exist in the device.
119 #[clap(long)]
120 storage: Option<PathBuf>,
121
122 /// Size of the storage. Used only if --storage is supplied but path does not exist
123 /// Default size is 10*1024*1024
124 #[clap(long)]
125 storage_size: Option<u64>,
126
127 /// Path to file for VM console output.
128 #[clap(long)]
129 console: Option<PathBuf>,
130
131 /// Path to file for VM log output.
132 #[clap(long)]
133 log: Option<PathBuf>,
134
Seungjae Yoofa22bb02022-12-08 16:38:42 +0900135 /// Debug level of the VM. Supported values: "none" (default), and "full".
Nikita Ioffe0a15fcf2022-12-01 21:12:50 +0000136 #[clap(long, default_value = "full", value_parser = parse_debug_level)]
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000137 debug: DebugLevel,
138
139 /// Run VM in protected mode.
140 #[clap(short, long)]
141 protected: bool,
142
143 /// Memory size (in MiB) of the VM. If unspecified, defaults to the value of `memory_mib`
144 /// in the VM config file.
145 #[clap(short, long)]
146 mem: Option<u32>,
147
David Brazdil7d1e5ec2023-02-06 17:56:29 +0000148 /// Run VM with vCPU topology matching that of the host. If unspecified, defaults to 1 vCPU.
149 #[clap(long, default_value = "one_cpu", value_parser = parse_cpu_topology)]
150 cpu_topology: CpuTopology,
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000151
152 /// Comma separated list of task profile names to apply to the VM
153 #[clap(long)]
154 task_profiles: Vec<String>,
155 },
David Brazdil20412d92021-03-18 10:53:06 +0000156 /// Run a virtual machine
157 Run {
158 /// Path to VM config JSON
David Brazdil20412d92021-03-18 10:53:06 +0000159 config: PathBuf,
David Brazdil3c2ddef2021-03-18 13:09:57 +0000160
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700161 /// Name of VM
162 #[clap(long)]
163 name: Option<String>,
164
David Brazdil7d1e5ec2023-02-06 17:56:29 +0000165 /// Run VM with vCPU topology matching that of the host. If unspecified, defaults to 1 vCPU.
166 #[clap(long, default_value = "one_cpu", value_parser = parse_cpu_topology)]
167 cpu_topology: CpuTopology,
Jiyong Park032615f2022-01-10 13:55:34 +0900168
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900169 /// Comma separated list of task profile names to apply to the VM
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700170 #[clap(long)]
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900171 task_profiles: Vec<String>,
172
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900173 /// Path to file for VM console output.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700174 #[clap(long)]
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900175 console: Option<PathBuf>,
Jooyung Hanb7983a22022-02-22 05:21:27 +0900176
177 /// Path to file for VM log output.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700178 #[clap(long)]
Jooyung Hanb7983a22022-02-22 05:21:27 +0900179 log: Option<PathBuf>,
David Brazdil3c2ddef2021-03-18 13:09:57 +0000180 },
David Brazdil20412d92021-03-18 10:53:06 +0000181 /// List running virtual machines
182 List,
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000183 /// Print information about virtual machine support
184 Info,
Andrew Walbrandff3b942021-06-09 15:20:36 +0000185 /// Create a new empty partition to be used as a writable partition for a VM
186 CreatePartition {
187 /// Path at which to create the image file
Andrew Walbrandff3b942021-06-09 15:20:36 +0000188 path: PathBuf,
189
190 /// The desired size of the partition, in bytes.
191 size: u64,
Jiyong Park9dd389e2021-08-23 20:42:59 +0900192
193 /// Type of the partition
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700194 #[clap(short = 't', long = "type", default_value = "raw",
195 value_parser = parse_partition_type)]
Jiyong Park9dd389e2021-08-23 20:42:59 +0900196 partition_type: PartitionType,
Andrew Walbrandff3b942021-06-09 15:20:36 +0000197 },
Jooyung Hanc221c052022-02-22 05:20:15 +0900198 /// Creates or update the idsig file by digesting the input APK file.
199 CreateIdsig {
200 /// Path to VM Payload APK
Jooyung Hanc221c052022-02-22 05:20:15 +0900201 apk: PathBuf,
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700202
Jooyung Hanc221c052022-02-22 05:20:15 +0900203 /// Path to idsig of the APK
Jooyung Hanc221c052022-02-22 05:20:15 +0900204 path: PathBuf,
205 },
David Brazdil20412d92021-03-18 10:53:06 +0000206}
207
Jiyong Parkc2a49cc2021-10-15 00:02:12 +0900208fn parse_debug_level(s: &str) -> Result<DebugLevel, String> {
209 match s {
210 "none" => Ok(DebugLevel::NONE),
Jiyong Parkc2a49cc2021-10-15 00:02:12 +0900211 "full" => Ok(DebugLevel::FULL),
212 _ => Err(format!("Invalid debug level {}", s)),
213 }
214}
215
Jiyong Park9dd389e2021-08-23 20:42:59 +0900216fn parse_partition_type(s: &str) -> Result<PartitionType, String> {
217 match s {
218 "raw" => Ok(PartitionType::RAW),
219 "instance" => Ok(PartitionType::ANDROID_VM_INSTANCE),
220 _ => Err(format!("Invalid partition type {}", s)),
221 }
222}
223
David Brazdil7d1e5ec2023-02-06 17:56:29 +0000224fn parse_cpu_topology(s: &str) -> Result<CpuTopology, String> {
225 match s {
226 "one_cpu" => Ok(CpuTopology::ONE_CPU),
227 "match_host" => Ok(CpuTopology::MATCH_HOST),
228 _ => Err(format!("Invalid cpu topology {}", s)),
229 }
230}
231
Alan Stokesc4d5def2023-02-14 17:01:59 +0000232fn get_service() -> Result<Strong<dyn IVirtualizationService>, Error> {
233 let virtmgr =
234 vmclient::VirtualizationService::new().context("Failed to spawn VirtualizationService")?;
235 virtmgr.connect().context("Failed to connect to VirtualizationService")
236}
237
Andrew Walbranea9fa482021-03-04 16:11:12 +0000238fn main() -> Result<(), Error> {
239 env_logger::init();
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700240 let opt = Opt::parse();
Andrew Walbranea9fa482021-03-04 16:11:12 +0000241
242 // We need to start the thread pool for Binder to work properly, especially link_to_death.
243 ProcessState::start_thread_pool();
244
David Brazdil20412d92021-03-18 10:53:06 +0000245 match opt {
Inseob Kima5a262f2021-11-17 19:41:03 +0900246 Opt::RunApp {
Seungjae Yoo62085c02022-08-12 04:44:52 +0000247 name,
Inseob Kima5a262f2021-11-17 19:41:03 +0900248 apk,
249 idsig,
250 instance,
Shikha Panwar22e70452022-10-10 18:32:55 +0000251 storage,
252 storage_size,
Inseob Kima5a262f2021-11-17 19:41:03 +0900253 config_path,
Alan Stokes8f12f2b2023-01-09 09:19:20 +0000254 payload_binary_name,
Inseob Kima5a262f2021-11-17 19:41:03 +0900255 console,
256 log,
257 debug,
Andrew Walbran3994f002022-01-27 17:33:45 +0000258 protected,
Inseob Kima5a262f2021-11-17 19:41:03 +0900259 mem,
David Brazdil7d1e5ec2023-02-06 17:56:29 +0000260 cpu_topology,
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900261 task_profiles,
Inseob Kima5a262f2021-11-17 19:41:03 +0900262 extra_idsigs,
263 } => command_run_app(
Seungjae Yoo62085c02022-08-12 04:44:52 +0000264 name,
Alan Stokesc4d5def2023-02-14 17:01:59 +0000265 get_service()?.as_ref(),
Inseob Kima5a262f2021-11-17 19:41:03 +0900266 &apk,
267 &idsig,
268 &instance,
Shikha Panwar22e70452022-10-10 18:32:55 +0000269 storage.as_deref(),
270 storage_size,
Inseob Kim7b5f65c2022-11-15 14:27:04 +0900271 config_path,
Alan Stokes8f12f2b2023-01-09 09:19:20 +0000272 payload_binary_name,
Inseob Kima5a262f2021-11-17 19:41:03 +0900273 console.as_deref(),
274 log.as_deref(),
275 debug,
Andrew Walbran3994f002022-01-27 17:33:45 +0000276 protected,
Inseob Kima5a262f2021-11-17 19:41:03 +0900277 mem,
David Brazdil7d1e5ec2023-02-06 17:56:29 +0000278 cpu_topology,
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900279 task_profiles,
Inseob Kima5a262f2021-11-17 19:41:03 +0900280 &extra_idsigs,
281 ),
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000282 Opt::RunMicrodroid {
283 name,
284 work_dir,
285 storage,
286 storage_size,
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000287 console,
288 log,
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000289 debug,
290 protected,
291 mem,
David Brazdil7d1e5ec2023-02-06 17:56:29 +0000292 cpu_topology,
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000293 task_profiles,
294 } => command_run_microdroid(
295 name,
Alan Stokesc4d5def2023-02-14 17:01:59 +0000296 get_service()?.as_ref(),
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000297 work_dir,
298 storage.as_deref(),
299 storage_size,
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000300 console.as_deref(),
301 log.as_deref(),
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000302 debug,
303 protected,
304 mem,
David Brazdil7d1e5ec2023-02-06 17:56:29 +0000305 cpu_topology,
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000306 task_profiles,
307 ),
David Brazdil7d1e5ec2023-02-06 17:56:29 +0000308 Opt::Run { name, config, cpu_topology, task_profiles, console, log } => {
Jiyong Park032615f2022-01-10 13:55:34 +0900309 command_run(
Seungjae Yoo62085c02022-08-12 04:44:52 +0000310 name,
Alan Stokesc4d5def2023-02-14 17:01:59 +0000311 get_service()?.as_ref(),
Jiyong Park032615f2022-01-10 13:55:34 +0900312 &config,
Jiyong Park032615f2022-01-10 13:55:34 +0900313 console.as_deref(),
Jooyung Hanb7983a22022-02-22 05:21:27 +0900314 log.as_deref(),
Jiyong Park032615f2022-01-10 13:55:34 +0900315 /* mem */ None,
David Brazdil7d1e5ec2023-02-06 17:56:29 +0000316 cpu_topology,
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900317 task_profiles,
Jiyong Park032615f2022-01-10 13:55:34 +0900318 )
Andrew Walbranbe429242021-06-28 12:22:54 +0000319 }
Alan Stokesc4d5def2023-02-14 17:01:59 +0000320 Opt::List => command_list(get_service()?.as_ref()),
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000321 Opt::Info => command_info(),
Jiyong Park9dd389e2021-08-23 20:42:59 +0900322 Opt::CreatePartition { path, size, partition_type } => {
Alan Stokesc4d5def2023-02-14 17:01:59 +0000323 command_create_partition(get_service()?.as_ref(), &path, size, partition_type)
Jiyong Park9dd389e2021-08-23 20:42:59 +0900324 }
Alan Stokesc4d5def2023-02-14 17:01:59 +0000325 Opt::CreateIdsig { apk, path } => {
326 command_create_idsig(get_service()?.as_ref(), &apk, &path)
327 }
Andrew Walbranea9fa482021-03-04 16:11:12 +0000328 }
329}
330
Andrew Walbran320b5602021-03-04 16:11:12 +0000331/// List the VMs currently running.
Andrew Walbran616d13f2022-05-12 18:35:55 +0000332fn command_list(service: &dyn IVirtualizationService) -> Result<(), Error> {
Andrew Walbran17de24f2021-05-27 13:27:30 +0000333 let vms = service.debugListVms().context("Failed to get list of VMs")?;
Andrew Walbran320b5602021-03-04 16:11:12 +0000334 println!("Running VMs: {:#?}", vms);
335 Ok(())
336}
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000337
338/// Print information about supported VM types.
339fn command_info() -> Result<(), Error> {
Alan Stokesc4d5def2023-02-14 17:01:59 +0000340 let non_protected_vm_supported = hypervisor_props::is_vm_supported()?;
341 let protected_vm_supported = hypervisor_props::is_protected_vm_supported()?;
Alan Stokes8d39a9b2023-01-10 15:01:00 +0000342 match (non_protected_vm_supported, protected_vm_supported) {
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000343 (false, false) => println!("VMs are not supported."),
344 (false, true) => println!("Only protected VMs are supported."),
Alan Stokes8d39a9b2023-01-10 15:01:00 +0000345 (true, false) => println!("Only non-protected VMs are supported."),
346 (true, true) => println!("Both protected and non-protected VMs are supported."),
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000347 }
348
Alan Stokesc4d5def2023-02-14 17:01:59 +0000349 if let Some(version) = hypervisor_props::version()? {
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000350 println!("Hypervisor version: {}", version);
351 } else {
352 println!("Hypervisor version not set.");
353 }
354
355 if Path::new("/dev/kvm").exists() {
356 println!("/dev/kvm exists.");
357 } else {
358 println!("/dev/kvm does not exist.");
359 }
360
361 Ok(())
362}
Andrew Walbran1f810b62022-08-10 13:33:57 +0000363
364#[cfg(test)]
365mod tests {
366 use super::*;
Andrew Walbranaa1efc42022-08-10 13:33:57 +0000367 use clap::CommandFactory;
Andrew Walbran1f810b62022-08-10 13:33:57 +0000368
369 #[test]
370 fn verify_app() {
Andrew Walbranaa1efc42022-08-10 13:33:57 +0000371 // Check that the command parsing has been configured in a valid way.
372 Opt::command().debug_assert();
Andrew Walbran1f810b62022-08-10 13:33:57 +0000373 }
374}