blob: 9ca0ea68da1141caa42ab9e076f227c80548b54c [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
Andrew Walbranf6bf6862021-05-21 12:41:13 +000020use android_system_virtualizationservice::aidl::android::system::virtualizationservice::IVirtualizationService::IVirtualizationService;
Andrew Walbrandff3b942021-06-09 15:20:36 +000021use android_system_virtualizationservice::binder::{wait_for_interface, ProcessState, Strong, ParcelFileDescriptor};
David Brazdil20412d92021-03-18 10:53:06 +000022use anyhow::{Context, Error};
Jooyung Han21e9b922021-06-26 04:14:16 +090023use run::{command_run, command_run_app};
Andrew Walbrandff3b942021-06-09 15:20:36 +000024use std::convert::TryInto;
25use std::fs::OpenOptions;
26use std::path::{PathBuf, Path};
David Brazdil20412d92021-03-18 10:53:06 +000027use structopt::clap::AppSettings;
28use structopt::StructOpt;
Andrew Walbranea9fa482021-03-04 16:11:12 +000029
Andrew Walbran17de24f2021-05-27 13:27:30 +000030const VIRTUALIZATION_SERVICE_BINDER_SERVICE_IDENTIFIER: &str =
31 "android.system.virtualizationservice";
Andrew Walbranea9fa482021-03-04 16:11:12 +000032
David Brazdil20412d92021-03-18 10:53:06 +000033#[derive(StructOpt)]
34#[structopt(no_version, global_settings = &[AppSettings::DisableVersion])]
35enum Opt {
Jooyung Han21e9b922021-06-26 04:14:16 +090036 /// Run a virtual machine with a config in APK
37 RunApp {
38 /// Path to VM Payload APK
39 #[structopt(parse(from_os_str))]
40 apk: PathBuf,
41
42 /// Path to idsig of the APK
43 #[structopt(parse(from_os_str))]
44 idsig: PathBuf,
45
46 /// Path to VM config JSON within APK (e.g. assets/vm_config.json)
47 config_path: String,
48
49 /// Detach VM from the terminal and run in the background
50 #[structopt(short, long)]
51 daemonize: bool,
52
53 /// Path to file for VM log output.
54 #[structopt(short, long)]
55 log: Option<PathBuf>,
Jiyong Park23601142021-07-05 13:15:32 +090056
57 /// Whether to run VM in debug mode.
58 #[structopt(short, long)]
59 debug: bool,
Jooyung Han21e9b922021-06-26 04:14:16 +090060 },
David Brazdil20412d92021-03-18 10:53:06 +000061 /// Run a virtual machine
62 Run {
63 /// Path to VM config JSON
64 #[structopt(parse(from_os_str))]
65 config: PathBuf,
David Brazdil3c2ddef2021-03-18 13:09:57 +000066
67 /// Detach VM from the terminal and run in the background
68 #[structopt(short, long)]
69 daemonize: bool,
Andrew Walbranbe429242021-06-28 12:22:54 +000070
71 /// Path to file for VM log output.
72 #[structopt(short, long)]
73 log: Option<PathBuf>,
David Brazdil3c2ddef2021-03-18 13:09:57 +000074 },
75 /// Stop a virtual machine running in the background
76 Stop {
77 /// CID of the virtual machine
78 cid: u32,
David Brazdil20412d92021-03-18 10:53:06 +000079 },
80 /// List running virtual machines
81 List,
Andrew Walbrandff3b942021-06-09 15:20:36 +000082 /// Create a new empty partition to be used as a writable partition for a VM
83 CreatePartition {
84 /// Path at which to create the image file
85 #[structopt(parse(from_os_str))]
86 path: PathBuf,
87
88 /// The desired size of the partition, in bytes.
89 size: u64,
90 },
David Brazdil20412d92021-03-18 10:53:06 +000091}
92
Andrew Walbranea9fa482021-03-04 16:11:12 +000093fn main() -> Result<(), Error> {
94 env_logger::init();
David Brazdil20412d92021-03-18 10:53:06 +000095 let opt = Opt::from_args();
Andrew Walbranea9fa482021-03-04 16:11:12 +000096
97 // We need to start the thread pool for Binder to work properly, especially link_to_death.
98 ProcessState::start_thread_pool();
99
Andrew Walbranf1453802021-03-29 17:12:54 +0000100 let service = wait_for_interface(VIRTUALIZATION_SERVICE_BINDER_SERVICE_IDENTIFIER)
Andrew Walbranf6bf6862021-05-21 12:41:13 +0000101 .context("Failed to find VirtualizationService")?;
Andrew Walbran320b5602021-03-04 16:11:12 +0000102
David Brazdil20412d92021-03-18 10:53:06 +0000103 match opt {
Jiyong Park23601142021-07-05 13:15:32 +0900104 Opt::RunApp { apk, idsig, config_path, daemonize, log, debug } => {
105 command_run_app(service, &apk, &idsig, &config_path, daemonize, log.as_deref(), debug)
Jooyung Han21e9b922021-06-26 04:14:16 +0900106 }
Andrew Walbranbe429242021-06-28 12:22:54 +0000107 Opt::Run { config, daemonize, log } => {
108 command_run(service, &config, daemonize, log.as_deref())
109 }
Andrew Walbran17de24f2021-05-27 13:27:30 +0000110 Opt::Stop { cid } => command_stop(service, cid),
111 Opt::List => command_list(service),
Andrew Walbrandff3b942021-06-09 15:20:36 +0000112 Opt::CreatePartition { path, size } => command_create_partition(service, &path, size),
Andrew Walbranea9fa482021-03-04 16:11:12 +0000113 }
114}
115
David Brazdil3c2ddef2021-03-18 13:09:57 +0000116/// Retrieve reference to a previously daemonized VM and stop it.
Andrew Walbran17de24f2021-05-27 13:27:30 +0000117fn command_stop(service: Strong<dyn IVirtualizationService>, cid: u32) -> Result<(), Error> {
118 service
David Brazdil3c2ddef2021-03-18 13:09:57 +0000119 .debugDropVmRef(cid as i32)
Andrew Walbranf6bf6862021-05-21 12:41:13 +0000120 .context("Failed to get VM from VirtualizationService")?
David Brazdil3c2ddef2021-03-18 13:09:57 +0000121 .context("CID does not correspond to a running background VM")?;
Andrew Walbranea9fa482021-03-04 16:11:12 +0000122 Ok(())
123}
124
Andrew Walbran320b5602021-03-04 16:11:12 +0000125/// List the VMs currently running.
Andrew Walbran17de24f2021-05-27 13:27:30 +0000126fn command_list(service: Strong<dyn IVirtualizationService>) -> Result<(), Error> {
127 let vms = service.debugListVms().context("Failed to get list of VMs")?;
Andrew Walbran320b5602021-03-04 16:11:12 +0000128 println!("Running VMs: {:#?}", vms);
129 Ok(())
130}
Andrew Walbrandff3b942021-06-09 15:20:36 +0000131
132/// Initialise an empty partition image of the given size to be used as a writable partition.
133fn command_create_partition(
134 service: Strong<dyn IVirtualizationService>,
135 image_path: &Path,
136 size: u64,
137) -> Result<(), Error> {
138 let image = OpenOptions::new()
139 .create_new(true)
Andrew Walbrandfc953d2021-06-10 13:59:56 +0000140 .read(true)
Andrew Walbrandff3b942021-06-09 15:20:36 +0000141 .write(true)
142 .open(image_path)
143 .with_context(|| format!("Failed to create {:?}", image_path))?;
144 service
145 .initializeWritablePartition(&ParcelFileDescriptor::new(image), size.try_into()?)
146 .context("Failed to initialize partition with size {}, size")?;
147 Ok(())
148}