Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 1 | // 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 Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 17 | mod create_partition; |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 18 | mod run; |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 19 | mod sync; |
| 20 | |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame] | 21 | use android_system_virtualizationservice::aidl::android::system::virtualizationservice::{ |
| 22 | IVirtualizationService::IVirtualizationService, PartitionType::PartitionType, |
| 23 | VirtualMachineAppConfig::DebugLevel::DebugLevel, |
| 24 | }; |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 25 | use android_system_virtualizationservice::binder::{wait_for_interface, ProcessState, Strong}; |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 26 | use anyhow::{Context, Error}; |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 27 | use create_partition::command_create_partition; |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 28 | use run::{command_run, command_run_app}; |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 29 | use std::path::PathBuf; |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 30 | use structopt::clap::AppSettings; |
| 31 | use structopt::StructOpt; |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 32 | |
Andrew Walbran | 17de24f | 2021-05-27 13:27:30 +0000 | [diff] [blame] | 33 | const VIRTUALIZATION_SERVICE_BINDER_SERVICE_IDENTIFIER: &str = |
| 34 | "android.system.virtualizationservice"; |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 35 | |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 36 | #[derive(Debug)] |
| 37 | struct Idsigs(Vec<PathBuf>); |
| 38 | |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 39 | #[derive(StructOpt)] |
| 40 | #[structopt(no_version, global_settings = &[AppSettings::DisableVersion])] |
| 41 | enum Opt { |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 42 | /// 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 Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 52 | /// Path to the instance image. Created if not exists. |
| 53 | #[structopt(parse(from_os_str))] |
| 54 | instance: PathBuf, |
| 55 | |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 56 | /// 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 Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 63 | /// Path to file for VM console output. |
| 64 | #[structopt(long)] |
| 65 | console: Option<PathBuf>, |
| 66 | |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 67 | /// Path to file for VM log output. |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 68 | #[structopt(long)] |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 69 | log: Option<PathBuf>, |
Jiyong Park | 2360114 | 2021-07-05 13:15:32 +0900 | [diff] [blame] | 70 | |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame] | 71 | /// Debug level of the VM. Supported values: "none" (default), "app_only", and "full". |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 72 | #[structopt(long, default_value = "none", parse(try_from_str=parse_debug_level))] |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame] | 73 | debug: DebugLevel, |
Jiyong Park | d63cfff | 2021-09-27 20:10:17 +0900 | [diff] [blame] | 74 | |
| 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 Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 79 | |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 80 | /// Number of vCPUs in the VM. If unspecified, defaults to 1. |
| 81 | #[structopt(long)] |
| 82 | cpus: Option<u32>, |
| 83 | |
| 84 | /// Host CPUs where vCPUs are run on. If unspecified, vCPU runs on any host CPU. |
| 85 | #[structopt(long)] |
| 86 | cpu_affinity: Option<String>, |
| 87 | |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 88 | /// Paths to extra idsig files. |
Victor Hsieh | 9978257 | 2022-01-05 15:38:33 -0800 | [diff] [blame^] | 89 | #[structopt(long = "extra-idsig")] |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 90 | extra_idsigs: Vec<PathBuf>, |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 91 | }, |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 92 | /// Run a virtual machine |
| 93 | Run { |
| 94 | /// Path to VM config JSON |
| 95 | #[structopt(parse(from_os_str))] |
| 96 | config: PathBuf, |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 97 | |
| 98 | /// Detach VM from the terminal and run in the background |
| 99 | #[structopt(short, long)] |
| 100 | daemonize: bool, |
Andrew Walbran | be42924 | 2021-06-28 12:22:54 +0000 | [diff] [blame] | 101 | |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 102 | /// Number of vCPUs in the VM. If unspecified, defaults to 1. |
| 103 | #[structopt(long)] |
| 104 | cpus: Option<u32>, |
| 105 | |
| 106 | /// Host CPUs where vCPUs are run on. If unspecified, vCPU runs on any host CPU. The format |
| 107 | /// can be either a comma-separated list of CPUs or CPU ranges to run vCPUs on (e.g. |
| 108 | /// "0,1-3,5" to choose host CPUs 0, 1, 2, 3, and 5, or a colon-separated list of |
| 109 | /// assignments of vCPU-to-host-CPU assignments e.g. "0=0:1=1:2=2" to map vCPU 0 to host |
| 110 | /// CPU 0 and so on. |
| 111 | #[structopt(long)] |
| 112 | cpu_affinity: Option<String>, |
| 113 | |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 114 | /// Path to file for VM console output. |
| 115 | #[structopt(long)] |
| 116 | console: Option<PathBuf>, |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 117 | }, |
| 118 | /// Stop a virtual machine running in the background |
| 119 | Stop { |
| 120 | /// CID of the virtual machine |
| 121 | cid: u32, |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 122 | }, |
| 123 | /// List running virtual machines |
| 124 | List, |
Andrew Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 125 | /// Create a new empty partition to be used as a writable partition for a VM |
| 126 | CreatePartition { |
| 127 | /// Path at which to create the image file |
| 128 | #[structopt(parse(from_os_str))] |
| 129 | path: PathBuf, |
| 130 | |
| 131 | /// The desired size of the partition, in bytes. |
| 132 | size: u64, |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 133 | |
| 134 | /// Type of the partition |
| 135 | #[structopt(short="t", long="type", default_value="raw", parse(try_from_str=parse_partition_type))] |
| 136 | partition_type: PartitionType, |
Andrew Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 137 | }, |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame] | 140 | fn parse_debug_level(s: &str) -> Result<DebugLevel, String> { |
| 141 | match s { |
| 142 | "none" => Ok(DebugLevel::NONE), |
| 143 | "app_only" => Ok(DebugLevel::APP_ONLY), |
| 144 | "full" => Ok(DebugLevel::FULL), |
| 145 | _ => Err(format!("Invalid debug level {}", s)), |
| 146 | } |
| 147 | } |
| 148 | |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 149 | fn parse_partition_type(s: &str) -> Result<PartitionType, String> { |
| 150 | match s { |
| 151 | "raw" => Ok(PartitionType::RAW), |
| 152 | "instance" => Ok(PartitionType::ANDROID_VM_INSTANCE), |
| 153 | _ => Err(format!("Invalid partition type {}", s)), |
| 154 | } |
| 155 | } |
| 156 | |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 157 | fn main() -> Result<(), Error> { |
| 158 | env_logger::init(); |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 159 | let opt = Opt::from_args(); |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 160 | |
| 161 | // We need to start the thread pool for Binder to work properly, especially link_to_death. |
| 162 | ProcessState::start_thread_pool(); |
| 163 | |
Andrew Walbran | f145380 | 2021-03-29 17:12:54 +0000 | [diff] [blame] | 164 | let service = wait_for_interface(VIRTUALIZATION_SERVICE_BINDER_SERVICE_IDENTIFIER) |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 165 | .context("Failed to find VirtualizationService")?; |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 166 | |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 167 | match opt { |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 168 | Opt::RunApp { |
| 169 | apk, |
| 170 | idsig, |
| 171 | instance, |
| 172 | config_path, |
| 173 | daemonize, |
| 174 | console, |
| 175 | log, |
| 176 | debug, |
| 177 | mem, |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 178 | cpus, |
| 179 | cpu_affinity, |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 180 | extra_idsigs, |
| 181 | } => command_run_app( |
| 182 | service, |
| 183 | &apk, |
| 184 | &idsig, |
| 185 | &instance, |
| 186 | &config_path, |
| 187 | daemonize, |
| 188 | console.as_deref(), |
| 189 | log.as_deref(), |
| 190 | debug, |
| 191 | mem, |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 192 | cpus, |
| 193 | cpu_affinity, |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 194 | &extra_idsigs, |
| 195 | ), |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 196 | Opt::Run { config, daemonize, cpus, cpu_affinity, console } => { |
| 197 | command_run( |
| 198 | service, |
| 199 | &config, |
| 200 | daemonize, |
| 201 | console.as_deref(), |
| 202 | /* mem */ None, |
| 203 | cpus, |
| 204 | cpu_affinity, |
| 205 | ) |
Andrew Walbran | be42924 | 2021-06-28 12:22:54 +0000 | [diff] [blame] | 206 | } |
Andrew Walbran | 17de24f | 2021-05-27 13:27:30 +0000 | [diff] [blame] | 207 | Opt::Stop { cid } => command_stop(service, cid), |
| 208 | Opt::List => command_list(service), |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 209 | Opt::CreatePartition { path, size, partition_type } => { |
| 210 | command_create_partition(service, &path, size, partition_type) |
| 211 | } |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 215 | /// Retrieve reference to a previously daemonized VM and stop it. |
Andrew Walbran | 17de24f | 2021-05-27 13:27:30 +0000 | [diff] [blame] | 216 | fn command_stop(service: Strong<dyn IVirtualizationService>, cid: u32) -> Result<(), Error> { |
| 217 | service |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 218 | .debugDropVmRef(cid as i32) |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 219 | .context("Failed to get VM from VirtualizationService")? |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 220 | .context("CID does not correspond to a running background VM")?; |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 221 | Ok(()) |
| 222 | } |
| 223 | |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 224 | /// List the VMs currently running. |
Andrew Walbran | 17de24f | 2021-05-27 13:27:30 +0000 | [diff] [blame] | 225 | fn command_list(service: Strong<dyn IVirtualizationService>) -> Result<(), Error> { |
| 226 | let vms = service.debugListVms().context("Failed to get list of VMs")?; |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 227 | println!("Running VMs: {:#?}", vms); |
| 228 | Ok(()) |
| 229 | } |