blob: 21cc74b95d044696d8659294c35e81e2497d9e2c [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
Jiyong Parkb8182bb2021-10-26 22:53:08 +090061 /// Path to file for VM console output.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070062 #[clap(long)]
Jiyong Parkb8182bb2021-10-26 22:53:08 +090063 console: Option<PathBuf>,
64
Jooyung Han21e9b922021-06-26 04:14:16 +090065 /// Path to file for VM log output.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070066 #[clap(long)]
Jooyung Han21e9b922021-06-26 04:14:16 +090067 log: Option<PathBuf>,
Jiyong Park23601142021-07-05 13:15:32 +090068
Jiyong Parke558ab12022-07-07 20:18:55 +090069 /// Path to file where ramdump is recorded on kernel panic
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070070 #[clap(long)]
Jiyong Parke558ab12022-07-07 20:18:55 +090071 ramdump: Option<PathBuf>,
72
Jiyong Parkc2a49cc2021-10-15 00:02:12 +090073 /// Debug level of the VM. Supported values: "none" (default), "app_only", and "full".
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070074 #[clap(long, default_value = "none", value_parser = parse_debug_level)]
Jiyong Parkc2a49cc2021-10-15 00:02:12 +090075 debug: DebugLevel,
Jiyong Parkd63cfff2021-09-27 20:10:17 +090076
Andrew Walbran3994f002022-01-27 17:33:45 +000077 /// Run VM in protected mode.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070078 #[clap(short, long)]
Andrew Walbran3994f002022-01-27 17:33:45 +000079 protected: bool,
80
Jiyong Parkd63cfff2021-09-27 20:10:17 +090081 /// Memory size (in MiB) of the VM. If unspecified, defaults to the value of `memory_mib`
82 /// in the VM config file.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070083 #[clap(short, long)]
Jiyong Parkd63cfff2021-09-27 20:10:17 +090084 mem: Option<u32>,
Inseob Kima5a262f2021-11-17 19:41:03 +090085
Jiyong Park032615f2022-01-10 13:55:34 +090086 /// Number of vCPUs in the VM. If unspecified, defaults to 1.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070087 #[clap(long)]
Jiyong Park032615f2022-01-10 13:55:34 +090088 cpus: Option<u32>,
89
Jiyong Parkdfe16d62022-04-20 17:32:12 +090090 /// Comma separated list of task profile names to apply to the VM
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070091 #[clap(long)]
Jiyong Parkdfe16d62022-04-20 17:32:12 +090092 task_profiles: Vec<String>,
93
Inseob Kima5a262f2021-11-17 19:41:03 +090094 /// Paths to extra idsig files.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -070095 #[clap(long = "extra-idsig")]
Inseob Kima5a262f2021-11-17 19:41:03 +090096 extra_idsigs: Vec<PathBuf>,
Jooyung Han21e9b922021-06-26 04:14:16 +090097 },
David Brazdil20412d92021-03-18 10:53:06 +000098 /// Run a virtual machine
99 Run {
100 /// Path to VM config JSON
David Brazdil20412d92021-03-18 10:53:06 +0000101 config: PathBuf,
David Brazdil3c2ddef2021-03-18 13:09:57 +0000102
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700103 /// Name of VM
104 #[clap(long)]
105 name: Option<String>,
106
David Brazdil3c2ddef2021-03-18 13:09:57 +0000107 /// Detach VM from the terminal and run in the background
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700108 #[clap(short, long)]
David Brazdil3c2ddef2021-03-18 13:09:57 +0000109 daemonize: bool,
Andrew Walbranbe429242021-06-28 12:22:54 +0000110
Jiyong Park032615f2022-01-10 13:55:34 +0900111 /// Number of vCPUs in the VM. If unspecified, defaults to 1.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700112 #[clap(long)]
Jiyong Park032615f2022-01-10 13:55:34 +0900113 cpus: Option<u32>,
114
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900115 /// Comma separated list of task profile names to apply to the VM
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700116 #[clap(long)]
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900117 task_profiles: Vec<String>,
118
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900119 /// Path to file for VM console output.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700120 #[clap(long)]
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900121 console: Option<PathBuf>,
Jooyung Hanb7983a22022-02-22 05:21:27 +0900122
123 /// Path to file for VM log output.
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700124 #[clap(long)]
Jooyung Hanb7983a22022-02-22 05:21:27 +0900125 log: Option<PathBuf>,
David Brazdil3c2ddef2021-03-18 13:09:57 +0000126 },
127 /// Stop a virtual machine running in the background
128 Stop {
129 /// CID of the virtual machine
130 cid: u32,
David Brazdil20412d92021-03-18 10:53:06 +0000131 },
132 /// List running virtual machines
133 List,
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000134 /// Print information about virtual machine support
135 Info,
Andrew Walbrandff3b942021-06-09 15:20:36 +0000136 /// Create a new empty partition to be used as a writable partition for a VM
137 CreatePartition {
138 /// Path at which to create the image file
Andrew Walbrandff3b942021-06-09 15:20:36 +0000139 path: PathBuf,
140
141 /// The desired size of the partition, in bytes.
142 size: u64,
Jiyong Park9dd389e2021-08-23 20:42:59 +0900143
144 /// Type of the partition
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700145 #[clap(short = 't', long = "type", default_value = "raw",
146 value_parser = parse_partition_type)]
Jiyong Park9dd389e2021-08-23 20:42:59 +0900147 partition_type: PartitionType,
Andrew Walbrandff3b942021-06-09 15:20:36 +0000148 },
Jooyung Hanc221c052022-02-22 05:20:15 +0900149 /// Creates or update the idsig file by digesting the input APK file.
150 CreateIdsig {
151 /// Path to VM Payload APK
Jooyung Hanc221c052022-02-22 05:20:15 +0900152 apk: PathBuf,
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700153
Jooyung Hanc221c052022-02-22 05:20:15 +0900154 /// Path to idsig of the APK
Jooyung Hanc221c052022-02-22 05:20:15 +0900155 path: PathBuf,
156 },
David Brazdil20412d92021-03-18 10:53:06 +0000157}
158
Jiyong Parkc2a49cc2021-10-15 00:02:12 +0900159fn parse_debug_level(s: &str) -> Result<DebugLevel, String> {
160 match s {
161 "none" => Ok(DebugLevel::NONE),
162 "app_only" => Ok(DebugLevel::APP_ONLY),
163 "full" => Ok(DebugLevel::FULL),
164 _ => Err(format!("Invalid debug level {}", s)),
165 }
166}
167
Jiyong Park9dd389e2021-08-23 20:42:59 +0900168fn parse_partition_type(s: &str) -> Result<PartitionType, String> {
169 match s {
170 "raw" => Ok(PartitionType::RAW),
171 "instance" => Ok(PartitionType::ANDROID_VM_INSTANCE),
172 _ => Err(format!("Invalid partition type {}", s)),
173 }
174}
175
Andrew Walbranea9fa482021-03-04 16:11:12 +0000176fn main() -> Result<(), Error> {
177 env_logger::init();
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700178 let opt = Opt::parse();
Andrew Walbranea9fa482021-03-04 16:11:12 +0000179
180 // We need to start the thread pool for Binder to work properly, especially link_to_death.
181 ProcessState::start_thread_pool();
182
Andrew Walbrand0ef4002022-05-16 16:14:10 +0000183 let service = vmclient::connect().context("Failed to find VirtualizationService")?;
Andrew Walbran320b5602021-03-04 16:11:12 +0000184
David Brazdil20412d92021-03-18 10:53:06 +0000185 match opt {
Inseob Kima5a262f2021-11-17 19:41:03 +0900186 Opt::RunApp {
Seungjae Yoo62085c02022-08-12 04:44:52 +0000187 name,
Inseob Kima5a262f2021-11-17 19:41:03 +0900188 apk,
189 idsig,
190 instance,
191 config_path,
192 daemonize,
193 console,
194 log,
Jiyong Parke558ab12022-07-07 20:18:55 +0900195 ramdump,
Inseob Kima5a262f2021-11-17 19:41:03 +0900196 debug,
Andrew Walbran3994f002022-01-27 17:33:45 +0000197 protected,
Inseob Kima5a262f2021-11-17 19:41:03 +0900198 mem,
Jiyong Park032615f2022-01-10 13:55:34 +0900199 cpus,
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900200 task_profiles,
Inseob Kima5a262f2021-11-17 19:41:03 +0900201 extra_idsigs,
202 } => command_run_app(
Seungjae Yoo62085c02022-08-12 04:44:52 +0000203 name,
Andrew Walbran616d13f2022-05-12 18:35:55 +0000204 service.as_ref(),
Inseob Kima5a262f2021-11-17 19:41:03 +0900205 &apk,
206 &idsig,
207 &instance,
Alan Stokes0d1ef782022-09-27 13:46:35 +0100208 config_path.as_deref().unwrap_or(""),
Inseob Kima5a262f2021-11-17 19:41:03 +0900209 daemonize,
210 console.as_deref(),
211 log.as_deref(),
Jiyong Parke558ab12022-07-07 20:18:55 +0900212 ramdump.as_deref(),
Inseob Kima5a262f2021-11-17 19:41:03 +0900213 debug,
Andrew Walbran3994f002022-01-27 17:33:45 +0000214 protected,
Inseob Kima5a262f2021-11-17 19:41:03 +0900215 mem,
Jiyong Park032615f2022-01-10 13:55:34 +0900216 cpus,
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900217 task_profiles,
Inseob Kima5a262f2021-11-17 19:41:03 +0900218 &extra_idsigs,
219 ),
Victor Hsiehf219cd82022-09-09 13:13:11 -0700220 Opt::Run { name, config, daemonize, cpus, task_profiles, console, log } => {
Jiyong Park032615f2022-01-10 13:55:34 +0900221 command_run(
Seungjae Yoo62085c02022-08-12 04:44:52 +0000222 name,
Andrew Walbran616d13f2022-05-12 18:35:55 +0000223 service.as_ref(),
Jiyong Park032615f2022-01-10 13:55:34 +0900224 &config,
225 daemonize,
226 console.as_deref(),
Jooyung Hanb7983a22022-02-22 05:21:27 +0900227 log.as_deref(),
Jiyong Park032615f2022-01-10 13:55:34 +0900228 /* mem */ None,
229 cpus,
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900230 task_profiles,
Jiyong Park032615f2022-01-10 13:55:34 +0900231 )
Andrew Walbranbe429242021-06-28 12:22:54 +0000232 }
Andrew Walbran616d13f2022-05-12 18:35:55 +0000233 Opt::Stop { cid } => command_stop(service.as_ref(), cid),
234 Opt::List => command_list(service.as_ref()),
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000235 Opt::Info => command_info(),
Jiyong Park9dd389e2021-08-23 20:42:59 +0900236 Opt::CreatePartition { path, size, partition_type } => {
Andrew Walbran616d13f2022-05-12 18:35:55 +0000237 command_create_partition(service.as_ref(), &path, size, partition_type)
Jiyong Park9dd389e2021-08-23 20:42:59 +0900238 }
Andrew Walbran616d13f2022-05-12 18:35:55 +0000239 Opt::CreateIdsig { apk, path } => command_create_idsig(service.as_ref(), &apk, &path),
Andrew Walbranea9fa482021-03-04 16:11:12 +0000240 }
241}
242
David Brazdil3c2ddef2021-03-18 13:09:57 +0000243/// Retrieve reference to a previously daemonized VM and stop it.
Andrew Walbran616d13f2022-05-12 18:35:55 +0000244fn command_stop(service: &dyn IVirtualizationService, cid: u32) -> Result<(), Error> {
Andrew Walbran17de24f2021-05-27 13:27:30 +0000245 service
David Brazdil3c2ddef2021-03-18 13:09:57 +0000246 .debugDropVmRef(cid as i32)
Andrew Walbranf6bf6862021-05-21 12:41:13 +0000247 .context("Failed to get VM from VirtualizationService")?
David Brazdil3c2ddef2021-03-18 13:09:57 +0000248 .context("CID does not correspond to a running background VM")?;
Andrew Walbranea9fa482021-03-04 16:11:12 +0000249 Ok(())
250}
251
Andrew Walbran320b5602021-03-04 16:11:12 +0000252/// List the VMs currently running.
Andrew Walbran616d13f2022-05-12 18:35:55 +0000253fn command_list(service: &dyn IVirtualizationService) -> Result<(), Error> {
Andrew Walbran17de24f2021-05-27 13:27:30 +0000254 let vms = service.debugListVms().context("Failed to get list of VMs")?;
Andrew Walbran320b5602021-03-04 16:11:12 +0000255 println!("Running VMs: {:#?}", vms);
256 Ok(())
257}
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000258
259/// Print information about supported VM types.
260fn command_info() -> Result<(), Error> {
261 let unprotected_vm_supported =
262 system_properties::read_bool("ro.boot.hypervisor.vm.supported", false)?;
263 let protected_vm_supported =
264 system_properties::read_bool("ro.boot.hypervisor.protected_vm.supported", false)?;
265 match (unprotected_vm_supported, protected_vm_supported) {
266 (false, false) => println!("VMs are not supported."),
267 (false, true) => println!("Only protected VMs are supported."),
268 (true, false) => println!("Only unprotected VMs are supported."),
269 (true, true) => println!("Both protected and unprotected VMs are supported."),
270 }
271
Andrew Walbran014efb52022-02-03 17:43:11 +0000272 if let Some(version) = system_properties::read("ro.boot.hypervisor.version")? {
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000273 println!("Hypervisor version: {}", version);
274 } else {
275 println!("Hypervisor version not set.");
276 }
277
278 if Path::new("/dev/kvm").exists() {
279 println!("/dev/kvm exists.");
280 } else {
281 println!("/dev/kvm does not exist.");
282 }
283
284 Ok(())
285}