blob: e1c3413ddc4ba8d02e899ab76cc258c4265c31a1 [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 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
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
Jooyung Han21e9b922021-06-26 04:14:16 +090077 /// Path to file for VM log output.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070078 #[clap(long)]
Jooyung Han21e9b922021-06-26 04:14:16 +090079 log: Option<PathBuf>,
Jiyong Park23601142021-07-05 13:15:32 +090080
Seungjae Yoofa22bb02022-12-08 16:38:42 +090081 /// Debug level of the VM. Supported values: "none" (default), and "full".
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070082 #[clap(long, default_value = "none", value_parser = parse_debug_level)]
Jiyong Parkc2a49cc2021-10-15 00:02:12 +090083 debug: DebugLevel,
Jiyong Parkd63cfff2021-09-27 20:10:17 +090084
Andrew Walbran3994f002022-01-27 17:33:45 +000085 /// Run VM in protected mode.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070086 #[clap(short, long)]
Andrew Walbran3994f002022-01-27 17:33:45 +000087 protected: bool,
88
Jiyong Parkd63cfff2021-09-27 20:10:17 +090089 /// Memory size (in MiB) of the VM. If unspecified, defaults to the value of `memory_mib`
90 /// in the VM config file.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070091 #[clap(short, long)]
Jiyong Parkd63cfff2021-09-27 20:10:17 +090092 mem: Option<u32>,
Inseob Kima5a262f2021-11-17 19:41:03 +090093
David Brazdil7d1e5ec2023-02-06 17:56:29 +000094 /// Run VM with vCPU topology matching that of the host. If unspecified, defaults to 1 vCPU.
95 #[clap(long, default_value = "one_cpu", value_parser = parse_cpu_topology)]
96 cpu_topology: CpuTopology,
Jiyong Park032615f2022-01-10 13:55:34 +090097
Jiyong Parkdfe16d62022-04-20 17:32:12 +090098 /// Comma separated list of task profile names to apply to the VM
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070099 #[clap(long)]
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900100 task_profiles: Vec<String>,
101
Inseob Kima5a262f2021-11-17 19:41:03 +0900102 /// Paths to extra idsig files.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700103 #[clap(long = "extra-idsig")]
Inseob Kima5a262f2021-11-17 19:41:03 +0900104 extra_idsigs: Vec<PathBuf>,
Jooyung Han21e9b922021-06-26 04:14:16 +0900105 },
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000106 /// Run a virtual machine with Microdroid inside
107 RunMicrodroid {
108 /// Path to the directory where VM-related files (e.g. instance.img, apk.idsig, etc.) will
109 /// be stored. If not specified a random directory under /data/local/tmp/microdroid will be
110 /// created and used.
111 #[clap(long)]
112 work_dir: Option<PathBuf>,
113
114 /// Name of VM
115 #[clap(long)]
116 name: Option<String>,
117
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000118 /// Path to the file backing the storage.
119 /// Created if the option is used but the path does not exist in the device.
120 #[clap(long)]
121 storage: Option<PathBuf>,
122
123 /// Size of the storage. Used only if --storage is supplied but path does not exist
124 /// Default size is 10*1024*1024
125 #[clap(long)]
126 storage_size: Option<u64>,
127
128 /// Path to file for VM console output.
129 #[clap(long)]
130 console: Option<PathBuf>,
131
132 /// Path to file for VM log output.
133 #[clap(long)]
134 log: Option<PathBuf>,
135
Seungjae Yoofa22bb02022-12-08 16:38:42 +0900136 /// Debug level of the VM. Supported values: "none" (default), and "full".
Nikita Ioffe0a15fcf2022-12-01 21:12:50 +0000137 #[clap(long, default_value = "full", value_parser = parse_debug_level)]
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000138 debug: DebugLevel,
139
140 /// Run VM in protected mode.
141 #[clap(short, long)]
142 protected: bool,
143
144 /// Memory size (in MiB) of the VM. If unspecified, defaults to the value of `memory_mib`
145 /// in the VM config file.
146 #[clap(short, long)]
147 mem: Option<u32>,
148
David Brazdil7d1e5ec2023-02-06 17:56:29 +0000149 /// Run VM with vCPU topology matching that of the host. If unspecified, defaults to 1 vCPU.
150 #[clap(long, default_value = "one_cpu", value_parser = parse_cpu_topology)]
151 cpu_topology: CpuTopology,
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000152
153 /// Comma separated list of task profile names to apply to the VM
154 #[clap(long)]
155 task_profiles: Vec<String>,
156 },
David Brazdil20412d92021-03-18 10:53:06 +0000157 /// Run a virtual machine
158 Run {
159 /// Path to VM config JSON
David Brazdil20412d92021-03-18 10:53:06 +0000160 config: PathBuf,
David Brazdil3c2ddef2021-03-18 13:09:57 +0000161
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700162 /// Name of VM
163 #[clap(long)]
164 name: Option<String>,
165
David Brazdil7d1e5ec2023-02-06 17:56:29 +0000166 /// Run VM with vCPU topology matching that of the host. If unspecified, defaults to 1 vCPU.
167 #[clap(long, default_value = "one_cpu", value_parser = parse_cpu_topology)]
168 cpu_topology: CpuTopology,
Jiyong Park032615f2022-01-10 13:55:34 +0900169
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900170 /// Comma separated list of task profile names to apply to the VM
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700171 #[clap(long)]
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900172 task_profiles: Vec<String>,
173
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900174 /// Path to file for VM console output.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700175 #[clap(long)]
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900176 console: Option<PathBuf>,
Jooyung Hanb7983a22022-02-22 05:21:27 +0900177
178 /// Path to file for VM log output.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700179 #[clap(long)]
Jooyung Hanb7983a22022-02-22 05:21:27 +0900180 log: Option<PathBuf>,
David Brazdil3c2ddef2021-03-18 13:09:57 +0000181 },
David Brazdil20412d92021-03-18 10:53:06 +0000182 /// List running virtual machines
183 List,
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000184 /// Print information about virtual machine support
185 Info,
Andrew Walbrandff3b942021-06-09 15:20:36 +0000186 /// Create a new empty partition to be used as a writable partition for a VM
187 CreatePartition {
188 /// Path at which to create the image file
Andrew Walbrandff3b942021-06-09 15:20:36 +0000189 path: PathBuf,
190
191 /// The desired size of the partition, in bytes.
192 size: u64,
Jiyong Park9dd389e2021-08-23 20:42:59 +0900193
194 /// Type of the partition
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700195 #[clap(short = 't', long = "type", default_value = "raw",
196 value_parser = parse_partition_type)]
Jiyong Park9dd389e2021-08-23 20:42:59 +0900197 partition_type: PartitionType,
Andrew Walbrandff3b942021-06-09 15:20:36 +0000198 },
Jooyung Hanc221c052022-02-22 05:20:15 +0900199 /// Creates or update the idsig file by digesting the input APK file.
200 CreateIdsig {
201 /// Path to VM Payload APK
Jooyung Hanc221c052022-02-22 05:20:15 +0900202 apk: PathBuf,
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700203
Jooyung Hanc221c052022-02-22 05:20:15 +0900204 /// Path to idsig of the APK
Jooyung Hanc221c052022-02-22 05:20:15 +0900205 path: PathBuf,
206 },
David Brazdil20412d92021-03-18 10:53:06 +0000207}
208
Jiyong Parkc2a49cc2021-10-15 00:02:12 +0900209fn parse_debug_level(s: &str) -> Result<DebugLevel, String> {
210 match s {
211 "none" => Ok(DebugLevel::NONE),
Jiyong Parkc2a49cc2021-10-15 00:02:12 +0900212 "full" => Ok(DebugLevel::FULL),
213 _ => Err(format!("Invalid debug level {}", s)),
214 }
215}
216
Jiyong Park9dd389e2021-08-23 20:42:59 +0900217fn parse_partition_type(s: &str) -> Result<PartitionType, String> {
218 match s {
219 "raw" => Ok(PartitionType::RAW),
220 "instance" => Ok(PartitionType::ANDROID_VM_INSTANCE),
221 _ => Err(format!("Invalid partition type {}", s)),
222 }
223}
224
David Brazdil7d1e5ec2023-02-06 17:56:29 +0000225fn parse_cpu_topology(s: &str) -> Result<CpuTopology, String> {
226 match s {
227 "one_cpu" => Ok(CpuTopology::ONE_CPU),
228 "match_host" => Ok(CpuTopology::MATCH_HOST),
229 _ => Err(format!("Invalid cpu topology {}", s)),
230 }
231}
232
Andrew Walbranea9fa482021-03-04 16:11:12 +0000233fn main() -> Result<(), Error> {
234 env_logger::init();
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700235 let opt = Opt::parse();
Andrew Walbranea9fa482021-03-04 16:11:12 +0000236
237 // We need to start the thread pool for Binder to work properly, especially link_to_death.
238 ProcessState::start_thread_pool();
239
David Brazdil4b4c5102022-12-19 22:56:20 +0000240 let virtmgr =
241 vmclient::VirtualizationService::new().context("Failed to spawn VirtualizationService")?;
242 let service = virtmgr.connect().context("Failed to connect to VirtualizationService")?;
Andrew Walbran320b5602021-03-04 16:11:12 +0000243
David Brazdil20412d92021-03-18 10:53:06 +0000244 match opt {
Inseob Kima5a262f2021-11-17 19:41:03 +0900245 Opt::RunApp {
Seungjae Yoo62085c02022-08-12 04:44:52 +0000246 name,
Inseob Kima5a262f2021-11-17 19:41:03 +0900247 apk,
248 idsig,
249 instance,
Shikha Panwar22e70452022-10-10 18:32:55 +0000250 storage,
251 storage_size,
Inseob Kima5a262f2021-11-17 19:41:03 +0900252 config_path,
Alan Stokes8f12f2b2023-01-09 09:19:20 +0000253 payload_binary_name,
Inseob Kima5a262f2021-11-17 19:41:03 +0900254 console,
255 log,
256 debug,
Andrew Walbran3994f002022-01-27 17:33:45 +0000257 protected,
Inseob Kima5a262f2021-11-17 19:41:03 +0900258 mem,
David Brazdil7d1e5ec2023-02-06 17:56:29 +0000259 cpu_topology,
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900260 task_profiles,
Inseob Kima5a262f2021-11-17 19:41:03 +0900261 extra_idsigs,
262 } => command_run_app(
Seungjae Yoo62085c02022-08-12 04:44:52 +0000263 name,
Andrew Walbran616d13f2022-05-12 18:35:55 +0000264 service.as_ref(),
Inseob Kima5a262f2021-11-17 19:41:03 +0900265 &apk,
266 &idsig,
267 &instance,
Shikha Panwar22e70452022-10-10 18:32:55 +0000268 storage.as_deref(),
269 storage_size,
Inseob Kim7b5f65c2022-11-15 14:27:04 +0900270 config_path,
Alan Stokes8f12f2b2023-01-09 09:19:20 +0000271 payload_binary_name,
Inseob Kima5a262f2021-11-17 19:41:03 +0900272 console.as_deref(),
273 log.as_deref(),
274 debug,
Andrew Walbran3994f002022-01-27 17:33:45 +0000275 protected,
Inseob Kima5a262f2021-11-17 19:41:03 +0900276 mem,
David Brazdil7d1e5ec2023-02-06 17:56:29 +0000277 cpu_topology,
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900278 task_profiles,
Inseob Kima5a262f2021-11-17 19:41:03 +0900279 &extra_idsigs,
280 ),
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000281 Opt::RunMicrodroid {
282 name,
283 work_dir,
284 storage,
285 storage_size,
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000286 console,
287 log,
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000288 debug,
289 protected,
290 mem,
David Brazdil7d1e5ec2023-02-06 17:56:29 +0000291 cpu_topology,
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000292 task_profiles,
293 } => command_run_microdroid(
294 name,
295 service.as_ref(),
296 work_dir,
297 storage.as_deref(),
298 storage_size,
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000299 console.as_deref(),
300 log.as_deref(),
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000301 debug,
302 protected,
303 mem,
David Brazdil7d1e5ec2023-02-06 17:56:29 +0000304 cpu_topology,
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000305 task_profiles,
306 ),
David Brazdil7d1e5ec2023-02-06 17:56:29 +0000307 Opt::Run { name, config, cpu_topology, task_profiles, console, log } => {
Jiyong Park032615f2022-01-10 13:55:34 +0900308 command_run(
Seungjae Yoo62085c02022-08-12 04:44:52 +0000309 name,
Andrew Walbran616d13f2022-05-12 18:35:55 +0000310 service.as_ref(),
Jiyong Park032615f2022-01-10 13:55:34 +0900311 &config,
Jiyong Park032615f2022-01-10 13:55:34 +0900312 console.as_deref(),
Jooyung Hanb7983a22022-02-22 05:21:27 +0900313 log.as_deref(),
Jiyong Park032615f2022-01-10 13:55:34 +0900314 /* mem */ None,
David Brazdil7d1e5ec2023-02-06 17:56:29 +0000315 cpu_topology,
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900316 task_profiles,
Jiyong Park032615f2022-01-10 13:55:34 +0900317 )
Andrew Walbranbe429242021-06-28 12:22:54 +0000318 }
Andrew Walbran616d13f2022-05-12 18:35:55 +0000319 Opt::List => command_list(service.as_ref()),
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000320 Opt::Info => command_info(),
Jiyong Park9dd389e2021-08-23 20:42:59 +0900321 Opt::CreatePartition { path, size, partition_type } => {
Andrew Walbran616d13f2022-05-12 18:35:55 +0000322 command_create_partition(service.as_ref(), &path, size, partition_type)
Jiyong Park9dd389e2021-08-23 20:42:59 +0900323 }
Andrew Walbran616d13f2022-05-12 18:35:55 +0000324 Opt::CreateIdsig { apk, path } => command_create_idsig(service.as_ref(), &apk, &path),
Andrew Walbranea9fa482021-03-04 16:11:12 +0000325 }
326}
327
Andrew Walbran320b5602021-03-04 16:11:12 +0000328/// List the VMs currently running.
Andrew Walbran616d13f2022-05-12 18:35:55 +0000329fn command_list(service: &dyn IVirtualizationService) -> Result<(), Error> {
Andrew Walbran17de24f2021-05-27 13:27:30 +0000330 let vms = service.debugListVms().context("Failed to get list of VMs")?;
Andrew Walbran320b5602021-03-04 16:11:12 +0000331 println!("Running VMs: {:#?}", vms);
332 Ok(())
333}
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000334
335/// Print information about supported VM types.
336fn command_info() -> Result<(), Error> {
Alan Stokes8d39a9b2023-01-10 15:01:00 +0000337 let non_protected_vm_supported =
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000338 system_properties::read_bool("ro.boot.hypervisor.vm.supported", false)?;
339 let protected_vm_supported =
340 system_properties::read_bool("ro.boot.hypervisor.protected_vm.supported", false)?;
Alan Stokes8d39a9b2023-01-10 15:01:00 +0000341 match (non_protected_vm_supported, protected_vm_supported) {
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000342 (false, false) => println!("VMs are not supported."),
343 (false, true) => println!("Only protected VMs are supported."),
Alan Stokes8d39a9b2023-01-10 15:01:00 +0000344 (true, false) => println!("Only non-protected VMs are supported."),
345 (true, true) => println!("Both protected and non-protected VMs are supported."),
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000346 }
347
Andrew Walbran014efb52022-02-03 17:43:11 +0000348 if let Some(version) = system_properties::read("ro.boot.hypervisor.version")? {
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000349 println!("Hypervisor version: {}", version);
350 } else {
351 println!("Hypervisor version not set.");
352 }
353
354 if Path::new("/dev/kvm").exists() {
355 println!("/dev/kvm exists.");
356 } else {
357 println!("/dev/kvm does not exist.");
358 }
359
360 Ok(())
361}
Andrew Walbran1f810b62022-08-10 13:33:57 +0000362
363#[cfg(test)]
364mod tests {
365 use super::*;
Andrew Walbranaa1efc42022-08-10 13:33:57 +0000366 use clap::CommandFactory;
Andrew Walbran1f810b62022-08-10 13:33:57 +0000367
368 #[test]
369 fn verify_app() {
Andrew Walbranaa1efc42022-08-10 13:33:57 +0000370 // Check that the command parsing has been configured in a valid way.
371 Opt::command().debug_assert();
Andrew Walbran1f810b62022-08-10 13:33:57 +0000372 }
373}