blob: b0abf664a4fb66da6a2eb1a47580c281e4b37e3f [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 Walbranf395b822021-05-05 10:38:59 +000017mod run;
Andrew Walbranea9fa482021-03-04 16:11:12 +000018mod sync;
19
20use android_system_virtmanager::aidl::android::system::virtmanager::IVirtManager::IVirtManager;
Andrew Walbranf395b822021-05-05 10:38:59 +000021use android_system_virtmanager::binder::{get_interface, ProcessState, Strong};
David Brazdil20412d92021-03-18 10:53:06 +000022use anyhow::{Context, Error};
Andrew Walbranf395b822021-05-05 10:38:59 +000023use run::command_run;
24use std::path::PathBuf;
David Brazdil20412d92021-03-18 10:53:06 +000025use structopt::clap::AppSettings;
26use structopt::StructOpt;
Andrew Walbranea9fa482021-03-04 16:11:12 +000027
28const VIRT_MANAGER_BINDER_SERVICE_IDENTIFIER: &str = "android.system.virtmanager";
29
David Brazdil20412d92021-03-18 10:53:06 +000030#[derive(StructOpt)]
31#[structopt(no_version, global_settings = &[AppSettings::DisableVersion])]
32enum Opt {
33 /// Run a virtual machine
34 Run {
35 /// Path to VM config JSON
36 #[structopt(parse(from_os_str))]
37 config: PathBuf,
David Brazdil3c2ddef2021-03-18 13:09:57 +000038
39 /// Detach VM from the terminal and run in the background
40 #[structopt(short, long)]
41 daemonize: bool,
42 },
43 /// Stop a virtual machine running in the background
44 Stop {
45 /// CID of the virtual machine
46 cid: u32,
David Brazdil20412d92021-03-18 10:53:06 +000047 },
48 /// List running virtual machines
49 List,
50}
51
Andrew Walbranea9fa482021-03-04 16:11:12 +000052fn main() -> Result<(), Error> {
53 env_logger::init();
David Brazdil20412d92021-03-18 10:53:06 +000054 let opt = Opt::from_args();
Andrew Walbranea9fa482021-03-04 16:11:12 +000055
56 // We need to start the thread pool for Binder to work properly, especially link_to_death.
57 ProcessState::start_thread_pool();
58
Andrew Walbran320b5602021-03-04 16:11:12 +000059 let virt_manager = get_interface(VIRT_MANAGER_BINDER_SERVICE_IDENTIFIER)
60 .context("Failed to find Virt Manager service")?;
61
David Brazdil20412d92021-03-18 10:53:06 +000062 match opt {
David Brazdil3c2ddef2021-03-18 13:09:57 +000063 Opt::Run { config, daemonize } => command_run(virt_manager, &config, daemonize),
64 Opt::Stop { cid } => command_stop(virt_manager, cid),
David Brazdil20412d92021-03-18 10:53:06 +000065 Opt::List => command_list(virt_manager),
Andrew Walbranea9fa482021-03-04 16:11:12 +000066 }
67}
68
David Brazdil3c2ddef2021-03-18 13:09:57 +000069/// Retrieve reference to a previously daemonized VM and stop it.
70fn command_stop(virt_manager: Strong<dyn IVirtManager>, cid: u32) -> Result<(), Error> {
71 virt_manager
72 .debugDropVmRef(cid as i32)
73 .context("Failed to get VM from Virt Manager")?
74 .context("CID does not correspond to a running background VM")?;
Andrew Walbranea9fa482021-03-04 16:11:12 +000075 Ok(())
76}
77
Andrew Walbran320b5602021-03-04 16:11:12 +000078/// List the VMs currently running.
79fn command_list(virt_manager: Strong<dyn IVirtManager>) -> Result<(), Error> {
80 let vms = virt_manager.debugListVms().context("Failed to get list of VMs")?;
81 println!("Running VMs: {:#?}", vms);
82 Ok(())
83}