blob: 32b165b0b437573aa556940d83e0a2dcf648765b [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
Inseob Kim7b5f65c2022-11-15 14:27:04 +090054 /// Path to VM payload binary within APK (e.g. MicrodroidTestNativeLib.so)
55 #[clap(long)]
56 payload_path: Option<String>,
57
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070058 /// Name of VM
59 #[clap(long)]
60 name: Option<String>,
61
Jooyung Han21e9b922021-06-26 04:14:16 +090062 /// Detach VM from the terminal and run in the background
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070063 #[clap(short, long)]
Jooyung Han21e9b922021-06-26 04:14:16 +090064 daemonize: bool,
65
Shikha Panwar22e70452022-10-10 18:32:55 +000066 /// Path to the file backing the storage.
67 /// Created if the option is used but the path does not exist in the device.
68 #[clap(long)]
69 storage: Option<PathBuf>,
70
71 /// Size of the storage. Used only if --storage is supplied but path does not exist
72 /// Default size is 10*1024*1024
73 #[clap(long)]
74 storage_size: Option<u64>,
75
Jiyong Parkb8182bb2021-10-26 22:53:08 +090076 /// Path to file for VM console output.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070077 #[clap(long)]
Jiyong Parkb8182bb2021-10-26 22:53:08 +090078 console: Option<PathBuf>,
79
Jooyung Han21e9b922021-06-26 04:14:16 +090080 /// Path to file for VM log output.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070081 #[clap(long)]
Jooyung Han21e9b922021-06-26 04:14:16 +090082 log: Option<PathBuf>,
Jiyong Park23601142021-07-05 13:15:32 +090083
Jiyong Parke558ab12022-07-07 20:18:55 +090084 /// Path to file where ramdump is recorded on kernel panic
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070085 #[clap(long)]
Jiyong Parke558ab12022-07-07 20:18:55 +090086 ramdump: Option<PathBuf>,
87
Seungjae Yoofa22bb02022-12-08 16:38:42 +090088 /// Debug level of the VM. Supported values: "none" (default), and "full".
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070089 #[clap(long, default_value = "none", value_parser = parse_debug_level)]
Jiyong Parkc2a49cc2021-10-15 00:02:12 +090090 debug: DebugLevel,
Jiyong Parkd63cfff2021-09-27 20:10:17 +090091
Andrew Walbran3994f002022-01-27 17:33:45 +000092 /// Run VM in protected mode.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070093 #[clap(short, long)]
Andrew Walbran3994f002022-01-27 17:33:45 +000094 protected: bool,
95
Jiyong Parkd63cfff2021-09-27 20:10:17 +090096 /// Memory size (in MiB) of the VM. If unspecified, defaults to the value of `memory_mib`
97 /// in the VM config file.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070098 #[clap(short, long)]
Jiyong Parkd63cfff2021-09-27 20:10:17 +090099 mem: Option<u32>,
Inseob Kima5a262f2021-11-17 19:41:03 +0900100
Jiyong Park032615f2022-01-10 13:55:34 +0900101 /// Number of vCPUs in the VM. If unspecified, defaults to 1.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700102 #[clap(long)]
Jiyong Park032615f2022-01-10 13:55:34 +0900103 cpus: Option<u32>,
104
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900105 /// Comma separated list of task profile names to apply to the VM
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700106 #[clap(long)]
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900107 task_profiles: Vec<String>,
108
Inseob Kima5a262f2021-11-17 19:41:03 +0900109 /// Paths to extra idsig files.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700110 #[clap(long = "extra-idsig")]
Inseob Kima5a262f2021-11-17 19:41:03 +0900111 extra_idsigs: Vec<PathBuf>,
Jooyung Han21e9b922021-06-26 04:14:16 +0900112 },
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000113 /// Run a virtual machine with Microdroid inside
114 RunMicrodroid {
115 /// Path to the directory where VM-related files (e.g. instance.img, apk.idsig, etc.) will
116 /// be stored. If not specified a random directory under /data/local/tmp/microdroid will be
117 /// created and used.
118 #[clap(long)]
119 work_dir: Option<PathBuf>,
120
121 /// Name of VM
122 #[clap(long)]
123 name: Option<String>,
124
125 /// Detach VM from the terminal and run in the background
126 #[clap(short, long)]
127 daemonize: bool,
128
129 /// Path to the file backing the storage.
130 /// Created if the option is used but the path does not exist in the device.
131 #[clap(long)]
132 storage: Option<PathBuf>,
133
134 /// Size of the storage. Used only if --storage is supplied but path does not exist
135 /// Default size is 10*1024*1024
136 #[clap(long)]
137 storage_size: Option<u64>,
138
139 /// Path to file for VM console output.
140 #[clap(long)]
141 console: Option<PathBuf>,
142
143 /// Path to file for VM log output.
144 #[clap(long)]
145 log: Option<PathBuf>,
146
147 /// Path to file where ramdump is recorded on kernel panic
148 #[clap(long)]
149 ramdump: Option<PathBuf>,
150
Seungjae Yoofa22bb02022-12-08 16:38:42 +0900151 /// Debug level of the VM. Supported values: "none" (default), and "full".
Nikita Ioffe0a15fcf2022-12-01 21:12:50 +0000152 #[clap(long, default_value = "full", value_parser = parse_debug_level)]
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000153 debug: DebugLevel,
154
155 /// Run VM in protected mode.
156 #[clap(short, long)]
157 protected: bool,
158
159 /// Memory size (in MiB) of the VM. If unspecified, defaults to the value of `memory_mib`
160 /// in the VM config file.
161 #[clap(short, long)]
162 mem: Option<u32>,
163
164 /// Number of vCPUs in the VM. If unspecified, defaults to 1.
165 #[clap(long)]
166 cpus: Option<u32>,
167
168 /// Comma separated list of task profile names to apply to the VM
169 #[clap(long)]
170 task_profiles: Vec<String>,
171 },
David Brazdil20412d92021-03-18 10:53:06 +0000172 /// Run a virtual machine
173 Run {
174 /// Path to VM config JSON
David Brazdil20412d92021-03-18 10:53:06 +0000175 config: PathBuf,
David Brazdil3c2ddef2021-03-18 13:09:57 +0000176
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700177 /// Name of VM
178 #[clap(long)]
179 name: Option<String>,
180
David Brazdil3c2ddef2021-03-18 13:09:57 +0000181 /// Detach VM from the terminal and run in the background
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700182 #[clap(short, long)]
David Brazdil3c2ddef2021-03-18 13:09:57 +0000183 daemonize: bool,
Andrew Walbranbe429242021-06-28 12:22:54 +0000184
Jiyong Park032615f2022-01-10 13:55:34 +0900185 /// Number of vCPUs in the VM. If unspecified, defaults to 1.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700186 #[clap(long)]
Jiyong Park032615f2022-01-10 13:55:34 +0900187 cpus: Option<u32>,
188
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900189 /// Comma separated list of task profile names to apply to the VM
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700190 #[clap(long)]
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900191 task_profiles: Vec<String>,
192
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900193 /// Path to file for VM console output.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700194 #[clap(long)]
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900195 console: Option<PathBuf>,
Jooyung Hanb7983a22022-02-22 05:21:27 +0900196
197 /// Path to file for VM log output.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700198 #[clap(long)]
Jooyung Hanb7983a22022-02-22 05:21:27 +0900199 log: Option<PathBuf>,
David Brazdil3c2ddef2021-03-18 13:09:57 +0000200 },
201 /// Stop a virtual machine running in the background
202 Stop {
203 /// CID of the virtual machine
204 cid: u32,
David Brazdil20412d92021-03-18 10:53:06 +0000205 },
206 /// List running virtual machines
207 List,
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000208 /// Print information about virtual machine support
209 Info,
Andrew Walbrandff3b942021-06-09 15:20:36 +0000210 /// Create a new empty partition to be used as a writable partition for a VM
211 CreatePartition {
212 /// Path at which to create the image file
Andrew Walbrandff3b942021-06-09 15:20:36 +0000213 path: PathBuf,
214
215 /// The desired size of the partition, in bytes.
216 size: u64,
Jiyong Park9dd389e2021-08-23 20:42:59 +0900217
218 /// Type of the partition
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700219 #[clap(short = 't', long = "type", default_value = "raw",
220 value_parser = parse_partition_type)]
Jiyong Park9dd389e2021-08-23 20:42:59 +0900221 partition_type: PartitionType,
Andrew Walbrandff3b942021-06-09 15:20:36 +0000222 },
Jooyung Hanc221c052022-02-22 05:20:15 +0900223 /// Creates or update the idsig file by digesting the input APK file.
224 CreateIdsig {
225 /// Path to VM Payload APK
Jooyung Hanc221c052022-02-22 05:20:15 +0900226 apk: PathBuf,
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700227
Jooyung Hanc221c052022-02-22 05:20:15 +0900228 /// Path to idsig of the APK
Jooyung Hanc221c052022-02-22 05:20:15 +0900229 path: PathBuf,
230 },
David Brazdil20412d92021-03-18 10:53:06 +0000231}
232
Jiyong Parkc2a49cc2021-10-15 00:02:12 +0900233fn parse_debug_level(s: &str) -> Result<DebugLevel, String> {
234 match s {
235 "none" => Ok(DebugLevel::NONE),
Jiyong Parkc2a49cc2021-10-15 00:02:12 +0900236 "full" => Ok(DebugLevel::FULL),
237 _ => Err(format!("Invalid debug level {}", s)),
238 }
239}
240
Jiyong Park9dd389e2021-08-23 20:42:59 +0900241fn parse_partition_type(s: &str) -> Result<PartitionType, String> {
242 match s {
243 "raw" => Ok(PartitionType::RAW),
244 "instance" => Ok(PartitionType::ANDROID_VM_INSTANCE),
245 _ => Err(format!("Invalid partition type {}", s)),
246 }
247}
248
Andrew Walbranea9fa482021-03-04 16:11:12 +0000249fn main() -> Result<(), Error> {
250 env_logger::init();
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700251 let opt = Opt::parse();
Andrew Walbranea9fa482021-03-04 16:11:12 +0000252
253 // We need to start the thread pool for Binder to work properly, especially link_to_death.
254 ProcessState::start_thread_pool();
255
Andrew Walbrand0ef4002022-05-16 16:14:10 +0000256 let service = vmclient::connect().context("Failed to find VirtualizationService")?;
Andrew Walbran320b5602021-03-04 16:11:12 +0000257
David Brazdil20412d92021-03-18 10:53:06 +0000258 match opt {
Inseob Kima5a262f2021-11-17 19:41:03 +0900259 Opt::RunApp {
Seungjae Yoo62085c02022-08-12 04:44:52 +0000260 name,
Inseob Kima5a262f2021-11-17 19:41:03 +0900261 apk,
262 idsig,
263 instance,
Shikha Panwar22e70452022-10-10 18:32:55 +0000264 storage,
265 storage_size,
Inseob Kima5a262f2021-11-17 19:41:03 +0900266 config_path,
Inseob Kim7b5f65c2022-11-15 14:27:04 +0900267 payload_path,
Inseob Kima5a262f2021-11-17 19:41:03 +0900268 daemonize,
269 console,
270 log,
Jiyong Parke558ab12022-07-07 20:18:55 +0900271 ramdump,
Inseob Kima5a262f2021-11-17 19:41:03 +0900272 debug,
Andrew Walbran3994f002022-01-27 17:33:45 +0000273 protected,
Inseob Kima5a262f2021-11-17 19:41:03 +0900274 mem,
Jiyong Park032615f2022-01-10 13:55:34 +0900275 cpus,
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900276 task_profiles,
Inseob Kima5a262f2021-11-17 19:41:03 +0900277 extra_idsigs,
278 } => command_run_app(
Seungjae Yoo62085c02022-08-12 04:44:52 +0000279 name,
Andrew Walbran616d13f2022-05-12 18:35:55 +0000280 service.as_ref(),
Inseob Kima5a262f2021-11-17 19:41:03 +0900281 &apk,
282 &idsig,
283 &instance,
Shikha Panwar22e70452022-10-10 18:32:55 +0000284 storage.as_deref(),
285 storage_size,
Inseob Kim7b5f65c2022-11-15 14:27:04 +0900286 config_path,
287 payload_path,
Inseob Kima5a262f2021-11-17 19:41:03 +0900288 daemonize,
289 console.as_deref(),
290 log.as_deref(),
Jiyong Parke558ab12022-07-07 20:18:55 +0900291 ramdump.as_deref(),
Inseob Kima5a262f2021-11-17 19:41:03 +0900292 debug,
Andrew Walbran3994f002022-01-27 17:33:45 +0000293 protected,
Inseob Kima5a262f2021-11-17 19:41:03 +0900294 mem,
Jiyong Park032615f2022-01-10 13:55:34 +0900295 cpus,
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900296 task_profiles,
Inseob Kima5a262f2021-11-17 19:41:03 +0900297 &extra_idsigs,
298 ),
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000299 Opt::RunMicrodroid {
300 name,
301 work_dir,
302 storage,
303 storage_size,
304 daemonize,
305 console,
306 log,
307 ramdump,
308 debug,
309 protected,
310 mem,
311 cpus,
312 task_profiles,
313 } => command_run_microdroid(
314 name,
315 service.as_ref(),
316 work_dir,
317 storage.as_deref(),
318 storage_size,
319 daemonize,
320 console.as_deref(),
321 log.as_deref(),
322 ramdump.as_deref(),
323 debug,
324 protected,
325 mem,
326 cpus,
327 task_profiles,
328 ),
Victor Hsiehf219cd82022-09-09 13:13:11 -0700329 Opt::Run { name, config, daemonize, cpus, task_profiles, console, log } => {
Jiyong Park032615f2022-01-10 13:55:34 +0900330 command_run(
Seungjae Yoo62085c02022-08-12 04:44:52 +0000331 name,
Andrew Walbran616d13f2022-05-12 18:35:55 +0000332 service.as_ref(),
Jiyong Park032615f2022-01-10 13:55:34 +0900333 &config,
334 daemonize,
335 console.as_deref(),
Jooyung Hanb7983a22022-02-22 05:21:27 +0900336 log.as_deref(),
Jiyong Park032615f2022-01-10 13:55:34 +0900337 /* mem */ None,
338 cpus,
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900339 task_profiles,
Jiyong Park032615f2022-01-10 13:55:34 +0900340 )
Andrew Walbranbe429242021-06-28 12:22:54 +0000341 }
Andrew Walbran616d13f2022-05-12 18:35:55 +0000342 Opt::Stop { cid } => command_stop(service.as_ref(), cid),
343 Opt::List => command_list(service.as_ref()),
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000344 Opt::Info => command_info(),
Jiyong Park9dd389e2021-08-23 20:42:59 +0900345 Opt::CreatePartition { path, size, partition_type } => {
Andrew Walbran616d13f2022-05-12 18:35:55 +0000346 command_create_partition(service.as_ref(), &path, size, partition_type)
Jiyong Park9dd389e2021-08-23 20:42:59 +0900347 }
Andrew Walbran616d13f2022-05-12 18:35:55 +0000348 Opt::CreateIdsig { apk, path } => command_create_idsig(service.as_ref(), &apk, &path),
Andrew Walbranea9fa482021-03-04 16:11:12 +0000349 }
350}
351
David Brazdil3c2ddef2021-03-18 13:09:57 +0000352/// Retrieve reference to a previously daemonized VM and stop it.
Andrew Walbran616d13f2022-05-12 18:35:55 +0000353fn command_stop(service: &dyn IVirtualizationService, cid: u32) -> Result<(), Error> {
Andrew Walbran17de24f2021-05-27 13:27:30 +0000354 service
David Brazdil3c2ddef2021-03-18 13:09:57 +0000355 .debugDropVmRef(cid as i32)
Andrew Walbranf6bf6862021-05-21 12:41:13 +0000356 .context("Failed to get VM from VirtualizationService")?
David Brazdil3c2ddef2021-03-18 13:09:57 +0000357 .context("CID does not correspond to a running background VM")?;
Andrew Walbranea9fa482021-03-04 16:11:12 +0000358 Ok(())
359}
360
Andrew Walbran320b5602021-03-04 16:11:12 +0000361/// List the VMs currently running.
Andrew Walbran616d13f2022-05-12 18:35:55 +0000362fn command_list(service: &dyn IVirtualizationService) -> Result<(), Error> {
Andrew Walbran17de24f2021-05-27 13:27:30 +0000363 let vms = service.debugListVms().context("Failed to get list of VMs")?;
Andrew Walbran320b5602021-03-04 16:11:12 +0000364 println!("Running VMs: {:#?}", vms);
365 Ok(())
366}
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000367
368/// Print information about supported VM types.
369fn command_info() -> Result<(), Error> {
370 let unprotected_vm_supported =
371 system_properties::read_bool("ro.boot.hypervisor.vm.supported", false)?;
372 let protected_vm_supported =
373 system_properties::read_bool("ro.boot.hypervisor.protected_vm.supported", false)?;
374 match (unprotected_vm_supported, protected_vm_supported) {
375 (false, false) => println!("VMs are not supported."),
376 (false, true) => println!("Only protected VMs are supported."),
377 (true, false) => println!("Only unprotected VMs are supported."),
378 (true, true) => println!("Both protected and unprotected VMs are supported."),
379 }
380
Andrew Walbran014efb52022-02-03 17:43:11 +0000381 if let Some(version) = system_properties::read("ro.boot.hypervisor.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
393 Ok(())
394}
Andrew Walbran1f810b62022-08-10 13:33:57 +0000395
396#[cfg(test)]
397mod tests {
398 use super::*;
399 use clap::IntoApp;
400
401 #[test]
402 fn verify_app() {
403 Opt::into_app().debug_assert();
404 }
405}