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