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