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 | |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 36 | #[derive(StructOpt)] |
| 37 | #[structopt(no_version, global_settings = &[AppSettings::DisableVersion])] |
| 38 | enum Opt { |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 39 | /// Run a virtual machine with a config in APK |
| 40 | RunApp { |
| 41 | /// Path to VM Payload APK |
| 42 | #[structopt(parse(from_os_str))] |
| 43 | apk: PathBuf, |
| 44 | |
| 45 | /// Path to idsig of the APK |
| 46 | #[structopt(parse(from_os_str))] |
| 47 | idsig: PathBuf, |
| 48 | |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 49 | /// Path to the instance image. Created if not exists. |
| 50 | #[structopt(parse(from_os_str))] |
| 51 | instance: PathBuf, |
| 52 | |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 53 | /// Path to VM config JSON within APK (e.g. assets/vm_config.json) |
| 54 | config_path: String, |
| 55 | |
| 56 | /// Detach VM from the terminal and run in the background |
| 57 | #[structopt(short, long)] |
| 58 | daemonize: bool, |
| 59 | |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 60 | /// Path to file for VM console output. |
| 61 | #[structopt(long)] |
| 62 | console: Option<PathBuf>, |
| 63 | |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 64 | /// Path to file for VM log output. |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 65 | #[structopt(long)] |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 66 | log: Option<PathBuf>, |
Jiyong Park | 2360114 | 2021-07-05 13:15:32 +0900 | [diff] [blame] | 67 | |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame] | 68 | /// 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] | 69 | #[structopt(long, default_value = "none", parse(try_from_str=parse_debug_level))] |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame] | 70 | debug: DebugLevel, |
Jiyong Park | d63cfff | 2021-09-27 20:10:17 +0900 | [diff] [blame] | 71 | |
| 72 | /// Memory size (in MiB) of the VM. If unspecified, defaults to the value of `memory_mib` |
| 73 | /// in the VM config file. |
| 74 | #[structopt(short, long)] |
| 75 | mem: Option<u32>, |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 76 | }, |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 77 | /// Run a virtual machine |
| 78 | Run { |
| 79 | /// Path to VM config JSON |
| 80 | #[structopt(parse(from_os_str))] |
| 81 | config: PathBuf, |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 82 | |
| 83 | /// Detach VM from the terminal and run in the background |
| 84 | #[structopt(short, long)] |
| 85 | daemonize: bool, |
Andrew Walbran | be42924 | 2021-06-28 12:22:54 +0000 | [diff] [blame] | 86 | |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 87 | /// Path to file for VM console output. |
| 88 | #[structopt(long)] |
| 89 | console: Option<PathBuf>, |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 90 | }, |
| 91 | /// Stop a virtual machine running in the background |
| 92 | Stop { |
| 93 | /// CID of the virtual machine |
| 94 | cid: u32, |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 95 | }, |
| 96 | /// List running virtual machines |
| 97 | List, |
Andrew Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 98 | /// Create a new empty partition to be used as a writable partition for a VM |
| 99 | CreatePartition { |
| 100 | /// Path at which to create the image file |
| 101 | #[structopt(parse(from_os_str))] |
| 102 | path: PathBuf, |
| 103 | |
| 104 | /// The desired size of the partition, in bytes. |
| 105 | size: u64, |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 106 | |
| 107 | /// Type of the partition |
| 108 | #[structopt(short="t", long="type", default_value="raw", parse(try_from_str=parse_partition_type))] |
| 109 | partition_type: PartitionType, |
Andrew Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 110 | }, |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame] | 113 | fn parse_debug_level(s: &str) -> Result<DebugLevel, String> { |
| 114 | match s { |
| 115 | "none" => Ok(DebugLevel::NONE), |
| 116 | "app_only" => Ok(DebugLevel::APP_ONLY), |
| 117 | "full" => Ok(DebugLevel::FULL), |
| 118 | _ => Err(format!("Invalid debug level {}", s)), |
| 119 | } |
| 120 | } |
| 121 | |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 122 | fn parse_partition_type(s: &str) -> Result<PartitionType, String> { |
| 123 | match s { |
| 124 | "raw" => Ok(PartitionType::RAW), |
| 125 | "instance" => Ok(PartitionType::ANDROID_VM_INSTANCE), |
| 126 | _ => Err(format!("Invalid partition type {}", s)), |
| 127 | } |
| 128 | } |
| 129 | |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 130 | fn main() -> Result<(), Error> { |
| 131 | env_logger::init(); |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 132 | let opt = Opt::from_args(); |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 133 | |
| 134 | // We need to start the thread pool for Binder to work properly, especially link_to_death. |
| 135 | ProcessState::start_thread_pool(); |
| 136 | |
Andrew Walbran | f145380 | 2021-03-29 17:12:54 +0000 | [diff] [blame] | 137 | let service = wait_for_interface(VIRTUALIZATION_SERVICE_BINDER_SERVICE_IDENTIFIER) |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 138 | .context("Failed to find VirtualizationService")?; |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 139 | |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 140 | match opt { |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 141 | Opt::RunApp { apk, idsig, instance, config_path, daemonize, console, log, debug, mem } => { |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 142 | command_run_app( |
| 143 | service, |
| 144 | &apk, |
| 145 | &idsig, |
| 146 | &instance, |
| 147 | &config_path, |
| 148 | daemonize, |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 149 | console.as_deref(), |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 150 | log.as_deref(), |
| 151 | debug, |
Jiyong Park | d63cfff | 2021-09-27 20:10:17 +0900 | [diff] [blame] | 152 | mem, |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 153 | ) |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 154 | } |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 155 | Opt::Run { config, daemonize, console } => { |
| 156 | command_run(service, &config, daemonize, console.as_deref(), /* mem */ None) |
Andrew Walbran | be42924 | 2021-06-28 12:22:54 +0000 | [diff] [blame] | 157 | } |
Andrew Walbran | 17de24f | 2021-05-27 13:27:30 +0000 | [diff] [blame] | 158 | Opt::Stop { cid } => command_stop(service, cid), |
| 159 | Opt::List => command_list(service), |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 160 | Opt::CreatePartition { path, size, partition_type } => { |
| 161 | command_create_partition(service, &path, size, partition_type) |
| 162 | } |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 166 | /// Retrieve reference to a previously daemonized VM and stop it. |
Andrew Walbran | 17de24f | 2021-05-27 13:27:30 +0000 | [diff] [blame] | 167 | fn command_stop(service: Strong<dyn IVirtualizationService>, cid: u32) -> Result<(), Error> { |
| 168 | service |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 169 | .debugDropVmRef(cid as i32) |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 170 | .context("Failed to get VM from VirtualizationService")? |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 171 | .context("CID does not correspond to a running background VM")?; |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 172 | Ok(()) |
| 173 | } |
| 174 | |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 175 | /// List the VMs currently running. |
Andrew Walbran | 17de24f | 2021-05-27 13:27:30 +0000 | [diff] [blame] | 176 | fn command_list(service: Strong<dyn IVirtualizationService>) -> Result<(), Error> { |
| 177 | let vms = service.debugListVms().context("Failed to get list of VMs")?; |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 178 | println!("Running VMs: {:#?}", vms); |
| 179 | Ok(()) |
| 180 | } |