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 | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 17 | mod run; |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 18 | mod sync; |
| 19 | |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 20 | use android_system_virtualizationservice::aidl::android::system::virtualizationservice::IVirtualizationService::IVirtualizationService; |
Andrew Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 21 | use android_system_virtualizationservice::binder::{wait_for_interface, ProcessState, Strong, ParcelFileDescriptor}; |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 22 | use anyhow::{Context, Error}; |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 23 | use run::command_run; |
Andrew Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 24 | use std::convert::TryInto; |
| 25 | use std::fs::OpenOptions; |
| 26 | use std::path::{PathBuf, Path}; |
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 { |
| 36 | /// Run a virtual machine |
| 37 | Run { |
| 38 | /// Path to VM config JSON |
| 39 | #[structopt(parse(from_os_str))] |
| 40 | config: PathBuf, |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 41 | |
| 42 | /// Detach VM from the terminal and run in the background |
| 43 | #[structopt(short, long)] |
| 44 | daemonize: bool, |
Andrew Walbran | be42924 | 2021-06-28 12:22:54 +0000 | [diff] [blame] | 45 | |
| 46 | /// Path to file for VM log output. |
| 47 | #[structopt(short, long)] |
| 48 | log: Option<PathBuf>, |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 49 | }, |
| 50 | /// Stop a virtual machine running in the background |
| 51 | Stop { |
| 52 | /// CID of the virtual machine |
| 53 | cid: u32, |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 54 | }, |
| 55 | /// List running virtual machines |
| 56 | List, |
Andrew Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 57 | /// Create a new empty partition to be used as a writable partition for a VM |
| 58 | CreatePartition { |
| 59 | /// Path at which to create the image file |
| 60 | #[structopt(parse(from_os_str))] |
| 61 | path: PathBuf, |
| 62 | |
| 63 | /// The desired size of the partition, in bytes. |
| 64 | size: u64, |
| 65 | }, |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 68 | fn main() -> Result<(), Error> { |
| 69 | env_logger::init(); |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 70 | let opt = Opt::from_args(); |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 71 | |
| 72 | // We need to start the thread pool for Binder to work properly, especially link_to_death. |
| 73 | ProcessState::start_thread_pool(); |
| 74 | |
Andrew Walbran | f145380 | 2021-03-29 17:12:54 +0000 | [diff] [blame] | 75 | let service = wait_for_interface(VIRTUALIZATION_SERVICE_BINDER_SERVICE_IDENTIFIER) |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 76 | .context("Failed to find VirtualizationService")?; |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 77 | |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 78 | match opt { |
Andrew Walbran | be42924 | 2021-06-28 12:22:54 +0000 | [diff] [blame] | 79 | Opt::Run { config, daemonize, log } => { |
| 80 | command_run(service, &config, daemonize, log.as_deref()) |
| 81 | } |
Andrew Walbran | 17de24f | 2021-05-27 13:27:30 +0000 | [diff] [blame] | 82 | Opt::Stop { cid } => command_stop(service, cid), |
| 83 | Opt::List => command_list(service), |
Andrew Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 84 | Opt::CreatePartition { path, size } => command_create_partition(service, &path, size), |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 88 | /// Retrieve reference to a previously daemonized VM and stop it. |
Andrew Walbran | 17de24f | 2021-05-27 13:27:30 +0000 | [diff] [blame] | 89 | fn command_stop(service: Strong<dyn IVirtualizationService>, cid: u32) -> Result<(), Error> { |
| 90 | service |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 91 | .debugDropVmRef(cid as i32) |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 92 | .context("Failed to get VM from VirtualizationService")? |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 93 | .context("CID does not correspond to a running background VM")?; |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 94 | Ok(()) |
| 95 | } |
| 96 | |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 97 | /// List the VMs currently running. |
Andrew Walbran | 17de24f | 2021-05-27 13:27:30 +0000 | [diff] [blame] | 98 | fn command_list(service: Strong<dyn IVirtualizationService>) -> Result<(), Error> { |
| 99 | let vms = service.debugListVms().context("Failed to get list of VMs")?; |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 100 | println!("Running VMs: {:#?}", vms); |
| 101 | Ok(()) |
| 102 | } |
Andrew Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 103 | |
| 104 | /// Initialise an empty partition image of the given size to be used as a writable partition. |
| 105 | fn command_create_partition( |
| 106 | service: Strong<dyn IVirtualizationService>, |
| 107 | image_path: &Path, |
| 108 | size: u64, |
| 109 | ) -> Result<(), Error> { |
| 110 | let image = OpenOptions::new() |
| 111 | .create_new(true) |
Andrew Walbran | dfc953d | 2021-06-10 13:59:56 +0000 | [diff] [blame] | 112 | .read(true) |
Andrew Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 113 | .write(true) |
| 114 | .open(image_path) |
| 115 | .with_context(|| format!("Failed to create {:?}", image_path))?; |
| 116 | service |
| 117 | .initializeWritablePartition(&ParcelFileDescriptor::new(image), size.try_into()?) |
| 118 | .context("Failed to initialize partition with size {}, size")?; |
| 119 | Ok(()) |
| 120 | } |