blob: 25f9bfb2f3ebb85b7b9adff6a6a97aa99f09b1f1 [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
Jiyong Park48b354d2021-07-15 15:04:38 +090017mod create_partition;
Andrew Walbranf395b822021-05-05 10:38:59 +000018mod run;
Andrew Walbranea9fa482021-03-04 16:11:12 +000019mod sync;
20
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};
Jiyong Park48b354d2021-07-15 15:04:38 +090025use android_system_virtualizationservice::binder::{wait_for_interface, ProcessState, Strong};
David Brazdil20412d92021-03-18 10:53:06 +000026use anyhow::{Context, Error};
Jiyong Park48b354d2021-07-15 15:04:38 +090027use create_partition::command_create_partition;
Jooyung Han21e9b922021-06-26 04:14:16 +090028use run::{command_run, command_run_app};
Jiyong Park48b354d2021-07-15 15:04:38 +090029use std::path::PathBuf;
David Brazdil20412d92021-03-18 10:53:06 +000030use structopt::clap::AppSettings;
31use structopt::StructOpt;
Andrew Walbranea9fa482021-03-04 16:11:12 +000032
Andrew Walbran17de24f2021-05-27 13:27:30 +000033const VIRTUALIZATION_SERVICE_BINDER_SERVICE_IDENTIFIER: &str =
34 "android.system.virtualizationservice";
Andrew Walbranea9fa482021-03-04 16:11:12 +000035
Inseob Kima5a262f2021-11-17 19:41:03 +090036#[derive(Debug)]
37struct Idsigs(Vec<PathBuf>);
38
David Brazdil20412d92021-03-18 10:53:06 +000039#[derive(StructOpt)]
40#[structopt(no_version, global_settings = &[AppSettings::DisableVersion])]
41enum Opt {
Jooyung Han21e9b922021-06-26 04:14:16 +090042 /// Run a virtual machine with a config in APK
43 RunApp {
44 /// Path to VM Payload APK
45 #[structopt(parse(from_os_str))]
46 apk: PathBuf,
47
48 /// Path to idsig of the APK
49 #[structopt(parse(from_os_str))]
50 idsig: PathBuf,
51
Jiyong Park48b354d2021-07-15 15:04:38 +090052 /// Path to the instance image. Created if not exists.
53 #[structopt(parse(from_os_str))]
54 instance: PathBuf,
55
Jooyung Han21e9b922021-06-26 04:14:16 +090056 /// Path to VM config JSON within APK (e.g. assets/vm_config.json)
57 config_path: String,
58
59 /// Detach VM from the terminal and run in the background
60 #[structopt(short, long)]
61 daemonize: bool,
62
Jiyong Parkb8182bb2021-10-26 22:53:08 +090063 /// Path to file for VM console output.
64 #[structopt(long)]
65 console: Option<PathBuf>,
66
Jooyung Han21e9b922021-06-26 04:14:16 +090067 /// Path to file for VM log output.
Jiyong Parkb8182bb2021-10-26 22:53:08 +090068 #[structopt(long)]
Jooyung Han21e9b922021-06-26 04:14:16 +090069 log: Option<PathBuf>,
Jiyong Park23601142021-07-05 13:15:32 +090070
Jiyong Parkc2a49cc2021-10-15 00:02:12 +090071 /// Debug level of the VM. Supported values: "none" (default), "app_only", and "full".
Jiyong Parkb8182bb2021-10-26 22:53:08 +090072 #[structopt(long, default_value = "none", parse(try_from_str=parse_debug_level))]
Jiyong Parkc2a49cc2021-10-15 00:02:12 +090073 debug: DebugLevel,
Jiyong Parkd63cfff2021-09-27 20:10:17 +090074
Andrew Walbran3994f002022-01-27 17:33:45 +000075 /// Run VM in protected mode.
76 #[structopt(short, long)]
77 protected: bool,
78
Jiyong Parkd63cfff2021-09-27 20:10:17 +090079 /// Memory size (in MiB) of the VM. If unspecified, defaults to the value of `memory_mib`
80 /// in the VM config file.
81 #[structopt(short, long)]
82 mem: Option<u32>,
Inseob Kima5a262f2021-11-17 19:41:03 +090083
Jiyong Park032615f2022-01-10 13:55:34 +090084 /// Number of vCPUs in the VM. If unspecified, defaults to 1.
85 #[structopt(long)]
86 cpus: Option<u32>,
87
88 /// Host CPUs where vCPUs are run on. If unspecified, vCPU runs on any host CPU.
89 #[structopt(long)]
90 cpu_affinity: Option<String>,
91
Inseob Kima5a262f2021-11-17 19:41:03 +090092 /// Paths to extra idsig files.
Victor Hsieh99782572022-01-05 15:38:33 -080093 #[structopt(long = "extra-idsig")]
Inseob Kima5a262f2021-11-17 19:41:03 +090094 extra_idsigs: Vec<PathBuf>,
Jooyung Han21e9b922021-06-26 04:14:16 +090095 },
David Brazdil20412d92021-03-18 10:53:06 +000096 /// Run a virtual machine
97 Run {
98 /// Path to VM config JSON
99 #[structopt(parse(from_os_str))]
100 config: PathBuf,
David Brazdil3c2ddef2021-03-18 13:09:57 +0000101
102 /// Detach VM from the terminal and run in the background
103 #[structopt(short, long)]
104 daemonize: bool,
Andrew Walbranbe429242021-06-28 12:22:54 +0000105
Jiyong Park032615f2022-01-10 13:55:34 +0900106 /// Number of vCPUs in the VM. If unspecified, defaults to 1.
107 #[structopt(long)]
108 cpus: Option<u32>,
109
110 /// Host CPUs where vCPUs are run on. If unspecified, vCPU runs on any host CPU. The format
111 /// can be either a comma-separated list of CPUs or CPU ranges to run vCPUs on (e.g.
112 /// "0,1-3,5" to choose host CPUs 0, 1, 2, 3, and 5, or a colon-separated list of
113 /// assignments of vCPU-to-host-CPU assignments e.g. "0=0:1=1:2=2" to map vCPU 0 to host
114 /// CPU 0 and so on.
115 #[structopt(long)]
116 cpu_affinity: Option<String>,
117
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900118 /// Path to file for VM console output.
119 #[structopt(long)]
120 console: Option<PathBuf>,
David Brazdil3c2ddef2021-03-18 13:09:57 +0000121 },
122 /// Stop a virtual machine running in the background
123 Stop {
124 /// CID of the virtual machine
125 cid: u32,
David Brazdil20412d92021-03-18 10:53:06 +0000126 },
127 /// List running virtual machines
128 List,
Andrew Walbrandff3b942021-06-09 15:20:36 +0000129 /// Create a new empty partition to be used as a writable partition for a VM
130 CreatePartition {
131 /// Path at which to create the image file
132 #[structopt(parse(from_os_str))]
133 path: PathBuf,
134
135 /// The desired size of the partition, in bytes.
136 size: u64,
Jiyong Park9dd389e2021-08-23 20:42:59 +0900137
138 /// Type of the partition
139 #[structopt(short="t", long="type", default_value="raw", parse(try_from_str=parse_partition_type))]
140 partition_type: PartitionType,
Andrew Walbrandff3b942021-06-09 15:20:36 +0000141 },
David Brazdil20412d92021-03-18 10:53:06 +0000142}
143
Jiyong Parkc2a49cc2021-10-15 00:02:12 +0900144fn parse_debug_level(s: &str) -> Result<DebugLevel, String> {
145 match s {
146 "none" => Ok(DebugLevel::NONE),
147 "app_only" => Ok(DebugLevel::APP_ONLY),
148 "full" => Ok(DebugLevel::FULL),
149 _ => Err(format!("Invalid debug level {}", s)),
150 }
151}
152
Jiyong Park9dd389e2021-08-23 20:42:59 +0900153fn parse_partition_type(s: &str) -> Result<PartitionType, String> {
154 match s {
155 "raw" => Ok(PartitionType::RAW),
156 "instance" => Ok(PartitionType::ANDROID_VM_INSTANCE),
157 _ => Err(format!("Invalid partition type {}", s)),
158 }
159}
160
Andrew Walbranea9fa482021-03-04 16:11:12 +0000161fn main() -> Result<(), Error> {
162 env_logger::init();
David Brazdil20412d92021-03-18 10:53:06 +0000163 let opt = Opt::from_args();
Andrew Walbranea9fa482021-03-04 16:11:12 +0000164
165 // We need to start the thread pool for Binder to work properly, especially link_to_death.
166 ProcessState::start_thread_pool();
167
Andrew Walbranf1453802021-03-29 17:12:54 +0000168 let service = wait_for_interface(VIRTUALIZATION_SERVICE_BINDER_SERVICE_IDENTIFIER)
Andrew Walbranf6bf6862021-05-21 12:41:13 +0000169 .context("Failed to find VirtualizationService")?;
Andrew Walbran320b5602021-03-04 16:11:12 +0000170
David Brazdil20412d92021-03-18 10:53:06 +0000171 match opt {
Inseob Kima5a262f2021-11-17 19:41:03 +0900172 Opt::RunApp {
173 apk,
174 idsig,
175 instance,
176 config_path,
177 daemonize,
178 console,
179 log,
180 debug,
Andrew Walbran3994f002022-01-27 17:33:45 +0000181 protected,
Inseob Kima5a262f2021-11-17 19:41:03 +0900182 mem,
Jiyong Park032615f2022-01-10 13:55:34 +0900183 cpus,
184 cpu_affinity,
Inseob Kima5a262f2021-11-17 19:41:03 +0900185 extra_idsigs,
186 } => command_run_app(
187 service,
188 &apk,
189 &idsig,
190 &instance,
191 &config_path,
192 daemonize,
193 console.as_deref(),
194 log.as_deref(),
195 debug,
Andrew Walbran3994f002022-01-27 17:33:45 +0000196 protected,
Inseob Kima5a262f2021-11-17 19:41:03 +0900197 mem,
Jiyong Park032615f2022-01-10 13:55:34 +0900198 cpus,
199 cpu_affinity,
Inseob Kima5a262f2021-11-17 19:41:03 +0900200 &extra_idsigs,
201 ),
Jiyong Park032615f2022-01-10 13:55:34 +0900202 Opt::Run { config, daemonize, cpus, cpu_affinity, console } => {
203 command_run(
204 service,
205 &config,
206 daemonize,
207 console.as_deref(),
208 /* mem */ None,
209 cpus,
210 cpu_affinity,
211 )
Andrew Walbranbe429242021-06-28 12:22:54 +0000212 }
Andrew Walbran17de24f2021-05-27 13:27:30 +0000213 Opt::Stop { cid } => command_stop(service, cid),
214 Opt::List => command_list(service),
Jiyong Park9dd389e2021-08-23 20:42:59 +0900215 Opt::CreatePartition { path, size, partition_type } => {
216 command_create_partition(service, &path, size, partition_type)
217 }
Andrew Walbranea9fa482021-03-04 16:11:12 +0000218 }
219}
220
David Brazdil3c2ddef2021-03-18 13:09:57 +0000221/// Retrieve reference to a previously daemonized VM and stop it.
Andrew Walbran17de24f2021-05-27 13:27:30 +0000222fn command_stop(service: Strong<dyn IVirtualizationService>, cid: u32) -> Result<(), Error> {
223 service
David Brazdil3c2ddef2021-03-18 13:09:57 +0000224 .debugDropVmRef(cid as i32)
Andrew Walbranf6bf6862021-05-21 12:41:13 +0000225 .context("Failed to get VM from VirtualizationService")?
David Brazdil3c2ddef2021-03-18 13:09:57 +0000226 .context("CID does not correspond to a running background VM")?;
Andrew Walbranea9fa482021-03-04 16:11:12 +0000227 Ok(())
228}
229
Andrew Walbran320b5602021-03-04 16:11:12 +0000230/// List the VMs currently running.
Andrew Walbran17de24f2021-05-27 13:27:30 +0000231fn command_list(service: Strong<dyn IVirtualizationService>) -> Result<(), Error> {
232 let vms = service.debugListVms().context("Failed to get list of VMs")?;
Andrew Walbran320b5602021-03-04 16:11:12 +0000233 println!("Running VMs: {:#?}", vms);
234 Ok(())
235}