blob: 56a0c9275ccb0876dedcd289842572c208db38dc [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 Walbranf6bf6862021-05-21 12:41:13 +000029const VIRT_MANAGER_BINDER_SERVICE_IDENTIFIER: &str = "android.system.virtualizationservice";
Andrew Walbranea9fa482021-03-04 16:11:12 +000030
David Brazdil20412d92021-03-18 10:53:06 +000031#[derive(StructOpt)]
32#[structopt(no_version, global_settings = &[AppSettings::DisableVersion])]
33enum Opt {
34 /// Run a virtual machine
35 Run {
36 /// Path to VM config JSON
37 #[structopt(parse(from_os_str))]
38 config: PathBuf,
David Brazdil3c2ddef2021-03-18 13:09:57 +000039
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 Brazdil20412d92021-03-18 10:53:06 +000048 },
49 /// List running virtual machines
50 List,
51}
52
Andrew Walbranea9fa482021-03-04 16:11:12 +000053fn main() -> Result<(), Error> {
54 env_logger::init();
David Brazdil20412d92021-03-18 10:53:06 +000055 let opt = Opt::from_args();
Andrew Walbranea9fa482021-03-04 16:11:12 +000056
57 // We need to start the thread pool for Binder to work properly, especially link_to_death.
58 ProcessState::start_thread_pool();
59
Andrew Walbran320b5602021-03-04 16:11:12 +000060 let virt_manager = get_interface(VIRT_MANAGER_BINDER_SERVICE_IDENTIFIER)
Andrew Walbranf6bf6862021-05-21 12:41:13 +000061 .context("Failed to find VirtualizationService")?;
Andrew Walbran320b5602021-03-04 16:11:12 +000062
David Brazdil20412d92021-03-18 10:53:06 +000063 match opt {
David Brazdil3c2ddef2021-03-18 13:09:57 +000064 Opt::Run { config, daemonize } => command_run(virt_manager, &config, daemonize),
65 Opt::Stop { cid } => command_stop(virt_manager, cid),
David Brazdil20412d92021-03-18 10:53:06 +000066 Opt::List => command_list(virt_manager),
Andrew Walbranea9fa482021-03-04 16:11:12 +000067 }
68}
69
David Brazdil3c2ddef2021-03-18 13:09:57 +000070/// Retrieve reference to a previously daemonized VM and stop it.
Andrew Walbranf6bf6862021-05-21 12:41:13 +000071fn command_stop(virt_manager: Strong<dyn IVirtualizationService>, cid: u32) -> Result<(), Error> {
David Brazdil3c2ddef2021-03-18 13:09:57 +000072 virt_manager
73 .debugDropVmRef(cid as i32)
Andrew Walbranf6bf6862021-05-21 12:41:13 +000074 .context("Failed to get VM from VirtualizationService")?
David Brazdil3c2ddef2021-03-18 13:09:57 +000075 .context("CID does not correspond to a running background VM")?;
Andrew Walbranea9fa482021-03-04 16:11:12 +000076 Ok(())
77}
78
Andrew Walbran320b5602021-03-04 16:11:12 +000079/// List the VMs currently running.
Andrew Walbranf6bf6862021-05-21 12:41:13 +000080fn command_list(virt_manager: Strong<dyn IVirtualizationService>) -> Result<(), Error> {
Andrew Walbran320b5602021-03-04 16:11:12 +000081 let vms = virt_manager.debugListVms().context("Failed to get list of VMs")?;
82 println!("Running VMs: {:#?}", vms);
83 Ok(())
84}