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; |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 27 | use clap::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}; |
Andrew Walbran | c4b1bde | 2022-02-03 15:26:02 +0000 | [diff] [blame] | 31 | use rustutils::system_properties; |
| 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 | |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 37 | #[derive(Parser)] |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 38 | enum Opt { |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 39 | /// Run a virtual machine with a config in APK |
| 40 | RunApp { |
| 41 | /// Path to VM Payload APK |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 42 | apk: PathBuf, |
| 43 | |
| 44 | /// Path to idsig of the APK |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 45 | idsig: PathBuf, |
| 46 | |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 47 | /// Path to the instance image. Created if not exists. |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 48 | instance: PathBuf, |
| 49 | |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 50 | /// Path to VM config JSON within APK (e.g. assets/vm_config.json) |
Inseob Kim | 7b5f65c | 2022-11-15 14:27:04 +0900 | [diff] [blame] | 51 | #[clap(long)] |
Alan Stokes | 0d1ef78 | 2022-09-27 13:46:35 +0100 | [diff] [blame] | 52 | config_path: Option<String>, |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 53 | |
Alan Stokes | 8f12f2b | 2023-01-09 09:19:20 +0000 | [diff] [blame] | 54 | /// Name of VM payload binary within APK (e.g. MicrodroidTestNativeLib.so) |
Inseob Kim | 7b5f65c | 2022-11-15 14:27:04 +0900 | [diff] [blame] | 55 | #[clap(long)] |
Alan Stokes | 8f12f2b | 2023-01-09 09:19:20 +0000 | [diff] [blame] | 56 | #[clap(alias = "payload_path")] |
| 57 | payload_binary_name: Option<String>, |
Inseob Kim | 7b5f65c | 2022-11-15 14:27:04 +0900 | [diff] [blame] | 58 | |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 59 | /// Name of VM |
| 60 | #[clap(long)] |
| 61 | name: Option<String>, |
| 62 | |
Shikha Panwar | 22e7045 | 2022-10-10 18:32:55 +0000 | [diff] [blame] | 63 | /// Path to the file backing the storage. |
| 64 | /// Created if the option is used but the path does not exist in the device. |
| 65 | #[clap(long)] |
| 66 | storage: Option<PathBuf>, |
| 67 | |
| 68 | /// Size of the storage. Used only if --storage is supplied but path does not exist |
| 69 | /// Default size is 10*1024*1024 |
| 70 | #[clap(long)] |
| 71 | storage_size: Option<u64>, |
| 72 | |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 73 | /// Path to file for VM console output. |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 74 | #[clap(long)] |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 75 | console: Option<PathBuf>, |
| 76 | |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 77 | /// Path to file for VM log output. |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 78 | #[clap(long)] |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 79 | log: Option<PathBuf>, |
Jiyong Park | 2360114 | 2021-07-05 13:15:32 +0900 | [diff] [blame] | 80 | |
Seungjae Yoo | fa22bb0 | 2022-12-08 16:38:42 +0900 | [diff] [blame] | 81 | /// Debug level of the VM. Supported values: "none" (default), and "full". |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 82 | #[clap(long, default_value = "none", value_parser = parse_debug_level)] |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame] | 83 | debug: DebugLevel, |
Jiyong Park | d63cfff | 2021-09-27 20:10:17 +0900 | [diff] [blame] | 84 | |
Andrew Walbran | 3994f00 | 2022-01-27 17:33:45 +0000 | [diff] [blame] | 85 | /// Run VM in protected mode. |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 86 | #[clap(short, long)] |
Andrew Walbran | 3994f00 | 2022-01-27 17:33:45 +0000 | [diff] [blame] | 87 | protected: bool, |
| 88 | |
Jiyong Park | d63cfff | 2021-09-27 20:10:17 +0900 | [diff] [blame] | 89 | /// Memory size (in MiB) of the VM. If unspecified, defaults to the value of `memory_mib` |
| 90 | /// in the VM config file. |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 91 | #[clap(short, long)] |
Jiyong Park | d63cfff | 2021-09-27 20:10:17 +0900 | [diff] [blame] | 92 | mem: Option<u32>, |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 93 | |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 94 | /// Number of vCPUs in the VM. If unspecified, defaults to 1. |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 95 | #[clap(long)] |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 96 | cpus: Option<u32>, |
| 97 | |
Jiyong Park | dfe16d6 | 2022-04-20 17:32:12 +0900 | [diff] [blame] | 98 | /// Comma separated list of task profile names to apply to the VM |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 99 | #[clap(long)] |
Jiyong Park | dfe16d6 | 2022-04-20 17:32:12 +0900 | [diff] [blame] | 100 | task_profiles: Vec<String>, |
| 101 | |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 102 | /// Paths to extra idsig files. |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 103 | #[clap(long = "extra-idsig")] |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 104 | extra_idsigs: Vec<PathBuf>, |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 105 | }, |
Nikita Ioffe | b0b6756 | 2022-11-22 15:48:06 +0000 | [diff] [blame] | 106 | /// Run a virtual machine with Microdroid inside |
| 107 | RunMicrodroid { |
| 108 | /// Path to the directory where VM-related files (e.g. instance.img, apk.idsig, etc.) will |
| 109 | /// be stored. If not specified a random directory under /data/local/tmp/microdroid will be |
| 110 | /// created and used. |
| 111 | #[clap(long)] |
| 112 | work_dir: Option<PathBuf>, |
| 113 | |
| 114 | /// Name of VM |
| 115 | #[clap(long)] |
| 116 | name: Option<String>, |
| 117 | |
Nikita Ioffe | b0b6756 | 2022-11-22 15:48:06 +0000 | [diff] [blame] | 118 | /// Path to the file backing the storage. |
| 119 | /// Created if the option is used but the path does not exist in the device. |
| 120 | #[clap(long)] |
| 121 | storage: Option<PathBuf>, |
| 122 | |
| 123 | /// Size of the storage. Used only if --storage is supplied but path does not exist |
| 124 | /// Default size is 10*1024*1024 |
| 125 | #[clap(long)] |
| 126 | storage_size: Option<u64>, |
| 127 | |
| 128 | /// Path to file for VM console output. |
| 129 | #[clap(long)] |
| 130 | console: Option<PathBuf>, |
| 131 | |
| 132 | /// Path to file for VM log output. |
| 133 | #[clap(long)] |
| 134 | log: Option<PathBuf>, |
| 135 | |
Seungjae Yoo | fa22bb0 | 2022-12-08 16:38:42 +0900 | [diff] [blame] | 136 | /// Debug level of the VM. Supported values: "none" (default), and "full". |
Nikita Ioffe | 0a15fcf | 2022-12-01 21:12:50 +0000 | [diff] [blame] | 137 | #[clap(long, default_value = "full", value_parser = parse_debug_level)] |
Nikita Ioffe | b0b6756 | 2022-11-22 15:48:06 +0000 | [diff] [blame] | 138 | debug: DebugLevel, |
| 139 | |
| 140 | /// Run VM in protected mode. |
| 141 | #[clap(short, long)] |
| 142 | protected: bool, |
| 143 | |
| 144 | /// Memory size (in MiB) of the VM. If unspecified, defaults to the value of `memory_mib` |
| 145 | /// in the VM config file. |
| 146 | #[clap(short, long)] |
| 147 | mem: Option<u32>, |
| 148 | |
| 149 | /// Number of vCPUs in the VM. If unspecified, defaults to 1. |
| 150 | #[clap(long)] |
| 151 | cpus: Option<u32>, |
| 152 | |
| 153 | /// Comma separated list of task profile names to apply to the VM |
| 154 | #[clap(long)] |
| 155 | task_profiles: Vec<String>, |
| 156 | }, |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 157 | /// Run a virtual machine |
| 158 | Run { |
| 159 | /// Path to VM config JSON |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 160 | config: PathBuf, |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 161 | |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 162 | /// Name of VM |
| 163 | #[clap(long)] |
| 164 | name: Option<String>, |
| 165 | |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 166 | /// Number of vCPUs in the VM. If unspecified, defaults to 1. |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 167 | #[clap(long)] |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 168 | cpus: Option<u32>, |
| 169 | |
Jiyong Park | dfe16d6 | 2022-04-20 17:32:12 +0900 | [diff] [blame] | 170 | /// Comma separated list of task profile names to apply to the VM |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 171 | #[clap(long)] |
Jiyong Park | dfe16d6 | 2022-04-20 17:32:12 +0900 | [diff] [blame] | 172 | task_profiles: Vec<String>, |
| 173 | |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 174 | /// Path to file for VM console output. |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 175 | #[clap(long)] |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 176 | console: Option<PathBuf>, |
Jooyung Han | b7983a2 | 2022-02-22 05:21:27 +0900 | [diff] [blame] | 177 | |
| 178 | /// Path to file for VM log output. |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 179 | #[clap(long)] |
Jooyung Han | b7983a2 | 2022-02-22 05:21:27 +0900 | [diff] [blame] | 180 | log: Option<PathBuf>, |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 181 | }, |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 182 | /// List running virtual machines |
| 183 | List, |
Andrew Walbran | c4b1bde | 2022-02-03 15:26:02 +0000 | [diff] [blame] | 184 | /// Print information about virtual machine support |
| 185 | Info, |
Andrew Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 186 | /// Create a new empty partition to be used as a writable partition for a VM |
| 187 | CreatePartition { |
| 188 | /// Path at which to create the image file |
Andrew Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 189 | path: PathBuf, |
| 190 | |
| 191 | /// The desired size of the partition, in bytes. |
| 192 | size: u64, |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 193 | |
| 194 | /// Type of the partition |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 195 | #[clap(short = 't', long = "type", default_value = "raw", |
| 196 | value_parser = parse_partition_type)] |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 197 | partition_type: PartitionType, |
Andrew Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 198 | }, |
Jooyung Han | c221c05 | 2022-02-22 05:20:15 +0900 | [diff] [blame] | 199 | /// Creates or update the idsig file by digesting the input APK file. |
| 200 | CreateIdsig { |
| 201 | /// Path to VM Payload APK |
Jooyung Han | c221c05 | 2022-02-22 05:20:15 +0900 | [diff] [blame] | 202 | apk: PathBuf, |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 203 | |
Jooyung Han | c221c05 | 2022-02-22 05:20:15 +0900 | [diff] [blame] | 204 | /// Path to idsig of the APK |
Jooyung Han | c221c05 | 2022-02-22 05:20:15 +0900 | [diff] [blame] | 205 | path: PathBuf, |
| 206 | }, |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame] | 209 | fn parse_debug_level(s: &str) -> Result<DebugLevel, String> { |
| 210 | match s { |
| 211 | "none" => Ok(DebugLevel::NONE), |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame] | 212 | "full" => Ok(DebugLevel::FULL), |
| 213 | _ => Err(format!("Invalid debug level {}", s)), |
| 214 | } |
| 215 | } |
| 216 | |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 217 | fn parse_partition_type(s: &str) -> Result<PartitionType, String> { |
| 218 | match s { |
| 219 | "raw" => Ok(PartitionType::RAW), |
| 220 | "instance" => Ok(PartitionType::ANDROID_VM_INSTANCE), |
| 221 | _ => Err(format!("Invalid partition type {}", s)), |
| 222 | } |
| 223 | } |
| 224 | |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 225 | fn main() -> Result<(), Error> { |
| 226 | env_logger::init(); |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 227 | let opt = Opt::parse(); |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 228 | |
| 229 | // We need to start the thread pool for Binder to work properly, especially link_to_death. |
| 230 | ProcessState::start_thread_pool(); |
| 231 | |
David Brazdil | 4b4c510 | 2022-12-19 22:56:20 +0000 | [diff] [blame] | 232 | let virtmgr = |
| 233 | vmclient::VirtualizationService::new().context("Failed to spawn VirtualizationService")?; |
| 234 | let service = virtmgr.connect().context("Failed to connect to VirtualizationService")?; |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 235 | |
David Brazdil | 20412d9 | 2021-03-18 10:53:06 +0000 | [diff] [blame] | 236 | match opt { |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 237 | Opt::RunApp { |
Seungjae Yoo | 62085c0 | 2022-08-12 04:44:52 +0000 | [diff] [blame] | 238 | name, |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 239 | apk, |
| 240 | idsig, |
| 241 | instance, |
Shikha Panwar | 22e7045 | 2022-10-10 18:32:55 +0000 | [diff] [blame] | 242 | storage, |
| 243 | storage_size, |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 244 | config_path, |
Alan Stokes | 8f12f2b | 2023-01-09 09:19:20 +0000 | [diff] [blame] | 245 | payload_binary_name, |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 246 | console, |
| 247 | log, |
| 248 | debug, |
Andrew Walbran | 3994f00 | 2022-01-27 17:33:45 +0000 | [diff] [blame] | 249 | protected, |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 250 | mem, |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 251 | cpus, |
Jiyong Park | dfe16d6 | 2022-04-20 17:32:12 +0900 | [diff] [blame] | 252 | task_profiles, |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 253 | extra_idsigs, |
| 254 | } => command_run_app( |
Seungjae Yoo | 62085c0 | 2022-08-12 04:44:52 +0000 | [diff] [blame] | 255 | name, |
Andrew Walbran | 616d13f | 2022-05-12 18:35:55 +0000 | [diff] [blame] | 256 | service.as_ref(), |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 257 | &apk, |
| 258 | &idsig, |
| 259 | &instance, |
Shikha Panwar | 22e7045 | 2022-10-10 18:32:55 +0000 | [diff] [blame] | 260 | storage.as_deref(), |
| 261 | storage_size, |
Inseob Kim | 7b5f65c | 2022-11-15 14:27:04 +0900 | [diff] [blame] | 262 | config_path, |
Alan Stokes | 8f12f2b | 2023-01-09 09:19:20 +0000 | [diff] [blame] | 263 | payload_binary_name, |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 264 | console.as_deref(), |
| 265 | log.as_deref(), |
| 266 | debug, |
Andrew Walbran | 3994f00 | 2022-01-27 17:33:45 +0000 | [diff] [blame] | 267 | protected, |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 268 | mem, |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 269 | cpus, |
Jiyong Park | dfe16d6 | 2022-04-20 17:32:12 +0900 | [diff] [blame] | 270 | task_profiles, |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 271 | &extra_idsigs, |
| 272 | ), |
Nikita Ioffe | b0b6756 | 2022-11-22 15:48:06 +0000 | [diff] [blame] | 273 | Opt::RunMicrodroid { |
| 274 | name, |
| 275 | work_dir, |
| 276 | storage, |
| 277 | storage_size, |
Nikita Ioffe | b0b6756 | 2022-11-22 15:48:06 +0000 | [diff] [blame] | 278 | console, |
| 279 | log, |
Nikita Ioffe | b0b6756 | 2022-11-22 15:48:06 +0000 | [diff] [blame] | 280 | debug, |
| 281 | protected, |
| 282 | mem, |
| 283 | cpus, |
| 284 | task_profiles, |
| 285 | } => command_run_microdroid( |
| 286 | name, |
| 287 | service.as_ref(), |
| 288 | work_dir, |
| 289 | storage.as_deref(), |
| 290 | storage_size, |
Nikita Ioffe | b0b6756 | 2022-11-22 15:48:06 +0000 | [diff] [blame] | 291 | console.as_deref(), |
| 292 | log.as_deref(), |
Nikita Ioffe | b0b6756 | 2022-11-22 15:48:06 +0000 | [diff] [blame] | 293 | debug, |
| 294 | protected, |
| 295 | mem, |
| 296 | cpus, |
| 297 | task_profiles, |
| 298 | ), |
David Brazdil | 2b6352f | 2023-01-12 11:01:17 +0000 | [diff] [blame] | 299 | Opt::Run { name, config, cpus, task_profiles, console, log } => { |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 300 | command_run( |
Seungjae Yoo | 62085c0 | 2022-08-12 04:44:52 +0000 | [diff] [blame] | 301 | name, |
Andrew Walbran | 616d13f | 2022-05-12 18:35:55 +0000 | [diff] [blame] | 302 | service.as_ref(), |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 303 | &config, |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 304 | console.as_deref(), |
Jooyung Han | b7983a2 | 2022-02-22 05:21:27 +0900 | [diff] [blame] | 305 | log.as_deref(), |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 306 | /* mem */ None, |
| 307 | cpus, |
Jiyong Park | dfe16d6 | 2022-04-20 17:32:12 +0900 | [diff] [blame] | 308 | task_profiles, |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 309 | ) |
Andrew Walbran | be42924 | 2021-06-28 12:22:54 +0000 | [diff] [blame] | 310 | } |
Andrew Walbran | 616d13f | 2022-05-12 18:35:55 +0000 | [diff] [blame] | 311 | Opt::List => command_list(service.as_ref()), |
Andrew Walbran | c4b1bde | 2022-02-03 15:26:02 +0000 | [diff] [blame] | 312 | Opt::Info => command_info(), |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 313 | Opt::CreatePartition { path, size, partition_type } => { |
Andrew Walbran | 616d13f | 2022-05-12 18:35:55 +0000 | [diff] [blame] | 314 | command_create_partition(service.as_ref(), &path, size, partition_type) |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 315 | } |
Andrew Walbran | 616d13f | 2022-05-12 18:35:55 +0000 | [diff] [blame] | 316 | Opt::CreateIdsig { apk, path } => command_create_idsig(service.as_ref(), &apk, &path), |
Andrew Walbran | ea9fa48 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 320 | /// List the VMs currently running. |
Andrew Walbran | 616d13f | 2022-05-12 18:35:55 +0000 | [diff] [blame] | 321 | fn command_list(service: &dyn IVirtualizationService) -> Result<(), Error> { |
Andrew Walbran | 17de24f | 2021-05-27 13:27:30 +0000 | [diff] [blame] | 322 | let vms = service.debugListVms().context("Failed to get list of VMs")?; |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 323 | println!("Running VMs: {:#?}", vms); |
| 324 | Ok(()) |
| 325 | } |
Andrew Walbran | c4b1bde | 2022-02-03 15:26:02 +0000 | [diff] [blame] | 326 | |
| 327 | /// Print information about supported VM types. |
| 328 | fn command_info() -> Result<(), Error> { |
Alan Stokes | 8d39a9b | 2023-01-10 15:01:00 +0000 | [diff] [blame] | 329 | let non_protected_vm_supported = |
Andrew Walbran | c4b1bde | 2022-02-03 15:26:02 +0000 | [diff] [blame] | 330 | system_properties::read_bool("ro.boot.hypervisor.vm.supported", false)?; |
| 331 | let protected_vm_supported = |
| 332 | system_properties::read_bool("ro.boot.hypervisor.protected_vm.supported", false)?; |
Alan Stokes | 8d39a9b | 2023-01-10 15:01:00 +0000 | [diff] [blame] | 333 | match (non_protected_vm_supported, protected_vm_supported) { |
Andrew Walbran | c4b1bde | 2022-02-03 15:26:02 +0000 | [diff] [blame] | 334 | (false, false) => println!("VMs are not supported."), |
| 335 | (false, true) => println!("Only protected VMs are supported."), |
Alan Stokes | 8d39a9b | 2023-01-10 15:01:00 +0000 | [diff] [blame] | 336 | (true, false) => println!("Only non-protected VMs are supported."), |
| 337 | (true, true) => println!("Both protected and non-protected VMs are supported."), |
Andrew Walbran | c4b1bde | 2022-02-03 15:26:02 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Andrew Walbran | 014efb5 | 2022-02-03 17:43:11 +0000 | [diff] [blame] | 340 | if let Some(version) = system_properties::read("ro.boot.hypervisor.version")? { |
Andrew Walbran | c4b1bde | 2022-02-03 15:26:02 +0000 | [diff] [blame] | 341 | println!("Hypervisor version: {}", version); |
| 342 | } else { |
| 343 | println!("Hypervisor version not set."); |
| 344 | } |
| 345 | |
| 346 | if Path::new("/dev/kvm").exists() { |
| 347 | println!("/dev/kvm exists."); |
| 348 | } else { |
| 349 | println!("/dev/kvm does not exist."); |
| 350 | } |
| 351 | |
| 352 | Ok(()) |
| 353 | } |
Andrew Walbran | 1f810b6 | 2022-08-10 13:33:57 +0000 | [diff] [blame] | 354 | |
| 355 | #[cfg(test)] |
| 356 | mod tests { |
| 357 | use super::*; |
Andrew Walbran | aa1efc4 | 2022-08-10 13:33:57 +0000 | [diff] [blame] | 358 | use clap::CommandFactory; |
Andrew Walbran | 1f810b6 | 2022-08-10 13:33:57 +0000 | [diff] [blame] | 359 | |
| 360 | #[test] |
| 361 | fn verify_app() { |
Andrew Walbran | aa1efc4 | 2022-08-10 13:33:57 +0000 | [diff] [blame] | 362 | // Check that the command parsing has been configured in a valid way. |
| 363 | Opt::command().debug_assert(); |
Andrew Walbran | 1f810b6 | 2022-08-10 13:33:57 +0000 | [diff] [blame] | 364 | } |
| 365 | } |