blob: 3b887d3a6d6a0ce20cb4a817a81bb9fa28064902 [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;
Jooyung Han21e9b922021-06-26 04:14:16 +090030use run::{command_run, command_run_app};
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)
Alan Stokes0d1ef782022-09-27 13:46:35 +010051 config_path: Option<String>,
Jooyung Han21e9b922021-06-26 04:14:16 +090052
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070053 /// Name of VM
54 #[clap(long)]
55 name: Option<String>,
56
Jooyung Han21e9b922021-06-26 04:14:16 +090057 /// Detach VM from the terminal and run in the background
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070058 #[clap(short, long)]
Jooyung Han21e9b922021-06-26 04:14:16 +090059 daemonize: bool,
60
Shikha Panwar22e70452022-10-10 18:32:55 +000061 /// Path to the file backing the storage.
62 /// Created if the option is used but the path does not exist in the device.
63 #[clap(long)]
64 storage: Option<PathBuf>,
65
66 /// Size of the storage. Used only if --storage is supplied but path does not exist
67 /// Default size is 10*1024*1024
68 #[clap(long)]
69 storage_size: Option<u64>,
70
Jiyong Parkb8182bb2021-10-26 22:53:08 +090071 /// Path to file for VM console output.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070072 #[clap(long)]
Jiyong Parkb8182bb2021-10-26 22:53:08 +090073 console: Option<PathBuf>,
74
Jooyung Han21e9b922021-06-26 04:14:16 +090075 /// Path to file for VM log output.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070076 #[clap(long)]
Jooyung Han21e9b922021-06-26 04:14:16 +090077 log: Option<PathBuf>,
Jiyong Park23601142021-07-05 13:15:32 +090078
Jiyong Parke558ab12022-07-07 20:18:55 +090079 /// Path to file where ramdump is recorded on kernel panic
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070080 #[clap(long)]
Jiyong Parke558ab12022-07-07 20:18:55 +090081 ramdump: Option<PathBuf>,
82
Jiyong Parkc2a49cc2021-10-15 00:02:12 +090083 /// Debug level of the VM. Supported values: "none" (default), "app_only", and "full".
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070084 #[clap(long, default_value = "none", value_parser = parse_debug_level)]
Jiyong Parkc2a49cc2021-10-15 00:02:12 +090085 debug: DebugLevel,
Jiyong Parkd63cfff2021-09-27 20:10:17 +090086
Andrew Walbran3994f002022-01-27 17:33:45 +000087 /// Run VM in protected mode.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070088 #[clap(short, long)]
Andrew Walbran3994f002022-01-27 17:33:45 +000089 protected: bool,
90
Jiyong Parkd63cfff2021-09-27 20:10:17 +090091 /// Memory size (in MiB) of the VM. If unspecified, defaults to the value of `memory_mib`
92 /// in the VM config file.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070093 #[clap(short, long)]
Jiyong Parkd63cfff2021-09-27 20:10:17 +090094 mem: Option<u32>,
Inseob Kima5a262f2021-11-17 19:41:03 +090095
Jiyong Park032615f2022-01-10 13:55:34 +090096 /// Number of vCPUs in the VM. If unspecified, defaults to 1.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070097 #[clap(long)]
Jiyong Park032615f2022-01-10 13:55:34 +090098 cpus: Option<u32>,
99
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900100 /// Comma separated list of task profile names to apply to the VM
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700101 #[clap(long)]
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900102 task_profiles: Vec<String>,
103
Inseob Kima5a262f2021-11-17 19:41:03 +0900104 /// Paths to extra idsig files.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700105 #[clap(long = "extra-idsig")]
Inseob Kima5a262f2021-11-17 19:41:03 +0900106 extra_idsigs: Vec<PathBuf>,
Jooyung Han21e9b922021-06-26 04:14:16 +0900107 },
David Brazdil20412d92021-03-18 10:53:06 +0000108 /// Run a virtual machine
109 Run {
110 /// Path to VM config JSON
David Brazdil20412d92021-03-18 10:53:06 +0000111 config: PathBuf,
David Brazdil3c2ddef2021-03-18 13:09:57 +0000112
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700113 /// Name of VM
114 #[clap(long)]
115 name: Option<String>,
116
David Brazdil3c2ddef2021-03-18 13:09:57 +0000117 /// Detach VM from the terminal and run in the background
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700118 #[clap(short, long)]
David Brazdil3c2ddef2021-03-18 13:09:57 +0000119 daemonize: bool,
Andrew Walbranbe429242021-06-28 12:22:54 +0000120
Jiyong Park032615f2022-01-10 13:55:34 +0900121 /// Number of vCPUs in the VM. If unspecified, defaults to 1.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700122 #[clap(long)]
Jiyong Park032615f2022-01-10 13:55:34 +0900123 cpus: Option<u32>,
124
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900125 /// Comma separated list of task profile names to apply to the VM
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700126 #[clap(long)]
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900127 task_profiles: Vec<String>,
128
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900129 /// Path to file for VM console output.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700130 #[clap(long)]
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900131 console: Option<PathBuf>,
Jooyung Hanb7983a22022-02-22 05:21:27 +0900132
133 /// Path to file for VM log output.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700134 #[clap(long)]
Jooyung Hanb7983a22022-02-22 05:21:27 +0900135 log: Option<PathBuf>,
David Brazdil3c2ddef2021-03-18 13:09:57 +0000136 },
137 /// Stop a virtual machine running in the background
138 Stop {
139 /// CID of the virtual machine
140 cid: u32,
David Brazdil20412d92021-03-18 10:53:06 +0000141 },
142 /// List running virtual machines
143 List,
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000144 /// Print information about virtual machine support
145 Info,
Andrew Walbrandff3b942021-06-09 15:20:36 +0000146 /// Create a new empty partition to be used as a writable partition for a VM
147 CreatePartition {
148 /// Path at which to create the image file
Andrew Walbrandff3b942021-06-09 15:20:36 +0000149 path: PathBuf,
150
151 /// The desired size of the partition, in bytes.
152 size: u64,
Jiyong Park9dd389e2021-08-23 20:42:59 +0900153
154 /// Type of the partition
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700155 #[clap(short = 't', long = "type", default_value = "raw",
156 value_parser = parse_partition_type)]
Jiyong Park9dd389e2021-08-23 20:42:59 +0900157 partition_type: PartitionType,
Andrew Walbrandff3b942021-06-09 15:20:36 +0000158 },
Jooyung Hanc221c052022-02-22 05:20:15 +0900159 /// Creates or update the idsig file by digesting the input APK file.
160 CreateIdsig {
161 /// Path to VM Payload APK
Jooyung Hanc221c052022-02-22 05:20:15 +0900162 apk: PathBuf,
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700163
Jooyung Hanc221c052022-02-22 05:20:15 +0900164 /// Path to idsig of the APK
Jooyung Hanc221c052022-02-22 05:20:15 +0900165 path: PathBuf,
166 },
David Brazdil20412d92021-03-18 10:53:06 +0000167}
168
Jiyong Parkc2a49cc2021-10-15 00:02:12 +0900169fn parse_debug_level(s: &str) -> Result<DebugLevel, String> {
170 match s {
171 "none" => Ok(DebugLevel::NONE),
172 "app_only" => Ok(DebugLevel::APP_ONLY),
173 "full" => Ok(DebugLevel::FULL),
174 _ => Err(format!("Invalid debug level {}", s)),
175 }
176}
177
Jiyong Park9dd389e2021-08-23 20:42:59 +0900178fn parse_partition_type(s: &str) -> Result<PartitionType, String> {
179 match s {
180 "raw" => Ok(PartitionType::RAW),
181 "instance" => Ok(PartitionType::ANDROID_VM_INSTANCE),
182 _ => Err(format!("Invalid partition type {}", s)),
183 }
184}
185
Andrew Walbranea9fa482021-03-04 16:11:12 +0000186fn main() -> Result<(), Error> {
187 env_logger::init();
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700188 let opt = Opt::parse();
Andrew Walbranea9fa482021-03-04 16:11:12 +0000189
190 // We need to start the thread pool for Binder to work properly, especially link_to_death.
191 ProcessState::start_thread_pool();
192
Andrew Walbrand0ef4002022-05-16 16:14:10 +0000193 let service = vmclient::connect().context("Failed to find VirtualizationService")?;
Andrew Walbran320b5602021-03-04 16:11:12 +0000194
David Brazdil20412d92021-03-18 10:53:06 +0000195 match opt {
Inseob Kima5a262f2021-11-17 19:41:03 +0900196 Opt::RunApp {
Seungjae Yoo62085c02022-08-12 04:44:52 +0000197 name,
Inseob Kima5a262f2021-11-17 19:41:03 +0900198 apk,
199 idsig,
200 instance,
Shikha Panwar22e70452022-10-10 18:32:55 +0000201 storage,
202 storage_size,
Inseob Kima5a262f2021-11-17 19:41:03 +0900203 config_path,
204 daemonize,
205 console,
206 log,
Jiyong Parke558ab12022-07-07 20:18:55 +0900207 ramdump,
Inseob Kima5a262f2021-11-17 19:41:03 +0900208 debug,
Andrew Walbran3994f002022-01-27 17:33:45 +0000209 protected,
Inseob Kima5a262f2021-11-17 19:41:03 +0900210 mem,
Jiyong Park032615f2022-01-10 13:55:34 +0900211 cpus,
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900212 task_profiles,
Inseob Kima5a262f2021-11-17 19:41:03 +0900213 extra_idsigs,
214 } => command_run_app(
Seungjae Yoo62085c02022-08-12 04:44:52 +0000215 name,
Andrew Walbran616d13f2022-05-12 18:35:55 +0000216 service.as_ref(),
Inseob Kima5a262f2021-11-17 19:41:03 +0900217 &apk,
218 &idsig,
219 &instance,
Shikha Panwar22e70452022-10-10 18:32:55 +0000220 storage.as_deref(),
221 storage_size,
Alan Stokes0d1ef782022-09-27 13:46:35 +0100222 config_path.as_deref().unwrap_or(""),
Inseob Kima5a262f2021-11-17 19:41:03 +0900223 daemonize,
224 console.as_deref(),
225 log.as_deref(),
Jiyong Parke558ab12022-07-07 20:18:55 +0900226 ramdump.as_deref(),
Inseob Kima5a262f2021-11-17 19:41:03 +0900227 debug,
Andrew Walbran3994f002022-01-27 17:33:45 +0000228 protected,
Inseob Kima5a262f2021-11-17 19:41:03 +0900229 mem,
Jiyong Park032615f2022-01-10 13:55:34 +0900230 cpus,
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900231 task_profiles,
Inseob Kima5a262f2021-11-17 19:41:03 +0900232 &extra_idsigs,
233 ),
Victor Hsiehf219cd82022-09-09 13:13:11 -0700234 Opt::Run { name, config, daemonize, cpus, task_profiles, console, log } => {
Jiyong Park032615f2022-01-10 13:55:34 +0900235 command_run(
Seungjae Yoo62085c02022-08-12 04:44:52 +0000236 name,
Andrew Walbran616d13f2022-05-12 18:35:55 +0000237 service.as_ref(),
Jiyong Park032615f2022-01-10 13:55:34 +0900238 &config,
239 daemonize,
240 console.as_deref(),
Jooyung Hanb7983a22022-02-22 05:21:27 +0900241 log.as_deref(),
Jiyong Park032615f2022-01-10 13:55:34 +0900242 /* mem */ None,
243 cpus,
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900244 task_profiles,
Jiyong Park032615f2022-01-10 13:55:34 +0900245 )
Andrew Walbranbe429242021-06-28 12:22:54 +0000246 }
Andrew Walbran616d13f2022-05-12 18:35:55 +0000247 Opt::Stop { cid } => command_stop(service.as_ref(), cid),
248 Opt::List => command_list(service.as_ref()),
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000249 Opt::Info => command_info(),
Jiyong Park9dd389e2021-08-23 20:42:59 +0900250 Opt::CreatePartition { path, size, partition_type } => {
Andrew Walbran616d13f2022-05-12 18:35:55 +0000251 command_create_partition(service.as_ref(), &path, size, partition_type)
Jiyong Park9dd389e2021-08-23 20:42:59 +0900252 }
Andrew Walbran616d13f2022-05-12 18:35:55 +0000253 Opt::CreateIdsig { apk, path } => command_create_idsig(service.as_ref(), &apk, &path),
Andrew Walbranea9fa482021-03-04 16:11:12 +0000254 }
255}
256
David Brazdil3c2ddef2021-03-18 13:09:57 +0000257/// Retrieve reference to a previously daemonized VM and stop it.
Andrew Walbran616d13f2022-05-12 18:35:55 +0000258fn command_stop(service: &dyn IVirtualizationService, cid: u32) -> Result<(), Error> {
Andrew Walbran17de24f2021-05-27 13:27:30 +0000259 service
David Brazdil3c2ddef2021-03-18 13:09:57 +0000260 .debugDropVmRef(cid as i32)
Andrew Walbranf6bf6862021-05-21 12:41:13 +0000261 .context("Failed to get VM from VirtualizationService")?
David Brazdil3c2ddef2021-03-18 13:09:57 +0000262 .context("CID does not correspond to a running background VM")?;
Andrew Walbranea9fa482021-03-04 16:11:12 +0000263 Ok(())
264}
265
Andrew Walbran320b5602021-03-04 16:11:12 +0000266/// List the VMs currently running.
Andrew Walbran616d13f2022-05-12 18:35:55 +0000267fn command_list(service: &dyn IVirtualizationService) -> Result<(), Error> {
Andrew Walbran17de24f2021-05-27 13:27:30 +0000268 let vms = service.debugListVms().context("Failed to get list of VMs")?;
Andrew Walbran320b5602021-03-04 16:11:12 +0000269 println!("Running VMs: {:#?}", vms);
270 Ok(())
271}
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000272
273/// Print information about supported VM types.
274fn command_info() -> Result<(), Error> {
275 let unprotected_vm_supported =
276 system_properties::read_bool("ro.boot.hypervisor.vm.supported", false)?;
277 let protected_vm_supported =
278 system_properties::read_bool("ro.boot.hypervisor.protected_vm.supported", false)?;
279 match (unprotected_vm_supported, protected_vm_supported) {
280 (false, false) => println!("VMs are not supported."),
281 (false, true) => println!("Only protected VMs are supported."),
282 (true, false) => println!("Only unprotected VMs are supported."),
283 (true, true) => println!("Both protected and unprotected VMs are supported."),
284 }
285
Andrew Walbran014efb52022-02-03 17:43:11 +0000286 if let Some(version) = system_properties::read("ro.boot.hypervisor.version")? {
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000287 println!("Hypervisor version: {}", version);
288 } else {
289 println!("Hypervisor version not set.");
290 }
291
292 if Path::new("/dev/kvm").exists() {
293 println!("/dev/kvm exists.");
294 } else {
295 println!("/dev/kvm does not exist.");
296 }
297
298 Ok(())
299}