Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 1 | // 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 | |
Jooyung Han | c221c05 | 2022-02-22 05:20:15 +0900 | [diff] [blame] | 17 | mod create_idsig; |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 18 | mod create_partition; |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 19 | mod run; |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 20 | |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame] | 21 | use android_system_virtualizationservice::aidl::android::system::virtualizationservice::{ |
| 22 | IVirtualizationService::IVirtualizationService, PartitionType::PartitionType, |
| 23 | VirtualMachineAppConfig::DebugLevel::DebugLevel, |
| 24 | }; |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 25 | use anyhow::{Context, Error}; |
Alan Stokes | 0e82b50 | 2022-08-08 14:44:48 +0100 | [diff] [blame] | 26 | use binder::ProcessState; |
Jooyung Han | c221c05 | 2022-02-22 05:20:15 +0900 | [diff] [blame] | 27 | use create_idsig::command_create_idsig; |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 28 | use create_partition::command_create_partition; |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 29 | use run::{command_run, command_run_app}; |
Andrew Walbran | c4b1bde | 2022-02-03 15:26:02 +0000 | [diff] [blame] | 30 | use rustutils::system_properties; |
| 31 | use std::path::{Path, PathBuf}; |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 32 | use structopt::clap::AppSettings; |
| 33 | use structopt::StructOpt; |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 34 | |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 35 | #[derive(Debug)] |
| 36 | struct Idsigs(Vec<PathBuf>); |
| 37 | |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 38 | #[derive(StructOpt)] |
| 39 | #[structopt(no_version, global_settings = &[AppSettings::DisableVersion])] |
| 40 | enum Opt { |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 41 | /// Run a virtual machine with a config in APK |
| 42 | RunApp { |
Seungjae Yoo | 62085c0 | 2022-08-12 04:44:52 +0000 | [diff] [blame] | 43 | /// Name of VM |
| 44 | #[structopt(long)] |
| 45 | name: Option<String>, |
| 46 | |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 47 | /// Path to VM Payload APK |
| 48 | #[structopt(parse(from_os_str))] |
| 49 | apk: PathBuf, |
| 50 | |
| 51 | /// Path to idsig of the APK |
| 52 | #[structopt(parse(from_os_str))] |
| 53 | idsig: PathBuf, |
| 54 | |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 55 | /// Path to the instance image. Created if not exists. |
| 56 | #[structopt(parse(from_os_str))] |
| 57 | instance: PathBuf, |
| 58 | |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 59 | /// Path to VM config JSON within APK (e.g. assets/vm_config.json) |
| 60 | config_path: String, |
| 61 | |
| 62 | /// Detach VM from the terminal and run in the background |
| 63 | #[structopt(short, long)] |
| 64 | daemonize: bool, |
| 65 | |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 66 | /// Path to file for VM console output. |
| 67 | #[structopt(long)] |
| 68 | console: Option<PathBuf>, |
| 69 | |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 70 | /// Path to file for VM log output. |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 71 | #[structopt(long)] |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 72 | log: Option<PathBuf>, |
Jiyong Park | 2360114 | 2021-07-05 13:15:32 +0900 | [diff] [blame] | 73 | |
Jiyong Park | e558ab1 | 2022-07-07 20:18:55 +0900 | [diff] [blame] | 74 | /// Path to file where ramdump is recorded on kernel panic |
| 75 | #[structopt(long)] |
| 76 | ramdump: Option<PathBuf>, |
| 77 | |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame] | 78 | /// Debug level of the VM. Supported values: "none" (default), "app_only", and "full". |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 79 | #[structopt(long, default_value = "none", parse(try_from_str=parse_debug_level))] |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame] | 80 | debug: DebugLevel, |
Jiyong Park | d63cfff | 2021-09-27 20:10:17 +0900 | [diff] [blame] | 81 | |
Andrew Walbran | 3994f00 | 2022-01-27 17:33:45 +0000 | [diff] [blame] | 82 | /// Run VM in protected mode. |
| 83 | #[structopt(short, long)] |
| 84 | protected: bool, |
| 85 | |
Jiyong Park | d63cfff | 2021-09-27 20:10:17 +0900 | [diff] [blame] | 86 | /// Memory size (in MiB) of the VM. If unspecified, defaults to the value of `memory_mib` |
| 87 | /// in the VM config file. |
| 88 | #[structopt(short, long)] |
| 89 | mem: Option<u32>, |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 90 | |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 91 | /// Number of vCPUs in the VM. If unspecified, defaults to 1. |
| 92 | #[structopt(long)] |
| 93 | cpus: Option<u32>, |
| 94 | |
Jiyong Park | dfe16d6 | 2022-04-20 17:32:12 +0900 | [diff] [blame] | 95 | /// Comma separated list of task profile names to apply to the VM |
| 96 | #[structopt(long)] |
| 97 | task_profiles: Vec<String>, |
| 98 | |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 99 | /// Paths to extra idsig files. |
Victor Hsieh | 9978257 | 2022-01-05 15:38:33 -0800 | [diff] [blame] | 100 | #[structopt(long = "extra-idsig")] |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 101 | extra_idsigs: Vec<PathBuf>, |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 102 | }, |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 103 | /// Run a virtual machine |
| 104 | Run { |
Seungjae Yoo | 62085c0 | 2022-08-12 04:44:52 +0000 | [diff] [blame] | 105 | /// Name of VM |
| 106 | #[structopt(long)] |
| 107 | name: Option<String>, |
| 108 | |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 109 | /// Path to VM config JSON |
| 110 | #[structopt(parse(from_os_str))] |
| 111 | config: PathBuf, |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 112 | |
| 113 | /// Detach VM from the terminal and run in the background |
| 114 | #[structopt(short, long)] |
| 115 | daemonize: bool, |
Andrew Walbran | be42924 | 2021-06-28 12:22:54 +0000 | [diff] [blame] | 116 | |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 117 | /// Number of vCPUs in the VM. If unspecified, defaults to 1. |
| 118 | #[structopt(long)] |
| 119 | cpus: Option<u32>, |
| 120 | |
Jiyong Park | dfe16d6 | 2022-04-20 17:32:12 +0900 | [diff] [blame] | 121 | /// Comma separated list of task profile names to apply to the VM |
| 122 | #[structopt(long)] |
| 123 | task_profiles: Vec<String>, |
| 124 | |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 125 | /// Path to file for VM console output. |
| 126 | #[structopt(long)] |
| 127 | console: Option<PathBuf>, |
Jooyung Han | b7983a2 | 2022-02-22 05:21:27 +0900 | [diff] [blame] | 128 | |
| 129 | /// Path to file for VM log output. |
| 130 | #[structopt(long)] |
| 131 | log: Option<PathBuf>, |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 132 | }, |
| 133 | /// Stop a virtual machine running in the background |
| 134 | Stop { |
| 135 | /// CID of the virtual machine |
| 136 | cid: u32, |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 137 | }, |
| 138 | /// List running virtual machines |
| 139 | List, |
Andrew Walbran | c4b1bde | 2022-02-03 15:26:02 +0000 | [diff] [blame] | 140 | /// Print information about virtual machine support |
| 141 | Info, |
Andrew Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 142 | /// Create a new empty partition to be used as a writable partition for a VM |
| 143 | CreatePartition { |
| 144 | /// Path at which to create the image file |
| 145 | #[structopt(parse(from_os_str))] |
| 146 | path: PathBuf, |
| 147 | |
| 148 | /// The desired size of the partition, in bytes. |
| 149 | size: u64, |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 150 | |
| 151 | /// Type of the partition |
| 152 | #[structopt(short="t", long="type", default_value="raw", parse(try_from_str=parse_partition_type))] |
| 153 | partition_type: PartitionType, |
Andrew Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 154 | }, |
Jooyung Han | c221c05 | 2022-02-22 05:20:15 +0900 | [diff] [blame] | 155 | /// Creates or update the idsig file by digesting the input APK file. |
| 156 | CreateIdsig { |
| 157 | /// Path to VM Payload APK |
| 158 | #[structopt(parse(from_os_str))] |
| 159 | apk: PathBuf, |
| 160 | /// Path to idsig of the APK |
| 161 | #[structopt(parse(from_os_str))] |
| 162 | path: PathBuf, |
| 163 | }, |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame] | 166 | fn parse_debug_level(s: &str) -> Result<DebugLevel, String> { |
| 167 | match s { |
| 168 | "none" => Ok(DebugLevel::NONE), |
| 169 | "app_only" => Ok(DebugLevel::APP_ONLY), |
| 170 | "full" => Ok(DebugLevel::FULL), |
| 171 | _ => Err(format!("Invalid debug level {}", s)), |
| 172 | } |
| 173 | } |
| 174 | |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 175 | fn parse_partition_type(s: &str) -> Result<PartitionType, String> { |
| 176 | match s { |
| 177 | "raw" => Ok(PartitionType::RAW), |
| 178 | "instance" => Ok(PartitionType::ANDROID_VM_INSTANCE), |
| 179 | _ => Err(format!("Invalid partition type {}", s)), |
| 180 | } |
| 181 | } |
| 182 | |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 183 | fn main() -> Result<(), Error> { |
| 184 | env_logger::init(); |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 185 | let opt = Opt::from_args(); |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 186 | |
| 187 | // We need to start the thread pool for Binder to work properly, especially link_to_death. |
| 188 | ProcessState::start_thread_pool(); |
| 189 | |
Andrew Walbran | d0ef400 | 2022-05-16 16:14:10 +0000 | [diff] [blame] | 190 | let service = vmclient::connect().context("Failed to find VirtualizationService")?; |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 191 | |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 192 | match opt { |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 193 | Opt::RunApp { |
Seungjae Yoo | 62085c0 | 2022-08-12 04:44:52 +0000 | [diff] [blame] | 194 | name, |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 195 | apk, |
| 196 | idsig, |
| 197 | instance, |
| 198 | config_path, |
| 199 | daemonize, |
| 200 | console, |
| 201 | log, |
Jiyong Park | e558ab1 | 2022-07-07 20:18:55 +0900 | [diff] [blame] | 202 | ramdump, |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 203 | debug, |
Andrew Walbran | 3994f00 | 2022-01-27 17:33:45 +0000 | [diff] [blame] | 204 | protected, |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 205 | mem, |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 206 | cpus, |
Jiyong Park | dfe16d6 | 2022-04-20 17:32:12 +0900 | [diff] [blame] | 207 | task_profiles, |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 208 | extra_idsigs, |
| 209 | } => command_run_app( |
Seungjae Yoo | 62085c0 | 2022-08-12 04:44:52 +0000 | [diff] [blame] | 210 | name, |
Andrew Walbran | 616d13f | 2022-05-12 18:35:55 +0000 | [diff] [blame] | 211 | service.as_ref(), |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 212 | &apk, |
| 213 | &idsig, |
| 214 | &instance, |
| 215 | &config_path, |
| 216 | daemonize, |
| 217 | console.as_deref(), |
| 218 | log.as_deref(), |
Jiyong Park | e558ab1 | 2022-07-07 20:18:55 +0900 | [diff] [blame] | 219 | ramdump.as_deref(), |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 220 | debug, |
Andrew Walbran | 3994f00 | 2022-01-27 17:33:45 +0000 | [diff] [blame] | 221 | protected, |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 222 | mem, |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 223 | cpus, |
Jiyong Park | dfe16d6 | 2022-04-20 17:32:12 +0900 | [diff] [blame] | 224 | task_profiles, |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 225 | &extra_idsigs, |
| 226 | ), |
Victor Hsieh | f219cd8 | 2022-09-09 13:13:11 -0700 | [diff] [blame^] | 227 | Opt::Run { name, config, daemonize, cpus, task_profiles, console, log } => { |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 228 | command_run( |
Seungjae Yoo | 62085c0 | 2022-08-12 04:44:52 +0000 | [diff] [blame] | 229 | name, |
Andrew Walbran | 616d13f | 2022-05-12 18:35:55 +0000 | [diff] [blame] | 230 | service.as_ref(), |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 231 | &config, |
| 232 | daemonize, |
| 233 | console.as_deref(), |
Jooyung Han | b7983a2 | 2022-02-22 05:21:27 +0900 | [diff] [blame] | 234 | log.as_deref(), |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 235 | /* mem */ None, |
| 236 | cpus, |
Jiyong Park | dfe16d6 | 2022-04-20 17:32:12 +0900 | [diff] [blame] | 237 | task_profiles, |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 238 | ) |
Andrew Walbran | be42924 | 2021-06-28 12:22:54 +0000 | [diff] [blame] | 239 | } |
Andrew Walbran | 616d13f | 2022-05-12 18:35:55 +0000 | [diff] [blame] | 240 | Opt::Stop { cid } => command_stop(service.as_ref(), cid), |
| 241 | Opt::List => command_list(service.as_ref()), |
Andrew Walbran | c4b1bde | 2022-02-03 15:26:02 +0000 | [diff] [blame] | 242 | Opt::Info => command_info(), |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 243 | Opt::CreatePartition { path, size, partition_type } => { |
Andrew Walbran | 616d13f | 2022-05-12 18:35:55 +0000 | [diff] [blame] | 244 | command_create_partition(service.as_ref(), &path, size, partition_type) |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 245 | } |
Andrew Walbran | 616d13f | 2022-05-12 18:35:55 +0000 | [diff] [blame] | 246 | Opt::CreateIdsig { apk, path } => command_create_idsig(service.as_ref(), &apk, &path), |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 250 | /// Retrieve reference to a previously daemonized VM and stop it. |
Andrew Walbran | 616d13f | 2022-05-12 18:35:55 +0000 | [diff] [blame] | 251 | fn command_stop(service: &dyn IVirtualizationService, cid: u32) -> Result<(), Error> { |
Andrew Walbran | 17de24f | 2021-05-27 13:27:30 +0000 | [diff] [blame] | 252 | service |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 253 | .debugDropVmRef(cid as i32) |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 254 | .context("Failed to get VM from VirtualizationService")? |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 255 | .context("CID does not correspond to a running background VM")?; |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 256 | Ok(()) |
| 257 | } |
| 258 | |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 259 | /// List the VMs currently running. |
Andrew Walbran | 616d13f | 2022-05-12 18:35:55 +0000 | [diff] [blame] | 260 | fn command_list(service: &dyn IVirtualizationService) -> Result<(), Error> { |
Andrew Walbran | 17de24f | 2021-05-27 13:27:30 +0000 | [diff] [blame] | 261 | let vms = service.debugListVms().context("Failed to get list of VMs")?; |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 262 | println!("Running VMs: {:#?}", vms); |
| 263 | Ok(()) |
| 264 | } |
Andrew Walbran | c4b1bde | 2022-02-03 15:26:02 +0000 | [diff] [blame] | 265 | |
| 266 | /// Print information about supported VM types. |
| 267 | fn command_info() -> Result<(), Error> { |
| 268 | let unprotected_vm_supported = |
| 269 | system_properties::read_bool("ro.boot.hypervisor.vm.supported", false)?; |
| 270 | let protected_vm_supported = |
| 271 | system_properties::read_bool("ro.boot.hypervisor.protected_vm.supported", false)?; |
| 272 | match (unprotected_vm_supported, protected_vm_supported) { |
| 273 | (false, false) => println!("VMs are not supported."), |
| 274 | (false, true) => println!("Only protected VMs are supported."), |
| 275 | (true, false) => println!("Only unprotected VMs are supported."), |
| 276 | (true, true) => println!("Both protected and unprotected VMs are supported."), |
| 277 | } |
| 278 | |
Andrew Walbran | 014efb5 | 2022-02-03 17:43:11 +0000 | [diff] [blame] | 279 | if let Some(version) = system_properties::read("ro.boot.hypervisor.version")? { |
Andrew Walbran | c4b1bde | 2022-02-03 15:26:02 +0000 | [diff] [blame] | 280 | println!("Hypervisor version: {}", version); |
| 281 | } else { |
| 282 | println!("Hypervisor version not set."); |
| 283 | } |
| 284 | |
| 285 | if Path::new("/dev/kvm").exists() { |
| 286 | println!("/dev/kvm exists."); |
| 287 | } else { |
| 288 | println!("/dev/kvm does not exist."); |
| 289 | } |
| 290 | |
| 291 | Ok(()) |
| 292 | } |