blob: fe47d2c550e47f50fa99b5d81f52b6e59277e19f [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
Jiyong Park48b354d2021-07-15 15:04:38 +090017mod create_partition;
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;
Jiyong Park9dd389e2021-08-23 20:42:59 +090022use android_system_virtualizationservice::aidl::android::system::virtualizationservice::PartitionType::PartitionType;
Jiyong Park48b354d2021-07-15 15:04:38 +090023use android_system_virtualizationservice::binder::{wait_for_interface, ProcessState, Strong};
David Brazdil20412d92021-03-18 10:53:06 +000024use anyhow::{Context, Error};
Jiyong Park48b354d2021-07-15 15:04:38 +090025use create_partition::command_create_partition;
Jooyung Han21e9b922021-06-26 04:14:16 +090026use run::{command_run, command_run_app};
Jiyong Park48b354d2021-07-15 15:04:38 +090027use std::path::PathBuf;
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 {
Jooyung Han21e9b922021-06-26 04:14:16 +090037 /// Run a virtual machine with a config in APK
38 RunApp {
39 /// Path to VM Payload APK
40 #[structopt(parse(from_os_str))]
41 apk: PathBuf,
42
43 /// Path to idsig of the APK
44 #[structopt(parse(from_os_str))]
45 idsig: PathBuf,
46
Jiyong Park48b354d2021-07-15 15:04:38 +090047 /// Path to the instance image. Created if not exists.
48 #[structopt(parse(from_os_str))]
49 instance: PathBuf,
50
Jooyung Han21e9b922021-06-26 04:14:16 +090051 /// Path to VM config JSON within APK (e.g. assets/vm_config.json)
52 config_path: String,
53
54 /// Detach VM from the terminal and run in the background
55 #[structopt(short, long)]
56 daemonize: bool,
57
58 /// Path to file for VM log output.
59 #[structopt(short, long)]
60 log: Option<PathBuf>,
Jiyong Park23601142021-07-05 13:15:32 +090061
62 /// Whether to run VM in debug mode.
63 #[structopt(short, long)]
64 debug: bool,
Jooyung Han21e9b922021-06-26 04:14:16 +090065 },
David Brazdil20412d92021-03-18 10:53:06 +000066 /// Run a virtual machine
67 Run {
68 /// Path to VM config JSON
69 #[structopt(parse(from_os_str))]
70 config: PathBuf,
David Brazdil3c2ddef2021-03-18 13:09:57 +000071
72 /// Detach VM from the terminal and run in the background
73 #[structopt(short, long)]
74 daemonize: bool,
Andrew Walbranbe429242021-06-28 12:22:54 +000075
76 /// Path to file for VM log output.
77 #[structopt(short, long)]
78 log: Option<PathBuf>,
David Brazdil3c2ddef2021-03-18 13:09:57 +000079 },
80 /// Stop a virtual machine running in the background
81 Stop {
82 /// CID of the virtual machine
83 cid: u32,
David Brazdil20412d92021-03-18 10:53:06 +000084 },
85 /// List running virtual machines
86 List,
Andrew Walbrandff3b942021-06-09 15:20:36 +000087 /// Create a new empty partition to be used as a writable partition for a VM
88 CreatePartition {
89 /// Path at which to create the image file
90 #[structopt(parse(from_os_str))]
91 path: PathBuf,
92
93 /// The desired size of the partition, in bytes.
94 size: u64,
Jiyong Park9dd389e2021-08-23 20:42:59 +090095
96 /// Type of the partition
97 #[structopt(short="t", long="type", default_value="raw", parse(try_from_str=parse_partition_type))]
98 partition_type: PartitionType,
Andrew Walbrandff3b942021-06-09 15:20:36 +000099 },
David Brazdil20412d92021-03-18 10:53:06 +0000100}
101
Jiyong Park9dd389e2021-08-23 20:42:59 +0900102fn parse_partition_type(s: &str) -> Result<PartitionType, String> {
103 match s {
104 "raw" => Ok(PartitionType::RAW),
105 "instance" => Ok(PartitionType::ANDROID_VM_INSTANCE),
106 _ => Err(format!("Invalid partition type {}", s)),
107 }
108}
109
Andrew Walbranea9fa482021-03-04 16:11:12 +0000110fn main() -> Result<(), Error> {
111 env_logger::init();
David Brazdil20412d92021-03-18 10:53:06 +0000112 let opt = Opt::from_args();
Andrew Walbranea9fa482021-03-04 16:11:12 +0000113
114 // We need to start the thread pool for Binder to work properly, especially link_to_death.
115 ProcessState::start_thread_pool();
116
Andrew Walbranf1453802021-03-29 17:12:54 +0000117 let service = wait_for_interface(VIRTUALIZATION_SERVICE_BINDER_SERVICE_IDENTIFIER)
Andrew Walbranf6bf6862021-05-21 12:41:13 +0000118 .context("Failed to find VirtualizationService")?;
Andrew Walbran320b5602021-03-04 16:11:12 +0000119
David Brazdil20412d92021-03-18 10:53:06 +0000120 match opt {
Jiyong Park48b354d2021-07-15 15:04:38 +0900121 Opt::RunApp { apk, idsig, instance, config_path, daemonize, log, debug } => {
122 command_run_app(
123 service,
124 &apk,
125 &idsig,
126 &instance,
127 &config_path,
128 daemonize,
129 log.as_deref(),
130 debug,
131 )
Jooyung Han21e9b922021-06-26 04:14:16 +0900132 }
Andrew Walbranbe429242021-06-28 12:22:54 +0000133 Opt::Run { config, daemonize, log } => {
134 command_run(service, &config, daemonize, log.as_deref())
135 }
Andrew Walbran17de24f2021-05-27 13:27:30 +0000136 Opt::Stop { cid } => command_stop(service, cid),
137 Opt::List => command_list(service),
Jiyong Park9dd389e2021-08-23 20:42:59 +0900138 Opt::CreatePartition { path, size, partition_type } => {
139 command_create_partition(service, &path, size, partition_type)
140 }
Andrew Walbranea9fa482021-03-04 16:11:12 +0000141 }
142}
143
David Brazdil3c2ddef2021-03-18 13:09:57 +0000144/// Retrieve reference to a previously daemonized VM and stop it.
Andrew Walbran17de24f2021-05-27 13:27:30 +0000145fn command_stop(service: Strong<dyn IVirtualizationService>, cid: u32) -> Result<(), Error> {
146 service
David Brazdil3c2ddef2021-03-18 13:09:57 +0000147 .debugDropVmRef(cid as i32)
Andrew Walbranf6bf6862021-05-21 12:41:13 +0000148 .context("Failed to get VM from VirtualizationService")?
David Brazdil3c2ddef2021-03-18 13:09:57 +0000149 .context("CID does not correspond to a running background VM")?;
Andrew Walbranea9fa482021-03-04 16:11:12 +0000150 Ok(())
151}
152
Andrew Walbran320b5602021-03-04 16:11:12 +0000153/// List the VMs currently running.
Andrew Walbran17de24f2021-05-27 13:27:30 +0000154fn command_list(service: Strong<dyn IVirtualizationService>) -> Result<(), Error> {
155 let vms = service.debugListVms().context("Failed to get list of VMs")?;
Andrew Walbran320b5602021-03-04 16:11:12 +0000156 println!("Running VMs: {:#?}", vms);
157 Ok(())
158}