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 | |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 21 | use android_system_virtualizationservice::aidl::android::system::virtualizationservice::IVirtualizationService::IVirtualizationService; |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame^] | 22 | use android_system_virtualizationservice::binder::{wait_for_interface, ProcessState, Strong}; |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 23 | use anyhow::{Context, Error}; |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame^] | 24 | use create_partition::command_create_partition; |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 25 | use run::{command_run, command_run_app}; |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame^] | 26 | use std::path::PathBuf; |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 27 | use structopt::clap::AppSettings; |
| 28 | use structopt::StructOpt; |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 29 | |
Andrew Walbran | 17de24f | 2021-05-27 13:27:30 +0000 | [diff] [blame] | 30 | const VIRTUALIZATION_SERVICE_BINDER_SERVICE_IDENTIFIER: &str = |
| 31 | "android.system.virtualizationservice"; |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 32 | |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 33 | #[derive(StructOpt)] |
| 34 | #[structopt(no_version, global_settings = &[AppSettings::DisableVersion])] |
| 35 | enum Opt { |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 36 | /// Run a virtual machine with a config in APK |
| 37 | RunApp { |
| 38 | /// Path to VM Payload APK |
| 39 | #[structopt(parse(from_os_str))] |
| 40 | apk: PathBuf, |
| 41 | |
| 42 | /// Path to idsig of the APK |
| 43 | #[structopt(parse(from_os_str))] |
| 44 | idsig: PathBuf, |
| 45 | |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame^] | 46 | /// Path to the instance image. Created if not exists. |
| 47 | #[structopt(parse(from_os_str))] |
| 48 | instance: PathBuf, |
| 49 | |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 50 | /// Path to VM config JSON within APK (e.g. assets/vm_config.json) |
| 51 | config_path: String, |
| 52 | |
| 53 | /// Detach VM from the terminal and run in the background |
| 54 | #[structopt(short, long)] |
| 55 | daemonize: bool, |
| 56 | |
| 57 | /// Path to file for VM log output. |
| 58 | #[structopt(short, long)] |
| 59 | log: Option<PathBuf>, |
Jiyong Park | 2360114 | 2021-07-05 13:15:32 +0900 | [diff] [blame] | 60 | |
| 61 | /// Whether to run VM in debug mode. |
| 62 | #[structopt(short, long)] |
| 63 | debug: bool, |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 64 | }, |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 65 | /// Run a virtual machine |
| 66 | Run { |
| 67 | /// Path to VM config JSON |
| 68 | #[structopt(parse(from_os_str))] |
| 69 | config: PathBuf, |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 70 | |
| 71 | /// Detach VM from the terminal and run in the background |
| 72 | #[structopt(short, long)] |
| 73 | daemonize: bool, |
Andrew Walbran | be42924 | 2021-06-28 12:22:54 +0000 | [diff] [blame] | 74 | |
| 75 | /// Path to file for VM log output. |
| 76 | #[structopt(short, long)] |
| 77 | log: Option<PathBuf>, |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 78 | }, |
| 79 | /// Stop a virtual machine running in the background |
| 80 | Stop { |
| 81 | /// CID of the virtual machine |
| 82 | cid: u32, |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 83 | }, |
| 84 | /// List running virtual machines |
| 85 | List, |
Andrew Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 86 | /// Create a new empty partition to be used as a writable partition for a VM |
| 87 | CreatePartition { |
| 88 | /// Path at which to create the image file |
| 89 | #[structopt(parse(from_os_str))] |
| 90 | path: PathBuf, |
| 91 | |
| 92 | /// The desired size of the partition, in bytes. |
| 93 | size: u64, |
| 94 | }, |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 97 | fn main() -> Result<(), Error> { |
| 98 | env_logger::init(); |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 99 | let opt = Opt::from_args(); |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 100 | |
| 101 | // We need to start the thread pool for Binder to work properly, especially link_to_death. |
| 102 | ProcessState::start_thread_pool(); |
| 103 | |
Andrew Walbran | f145380 | 2021-03-29 17:12:54 +0000 | [diff] [blame] | 104 | let service = wait_for_interface(VIRTUALIZATION_SERVICE_BINDER_SERVICE_IDENTIFIER) |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 105 | .context("Failed to find VirtualizationService")?; |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 106 | |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 107 | match opt { |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame^] | 108 | Opt::RunApp { apk, idsig, instance, config_path, daemonize, log, debug } => { |
| 109 | command_run_app( |
| 110 | service, |
| 111 | &apk, |
| 112 | &idsig, |
| 113 | &instance, |
| 114 | &config_path, |
| 115 | daemonize, |
| 116 | log.as_deref(), |
| 117 | debug, |
| 118 | ) |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 119 | } |
Andrew Walbran | be42924 | 2021-06-28 12:22:54 +0000 | [diff] [blame] | 120 | Opt::Run { config, daemonize, log } => { |
| 121 | command_run(service, &config, daemonize, log.as_deref()) |
| 122 | } |
Andrew Walbran | 17de24f | 2021-05-27 13:27:30 +0000 | [diff] [blame] | 123 | Opt::Stop { cid } => command_stop(service, cid), |
| 124 | Opt::List => command_list(service), |
Andrew Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 125 | Opt::CreatePartition { path, size } => command_create_partition(service, &path, size), |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 129 | /// Retrieve reference to a previously daemonized VM and stop it. |
Andrew Walbran | 17de24f | 2021-05-27 13:27:30 +0000 | [diff] [blame] | 130 | fn command_stop(service: Strong<dyn IVirtualizationService>, cid: u32) -> Result<(), Error> { |
| 131 | service |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 132 | .debugDropVmRef(cid as i32) |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 133 | .context("Failed to get VM from VirtualizationService")? |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 134 | .context("CID does not correspond to a running background VM")?; |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 135 | Ok(()) |
| 136 | } |
| 137 | |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 138 | /// List the VMs currently running. |
Andrew Walbran | 17de24f | 2021-05-27 13:27:30 +0000 | [diff] [blame] | 139 | fn command_list(service: Strong<dyn IVirtualizationService>) -> Result<(), Error> { |
| 140 | let vms = service.debugListVms().context("Failed to get list of VMs")?; |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 141 | println!("Running VMs: {:#?}", vms); |
| 142 | Ok(()) |
| 143 | } |