blob: c421b04525787ce60ca680fded287eb0c19809cf [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;
Jooyung Hanc221c052022-02-22 05:20:15 +090027use create_idsig::command_create_idsig;
Jiyong Park48b354d2021-07-15 15:04:38 +090028use create_partition::command_create_partition;
Jooyung Han21e9b922021-06-26 04:14:16 +090029use run::{command_run, command_run_app};
Andrew Walbranc4b1bde2022-02-03 15:26:02 +000030use rustutils::system_properties;
31use std::path::{Path, PathBuf};
David Brazdil20412d92021-03-18 10:53:06 +000032use structopt::clap::AppSettings;
33use structopt::StructOpt;
Andrew Walbranea9fa482021-03-04 16:11:12 +000034
Inseob Kima5a262f2021-11-17 19:41:03 +090035#[derive(Debug)]
36struct Idsigs(Vec<PathBuf>);
37
David Brazdil20412d92021-03-18 10:53:06 +000038#[derive(StructOpt)]
39#[structopt(no_version, global_settings = &[AppSettings::DisableVersion])]
40enum Opt {
Jooyung Han21e9b922021-06-26 04:14:16 +090041 /// Run a virtual machine with a config in APK
42 RunApp {
43 /// Path to VM Payload APK
44 #[structopt(parse(from_os_str))]
45 apk: PathBuf,
46
47 /// Path to idsig of the APK
48 #[structopt(parse(from_os_str))]
49 idsig: PathBuf,
50
Jiyong Park48b354d2021-07-15 15:04:38 +090051 /// Path to the instance image. Created if not exists.
52 #[structopt(parse(from_os_str))]
53 instance: PathBuf,
54
Jooyung Han21e9b922021-06-26 04:14:16 +090055 /// Path to VM config JSON within APK (e.g. assets/vm_config.json)
56 config_path: String,
57
58 /// Detach VM from the terminal and run in the background
59 #[structopt(short, long)]
60 daemonize: bool,
61
Jiyong Parkb8182bb2021-10-26 22:53:08 +090062 /// Path to file for VM console output.
63 #[structopt(long)]
64 console: Option<PathBuf>,
65
Jooyung Han21e9b922021-06-26 04:14:16 +090066 /// Path to file for VM log output.
Jiyong Parkb8182bb2021-10-26 22:53:08 +090067 #[structopt(long)]
Jooyung Han21e9b922021-06-26 04:14:16 +090068 log: Option<PathBuf>,
Jiyong Park23601142021-07-05 13:15:32 +090069
Jiyong Parke558ab12022-07-07 20:18:55 +090070 /// Path to file where ramdump is recorded on kernel panic
71 #[structopt(long)]
72 ramdump: Option<PathBuf>,
73
Jiyong Parkc2a49cc2021-10-15 00:02:12 +090074 /// Debug level of the VM. Supported values: "none" (default), "app_only", and "full".
Jiyong Parkb8182bb2021-10-26 22:53:08 +090075 #[structopt(long, default_value = "none", parse(try_from_str=parse_debug_level))]
Jiyong Parkc2a49cc2021-10-15 00:02:12 +090076 debug: DebugLevel,
Jiyong Parkd63cfff2021-09-27 20:10:17 +090077
Andrew Walbran3994f002022-01-27 17:33:45 +000078 /// Run VM in protected mode.
79 #[structopt(short, long)]
80 protected: bool,
81
Jiyong Parkd63cfff2021-09-27 20:10:17 +090082 /// Memory size (in MiB) of the VM. If unspecified, defaults to the value of `memory_mib`
83 /// in the VM config file.
84 #[structopt(short, long)]
85 mem: Option<u32>,
Inseob Kima5a262f2021-11-17 19:41:03 +090086
Jiyong Park032615f2022-01-10 13:55:34 +090087 /// Number of vCPUs in the VM. If unspecified, defaults to 1.
88 #[structopt(long)]
89 cpus: Option<u32>,
90
91 /// Host CPUs where vCPUs are run on. If unspecified, vCPU runs on any host CPU.
92 #[structopt(long)]
93 cpu_affinity: Option<String>,
94
Jiyong Parkdfe16d62022-04-20 17:32:12 +090095 /// Comma separated list of task profile names to apply to the VM
96 #[structopt(long)]
97 task_profiles: Vec<String>,
98
Inseob Kima5a262f2021-11-17 19:41:03 +090099 /// Paths to extra idsig files.
Victor Hsieh99782572022-01-05 15:38:33 -0800100 #[structopt(long = "extra-idsig")]
Inseob Kima5a262f2021-11-17 19:41:03 +0900101 extra_idsigs: Vec<PathBuf>,
Jooyung Han21e9b922021-06-26 04:14:16 +0900102 },
David Brazdil20412d92021-03-18 10:53:06 +0000103 /// Run a virtual machine
104 Run {
105 /// Path to VM config JSON
106 #[structopt(parse(from_os_str))]
107 config: PathBuf,
David Brazdil3c2ddef2021-03-18 13:09:57 +0000108
109 /// Detach VM from the terminal and run in the background
110 #[structopt(short, long)]
111 daemonize: bool,
Andrew Walbranbe429242021-06-28 12:22:54 +0000112
Jiyong Park032615f2022-01-10 13:55:34 +0900113 /// Number of vCPUs in the VM. If unspecified, defaults to 1.
114 #[structopt(long)]
115 cpus: Option<u32>,
116
117 /// Host CPUs where vCPUs are run on. If unspecified, vCPU runs on any host CPU. The format
118 /// can be either a comma-separated list of CPUs or CPU ranges to run vCPUs on (e.g.
119 /// "0,1-3,5" to choose host CPUs 0, 1, 2, 3, and 5, or a colon-separated list of
120 /// assignments of vCPU-to-host-CPU assignments e.g. "0=0:1=1:2=2" to map vCPU 0 to host
121 /// CPU 0 and so on.
122 #[structopt(long)]
123 cpu_affinity: Option<String>,
124
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900125 /// Comma separated list of task profile names to apply to the VM
126 #[structopt(long)]
127 task_profiles: Vec<String>,
128
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900129 /// Path to file for VM console output.
130 #[structopt(long)]
131 console: Option<PathBuf>,
Jooyung Hanb7983a22022-02-22 05:21:27 +0900132
133 /// Path to file for VM log output.
134 #[structopt(long)]
135 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
149 #[structopt(parse(from_os_str))]
150 path: PathBuf,
151
152 /// The desired size of the partition, in bytes.
153 size: u64,
Jiyong Park9dd389e2021-08-23 20:42:59 +0900154
155 /// Type of the partition
156 #[structopt(short="t", long="type", default_value="raw", parse(try_from_str=parse_partition_type))]
157 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
162 #[structopt(parse(from_os_str))]
163 apk: PathBuf,
164 /// Path to idsig of the APK
165 #[structopt(parse(from_os_str))]
166 path: PathBuf,
167 },
David Brazdil20412d92021-03-18 10:53:06 +0000168}
169
Jiyong Parkc2a49cc2021-10-15 00:02:12 +0900170fn parse_debug_level(s: &str) -> Result<DebugLevel, String> {
171 match s {
172 "none" => Ok(DebugLevel::NONE),
173 "app_only" => Ok(DebugLevel::APP_ONLY),
174 "full" => Ok(DebugLevel::FULL),
175 _ => Err(format!("Invalid debug level {}", s)),
176 }
177}
178
Jiyong Park9dd389e2021-08-23 20:42:59 +0900179fn parse_partition_type(s: &str) -> Result<PartitionType, String> {
180 match s {
181 "raw" => Ok(PartitionType::RAW),
182 "instance" => Ok(PartitionType::ANDROID_VM_INSTANCE),
183 _ => Err(format!("Invalid partition type {}", s)),
184 }
185}
186
Andrew Walbranea9fa482021-03-04 16:11:12 +0000187fn main() -> Result<(), Error> {
188 env_logger::init();
David Brazdil20412d92021-03-18 10:53:06 +0000189 let opt = Opt::from_args();
Andrew Walbranea9fa482021-03-04 16:11:12 +0000190
191 // We need to start the thread pool for Binder to work properly, especially link_to_death.
192 ProcessState::start_thread_pool();
193
Andrew Walbrand0ef4002022-05-16 16:14:10 +0000194 let service = vmclient::connect().context("Failed to find VirtualizationService")?;
Andrew Walbran320b5602021-03-04 16:11:12 +0000195
David Brazdil20412d92021-03-18 10:53:06 +0000196 match opt {
Inseob Kima5a262f2021-11-17 19:41:03 +0900197 Opt::RunApp {
198 apk,
199 idsig,
200 instance,
201 config_path,
202 daemonize,
203 console,
204 log,
Jiyong Parke558ab12022-07-07 20:18:55 +0900205 ramdump,
Inseob Kima5a262f2021-11-17 19:41:03 +0900206 debug,
Andrew Walbran3994f002022-01-27 17:33:45 +0000207 protected,
Inseob Kima5a262f2021-11-17 19:41:03 +0900208 mem,
Jiyong Park032615f2022-01-10 13:55:34 +0900209 cpus,
210 cpu_affinity,
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900211 task_profiles,
Inseob Kima5a262f2021-11-17 19:41:03 +0900212 extra_idsigs,
213 } => command_run_app(
Andrew Walbran616d13f2022-05-12 18:35:55 +0000214 service.as_ref(),
Inseob Kima5a262f2021-11-17 19:41:03 +0900215 &apk,
216 &idsig,
217 &instance,
218 &config_path,
219 daemonize,
220 console.as_deref(),
221 log.as_deref(),
Jiyong Parke558ab12022-07-07 20:18:55 +0900222 ramdump.as_deref(),
Inseob Kima5a262f2021-11-17 19:41:03 +0900223 debug,
Andrew Walbran3994f002022-01-27 17:33:45 +0000224 protected,
Inseob Kima5a262f2021-11-17 19:41:03 +0900225 mem,
Jiyong Park032615f2022-01-10 13:55:34 +0900226 cpus,
227 cpu_affinity,
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900228 task_profiles,
Inseob Kima5a262f2021-11-17 19:41:03 +0900229 &extra_idsigs,
230 ),
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900231 Opt::Run { config, daemonize, cpus, cpu_affinity, task_profiles, console, log } => {
Jiyong Park032615f2022-01-10 13:55:34 +0900232 command_run(
Andrew Walbran616d13f2022-05-12 18:35:55 +0000233 service.as_ref(),
Jiyong Park032615f2022-01-10 13:55:34 +0900234 &config,
235 daemonize,
236 console.as_deref(),
Jooyung Hanb7983a22022-02-22 05:21:27 +0900237 log.as_deref(),
Jiyong Park032615f2022-01-10 13:55:34 +0900238 /* mem */ None,
239 cpus,
240 cpu_affinity,
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900241 task_profiles,
Jiyong Park032615f2022-01-10 13:55:34 +0900242 )
Andrew Walbranbe429242021-06-28 12:22:54 +0000243 }
Andrew Walbran616d13f2022-05-12 18:35:55 +0000244 Opt::Stop { cid } => command_stop(service.as_ref(), cid),
245 Opt::List => command_list(service.as_ref()),
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000246 Opt::Info => command_info(),
Jiyong Park9dd389e2021-08-23 20:42:59 +0900247 Opt::CreatePartition { path, size, partition_type } => {
Andrew Walbran616d13f2022-05-12 18:35:55 +0000248 command_create_partition(service.as_ref(), &path, size, partition_type)
Jiyong Park9dd389e2021-08-23 20:42:59 +0900249 }
Andrew Walbran616d13f2022-05-12 18:35:55 +0000250 Opt::CreateIdsig { apk, path } => command_create_idsig(service.as_ref(), &apk, &path),
Andrew Walbranea9fa482021-03-04 16:11:12 +0000251 }
252}
253
David Brazdil3c2ddef2021-03-18 13:09:57 +0000254/// Retrieve reference to a previously daemonized VM and stop it.
Andrew Walbran616d13f2022-05-12 18:35:55 +0000255fn command_stop(service: &dyn IVirtualizationService, cid: u32) -> Result<(), Error> {
Andrew Walbran17de24f2021-05-27 13:27:30 +0000256 service
David Brazdil3c2ddef2021-03-18 13:09:57 +0000257 .debugDropVmRef(cid as i32)
Andrew Walbranf6bf6862021-05-21 12:41:13 +0000258 .context("Failed to get VM from VirtualizationService")?
David Brazdil3c2ddef2021-03-18 13:09:57 +0000259 .context("CID does not correspond to a running background VM")?;
Andrew Walbranea9fa482021-03-04 16:11:12 +0000260 Ok(())
261}
262
Andrew Walbran320b5602021-03-04 16:11:12 +0000263/// List the VMs currently running.
Andrew Walbran616d13f2022-05-12 18:35:55 +0000264fn command_list(service: &dyn IVirtualizationService) -> Result<(), Error> {
Andrew Walbran17de24f2021-05-27 13:27:30 +0000265 let vms = service.debugListVms().context("Failed to get list of VMs")?;
Andrew Walbran320b5602021-03-04 16:11:12 +0000266 println!("Running VMs: {:#?}", vms);
267 Ok(())
268}
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000269
270/// Print information about supported VM types.
271fn command_info() -> Result<(), Error> {
272 let unprotected_vm_supported =
273 system_properties::read_bool("ro.boot.hypervisor.vm.supported", false)?;
274 let protected_vm_supported =
275 system_properties::read_bool("ro.boot.hypervisor.protected_vm.supported", false)?;
276 match (unprotected_vm_supported, protected_vm_supported) {
277 (false, false) => println!("VMs are not supported."),
278 (false, true) => println!("Only protected VMs are supported."),
279 (true, false) => println!("Only unprotected VMs are supported."),
280 (true, true) => println!("Both protected and unprotected VMs are supported."),
281 }
282
Andrew Walbran014efb52022-02-03 17:43:11 +0000283 if let Some(version) = system_properties::read("ro.boot.hypervisor.version")? {
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000284 println!("Hypervisor version: {}", version);
285 } else {
286 println!("Hypervisor version not set.");
287 }
288
289 if Path::new("/dev/kvm").exists() {
290 println!("/dev/kvm exists.");
291 } else {
292 println!("/dev/kvm does not exist.");
293 }
294
295 Ok(())
296}