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 | |
| 15 | //! Struct for VM configuration. |
| 16 | |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 17 | use android_system_virtualizationservice::{ |
| 18 | aidl::android::system::virtualizationservice::DiskImage::DiskImage as AidlDiskImage, |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 19 | aidl::android::system::virtualizationservice::Partition::Partition as AidlPartition, |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 20 | aidl::android::system::virtualizationservice::VirtualMachineConfig::VirtualMachineConfig, |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 21 | binder::ParcelFileDescriptor, |
| 22 | }; |
| 23 | use anyhow::{bail, Context, Error}; |
Andrew Walbran | 0c4d3df | 2021-05-27 14:00:42 +0000 | [diff] [blame] | 24 | use compositediskconfig::Partition; |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 25 | use serde::{Deserialize, Serialize}; |
| 26 | use std::fs::{File, OpenOptions}; |
| 27 | use std::io::BufReader; |
| 28 | use std::path::{Path, PathBuf}; |
| 29 | |
| 30 | /// Configuration for a particular VM to be started. |
| 31 | #[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] |
| 32 | pub struct VmConfig { |
| 33 | /// The filename of the kernel image, if any. |
| 34 | pub kernel: Option<PathBuf>, |
| 35 | /// The filename of the initial ramdisk for the kernel, if any. |
| 36 | pub initrd: Option<PathBuf>, |
| 37 | /// Parameters to pass to the kernel. As far as the VMM and boot protocol are concerned this is |
| 38 | /// just a string, but typically it will contain multiple parameters separated by spaces. |
| 39 | pub params: Option<String>, |
| 40 | /// The bootloader to use. If this is supplied then the kernel and initrd must not be supplied; |
| 41 | /// the bootloader is instead responsibly for loading the kernel from one of the disks. |
| 42 | pub bootloader: Option<PathBuf>, |
| 43 | /// Disk images to be made available to the VM. |
| 44 | #[serde(default)] |
| 45 | pub disks: Vec<DiskImage>, |
Andrew Walbran | f865042 | 2021-06-09 15:54:09 +0000 | [diff] [blame] | 46 | /// Whether the VM should be a protected VM. |
| 47 | #[serde(default)] |
| 48 | pub protected: bool, |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | impl VmConfig { |
| 52 | /// Ensure that the configuration has a valid combination of fields set, or return an error if |
| 53 | /// not. |
| 54 | pub fn validate(&self) -> Result<(), Error> { |
| 55 | if self.bootloader.is_none() && self.kernel.is_none() { |
| 56 | bail!("VM must have either a bootloader or a kernel image."); |
| 57 | } |
| 58 | if self.bootloader.is_some() && (self.kernel.is_some() || self.initrd.is_some()) { |
| 59 | bail!("Can't have both bootloader and kernel/initrd image."); |
| 60 | } |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 61 | for disk in &self.disks { |
| 62 | if disk.image.is_none() == disk.partitions.is_empty() { |
| 63 | bail!("Exactly one of image and partitions must be specified. (Was {:?}.)", disk); |
| 64 | } |
| 65 | } |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 66 | Ok(()) |
| 67 | } |
| 68 | |
| 69 | /// Load the configuration for a VM from the given JSON file, and check that it is valid. |
| 70 | pub fn load(file: &File) -> Result<VmConfig, Error> { |
| 71 | let buffered = BufReader::new(file); |
| 72 | let config: VmConfig = serde_json::from_reader(buffered)?; |
| 73 | config.validate()?; |
| 74 | Ok(config) |
| 75 | } |
| 76 | |
| 77 | /// Convert the `VmConfig` to a [`VirtualMachineConfig`] which can be passed to the Virt |
| 78 | /// Manager. |
| 79 | pub fn to_parcelable(&self) -> Result<VirtualMachineConfig, Error> { |
| 80 | Ok(VirtualMachineConfig { |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 81 | kernel: maybe_open_parcel_file(&self.kernel, false)?, |
| 82 | initrd: maybe_open_parcel_file(&self.initrd, false)?, |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 83 | params: self.params.clone(), |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 84 | bootloader: maybe_open_parcel_file(&self.bootloader, false)?, |
| 85 | disks: self.disks.iter().map(DiskImage::to_parcelable).collect::<Result<_, Error>>()?, |
Andrew Walbran | f865042 | 2021-06-09 15:54:09 +0000 | [diff] [blame] | 86 | protected_vm: self.protected, |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 87 | }) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /// A disk image to be made available to the VM. |
| 92 | #[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] |
| 93 | pub struct DiskImage { |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 94 | /// The filename of the disk image, if it already exists. Exactly one of this and `partitions` |
| 95 | /// must be specified. |
| 96 | #[serde(default)] |
| 97 | pub image: Option<PathBuf>, |
| 98 | /// A set of partitions to be assembled into a composite image. |
| 99 | #[serde(default)] |
| 100 | pub partitions: Vec<Partition>, |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 101 | /// Whether this disk should be writable by the VM. |
| 102 | pub writable: bool, |
| 103 | } |
| 104 | |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 105 | impl DiskImage { |
| 106 | fn to_parcelable(&self) -> Result<AidlDiskImage, Error> { |
| 107 | let partitions = |
Andrew Walbran | 0c4d3df | 2021-05-27 14:00:42 +0000 | [diff] [blame] | 108 | self.partitions.iter().map(partition_to_parcelable).collect::<Result<_, Error>>()?; |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 109 | Ok(AidlDiskImage { |
| 110 | image: maybe_open_parcel_file(&self.image, self.writable)?, |
| 111 | writable: self.writable, |
| 112 | partitions, |
| 113 | }) |
| 114 | } |
| 115 | } |
| 116 | |
Andrew Walbran | 0c4d3df | 2021-05-27 14:00:42 +0000 | [diff] [blame] | 117 | fn partition_to_parcelable(partition: &Partition) -> Result<AidlPartition, Error> { |
Jooyung Han | 9713bd4 | 2021-06-26 03:00:18 +0900 | [diff] [blame^] | 118 | if !partition.paths.is_empty() { |
| 119 | if partition.path.is_some() { |
| 120 | bail!("Partition {} contains both path/paths", &partition.label); |
| 121 | } |
| 122 | let images = partition |
| 123 | .paths |
| 124 | .iter() |
| 125 | .map(|path| open_parcel_file(&path, partition.writable)) |
| 126 | .collect::<Result<Vec<_>, _>>()?; |
| 127 | Ok(AidlPartition { |
| 128 | images, |
| 129 | writable: partition.writable, |
| 130 | label: partition.label.to_owned(), |
| 131 | }) |
| 132 | } else { |
| 133 | let path = partition.path.as_ref().ok_or_else(|| { |
| 134 | Error::msg(format!("Partition {} doesn't set path/paths", &partition.label)) |
| 135 | })?; |
| 136 | Ok(AidlPartition { |
| 137 | images: vec![open_parcel_file(&path, partition.writable)?], |
| 138 | writable: partition.writable, |
| 139 | label: partition.label.to_owned(), |
| 140 | }) |
| 141 | } |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 144 | /// Try to open the given file and wrap it in a [`ParcelFileDescriptor`]. |
| 145 | fn open_parcel_file(filename: &Path, writable: bool) -> Result<ParcelFileDescriptor, Error> { |
| 146 | Ok(ParcelFileDescriptor::new( |
| 147 | OpenOptions::new() |
| 148 | .read(true) |
| 149 | .write(writable) |
| 150 | .open(filename) |
| 151 | .with_context(|| format!("Failed to open {:?}", filename))?, |
| 152 | )) |
| 153 | } |
| 154 | |
| 155 | /// If the given filename is `Some`, try to open it and wrap it in a [`ParcelFileDescriptor`]. |
| 156 | fn maybe_open_parcel_file( |
| 157 | filename: &Option<PathBuf>, |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 158 | writable: bool, |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 159 | ) -> Result<Option<ParcelFileDescriptor>, Error> { |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 160 | filename.as_deref().map(|filename| open_parcel_file(filename, writable)).transpose() |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 161 | } |