blob: d53305bb90bf3f63b2e8bae0c5200e856b548958 [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
75 /// Memory size (in MiB) of the VM. If unspecified, defaults to the value of `memory_mib`
76 /// in the VM config file.
77 #[structopt(short, long)]
78 mem: Option<u32>,
Inseob Kima5a262f2021-11-17 19:41:03 +090079
80 /// Paths to extra idsig files.
81 #[structopt(long)]
82 extra_idsigs: Vec<PathBuf>,
Jooyung Han21e9b922021-06-26 04:14:16 +090083 },
David Brazdil20412d92021-03-18 10:53:06 +000084 /// Run a virtual machine
85 Run {
86 /// Path to VM config JSON
87 #[structopt(parse(from_os_str))]
88 config: PathBuf,
David Brazdil3c2ddef2021-03-18 13:09:57 +000089
90 /// Detach VM from the terminal and run in the background
91 #[structopt(short, long)]
92 daemonize: bool,
Andrew Walbranbe429242021-06-28 12:22:54 +000093
Jiyong Parkb8182bb2021-10-26 22:53:08 +090094 /// Path to file for VM console output.
95 #[structopt(long)]
96 console: Option<PathBuf>,
David Brazdil3c2ddef2021-03-18 13:09:57 +000097 },
98 /// Stop a virtual machine running in the background
99 Stop {
100 /// CID of the virtual machine
101 cid: u32,
David Brazdil20412d92021-03-18 10:53:06 +0000102 },
103 /// List running virtual machines
104 List,
Andrew Walbrandff3b942021-06-09 15:20:36 +0000105 /// Create a new empty partition to be used as a writable partition for a VM
106 CreatePartition {
107 /// Path at which to create the image file
108 #[structopt(parse(from_os_str))]
109 path: PathBuf,
110
111 /// The desired size of the partition, in bytes.
112 size: u64,
Jiyong Park9dd389e2021-08-23 20:42:59 +0900113
114 /// Type of the partition
115 #[structopt(short="t", long="type", default_value="raw", parse(try_from_str=parse_partition_type))]
116 partition_type: PartitionType,
Andrew Walbrandff3b942021-06-09 15:20:36 +0000117 },
David Brazdil20412d92021-03-18 10:53:06 +0000118}
119
Jiyong Parkc2a49cc2021-10-15 00:02:12 +0900120fn parse_debug_level(s: &str) -> Result<DebugLevel, String> {
121 match s {
122 "none" => Ok(DebugLevel::NONE),
123 "app_only" => Ok(DebugLevel::APP_ONLY),
124 "full" => Ok(DebugLevel::FULL),
125 _ => Err(format!("Invalid debug level {}", s)),
126 }
127}
128
Jiyong Park9dd389e2021-08-23 20:42:59 +0900129fn parse_partition_type(s: &str) -> Result<PartitionType, String> {
130 match s {
131 "raw" => Ok(PartitionType::RAW),
132 "instance" => Ok(PartitionType::ANDROID_VM_INSTANCE),
133 _ => Err(format!("Invalid partition type {}", s)),
134 }
135}
136
Andrew Walbranea9fa482021-03-04 16:11:12 +0000137fn main() -> Result<(), Error> {
138 env_logger::init();
David Brazdil20412d92021-03-18 10:53:06 +0000139 let opt = Opt::from_args();
Andrew Walbranea9fa482021-03-04 16:11:12 +0000140
141 // We need to start the thread pool for Binder to work properly, especially link_to_death.
142 ProcessState::start_thread_pool();
143
Andrew Walbranf1453802021-03-29 17:12:54 +0000144 let service = wait_for_interface(VIRTUALIZATION_SERVICE_BINDER_SERVICE_IDENTIFIER)
Andrew Walbranf6bf6862021-05-21 12:41:13 +0000145 .context("Failed to find VirtualizationService")?;
Andrew Walbran320b5602021-03-04 16:11:12 +0000146
David Brazdil20412d92021-03-18 10:53:06 +0000147 match opt {
Inseob Kima5a262f2021-11-17 19:41:03 +0900148 Opt::RunApp {
149 apk,
150 idsig,
151 instance,
152 config_path,
153 daemonize,
154 console,
155 log,
156 debug,
157 mem,
158 extra_idsigs,
159 } => command_run_app(
160 service,
161 &apk,
162 &idsig,
163 &instance,
164 &config_path,
165 daemonize,
166 console.as_deref(),
167 log.as_deref(),
168 debug,
169 mem,
170 &extra_idsigs,
171 ),
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900172 Opt::Run { config, daemonize, console } => {
173 command_run(service, &config, daemonize, console.as_deref(), /* mem */ None)
Andrew Walbranbe429242021-06-28 12:22:54 +0000174 }
Andrew Walbran17de24f2021-05-27 13:27:30 +0000175 Opt::Stop { cid } => command_stop(service, cid),
176 Opt::List => command_list(service),
Jiyong Park9dd389e2021-08-23 20:42:59 +0900177 Opt::CreatePartition { path, size, partition_type } => {
178 command_create_partition(service, &path, size, partition_type)
179 }
Andrew Walbranea9fa482021-03-04 16:11:12 +0000180 }
181}
182
David Brazdil3c2ddef2021-03-18 13:09:57 +0000183/// Retrieve reference to a previously daemonized VM and stop it.
Andrew Walbran17de24f2021-05-27 13:27:30 +0000184fn command_stop(service: Strong<dyn IVirtualizationService>, cid: u32) -> Result<(), Error> {
185 service
David Brazdil3c2ddef2021-03-18 13:09:57 +0000186 .debugDropVmRef(cid as i32)
Andrew Walbranf6bf6862021-05-21 12:41:13 +0000187 .context("Failed to get VM from VirtualizationService")?
David Brazdil3c2ddef2021-03-18 13:09:57 +0000188 .context("CID does not correspond to a running background VM")?;
Andrew Walbranea9fa482021-03-04 16:11:12 +0000189 Ok(())
190}
191
Andrew Walbran320b5602021-03-04 16:11:12 +0000192/// List the VMs currently running.
Andrew Walbran17de24f2021-05-27 13:27:30 +0000193fn command_list(service: Strong<dyn IVirtualizationService>) -> Result<(), Error> {
194 let vms = service.debugListVms().context("Failed to get list of VMs")?;
Andrew Walbran320b5602021-03-04 16:11:12 +0000195 println!("Running VMs: {:#?}", vms);
196 Ok(())
197}