blob: e2c11a840206ca6e99c5a54676b657779732ce94 [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,
46 },
47 /// Stop a virtual machine running in the background
48 Stop {
49 /// CID of the virtual machine
50 cid: u32,
David Brazdil20412d92021-03-18 10:53:06 +000051 },
52 /// List running virtual machines
53 List,
Andrew Walbrandff3b942021-06-09 15:20:36 +000054 /// Create a new empty partition to be used as a writable partition for a VM
55 CreatePartition {
56 /// Path at which to create the image file
57 #[structopt(parse(from_os_str))]
58 path: PathBuf,
59
60 /// The desired size of the partition, in bytes.
61 size: u64,
62 },
David Brazdil20412d92021-03-18 10:53:06 +000063}
64
Andrew Walbranea9fa482021-03-04 16:11:12 +000065fn main() -> Result<(), Error> {
66 env_logger::init();
David Brazdil20412d92021-03-18 10:53:06 +000067 let opt = Opt::from_args();
Andrew Walbranea9fa482021-03-04 16:11:12 +000068
69 // We need to start the thread pool for Binder to work properly, especially link_to_death.
70 ProcessState::start_thread_pool();
71
Andrew Walbranf1453802021-03-29 17:12:54 +000072 let service = wait_for_interface(VIRTUALIZATION_SERVICE_BINDER_SERVICE_IDENTIFIER)
Andrew Walbranf6bf6862021-05-21 12:41:13 +000073 .context("Failed to find VirtualizationService")?;
Andrew Walbran320b5602021-03-04 16:11:12 +000074
David Brazdil20412d92021-03-18 10:53:06 +000075 match opt {
Andrew Walbran17de24f2021-05-27 13:27:30 +000076 Opt::Run { config, daemonize } => command_run(service, &config, daemonize),
77 Opt::Stop { cid } => command_stop(service, cid),
78 Opt::List => command_list(service),
Andrew Walbrandff3b942021-06-09 15:20:36 +000079 Opt::CreatePartition { path, size } => command_create_partition(service, &path, size),
Andrew Walbranea9fa482021-03-04 16:11:12 +000080 }
81}
82
David Brazdil3c2ddef2021-03-18 13:09:57 +000083/// Retrieve reference to a previously daemonized VM and stop it.
Andrew Walbran17de24f2021-05-27 13:27:30 +000084fn command_stop(service: Strong<dyn IVirtualizationService>, cid: u32) -> Result<(), Error> {
85 service
David Brazdil3c2ddef2021-03-18 13:09:57 +000086 .debugDropVmRef(cid as i32)
Andrew Walbranf6bf6862021-05-21 12:41:13 +000087 .context("Failed to get VM from VirtualizationService")?
David Brazdil3c2ddef2021-03-18 13:09:57 +000088 .context("CID does not correspond to a running background VM")?;
Andrew Walbranea9fa482021-03-04 16:11:12 +000089 Ok(())
90}
91
Andrew Walbran320b5602021-03-04 16:11:12 +000092/// List the VMs currently running.
Andrew Walbran17de24f2021-05-27 13:27:30 +000093fn command_list(service: Strong<dyn IVirtualizationService>) -> Result<(), Error> {
94 let vms = service.debugListVms().context("Failed to get list of VMs")?;
Andrew Walbran320b5602021-03-04 16:11:12 +000095 println!("Running VMs: {:#?}", vms);
96 Ok(())
97}
Andrew Walbrandff3b942021-06-09 15:20:36 +000098
99/// Initialise an empty partition image of the given size to be used as a writable partition.
100fn command_create_partition(
101 service: Strong<dyn IVirtualizationService>,
102 image_path: &Path,
103 size: u64,
104) -> Result<(), Error> {
105 let image = OpenOptions::new()
106 .create_new(true)
107 .write(true)
108 .open(image_path)
109 .with_context(|| format!("Failed to create {:?}", image_path))?;
110 service
111 .initializeWritablePartition(&ParcelFileDescriptor::new(image), size.try_into()?)
112 .context("Failed to initialize partition with size {}, size")?;
113 Ok(())
114}