Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +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 | |
Jooyung Han | fc732f5 | 2021-06-26 02:54:20 +0900 | [diff] [blame] | 15 | //! Struct for VM configuration with JSON (de)serialization and AIDL parcelables |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 16 | |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 17 | use android_system_virtualizationservice::{ |
Jeongik Cha | 529bfc2 | 2024-03-22 14:05:36 +0900 | [diff] [blame] | 18 | aidl::android::system::virtualizationservice::CpuTopology::CpuTopology, |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 19 | aidl::android::system::virtualizationservice::DiskImage::DiskImage as AidlDiskImage, |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 20 | aidl::android::system::virtualizationservice::Partition::Partition as AidlPartition, |
Elie Kheirallah | 29d7291 | 2024-08-07 20:17:26 +0000 | [diff] [blame] | 21 | aidl::android::system::virtualizationservice::UsbConfig::UsbConfig as AidlUsbConfig, |
Pierre-Clément Tosi | d3bbe1d | 2024-04-15 18:03:51 +0100 | [diff] [blame] | 22 | aidl::android::system::virtualizationservice::VirtualMachineAppConfig::DebugLevel::DebugLevel, |
| 23 | aidl::android::system::virtualizationservice::VirtualMachineConfig::VirtualMachineConfig, |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 24 | aidl::android::system::virtualizationservice::VirtualMachineRawConfig::VirtualMachineRawConfig, |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 25 | binder::ParcelFileDescriptor, |
| 26 | }; |
Jooyung Han | fc732f5 | 2021-06-26 02:54:20 +0900 | [diff] [blame] | 27 | |
Inseob Kim | 6ef8097 | 2023-07-20 17:23:36 +0900 | [diff] [blame] | 28 | use anyhow::{anyhow, bail, Context, Error, Result}; |
Jiyong Park | dcf1741 | 2022-02-08 15:07:23 +0900 | [diff] [blame] | 29 | use semver::VersionReq; |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 30 | use serde::{Deserialize, Serialize}; |
Andrew Walbran | b15cd6e | 2021-07-05 16:38:07 +0000 | [diff] [blame] | 31 | use std::convert::TryInto; |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 32 | use std::fs::{File, OpenOptions}; |
| 33 | use std::io::BufReader; |
Andrew Walbran | b15cd6e | 2021-07-05 16:38:07 +0000 | [diff] [blame] | 34 | use std::num::NonZeroU32; |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 35 | use std::path::{Path, PathBuf}; |
Jiyong Park | 3f9b509 | 2024-07-10 13:38:29 +0900 | [diff] [blame] | 36 | use uuid::Uuid; |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 37 | |
| 38 | /// Configuration for a particular VM to be started. |
| 39 | #[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] |
| 40 | pub struct VmConfig { |
Seungjae Yoo | 62085c0 | 2022-08-12 04:44:52 +0000 | [diff] [blame] | 41 | /// The name of VM. |
| 42 | pub name: Option<String>, |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 43 | /// The filename of the kernel image, if any. |
| 44 | pub kernel: Option<PathBuf>, |
| 45 | /// The filename of the initial ramdisk for the kernel, if any. |
| 46 | pub initrd: Option<PathBuf>, |
| 47 | /// Parameters to pass to the kernel. As far as the VMM and boot protocol are concerned this is |
| 48 | /// just a string, but typically it will contain multiple parameters separated by spaces. |
| 49 | pub params: Option<String>, |
| 50 | /// The bootloader to use. If this is supplied then the kernel and initrd must not be supplied; |
| 51 | /// the bootloader is instead responsibly for loading the kernel from one of the disks. |
| 52 | pub bootloader: Option<PathBuf>, |
| 53 | /// Disk images to be made available to the VM. |
| 54 | #[serde(default)] |
| 55 | pub disks: Vec<DiskImage>, |
Andrew Walbran | f865042 | 2021-06-09 15:54:09 +0000 | [diff] [blame] | 56 | /// Whether the VM should be a protected VM. |
| 57 | #[serde(default)] |
| 58 | pub protected: bool, |
Andrew Walbran | b15cd6e | 2021-07-05 16:38:07 +0000 | [diff] [blame] | 59 | /// The amount of RAM to give the VM, in MiB. |
| 60 | #[serde(default)] |
| 61 | pub memory_mib: Option<NonZeroU32>, |
Jeongik Cha | 529bfc2 | 2024-03-22 14:05:36 +0900 | [diff] [blame] | 62 | /// The CPU topology: either "one_cpu"(default) or "match_host" |
| 63 | pub cpu_topology: Option<String>, |
Jiyong Park | dcf1741 | 2022-02-08 15:07:23 +0900 | [diff] [blame] | 64 | /// Version or range of versions of the virtual platform that this config is compatible with. |
| 65 | /// The format follows SemVer (https://semver.org). |
| 66 | pub platform_version: VersionReq, |
Inseob Kim | 6ef8097 | 2023-07-20 17:23:36 +0900 | [diff] [blame] | 67 | /// SysFS paths of devices assigned to the VM. |
| 68 | #[serde(default)] |
| 69 | pub devices: Vec<PathBuf>, |
Yi-Yo Chiang | 8dd3255 | 2024-05-22 19:38:16 +0800 | [diff] [blame] | 70 | /// The serial device for VM console input. |
| 71 | pub console_input_device: Option<String>, |
Elie Kheirallah | 29d7291 | 2024-08-07 20:17:26 +0000 | [diff] [blame] | 72 | /// The USB config of the VM. |
| 73 | pub usb_config: Option<UsbConfig>, |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | impl VmConfig { |
| 77 | /// Ensure that the configuration has a valid combination of fields set, or return an error if |
| 78 | /// not. |
| 79 | pub fn validate(&self) -> Result<(), Error> { |
| 80 | if self.bootloader.is_none() && self.kernel.is_none() { |
| 81 | bail!("VM must have either a bootloader or a kernel image."); |
| 82 | } |
| 83 | if self.bootloader.is_some() && (self.kernel.is_some() || self.initrd.is_some()) { |
| 84 | bail!("Can't have both bootloader and kernel/initrd image."); |
| 85 | } |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 86 | for disk in &self.disks { |
| 87 | if disk.image.is_none() == disk.partitions.is_empty() { |
| 88 | bail!("Exactly one of image and partitions must be specified. (Was {:?}.)", disk); |
| 89 | } |
| 90 | } |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 91 | Ok(()) |
| 92 | } |
| 93 | |
| 94 | /// Load the configuration for a VM from the given JSON file, and check that it is valid. |
| 95 | pub fn load(file: &File) -> Result<VmConfig, Error> { |
| 96 | let buffered = BufReader::new(file); |
| 97 | let config: VmConfig = serde_json::from_reader(buffered)?; |
| 98 | config.validate()?; |
| 99 | Ok(config) |
| 100 | } |
| 101 | |
| 102 | /// Convert the `VmConfig` to a [`VirtualMachineConfig`] which can be passed to the Virt |
| 103 | /// Manager. |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 104 | pub fn to_parcelable(&self) -> Result<VirtualMachineRawConfig, Error> { |
Andrew Walbran | b15cd6e | 2021-07-05 16:38:07 +0000 | [diff] [blame] | 105 | let memory_mib = if let Some(memory_mib) = self.memory_mib { |
| 106 | memory_mib.get().try_into().context("Invalid memory_mib")? |
| 107 | } else { |
| 108 | 0 |
| 109 | }; |
Jeongik Cha | 529bfc2 | 2024-03-22 14:05:36 +0900 | [diff] [blame] | 110 | let cpu_topology = match self.cpu_topology.as_deref() { |
| 111 | None => CpuTopology::ONE_CPU, |
| 112 | Some("one_cpu") => CpuTopology::ONE_CPU, |
| 113 | Some("match_host") => CpuTopology::MATCH_HOST, |
| 114 | Some(cpu_topology) => bail!("Invalid cpu topology {}", cpu_topology), |
| 115 | }; |
Elie Kheirallah | 29d7291 | 2024-08-07 20:17:26 +0000 | [diff] [blame] | 116 | let usb_config = self.usb_config.clone().map(|x| x.to_parcelable()).transpose()?; |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 117 | Ok(VirtualMachineRawConfig { |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 118 | kernel: maybe_open_parcel_file(&self.kernel, false)?, |
| 119 | initrd: maybe_open_parcel_file(&self.initrd, false)?, |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 120 | params: self.params.clone(), |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 121 | bootloader: maybe_open_parcel_file(&self.bootloader, false)?, |
| 122 | disks: self.disks.iter().map(DiskImage::to_parcelable).collect::<Result<_, Error>>()?, |
Andrew Walbran | cc04590 | 2021-07-27 16:06:17 +0000 | [diff] [blame] | 123 | protectedVm: self.protected, |
| 124 | memoryMib: memory_mib, |
Jeongik Cha | 529bfc2 | 2024-03-22 14:05:36 +0900 | [diff] [blame] | 125 | cpuTopology: cpu_topology, |
Jiyong Park | dcf1741 | 2022-02-08 15:07:23 +0900 | [diff] [blame] | 126 | platformVersion: self.platform_version.to_string(), |
Inseob Kim | 6ef8097 | 2023-07-20 17:23:36 +0900 | [diff] [blame] | 127 | devices: self |
| 128 | .devices |
| 129 | .iter() |
| 130 | .map(|x| { |
| 131 | x.to_str().map(String::from).ok_or(anyhow!("Failed to convert {x:?} to String")) |
| 132 | }) |
| 133 | .collect::<Result<_>>()?, |
Yi-Yo Chiang | 8dd3255 | 2024-05-22 19:38:16 +0800 | [diff] [blame] | 134 | consoleInputDevice: self.console_input_device.clone(), |
Elie Kheirallah | 29d7291 | 2024-08-07 20:17:26 +0000 | [diff] [blame] | 135 | usbConfig: usb_config, |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 136 | ..Default::default() |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 137 | }) |
| 138 | } |
| 139 | } |
| 140 | |
Pierre-Clément Tosi | d3bbe1d | 2024-04-15 18:03:51 +0100 | [diff] [blame] | 141 | /// Returns the debug level of the VM from its configuration. |
| 142 | pub fn get_debug_level(config: &VirtualMachineConfig) -> Option<DebugLevel> { |
| 143 | match config { |
| 144 | VirtualMachineConfig::AppConfig(config) => Some(config.debugLevel), |
| 145 | VirtualMachineConfig::RawConfig(_) => None, |
| 146 | } |
| 147 | } |
| 148 | |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 149 | /// A disk image to be made available to the VM. |
| 150 | #[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] |
| 151 | pub struct DiskImage { |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 152 | /// The filename of the disk image, if it already exists. Exactly one of this and `partitions` |
| 153 | /// must be specified. |
| 154 | #[serde(default)] |
| 155 | pub image: Option<PathBuf>, |
| 156 | /// A set of partitions to be assembled into a composite image. |
| 157 | #[serde(default)] |
| 158 | pub partitions: Vec<Partition>, |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 159 | /// Whether this disk should be writable by the VM. |
| 160 | pub writable: bool, |
| 161 | } |
| 162 | |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 163 | impl DiskImage { |
| 164 | fn to_parcelable(&self) -> Result<AidlDiskImage, Error> { |
| 165 | let partitions = |
Jooyung Han | fc732f5 | 2021-06-26 02:54:20 +0900 | [diff] [blame] | 166 | self.partitions.iter().map(Partition::to_parcelable).collect::<Result<_>>()?; |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 167 | Ok(AidlDiskImage { |
| 168 | image: maybe_open_parcel_file(&self.image, self.writable)?, |
| 169 | writable: self.writable, |
| 170 | partitions, |
| 171 | }) |
| 172 | } |
| 173 | } |
| 174 | |
Jooyung Han | fc732f5 | 2021-06-26 02:54:20 +0900 | [diff] [blame] | 175 | /// A partition to be assembled into a composite image. |
| 176 | #[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] |
| 177 | pub struct Partition { |
| 178 | /// A label for the partition. |
| 179 | pub label: String, |
| 180 | /// The filename of the partition image. |
Jooyung Han | 631d588 | 2021-07-29 06:34:05 +0900 | [diff] [blame] | 181 | pub path: PathBuf, |
Jooyung Han | fc732f5 | 2021-06-26 02:54:20 +0900 | [diff] [blame] | 182 | /// Whether the partition should be writable. |
| 183 | #[serde(default)] |
| 184 | pub writable: bool, |
Jiyong Park | 3f9b509 | 2024-07-10 13:38:29 +0900 | [diff] [blame] | 185 | /// GUID of this partition. |
| 186 | #[serde(default)] |
| 187 | pub guid: Option<Uuid>, |
Jooyung Han | fc732f5 | 2021-06-26 02:54:20 +0900 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | impl Partition { |
| 191 | fn to_parcelable(&self) -> Result<AidlPartition> { |
Jooyung Han | 631d588 | 2021-07-29 06:34:05 +0900 | [diff] [blame] | 192 | Ok(AidlPartition { |
| 193 | image: Some(open_parcel_file(&self.path, self.writable)?), |
| 194 | writable: self.writable, |
| 195 | label: self.label.to_owned(), |
Jiyong Park | 3f9b509 | 2024-07-10 13:38:29 +0900 | [diff] [blame] | 196 | guid: None, |
Jooyung Han | 631d588 | 2021-07-29 06:34:05 +0900 | [diff] [blame] | 197 | }) |
Jooyung Han | 9713bd4 | 2021-06-26 03:00:18 +0900 | [diff] [blame] | 198 | } |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Elie Kheirallah | 29d7291 | 2024-08-07 20:17:26 +0000 | [diff] [blame] | 201 | /// USB controller and available USB devices |
| 202 | #[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] |
| 203 | pub struct UsbConfig { |
| 204 | /// Enable USB controller |
| 205 | pub controller: bool, |
| 206 | } |
| 207 | |
| 208 | impl UsbConfig { |
| 209 | fn to_parcelable(&self) -> Result<AidlUsbConfig> { |
| 210 | Ok(AidlUsbConfig { controller: self.controller }) |
| 211 | } |
| 212 | } |
| 213 | |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 214 | /// Try to open the given file and wrap it in a [`ParcelFileDescriptor`]. |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 215 | pub fn open_parcel_file(filename: &Path, writable: bool) -> Result<ParcelFileDescriptor> { |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 216 | Ok(ParcelFileDescriptor::new( |
| 217 | OpenOptions::new() |
| 218 | .read(true) |
| 219 | .write(writable) |
| 220 | .open(filename) |
| 221 | .with_context(|| format!("Failed to open {:?}", filename))?, |
| 222 | )) |
| 223 | } |
| 224 | |
| 225 | /// If the given filename is `Some`, try to open it and wrap it in a [`ParcelFileDescriptor`]. |
| 226 | fn maybe_open_parcel_file( |
| 227 | filename: &Option<PathBuf>, |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 228 | writable: bool, |
Jooyung Han | fc732f5 | 2021-06-26 02:54:20 +0900 | [diff] [blame] | 229 | ) -> Result<Option<ParcelFileDescriptor>> { |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 230 | filename.as_deref().map(|filename| open_parcel_file(filename, writable)).transpose() |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 231 | } |