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