blob: f70ce54a0e419ec4176e8637b3d0506484d41446 [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::{
22 IVirtualizationService::IVirtualizationService, PartitionType::PartitionType,
23 VirtualMachineAppConfig::DebugLevel::DebugLevel,
24};
David Brazdil20412d92021-03-18 10:53:06 +000025use anyhow::{Context, Error};
Alan Stokes0e82b502022-08-08 14:44:48 +010026use binder::ProcessState;
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 rustutils::system_properties;
32use 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
Jooyung Han21e9b922021-06-26 04:14:16 +090063 /// Detach VM from the terminal and run in the background
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070064 #[clap(short, long)]
Jooyung Han21e9b922021-06-26 04:14:16 +090065 daemonize: bool,
66
Shikha Panwar22e70452022-10-10 18:32:55 +000067 /// Path to the file backing the storage.
68 /// Created if the option is used but the path does not exist in the device.
69 #[clap(long)]
70 storage: Option<PathBuf>,
71
72 /// Size of the storage. Used only if --storage is supplied but path does not exist
73 /// Default size is 10*1024*1024
74 #[clap(long)]
75 storage_size: Option<u64>,
76
Jiyong Parkb8182bb2021-10-26 22:53:08 +090077 /// Path to file for VM console output.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070078 #[clap(long)]
Jiyong Parkb8182bb2021-10-26 22:53:08 +090079 console: 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
Seungjae Yoofa22bb02022-12-08 16:38:42 +090085 /// Debug level of the VM. Supported values: "none" (default), and "full".
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070086 #[clap(long, default_value = "none", 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
Jiyong Park032615f2022-01-10 13:55:34 +090098 /// Number of vCPUs in the VM. If unspecified, defaults to 1.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070099 #[clap(long)]
Jiyong Park032615f2022-01-10 13:55:34 +0900100 cpus: Option<u32>,
101
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>,
Jooyung Han21e9b922021-06-26 04:14:16 +0900109 },
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000110 /// Run a virtual machine with Microdroid inside
111 RunMicrodroid {
112 /// Path to the directory where VM-related files (e.g. instance.img, apk.idsig, etc.) will
113 /// be stored. If not specified a random directory under /data/local/tmp/microdroid will be
114 /// created and used.
115 #[clap(long)]
116 work_dir: Option<PathBuf>,
117
118 /// Name of VM
119 #[clap(long)]
120 name: Option<String>,
121
122 /// Detach VM from the terminal and run in the background
123 #[clap(short, long)]
124 daemonize: bool,
125
126 /// Path to the file backing the storage.
127 /// Created if the option is used but the path does not exist in the device.
128 #[clap(long)]
129 storage: Option<PathBuf>,
130
131 /// Size of the storage. Used only if --storage is supplied but path does not exist
132 /// Default size is 10*1024*1024
133 #[clap(long)]
134 storage_size: Option<u64>,
135
136 /// Path to file for VM console output.
137 #[clap(long)]
138 console: Option<PathBuf>,
139
140 /// Path to file for VM log output.
141 #[clap(long)]
142 log: Option<PathBuf>,
143
Seungjae Yoofa22bb02022-12-08 16:38:42 +0900144 /// Debug level of the VM. Supported values: "none" (default), and "full".
Nikita Ioffe0a15fcf2022-12-01 21:12:50 +0000145 #[clap(long, default_value = "full", value_parser = parse_debug_level)]
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000146 debug: DebugLevel,
147
148 /// Run VM in protected mode.
149 #[clap(short, long)]
150 protected: bool,
151
152 /// Memory size (in MiB) of the VM. If unspecified, defaults to the value of `memory_mib`
153 /// in the VM config file.
154 #[clap(short, long)]
155 mem: Option<u32>,
156
157 /// Number of vCPUs in the VM. If unspecified, defaults to 1.
158 #[clap(long)]
159 cpus: Option<u32>,
160
161 /// Comma separated list of task profile names to apply to the VM
162 #[clap(long)]
163 task_profiles: Vec<String>,
164 },
David Brazdil20412d92021-03-18 10:53:06 +0000165 /// Run a virtual machine
166 Run {
167 /// Path to VM config JSON
David Brazdil20412d92021-03-18 10:53:06 +0000168 config: PathBuf,
David Brazdil3c2ddef2021-03-18 13:09:57 +0000169
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700170 /// Name of VM
171 #[clap(long)]
172 name: Option<String>,
173
David Brazdil3c2ddef2021-03-18 13:09:57 +0000174 /// Detach VM from the terminal and run in the background
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700175 #[clap(short, long)]
David Brazdil3c2ddef2021-03-18 13:09:57 +0000176 daemonize: bool,
Andrew Walbranbe429242021-06-28 12:22:54 +0000177
Jiyong Park032615f2022-01-10 13:55:34 +0900178 /// Number of vCPUs in the VM. If unspecified, defaults to 1.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700179 #[clap(long)]
Jiyong Park032615f2022-01-10 13:55:34 +0900180 cpus: Option<u32>,
181
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900182 /// Comma separated list of task profile names to apply to the VM
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700183 #[clap(long)]
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900184 task_profiles: Vec<String>,
185
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900186 /// Path to file for VM console output.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700187 #[clap(long)]
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900188 console: Option<PathBuf>,
Jooyung Hanb7983a22022-02-22 05:21:27 +0900189
190 /// Path to file for VM log output.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700191 #[clap(long)]
Jooyung Hanb7983a22022-02-22 05:21:27 +0900192 log: Option<PathBuf>,
David Brazdil3c2ddef2021-03-18 13:09:57 +0000193 },
194 /// Stop a virtual machine running in the background
195 Stop {
196 /// CID of the virtual machine
197 cid: u32,
David Brazdil20412d92021-03-18 10:53:06 +0000198 },
199 /// List running virtual machines
200 List,
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000201 /// Print information about virtual machine support
202 Info,
Andrew Walbrandff3b942021-06-09 15:20:36 +0000203 /// Create a new empty partition to be used as a writable partition for a VM
204 CreatePartition {
205 /// Path at which to create the image file
Andrew Walbrandff3b942021-06-09 15:20:36 +0000206 path: PathBuf,
207
208 /// The desired size of the partition, in bytes.
209 size: u64,
Jiyong Park9dd389e2021-08-23 20:42:59 +0900210
211 /// Type of the partition
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700212 #[clap(short = 't', long = "type", default_value = "raw",
213 value_parser = parse_partition_type)]
Jiyong Park9dd389e2021-08-23 20:42:59 +0900214 partition_type: PartitionType,
Andrew Walbrandff3b942021-06-09 15:20:36 +0000215 },
Jooyung Hanc221c052022-02-22 05:20:15 +0900216 /// Creates or update the idsig file by digesting the input APK file.
217 CreateIdsig {
218 /// Path to VM Payload APK
Jooyung Hanc221c052022-02-22 05:20:15 +0900219 apk: PathBuf,
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700220
Jooyung Hanc221c052022-02-22 05:20:15 +0900221 /// Path to idsig of the APK
Jooyung Hanc221c052022-02-22 05:20:15 +0900222 path: PathBuf,
223 },
David Brazdil20412d92021-03-18 10:53:06 +0000224}
225
Jiyong Parkc2a49cc2021-10-15 00:02:12 +0900226fn parse_debug_level(s: &str) -> Result<DebugLevel, String> {
227 match s {
228 "none" => Ok(DebugLevel::NONE),
Jiyong Parkc2a49cc2021-10-15 00:02:12 +0900229 "full" => Ok(DebugLevel::FULL),
230 _ => Err(format!("Invalid debug level {}", s)),
231 }
232}
233
Jiyong Park9dd389e2021-08-23 20:42:59 +0900234fn parse_partition_type(s: &str) -> Result<PartitionType, String> {
235 match s {
236 "raw" => Ok(PartitionType::RAW),
237 "instance" => Ok(PartitionType::ANDROID_VM_INSTANCE),
238 _ => Err(format!("Invalid partition type {}", s)),
239 }
240}
241
Andrew Walbranea9fa482021-03-04 16:11:12 +0000242fn main() -> Result<(), Error> {
243 env_logger::init();
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700244 let opt = Opt::parse();
Andrew Walbranea9fa482021-03-04 16:11:12 +0000245
246 // We need to start the thread pool for Binder to work properly, especially link_to_death.
247 ProcessState::start_thread_pool();
248
David Brazdil4b4c5102022-12-19 22:56:20 +0000249 let virtmgr =
250 vmclient::VirtualizationService::new().context("Failed to spawn VirtualizationService")?;
251 let service = virtmgr.connect().context("Failed to connect to VirtualizationService")?;
Andrew Walbran320b5602021-03-04 16:11:12 +0000252
David Brazdil20412d92021-03-18 10:53:06 +0000253 match opt {
Inseob Kima5a262f2021-11-17 19:41:03 +0900254 Opt::RunApp {
Seungjae Yoo62085c02022-08-12 04:44:52 +0000255 name,
Inseob Kima5a262f2021-11-17 19:41:03 +0900256 apk,
257 idsig,
258 instance,
Shikha Panwar22e70452022-10-10 18:32:55 +0000259 storage,
260 storage_size,
Inseob Kima5a262f2021-11-17 19:41:03 +0900261 config_path,
Alan Stokes8f12f2b2023-01-09 09:19:20 +0000262 payload_binary_name,
Inseob Kima5a262f2021-11-17 19:41:03 +0900263 daemonize,
264 console,
265 log,
266 debug,
Andrew Walbran3994f002022-01-27 17:33:45 +0000267 protected,
Inseob Kima5a262f2021-11-17 19:41:03 +0900268 mem,
Jiyong Park032615f2022-01-10 13:55:34 +0900269 cpus,
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900270 task_profiles,
Inseob Kima5a262f2021-11-17 19:41:03 +0900271 extra_idsigs,
272 } => command_run_app(
Seungjae Yoo62085c02022-08-12 04:44:52 +0000273 name,
Andrew Walbran616d13f2022-05-12 18:35:55 +0000274 service.as_ref(),
Inseob Kima5a262f2021-11-17 19:41:03 +0900275 &apk,
276 &idsig,
277 &instance,
Shikha Panwar22e70452022-10-10 18:32:55 +0000278 storage.as_deref(),
279 storage_size,
Inseob Kim7b5f65c2022-11-15 14:27:04 +0900280 config_path,
Alan Stokes8f12f2b2023-01-09 09:19:20 +0000281 payload_binary_name,
Inseob Kima5a262f2021-11-17 19:41:03 +0900282 daemonize,
283 console.as_deref(),
284 log.as_deref(),
285 debug,
Andrew Walbran3994f002022-01-27 17:33:45 +0000286 protected,
Inseob Kima5a262f2021-11-17 19:41:03 +0900287 mem,
Jiyong Park032615f2022-01-10 13:55:34 +0900288 cpus,
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900289 task_profiles,
Inseob Kima5a262f2021-11-17 19:41:03 +0900290 &extra_idsigs,
291 ),
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000292 Opt::RunMicrodroid {
293 name,
294 work_dir,
295 storage,
296 storage_size,
297 daemonize,
298 console,
299 log,
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000300 debug,
301 protected,
302 mem,
303 cpus,
304 task_profiles,
305 } => command_run_microdroid(
306 name,
307 service.as_ref(),
308 work_dir,
309 storage.as_deref(),
310 storage_size,
311 daemonize,
312 console.as_deref(),
313 log.as_deref(),
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000314 debug,
315 protected,
316 mem,
317 cpus,
318 task_profiles,
319 ),
Victor Hsiehf219cd82022-09-09 13:13:11 -0700320 Opt::Run { name, config, daemonize, cpus, task_profiles, console, log } => {
Jiyong Park032615f2022-01-10 13:55:34 +0900321 command_run(
Seungjae Yoo62085c02022-08-12 04:44:52 +0000322 name,
Andrew Walbran616d13f2022-05-12 18:35:55 +0000323 service.as_ref(),
Jiyong Park032615f2022-01-10 13:55:34 +0900324 &config,
325 daemonize,
326 console.as_deref(),
Jooyung Hanb7983a22022-02-22 05:21:27 +0900327 log.as_deref(),
Jiyong Park032615f2022-01-10 13:55:34 +0900328 /* mem */ None,
329 cpus,
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900330 task_profiles,
Jiyong Park032615f2022-01-10 13:55:34 +0900331 )
Andrew Walbranbe429242021-06-28 12:22:54 +0000332 }
Andrew Walbran616d13f2022-05-12 18:35:55 +0000333 Opt::Stop { cid } => command_stop(service.as_ref(), cid),
334 Opt::List => command_list(service.as_ref()),
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000335 Opt::Info => command_info(),
Jiyong Park9dd389e2021-08-23 20:42:59 +0900336 Opt::CreatePartition { path, size, partition_type } => {
Andrew Walbran616d13f2022-05-12 18:35:55 +0000337 command_create_partition(service.as_ref(), &path, size, partition_type)
Jiyong Park9dd389e2021-08-23 20:42:59 +0900338 }
Andrew Walbran616d13f2022-05-12 18:35:55 +0000339 Opt::CreateIdsig { apk, path } => command_create_idsig(service.as_ref(), &apk, &path),
Andrew Walbranea9fa482021-03-04 16:11:12 +0000340 }
341}
342
David Brazdil3c2ddef2021-03-18 13:09:57 +0000343/// Retrieve reference to a previously daemonized VM and stop it.
Andrew Walbran616d13f2022-05-12 18:35:55 +0000344fn command_stop(service: &dyn IVirtualizationService, cid: u32) -> Result<(), Error> {
Andrew Walbran17de24f2021-05-27 13:27:30 +0000345 service
David Brazdil3c2ddef2021-03-18 13:09:57 +0000346 .debugDropVmRef(cid as i32)
Andrew Walbranf6bf6862021-05-21 12:41:13 +0000347 .context("Failed to get VM from VirtualizationService")?
David Brazdil3c2ddef2021-03-18 13:09:57 +0000348 .context("CID does not correspond to a running background VM")?;
Andrew Walbranea9fa482021-03-04 16:11:12 +0000349 Ok(())
350}
351
Andrew Walbran320b5602021-03-04 16:11:12 +0000352/// List the VMs currently running.
Andrew Walbran616d13f2022-05-12 18:35:55 +0000353fn command_list(service: &dyn IVirtualizationService) -> Result<(), Error> {
Andrew Walbran17de24f2021-05-27 13:27:30 +0000354 let vms = service.debugListVms().context("Failed to get list of VMs")?;
Andrew Walbran320b5602021-03-04 16:11:12 +0000355 println!("Running VMs: {:#?}", vms);
356 Ok(())
357}
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000358
359/// Print information about supported VM types.
360fn command_info() -> Result<(), Error> {
Alan Stokes8d39a9b2023-01-10 15:01:00 +0000361 let non_protected_vm_supported =
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000362 system_properties::read_bool("ro.boot.hypervisor.vm.supported", false)?;
363 let protected_vm_supported =
364 system_properties::read_bool("ro.boot.hypervisor.protected_vm.supported", false)?;
Alan Stokes8d39a9b2023-01-10 15:01:00 +0000365 match (non_protected_vm_supported, protected_vm_supported) {
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000366 (false, false) => println!("VMs are not supported."),
367 (false, true) => println!("Only protected VMs are supported."),
Alan Stokes8d39a9b2023-01-10 15:01:00 +0000368 (true, false) => println!("Only non-protected VMs are supported."),
369 (true, true) => println!("Both protected and non-protected VMs are supported."),
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000370 }
371
Andrew Walbran014efb52022-02-03 17:43:11 +0000372 if let Some(version) = system_properties::read("ro.boot.hypervisor.version")? {
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000373 println!("Hypervisor version: {}", version);
374 } else {
375 println!("Hypervisor version not set.");
376 }
377
378 if Path::new("/dev/kvm").exists() {
379 println!("/dev/kvm exists.");
380 } else {
381 println!("/dev/kvm does not exist.");
382 }
383
384 Ok(())
385}
Andrew Walbran1f810b62022-08-10 13:33:57 +0000386
387#[cfg(test)]
388mod tests {
389 use super::*;
390 use clap::IntoApp;
391
392 #[test]
393 fn verify_app() {
394 Opt::into_app().debug_assert();
395 }
396}