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 | |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 17 | mod config; |
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; |
| 22 | use android_system_virtualizationservice::binder::{get_interface, ProcessState, Strong}; |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 23 | use anyhow::{Context, Error}; |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 24 | use run::command_run; |
| 25 | use std::path::PathBuf; |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 26 | use structopt::clap::AppSettings; |
| 27 | use structopt::StructOpt; |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 28 | |
Andrew Walbran | 17de24f | 2021-05-27 13:27:30 +0000 | [diff] [blame^] | 29 | const VIRTUALIZATION_SERVICE_BINDER_SERVICE_IDENTIFIER: &str = |
| 30 | "android.system.virtualizationservice"; |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 31 | |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 32 | #[derive(StructOpt)] |
| 33 | #[structopt(no_version, global_settings = &[AppSettings::DisableVersion])] |
| 34 | enum Opt { |
| 35 | /// Run a virtual machine |
| 36 | Run { |
| 37 | /// Path to VM config JSON |
| 38 | #[structopt(parse(from_os_str))] |
| 39 | config: PathBuf, |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 40 | |
| 41 | /// Detach VM from the terminal and run in the background |
| 42 | #[structopt(short, long)] |
| 43 | daemonize: bool, |
| 44 | }, |
| 45 | /// Stop a virtual machine running in the background |
| 46 | Stop { |
| 47 | /// CID of the virtual machine |
| 48 | cid: u32, |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 49 | }, |
| 50 | /// List running virtual machines |
| 51 | List, |
| 52 | } |
| 53 | |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 54 | fn main() -> Result<(), Error> { |
| 55 | env_logger::init(); |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 56 | let opt = Opt::from_args(); |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 57 | |
| 58 | // We need to start the thread pool for Binder to work properly, especially link_to_death. |
| 59 | ProcessState::start_thread_pool(); |
| 60 | |
Andrew Walbran | 17de24f | 2021-05-27 13:27:30 +0000 | [diff] [blame^] | 61 | let service = get_interface(VIRTUALIZATION_SERVICE_BINDER_SERVICE_IDENTIFIER) |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 62 | .context("Failed to find VirtualizationService")?; |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 63 | |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 64 | match opt { |
Andrew Walbran | 17de24f | 2021-05-27 13:27:30 +0000 | [diff] [blame^] | 65 | Opt::Run { config, daemonize } => command_run(service, &config, daemonize), |
| 66 | Opt::Stop { cid } => command_stop(service, cid), |
| 67 | Opt::List => command_list(service), |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 71 | /// Retrieve reference to a previously daemonized VM and stop it. |
Andrew Walbran | 17de24f | 2021-05-27 13:27:30 +0000 | [diff] [blame^] | 72 | fn command_stop(service: Strong<dyn IVirtualizationService>, cid: u32) -> Result<(), Error> { |
| 73 | service |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 74 | .debugDropVmRef(cid as i32) |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 75 | .context("Failed to get VM from VirtualizationService")? |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 76 | .context("CID does not correspond to a running background VM")?; |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 77 | Ok(()) |
| 78 | } |
| 79 | |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 80 | /// List the VMs currently running. |
Andrew Walbran | 17de24f | 2021-05-27 13:27:30 +0000 | [diff] [blame^] | 81 | fn command_list(service: Strong<dyn IVirtualizationService>) -> Result<(), Error> { |
| 82 | let vms = service.debugListVms().context("Failed to get list of VMs")?; |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 83 | println!("Running VMs: {:#?}", vms); |
| 84 | Ok(()) |
| 85 | } |