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 | |
Jooyung Han | c221c05 | 2022-02-22 05:20:15 +0900 | [diff] [blame] | 17 | mod create_idsig; |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 18 | mod create_partition; |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 19 | mod run; |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 20 | mod sync; |
| 21 | |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame] | 22 | use android_system_virtualizationservice::aidl::android::system::virtualizationservice::{ |
| 23 | IVirtualizationService::IVirtualizationService, PartitionType::PartitionType, |
| 24 | VirtualMachineAppConfig::DebugLevel::DebugLevel, |
| 25 | }; |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 26 | use android_system_virtualizationservice::binder::{wait_for_interface, ProcessState, Strong}; |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 27 | use anyhow::{Context, Error}; |
Jooyung Han | c221c05 | 2022-02-22 05:20:15 +0900 | [diff] [blame] | 28 | use create_idsig::command_create_idsig; |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 29 | use create_partition::command_create_partition; |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 30 | use run::{command_run, command_run_app}; |
Andrew Walbran | c4b1bde | 2022-02-03 15:26:02 +0000 | [diff] [blame] | 31 | use rustutils::system_properties; |
| 32 | use std::path::{Path, PathBuf}; |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 33 | use structopt::clap::AppSettings; |
| 34 | use structopt::StructOpt; |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 35 | |
Andrew Walbran | 17de24f | 2021-05-27 13:27:30 +0000 | [diff] [blame] | 36 | const VIRTUALIZATION_SERVICE_BINDER_SERVICE_IDENTIFIER: &str = |
| 37 | "android.system.virtualizationservice"; |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 38 | |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 39 | #[derive(Debug)] |
| 40 | struct Idsigs(Vec<PathBuf>); |
| 41 | |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 42 | #[derive(StructOpt)] |
| 43 | #[structopt(no_version, global_settings = &[AppSettings::DisableVersion])] |
| 44 | enum Opt { |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 45 | /// Run a virtual machine with a config in APK |
| 46 | RunApp { |
| 47 | /// Path to VM Payload APK |
| 48 | #[structopt(parse(from_os_str))] |
| 49 | apk: PathBuf, |
| 50 | |
| 51 | /// Path to idsig of the APK |
| 52 | #[structopt(parse(from_os_str))] |
| 53 | idsig: PathBuf, |
| 54 | |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 55 | /// Path to the instance image. Created if not exists. |
| 56 | #[structopt(parse(from_os_str))] |
| 57 | instance: PathBuf, |
| 58 | |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 59 | /// Path to VM config JSON within APK (e.g. assets/vm_config.json) |
| 60 | config_path: String, |
| 61 | |
| 62 | /// Detach VM from the terminal and run in the background |
| 63 | #[structopt(short, long)] |
| 64 | daemonize: bool, |
| 65 | |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 66 | /// Path to file for VM console output. |
| 67 | #[structopt(long)] |
| 68 | console: Option<PathBuf>, |
| 69 | |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 70 | /// Path to file for VM log output. |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 71 | #[structopt(long)] |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 72 | log: Option<PathBuf>, |
Jiyong Park | 2360114 | 2021-07-05 13:15:32 +0900 | [diff] [blame] | 73 | |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame] | 74 | /// 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] | 75 | #[structopt(long, default_value = "none", parse(try_from_str=parse_debug_level))] |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame] | 76 | debug: DebugLevel, |
Jiyong Park | d63cfff | 2021-09-27 20:10:17 +0900 | [diff] [blame] | 77 | |
Andrew Walbran | 3994f00 | 2022-01-27 17:33:45 +0000 | [diff] [blame] | 78 | /// Run VM in protected mode. |
| 79 | #[structopt(short, long)] |
| 80 | protected: bool, |
| 81 | |
Jiyong Park | d63cfff | 2021-09-27 20:10:17 +0900 | [diff] [blame] | 82 | /// 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 Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 86 | |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 87 | /// 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 Park | dfe16d6 | 2022-04-20 17:32:12 +0900 | [diff] [blame^] | 95 | /// Comma separated list of task profile names to apply to the VM |
| 96 | #[structopt(long)] |
| 97 | task_profiles: Vec<String>, |
| 98 | |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 99 | /// Paths to extra idsig files. |
Victor Hsieh | 9978257 | 2022-01-05 15:38:33 -0800 | [diff] [blame] | 100 | #[structopt(long = "extra-idsig")] |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 101 | extra_idsigs: Vec<PathBuf>, |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 102 | }, |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 103 | /// Run a virtual machine |
| 104 | Run { |
| 105 | /// Path to VM config JSON |
| 106 | #[structopt(parse(from_os_str))] |
| 107 | config: PathBuf, |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 108 | |
| 109 | /// Detach VM from the terminal and run in the background |
| 110 | #[structopt(short, long)] |
| 111 | daemonize: bool, |
Andrew Walbran | be42924 | 2021-06-28 12:22:54 +0000 | [diff] [blame] | 112 | |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 113 | /// 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 Park | dfe16d6 | 2022-04-20 17:32:12 +0900 | [diff] [blame^] | 125 | /// Comma separated list of task profile names to apply to the VM |
| 126 | #[structopt(long)] |
| 127 | task_profiles: Vec<String>, |
| 128 | |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 129 | /// Path to file for VM console output. |
| 130 | #[structopt(long)] |
| 131 | console: Option<PathBuf>, |
Jooyung Han | b7983a2 | 2022-02-22 05:21:27 +0900 | [diff] [blame] | 132 | |
| 133 | /// Path to file for VM log output. |
| 134 | #[structopt(long)] |
| 135 | log: Option<PathBuf>, |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 136 | }, |
| 137 | /// Stop a virtual machine running in the background |
| 138 | Stop { |
| 139 | /// CID of the virtual machine |
| 140 | cid: u32, |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 141 | }, |
| 142 | /// List running virtual machines |
| 143 | List, |
Andrew Walbran | c4b1bde | 2022-02-03 15:26:02 +0000 | [diff] [blame] | 144 | /// Print information about virtual machine support |
| 145 | Info, |
Andrew Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 146 | /// 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 Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 154 | |
| 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 Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 158 | }, |
Jooyung Han | c221c05 | 2022-02-22 05:20:15 +0900 | [diff] [blame] | 159 | /// 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 Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame] | 170 | fn 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 Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 179 | fn 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 Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 187 | fn main() -> Result<(), Error> { |
| 188 | env_logger::init(); |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 189 | let opt = Opt::from_args(); |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 190 | |
| 191 | // We need to start the thread pool for Binder to work properly, especially link_to_death. |
| 192 | ProcessState::start_thread_pool(); |
| 193 | |
Andrew Walbran | f145380 | 2021-03-29 17:12:54 +0000 | [diff] [blame] | 194 | let service = wait_for_interface(VIRTUALIZATION_SERVICE_BINDER_SERVICE_IDENTIFIER) |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 195 | .context("Failed to find VirtualizationService")?; |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 196 | |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 197 | match opt { |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 198 | Opt::RunApp { |
| 199 | apk, |
| 200 | idsig, |
| 201 | instance, |
| 202 | config_path, |
| 203 | daemonize, |
| 204 | console, |
| 205 | log, |
| 206 | debug, |
Andrew Walbran | 3994f00 | 2022-01-27 17:33:45 +0000 | [diff] [blame] | 207 | protected, |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 208 | mem, |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 209 | cpus, |
| 210 | cpu_affinity, |
Jiyong Park | dfe16d6 | 2022-04-20 17:32:12 +0900 | [diff] [blame^] | 211 | task_profiles, |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 212 | extra_idsigs, |
| 213 | } => command_run_app( |
| 214 | service, |
| 215 | &apk, |
| 216 | &idsig, |
| 217 | &instance, |
| 218 | &config_path, |
| 219 | daemonize, |
| 220 | console.as_deref(), |
| 221 | log.as_deref(), |
| 222 | debug, |
Andrew Walbran | 3994f00 | 2022-01-27 17:33:45 +0000 | [diff] [blame] | 223 | protected, |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 224 | mem, |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 225 | cpus, |
| 226 | cpu_affinity, |
Jiyong Park | dfe16d6 | 2022-04-20 17:32:12 +0900 | [diff] [blame^] | 227 | task_profiles, |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 228 | &extra_idsigs, |
| 229 | ), |
Jiyong Park | dfe16d6 | 2022-04-20 17:32:12 +0900 | [diff] [blame^] | 230 | Opt::Run { config, daemonize, cpus, cpu_affinity, task_profiles, console, log } => { |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 231 | command_run( |
| 232 | service, |
| 233 | &config, |
| 234 | daemonize, |
| 235 | console.as_deref(), |
Jooyung Han | b7983a2 | 2022-02-22 05:21:27 +0900 | [diff] [blame] | 236 | log.as_deref(), |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 237 | /* mem */ None, |
| 238 | cpus, |
| 239 | cpu_affinity, |
Jiyong Park | dfe16d6 | 2022-04-20 17:32:12 +0900 | [diff] [blame^] | 240 | task_profiles, |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 241 | ) |
Andrew Walbran | be42924 | 2021-06-28 12:22:54 +0000 | [diff] [blame] | 242 | } |
Andrew Walbran | 17de24f | 2021-05-27 13:27:30 +0000 | [diff] [blame] | 243 | Opt::Stop { cid } => command_stop(service, cid), |
| 244 | Opt::List => command_list(service), |
Andrew Walbran | c4b1bde | 2022-02-03 15:26:02 +0000 | [diff] [blame] | 245 | Opt::Info => command_info(), |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 246 | Opt::CreatePartition { path, size, partition_type } => { |
| 247 | command_create_partition(service, &path, size, partition_type) |
| 248 | } |
Jooyung Han | c221c05 | 2022-02-22 05:20:15 +0900 | [diff] [blame] | 249 | Opt::CreateIdsig { apk, path } => command_create_idsig(service, &apk, &path), |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 253 | /// Retrieve reference to a previously daemonized VM and stop it. |
Andrew Walbran | 17de24f | 2021-05-27 13:27:30 +0000 | [diff] [blame] | 254 | fn command_stop(service: Strong<dyn IVirtualizationService>, cid: u32) -> Result<(), Error> { |
| 255 | service |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 256 | .debugDropVmRef(cid as i32) |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 257 | .context("Failed to get VM from VirtualizationService")? |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 258 | .context("CID does not correspond to a running background VM")?; |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 259 | Ok(()) |
| 260 | } |
| 261 | |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 262 | /// List the VMs currently running. |
Andrew Walbran | 17de24f | 2021-05-27 13:27:30 +0000 | [diff] [blame] | 263 | fn command_list(service: Strong<dyn IVirtualizationService>) -> Result<(), Error> { |
| 264 | let vms = service.debugListVms().context("Failed to get list of VMs")?; |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 265 | println!("Running VMs: {:#?}", vms); |
| 266 | Ok(()) |
| 267 | } |
Andrew Walbran | c4b1bde | 2022-02-03 15:26:02 +0000 | [diff] [blame] | 268 | |
| 269 | /// Print information about supported VM types. |
| 270 | fn command_info() -> Result<(), Error> { |
| 271 | let unprotected_vm_supported = |
| 272 | system_properties::read_bool("ro.boot.hypervisor.vm.supported", false)?; |
| 273 | let protected_vm_supported = |
| 274 | system_properties::read_bool("ro.boot.hypervisor.protected_vm.supported", false)?; |
| 275 | match (unprotected_vm_supported, protected_vm_supported) { |
| 276 | (false, false) => println!("VMs are not supported."), |
| 277 | (false, true) => println!("Only protected VMs are supported."), |
| 278 | (true, false) => println!("Only unprotected VMs are supported."), |
| 279 | (true, true) => println!("Both protected and unprotected VMs are supported."), |
| 280 | } |
| 281 | |
Andrew Walbran | 014efb5 | 2022-02-03 17:43:11 +0000 | [diff] [blame] | 282 | if let Some(version) = system_properties::read("ro.boot.hypervisor.version")? { |
Andrew Walbran | c4b1bde | 2022-02-03 15:26:02 +0000 | [diff] [blame] | 283 | println!("Hypervisor version: {}", version); |
| 284 | } else { |
| 285 | println!("Hypervisor version not set."); |
| 286 | } |
| 287 | |
| 288 | if Path::new("/dev/kvm").exists() { |
| 289 | println!("/dev/kvm exists."); |
| 290 | } else { |
| 291 | println!("/dev/kvm does not exist."); |
| 292 | } |
| 293 | |
| 294 | Ok(()) |
| 295 | } |