blob: 84f7d1883bd1c6373a1289177cf2dc72a39988e4 [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;
22use android_system_virtualizationservice::binder::{get_interface, ProcessState, Strong};
David Brazdil20412d92021-03-18 10:53:06 +000023use anyhow::{Context, Error};
Andrew Walbranf395b822021-05-05 10:38:59 +000024use run::command_run;
25use std::path::PathBuf;
David Brazdil20412d92021-03-18 10:53:06 +000026use structopt::clap::AppSettings;
27use structopt::StructOpt;
Andrew Walbranea9fa482021-03-04 16:11:12 +000028
Andrew Walbran17de24f2021-05-27 13:27:30 +000029const VIRTUALIZATION_SERVICE_BINDER_SERVICE_IDENTIFIER: &str =
30 "android.system.virtualizationservice";
Andrew Walbranea9fa482021-03-04 16:11:12 +000031
David Brazdil20412d92021-03-18 10:53:06 +000032#[derive(StructOpt)]
33#[structopt(no_version, global_settings = &[AppSettings::DisableVersion])]
34enum Opt {
35 /// Run a virtual machine
36 Run {
37 /// Path to VM config JSON
38 #[structopt(parse(from_os_str))]
39 config: PathBuf,
David Brazdil3c2ddef2021-03-18 13:09:57 +000040
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 Brazdil20412d92021-03-18 10:53:06 +000049 },
50 /// List running virtual machines
51 List,
52}
53
Andrew Walbranea9fa482021-03-04 16:11:12 +000054fn main() -> Result<(), Error> {
55 env_logger::init();
David Brazdil20412d92021-03-18 10:53:06 +000056 let opt = Opt::from_args();
Andrew Walbranea9fa482021-03-04 16:11:12 +000057
58 // We need to start the thread pool for Binder to work properly, especially link_to_death.
59 ProcessState::start_thread_pool();
60
Andrew Walbran17de24f2021-05-27 13:27:30 +000061 let service = get_interface(VIRTUALIZATION_SERVICE_BINDER_SERVICE_IDENTIFIER)
Andrew Walbranf6bf6862021-05-21 12:41:13 +000062 .context("Failed to find VirtualizationService")?;
Andrew Walbran320b5602021-03-04 16:11:12 +000063
David Brazdil20412d92021-03-18 10:53:06 +000064 match opt {
Andrew Walbran17de24f2021-05-27 13:27:30 +000065 Opt::Run { config, daemonize } => command_run(service, &config, daemonize),
66 Opt::Stop { cid } => command_stop(service, cid),
67 Opt::List => command_list(service),
Andrew Walbranea9fa482021-03-04 16:11:12 +000068 }
69}
70
David Brazdil3c2ddef2021-03-18 13:09:57 +000071/// Retrieve reference to a previously daemonized VM and stop it.
Andrew Walbran17de24f2021-05-27 13:27:30 +000072fn command_stop(service: Strong<dyn IVirtualizationService>, cid: u32) -> Result<(), Error> {
73 service
David Brazdil3c2ddef2021-03-18 13:09:57 +000074 .debugDropVmRef(cid as i32)
Andrew Walbranf6bf6862021-05-21 12:41:13 +000075 .context("Failed to get VM from VirtualizationService")?
David Brazdil3c2ddef2021-03-18 13:09:57 +000076 .context("CID does not correspond to a running background VM")?;
Andrew Walbranea9fa482021-03-04 16:11:12 +000077 Ok(())
78}
79
Andrew Walbran320b5602021-03-04 16:11:12 +000080/// List the VMs currently running.
Andrew Walbran17de24f2021-05-27 13:27:30 +000081fn command_list(service: Strong<dyn IVirtualizationService>) -> Result<(), Error> {
82 let vms = service.debugListVms().context("Failed to get list of VMs")?;
Andrew Walbran320b5602021-03-04 16:11:12 +000083 println!("Running VMs: {:#?}", vms);
84 Ok(())
85}