blob: b79f42a7e1f9d4992ac470c1ce0a02636556c870 [file] [log] [blame]
Andrew Walbranea9fa482021-03-04 16:11:12 +00001// 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 Walbran3a5a9212021-05-04 17:09:08 +000017mod config;
Andrew Walbranf395b822021-05-05 10:38:59 +000018mod run;
Andrew Walbranea9fa482021-03-04 16:11:12 +000019mod sync;
20
Andrew Walbranf6bf6862021-05-21 12:41:13 +000021use android_system_virtualizationservice::aidl::android::system::virtualizationservice::IVirtualizationService::IVirtualizationService;
Andrew Walbrandff3b942021-06-09 15:20:36 +000022use android_system_virtualizationservice::binder::{wait_for_interface, ProcessState, Strong, ParcelFileDescriptor};
David Brazdil20412d92021-03-18 10:53:06 +000023use anyhow::{Context, Error};
Andrew Walbranf395b822021-05-05 10:38:59 +000024use run::command_run;
Andrew Walbrandff3b942021-06-09 15:20:36 +000025use std::convert::TryInto;
26use std::fs::OpenOptions;
27use std::path::{PathBuf, Path};
David Brazdil20412d92021-03-18 10:53:06 +000028use structopt::clap::AppSettings;
29use structopt::StructOpt;
Andrew Walbranea9fa482021-03-04 16:11:12 +000030
Andrew Walbran17de24f2021-05-27 13:27:30 +000031const VIRTUALIZATION_SERVICE_BINDER_SERVICE_IDENTIFIER: &str =
32 "android.system.virtualizationservice";
Andrew Walbranea9fa482021-03-04 16:11:12 +000033
David Brazdil20412d92021-03-18 10:53:06 +000034#[derive(StructOpt)]
35#[structopt(no_version, global_settings = &[AppSettings::DisableVersion])]
36enum Opt {
37 /// Run a virtual machine
38 Run {
39 /// Path to VM config JSON
40 #[structopt(parse(from_os_str))]
41 config: PathBuf,
David Brazdil3c2ddef2021-03-18 13:09:57 +000042
43 /// Detach VM from the terminal and run in the background
44 #[structopt(short, long)]
45 daemonize: bool,
Andrew Walbranbe429242021-06-28 12:22:54 +000046
47 /// Path to file for VM log output.
48 #[structopt(short, long)]
49 log: Option<PathBuf>,
David Brazdil3c2ddef2021-03-18 13:09:57 +000050 },
51 /// Stop a virtual machine running in the background
52 Stop {
53 /// CID of the virtual machine
54 cid: u32,
David Brazdil20412d92021-03-18 10:53:06 +000055 },
56 /// List running virtual machines
57 List,
Andrew Walbrandff3b942021-06-09 15:20:36 +000058 /// Create a new empty partition to be used as a writable partition for a VM
59 CreatePartition {
60 /// Path at which to create the image file
61 #[structopt(parse(from_os_str))]
62 path: PathBuf,
63
64 /// The desired size of the partition, in bytes.
65 size: u64,
66 },
David Brazdil20412d92021-03-18 10:53:06 +000067}
68
Andrew Walbranea9fa482021-03-04 16:11:12 +000069fn main() -> Result<(), Error> {
70 env_logger::init();
David Brazdil20412d92021-03-18 10:53:06 +000071 let opt = Opt::from_args();
Andrew Walbranea9fa482021-03-04 16:11:12 +000072
73 // We need to start the thread pool for Binder to work properly, especially link_to_death.
74 ProcessState::start_thread_pool();
75
Andrew Walbranf1453802021-03-29 17:12:54 +000076 let service = wait_for_interface(VIRTUALIZATION_SERVICE_BINDER_SERVICE_IDENTIFIER)
Andrew Walbranf6bf6862021-05-21 12:41:13 +000077 .context("Failed to find VirtualizationService")?;
Andrew Walbran320b5602021-03-04 16:11:12 +000078
David Brazdil20412d92021-03-18 10:53:06 +000079 match opt {
Andrew Walbranbe429242021-06-28 12:22:54 +000080 Opt::Run { config, daemonize, log } => {
81 command_run(service, &config, daemonize, log.as_deref())
82 }
Andrew Walbran17de24f2021-05-27 13:27:30 +000083 Opt::Stop { cid } => command_stop(service, cid),
84 Opt::List => command_list(service),
Andrew Walbrandff3b942021-06-09 15:20:36 +000085 Opt::CreatePartition { path, size } => command_create_partition(service, &path, size),
Andrew Walbranea9fa482021-03-04 16:11:12 +000086 }
87}
88
David Brazdil3c2ddef2021-03-18 13:09:57 +000089/// Retrieve reference to a previously daemonized VM and stop it.
Andrew Walbran17de24f2021-05-27 13:27:30 +000090fn command_stop(service: Strong<dyn IVirtualizationService>, cid: u32) -> Result<(), Error> {
91 service
David Brazdil3c2ddef2021-03-18 13:09:57 +000092 .debugDropVmRef(cid as i32)
Andrew Walbranf6bf6862021-05-21 12:41:13 +000093 .context("Failed to get VM from VirtualizationService")?
David Brazdil3c2ddef2021-03-18 13:09:57 +000094 .context("CID does not correspond to a running background VM")?;
Andrew Walbranea9fa482021-03-04 16:11:12 +000095 Ok(())
96}
97
Andrew Walbran320b5602021-03-04 16:11:12 +000098/// List the VMs currently running.
Andrew Walbran17de24f2021-05-27 13:27:30 +000099fn command_list(service: Strong<dyn IVirtualizationService>) -> Result<(), Error> {
100 let vms = service.debugListVms().context("Failed to get list of VMs")?;
Andrew Walbran320b5602021-03-04 16:11:12 +0000101 println!("Running VMs: {:#?}", vms);
102 Ok(())
103}
Andrew Walbrandff3b942021-06-09 15:20:36 +0000104
105/// Initialise an empty partition image of the given size to be used as a writable partition.
106fn command_create_partition(
107 service: Strong<dyn IVirtualizationService>,
108 image_path: &Path,
109 size: u64,
110) -> Result<(), Error> {
111 let image = OpenOptions::new()
112 .create_new(true)
Andrew Walbrandfc953d2021-06-10 13:59:56 +0000113 .read(true)
Andrew Walbrandff3b942021-06-09 15:20:36 +0000114 .write(true)
115 .open(image_path)
116 .with_context(|| format!("Failed to create {:?}", image_path))?;
117 service
118 .initializeWritablePartition(&ParcelFileDescriptor::new(image), size.try_into()?)
119 .context("Failed to initialize partition with size {}, size")?;
120 Ok(())
121}