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::{ |
David Brazdil | 7d1e5ec | 2023-02-06 17:56:29 +0000 | [diff] [blame] | 22 | CpuTopology::CpuTopology, IVirtualizationService::IVirtualizationService, |
| 23 | PartitionType::PartitionType, VirtualMachineAppConfig::DebugLevel::DebugLevel, |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame] | 24 | }; |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 25 | use anyhow::{Context, Error}; |
Alan Stokes | c4d5def | 2023-02-14 17:01:59 +0000 | [diff] [blame] | 26 | use binder::{ProcessState, Strong}; |
Jiyong Park | b1935ef | 2023-08-10 17:22:39 +0900 | [diff] [blame^] | 27 | use clap::{Args, Parser}; |
Jooyung Han | c221c05 | 2022-02-22 05:20:15 +0900 | [diff] [blame] | 28 | use create_idsig::command_create_idsig; |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 29 | use create_partition::command_create_partition; |
Nikita Ioffe | b0b6756 | 2022-11-22 15:48:06 +0000 | [diff] [blame] | 30 | use run::{command_run, command_run_app, command_run_microdroid}; |
Nikita Ioffe | 5776f08 | 2023-02-10 21:38:26 +0000 | [diff] [blame] | 31 | use std::num::NonZeroU16; |
Andrew Walbran | c4b1bde | 2022-02-03 15:26:02 +0000 | [diff] [blame] | 32 | use std::path::{Path, PathBuf}; |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 33 | |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 34 | #[derive(Debug)] |
| 35 | struct Idsigs(Vec<PathBuf>); |
| 36 | |
Jiyong Park | b1935ef | 2023-08-10 17:22:39 +0900 | [diff] [blame^] | 37 | #[derive(Args)] |
| 38 | /// Collection of flags that are at VM level and therefore applicable to all subcommands |
| 39 | pub struct CommonConfig { |
| 40 | /// Name of VM |
| 41 | #[arg(long)] |
| 42 | name: Option<String>, |
| 43 | |
| 44 | /// Run VM with vCPU topology matching that of the host. If unspecified, defaults to 1 vCPU. |
| 45 | #[arg(long, default_value = "one_cpu", value_parser = parse_cpu_topology)] |
| 46 | cpu_topology: CpuTopology, |
| 47 | |
| 48 | /// Comma separated list of task profile names to apply to the VM |
| 49 | #[arg(long)] |
| 50 | task_profiles: Vec<String>, |
| 51 | |
| 52 | /// Memory size (in MiB) of the VM. If unspecified, defaults to the value of `memory_mib` |
| 53 | /// in the VM config file. |
| 54 | #[arg(short, long)] |
| 55 | mem: Option<u32>, |
| 56 | |
| 57 | /// Run VM in protected mode. |
| 58 | #[arg(short, long)] |
| 59 | protected: bool, |
| 60 | } |
| 61 | |
| 62 | #[derive(Args)] |
| 63 | /// Collection of flags for debugging |
| 64 | pub struct DebugConfig { |
| 65 | /// Debug level of the VM. Supported values: "full" (default), and "none". |
| 66 | #[arg(long, default_value = "full", value_parser = parse_debug_level)] |
| 67 | debug: DebugLevel, |
| 68 | |
| 69 | /// Path to file for VM console output. |
| 70 | #[arg(long)] |
| 71 | console: Option<PathBuf>, |
| 72 | |
| 73 | /// Path to file for VM console input. |
| 74 | #[arg(long)] |
| 75 | console_in: Option<PathBuf>, |
| 76 | |
| 77 | /// Path to file for VM log output. |
| 78 | #[arg(long)] |
| 79 | log: Option<PathBuf>, |
| 80 | |
| 81 | /// Port at which crosvm will start a gdb server to debug guest kernel. |
| 82 | /// Note: this is only supported on Android kernels android14-5.15 and higher. |
| 83 | #[arg(long)] |
| 84 | gdb: Option<NonZeroU16>, |
| 85 | } |
| 86 | |
| 87 | #[derive(Args)] |
| 88 | /// Collection of flags that are Microdroid specific |
| 89 | pub struct MicrodroidConfig { |
| 90 | /// Path to the file backing the storage. |
| 91 | /// Created if the option is used but the path does not exist in the device. |
| 92 | #[arg(long)] |
| 93 | storage: Option<PathBuf>, |
| 94 | |
| 95 | /// Size of the storage. Used only if --storage is supplied but path does not exist |
| 96 | /// Default size is 10*1024*1024 |
| 97 | #[arg(long)] |
| 98 | storage_size: Option<u64>, |
| 99 | |
| 100 | /// Path to custom kernel image to use when booting Microdroid. |
| 101 | #[arg(long)] |
| 102 | kernel: Option<PathBuf>, |
| 103 | |
| 104 | /// Path to disk image containing vendor-specific modules. |
| 105 | #[arg(long)] |
| 106 | vendor: Option<PathBuf>, |
| 107 | |
| 108 | /// SysFS nodes of devices to assign to VM |
| 109 | #[arg(long)] |
| 110 | devices: Vec<PathBuf>, |
| 111 | } |
| 112 | |
| 113 | #[derive(Args)] |
| 114 | /// Flags for the run_app subcommand |
| 115 | pub struct RunAppConfig { |
| 116 | #[command(flatten)] |
| 117 | common: CommonConfig, |
| 118 | |
| 119 | #[command(flatten)] |
| 120 | debug: DebugConfig, |
| 121 | |
| 122 | #[command(flatten)] |
| 123 | microdroid: MicrodroidConfig, |
| 124 | |
| 125 | /// Path to VM Payload APK |
| 126 | apk: PathBuf, |
| 127 | |
| 128 | /// Path to idsig of the APK |
| 129 | idsig: PathBuf, |
| 130 | |
| 131 | /// Path to the instance image. Created if not exists. |
| 132 | instance: PathBuf, |
| 133 | |
| 134 | /// Path to VM config JSON within APK (e.g. assets/vm_config.json) |
| 135 | #[arg(long)] |
| 136 | config_path: Option<String>, |
| 137 | |
| 138 | /// Name of VM payload binary within APK (e.g. MicrodroidTestNativeLib.so) |
| 139 | #[arg(long)] |
| 140 | #[arg(alias = "payload_path")] |
| 141 | payload_binary_name: Option<String>, |
| 142 | |
| 143 | /// Paths to extra idsig files. |
| 144 | #[arg(long = "extra-idsig")] |
| 145 | extra_idsigs: Vec<PathBuf>, |
| 146 | } |
| 147 | |
| 148 | #[derive(Args)] |
| 149 | /// Flags for the run_microdroid subcommand |
| 150 | pub struct RunMicrodroidConfig { |
| 151 | #[command(flatten)] |
| 152 | common: CommonConfig, |
| 153 | |
| 154 | #[command(flatten)] |
| 155 | debug: DebugConfig, |
| 156 | |
| 157 | #[command(flatten)] |
| 158 | microdroid: MicrodroidConfig, |
| 159 | |
| 160 | /// Path to the directory where VM-related files (e.g. instance.img, apk.idsig, etc.) will |
| 161 | /// be stored. If not specified a random directory under /data/local/tmp/microdroid will be |
| 162 | /// created and used. |
| 163 | #[arg(long)] |
| 164 | work_dir: Option<PathBuf>, |
| 165 | } |
| 166 | |
| 167 | #[derive(Args)] |
| 168 | /// Flags for the run subcommand |
| 169 | pub struct RunCustomVmConfig { |
| 170 | #[command(flatten)] |
| 171 | common: CommonConfig, |
| 172 | |
| 173 | #[command(flatten)] |
| 174 | debug: DebugConfig, |
| 175 | |
| 176 | /// Path to VM config JSON |
| 177 | config: PathBuf, |
| 178 | } |
| 179 | |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 180 | #[derive(Parser)] |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 181 | enum Opt { |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 182 | /// Run a virtual machine with a config in APK |
| 183 | RunApp { |
Jiyong Park | b1935ef | 2023-08-10 17:22:39 +0900 | [diff] [blame^] | 184 | #[command(flatten)] |
| 185 | config: RunAppConfig, |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 186 | }, |
Nikita Ioffe | b0b6756 | 2022-11-22 15:48:06 +0000 | [diff] [blame] | 187 | /// Run a virtual machine with Microdroid inside |
| 188 | RunMicrodroid { |
Jiyong Park | b1935ef | 2023-08-10 17:22:39 +0900 | [diff] [blame^] | 189 | #[command(flatten)] |
| 190 | config: RunMicrodroidConfig, |
Nikita Ioffe | b0b6756 | 2022-11-22 15:48:06 +0000 | [diff] [blame] | 191 | }, |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 192 | /// Run a virtual machine |
| 193 | Run { |
Jiyong Park | b1935ef | 2023-08-10 17:22:39 +0900 | [diff] [blame^] | 194 | #[command(flatten)] |
| 195 | config: RunCustomVmConfig, |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 196 | }, |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 197 | /// List running virtual machines |
| 198 | List, |
Andrew Walbran | c4b1bde | 2022-02-03 15:26:02 +0000 | [diff] [blame] | 199 | /// Print information about virtual machine support |
| 200 | Info, |
Andrew Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 201 | /// Create a new empty partition to be used as a writable partition for a VM |
| 202 | CreatePartition { |
| 203 | /// Path at which to create the image file |
Andrew Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 204 | path: PathBuf, |
| 205 | |
| 206 | /// The desired size of the partition, in bytes. |
| 207 | size: u64, |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 208 | |
| 209 | /// Type of the partition |
Jiyong Park | b1935ef | 2023-08-10 17:22:39 +0900 | [diff] [blame^] | 210 | #[arg(short = 't', long = "type", default_value = "raw", |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 211 | value_parser = parse_partition_type)] |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 212 | partition_type: PartitionType, |
Andrew Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 213 | }, |
Jooyung Han | c221c05 | 2022-02-22 05:20:15 +0900 | [diff] [blame] | 214 | /// Creates or update the idsig file by digesting the input APK file. |
| 215 | CreateIdsig { |
| 216 | /// Path to VM Payload APK |
Jooyung Han | c221c05 | 2022-02-22 05:20:15 +0900 | [diff] [blame] | 217 | apk: PathBuf, |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 218 | |
Jooyung Han | c221c05 | 2022-02-22 05:20:15 +0900 | [diff] [blame] | 219 | /// Path to idsig of the APK |
Jooyung Han | c221c05 | 2022-02-22 05:20:15 +0900 | [diff] [blame] | 220 | path: PathBuf, |
| 221 | }, |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame] | 224 | fn parse_debug_level(s: &str) -> Result<DebugLevel, String> { |
| 225 | match s { |
| 226 | "none" => Ok(DebugLevel::NONE), |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame] | 227 | "full" => Ok(DebugLevel::FULL), |
| 228 | _ => Err(format!("Invalid debug level {}", s)), |
| 229 | } |
| 230 | } |
| 231 | |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 232 | fn parse_partition_type(s: &str) -> Result<PartitionType, String> { |
| 233 | match s { |
| 234 | "raw" => Ok(PartitionType::RAW), |
| 235 | "instance" => Ok(PartitionType::ANDROID_VM_INSTANCE), |
| 236 | _ => Err(format!("Invalid partition type {}", s)), |
| 237 | } |
| 238 | } |
| 239 | |
David Brazdil | 7d1e5ec | 2023-02-06 17:56:29 +0000 | [diff] [blame] | 240 | fn parse_cpu_topology(s: &str) -> Result<CpuTopology, String> { |
| 241 | match s { |
| 242 | "one_cpu" => Ok(CpuTopology::ONE_CPU), |
| 243 | "match_host" => Ok(CpuTopology::MATCH_HOST), |
| 244 | _ => Err(format!("Invalid cpu topology {}", s)), |
| 245 | } |
| 246 | } |
| 247 | |
Alan Stokes | c4d5def | 2023-02-14 17:01:59 +0000 | [diff] [blame] | 248 | fn get_service() -> Result<Strong<dyn IVirtualizationService>, Error> { |
| 249 | let virtmgr = |
| 250 | vmclient::VirtualizationService::new().context("Failed to spawn VirtualizationService")?; |
| 251 | virtmgr.connect().context("Failed to connect to VirtualizationService") |
| 252 | } |
| 253 | |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 254 | fn main() -> Result<(), Error> { |
| 255 | env_logger::init(); |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 256 | let opt = Opt::parse(); |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 257 | |
| 258 | // We need to start the thread pool for Binder to work properly, especially link_to_death. |
| 259 | ProcessState::start_thread_pool(); |
| 260 | |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 261 | match opt { |
Jiyong Park | b1935ef | 2023-08-10 17:22:39 +0900 | [diff] [blame^] | 262 | Opt::RunApp { config } => command_run_app(config), |
| 263 | Opt::RunMicrodroid { config } => command_run_microdroid(config), |
| 264 | Opt::Run { config } => command_run(config), |
Alan Stokes | c4d5def | 2023-02-14 17:01:59 +0000 | [diff] [blame] | 265 | Opt::List => command_list(get_service()?.as_ref()), |
Andrew Walbran | c4b1bde | 2022-02-03 15:26:02 +0000 | [diff] [blame] | 266 | Opt::Info => command_info(), |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 267 | Opt::CreatePartition { path, size, partition_type } => { |
Alan Stokes | c4d5def | 2023-02-14 17:01:59 +0000 | [diff] [blame] | 268 | command_create_partition(get_service()?.as_ref(), &path, size, partition_type) |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 269 | } |
Alan Stokes | c4d5def | 2023-02-14 17:01:59 +0000 | [diff] [blame] | 270 | Opt::CreateIdsig { apk, path } => { |
| 271 | command_create_idsig(get_service()?.as_ref(), &apk, &path) |
| 272 | } |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 276 | /// List the VMs currently running. |
Andrew Walbran | 616d13f | 2022-05-12 18:35:55 +0000 | [diff] [blame] | 277 | fn command_list(service: &dyn IVirtualizationService) -> Result<(), Error> { |
Andrew Walbran | 17de24f | 2021-05-27 13:27:30 +0000 | [diff] [blame] | 278 | let vms = service.debugListVms().context("Failed to get list of VMs")?; |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 279 | println!("Running VMs: {:#?}", vms); |
| 280 | Ok(()) |
| 281 | } |
Andrew Walbran | c4b1bde | 2022-02-03 15:26:02 +0000 | [diff] [blame] | 282 | |
| 283 | /// Print information about supported VM types. |
| 284 | fn command_info() -> Result<(), Error> { |
Alan Stokes | c4d5def | 2023-02-14 17:01:59 +0000 | [diff] [blame] | 285 | let non_protected_vm_supported = hypervisor_props::is_vm_supported()?; |
| 286 | let protected_vm_supported = hypervisor_props::is_protected_vm_supported()?; |
Alan Stokes | 8d39a9b | 2023-01-10 15:01:00 +0000 | [diff] [blame] | 287 | match (non_protected_vm_supported, protected_vm_supported) { |
Andrew Walbran | c4b1bde | 2022-02-03 15:26:02 +0000 | [diff] [blame] | 288 | (false, false) => println!("VMs are not supported."), |
| 289 | (false, true) => println!("Only protected VMs are supported."), |
Alan Stokes | 8d39a9b | 2023-01-10 15:01:00 +0000 | [diff] [blame] | 290 | (true, false) => println!("Only non-protected VMs are supported."), |
| 291 | (true, true) => println!("Both protected and non-protected VMs are supported."), |
Andrew Walbran | c4b1bde | 2022-02-03 15:26:02 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Alan Stokes | c4d5def | 2023-02-14 17:01:59 +0000 | [diff] [blame] | 294 | if let Some(version) = hypervisor_props::version()? { |
Andrew Walbran | c4b1bde | 2022-02-03 15:26:02 +0000 | [diff] [blame] | 295 | println!("Hypervisor version: {}", version); |
| 296 | } else { |
| 297 | println!("Hypervisor version not set."); |
| 298 | } |
| 299 | |
| 300 | if Path::new("/dev/kvm").exists() { |
| 301 | println!("/dev/kvm exists."); |
| 302 | } else { |
| 303 | println!("/dev/kvm does not exist."); |
| 304 | } |
| 305 | |
Inseob Kim | 6ef8097 | 2023-07-20 17:23:36 +0900 | [diff] [blame] | 306 | if Path::new("/dev/vfio/vfio").exists() { |
| 307 | println!("/dev/vfio/vfio exists."); |
| 308 | } else { |
| 309 | println!("/dev/vfio/vfio does not exist."); |
| 310 | } |
| 311 | |
| 312 | if Path::new("/sys/bus/platform/drivers/vfio-platform").exists() { |
| 313 | println!("VFIO-platform is supported."); |
| 314 | } else { |
| 315 | println!("VFIO-platform is not supported."); |
| 316 | } |
| 317 | |
Inseob Kim | 75460b3 | 2023-08-09 13:41:31 +0900 | [diff] [blame] | 318 | let devices = get_service()?.getAssignableDevices()?; |
| 319 | let devices = devices.into_iter().map(|x| x.node).collect::<Vec<_>>(); |
| 320 | println!("Assignable devices: {}", serde_json::to_string(&devices)?); |
| 321 | |
Andrew Walbran | c4b1bde | 2022-02-03 15:26:02 +0000 | [diff] [blame] | 322 | Ok(()) |
| 323 | } |
Andrew Walbran | 1f810b6 | 2022-08-10 13:33:57 +0000 | [diff] [blame] | 324 | |
| 325 | #[cfg(test)] |
| 326 | mod tests { |
| 327 | use super::*; |
Andrew Walbran | aa1efc4 | 2022-08-10 13:33:57 +0000 | [diff] [blame] | 328 | use clap::CommandFactory; |
Andrew Walbran | 1f810b6 | 2022-08-10 13:33:57 +0000 | [diff] [blame] | 329 | |
| 330 | #[test] |
| 331 | fn verify_app() { |
Andrew Walbran | aa1efc4 | 2022-08-10 13:33:57 +0000 | [diff] [blame] | 332 | // Check that the command parsing has been configured in a valid way. |
| 333 | Opt::command().debug_assert(); |
Andrew Walbran | 1f810b6 | 2022-08-10 13:33:57 +0000 | [diff] [blame] | 334 | } |
| 335 | } |