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