Merge VM and CDISK config into libvmconfig
so that virtualizationservice and vm use it.
Bug: 190503456
Test: MicrodroidHostTestCases
Change-Id: I63eaf0ac1c6d0e1e17d19f4863c5f4f14525aa32
diff --git a/compositediskconfig/src/lib.rs b/compositediskconfig/src/lib.rs
deleted file mode 100644
index 54f26e5..0000000
--- a/compositediskconfig/src/lib.rs
+++ /dev/null
@@ -1,41 +0,0 @@
-// Copyright 2021, The Android Open Source Project
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-//! JSON configuration for composite disks, as used for running `mk_cdisk` and by the `vm` tool.
-
-use serde::{Deserialize, Serialize};
-use std::path::PathBuf;
-
-/// Configuration for running `mk_cdisk`.
-#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
-pub struct Config {
- /// The set of partitions to be assembled into a composite image.
- pub partitions: Vec<Partition>,
-}
-
-/// A partition to be assembled into a composite image.
-#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
-pub struct Partition {
- /// A label for the partition.
- pub label: String,
- /// The filename of the partition image.
- #[serde(default)]
- pub path: Option<PathBuf>,
- /// The filename of the partition image.
- #[serde(default)]
- pub paths: Vec<PathBuf>,
- /// Whether the partition should be writable.
- #[serde(default)]
- pub writable: bool,
-}
diff --git a/vm/Android.bp b/vm/Android.bp
index c07beb2..734f2d3 100644
--- a/vm/Android.bp
+++ b/vm/Android.bp
@@ -10,13 +10,13 @@
rustlibs: [
"android.system.virtualizationservice-rust",
"libanyhow",
- "libcompositediskconfig",
"libenv_logger",
"liblibc",
"liblog_rust",
"libserde_json",
"libserde",
"libstructopt",
+ "libvmconfig",
],
apex_available: [
"com.android.virt",
diff --git a/vm/src/main.rs b/vm/src/main.rs
index b79f42a..8f16eb5 100644
--- a/vm/src/main.rs
+++ b/vm/src/main.rs
@@ -14,7 +14,6 @@
//! Android VM control tool.
-mod config;
mod run;
mod sync;
diff --git a/vm/src/run.rs b/vm/src/run.rs
index ec95646..fbf849b 100644
--- a/vm/src/run.rs
+++ b/vm/src/run.rs
@@ -14,7 +14,6 @@
//! Command to run a VM.
-use crate::config::VmConfig;
use crate::sync::AtomicFlag;
use android_system_virtualizationservice::aidl::android::system::virtualizationservice::IVirtualizationService::IVirtualizationService;
use android_system_virtualizationservice::aidl::android::system::virtualizationservice::IVirtualMachine::IVirtualMachine;
@@ -30,6 +29,7 @@
use std::io;
use std::os::unix::io::{AsRawFd, FromRawFd};
use std::path::Path;
+use vmconfig::VmConfig;
/// Run a VM from the given configuration file.
pub fn command_run(
diff --git a/compositediskconfig/Android.bp b/vmconfig/Android.bp
similarity index 66%
rename from compositediskconfig/Android.bp
rename to vmconfig/Android.bp
index 4608323..321eba0 100644
--- a/compositediskconfig/Android.bp
+++ b/vmconfig/Android.bp
@@ -3,12 +3,13 @@
}
rust_library {
- name: "libcompositediskconfig",
- host_supported: true,
- crate_name: "compositediskconfig",
+ name: "libvmconfig",
+ crate_name: "vmconfig",
srcs: ["src/lib.rs"],
edition: "2018",
rustlibs: [
+ "android.system.virtualizationservice-rust",
+ "libanyhow",
"libserde_json",
"libserde",
],
diff --git a/vm/src/config.rs b/vmconfig/src/lib.rs
similarity index 75%
rename from vm/src/config.rs
rename to vmconfig/src/lib.rs
index c36c714..c9385f3 100644
--- a/vm/src/config.rs
+++ b/vmconfig/src/lib.rs
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-//! Struct for VM configuration.
+//! Struct for VM configuration with JSON (de)serialization and AIDL parcelables
use android_system_virtualizationservice::{
aidl::android::system::virtualizationservice::DiskImage::DiskImage as AidlDiskImage,
@@ -20,8 +20,8 @@
aidl::android::system::virtualizationservice::VirtualMachineConfig::VirtualMachineConfig,
binder::ParcelFileDescriptor,
};
-use anyhow::{bail, Context, Error};
-use compositediskconfig::Partition;
+
+use anyhow::{bail, Context, Error, Result};
use serde::{Deserialize, Serialize};
use std::fs::{File, OpenOptions};
use std::io::BufReader;
@@ -105,7 +105,7 @@
impl DiskImage {
fn to_parcelable(&self) -> Result<AidlDiskImage, Error> {
let partitions =
- self.partitions.iter().map(partition_to_parcelable).collect::<Result<_, Error>>()?;
+ self.partitions.iter().map(Partition::to_parcelable).collect::<Result<_>>()?;
Ok(AidlDiskImage {
image: maybe_open_parcel_file(&self.image, self.writable)?,
writable: self.writable,
@@ -114,35 +114,49 @@
}
}
-fn partition_to_parcelable(partition: &Partition) -> Result<AidlPartition, Error> {
- if !partition.paths.is_empty() {
- if partition.path.is_some() {
- bail!("Partition {} contains both path/paths", &partition.label);
+/// A partition to be assembled into a composite image.
+#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
+pub struct Partition {
+ /// A label for the partition.
+ pub label: String,
+ /// The filename of the partition image.
+ #[serde(default)]
+ pub path: Option<PathBuf>,
+ /// The filename of the partition image.
+ #[serde(default)]
+ pub paths: Vec<PathBuf>,
+ /// Whether the partition should be writable.
+ #[serde(default)]
+ pub writable: bool,
+}
+
+impl Partition {
+ fn to_parcelable(&self) -> Result<AidlPartition> {
+ if !self.paths.is_empty() {
+ if self.path.is_some() {
+ bail!("Partition {} contains both path/paths", &self.label);
+ }
+ let images = self
+ .paths
+ .iter()
+ .map(|path| open_parcel_file(&path, self.writable))
+ .collect::<Result<Vec<_>, _>>()?;
+ Ok(AidlPartition { images, writable: self.writable, label: self.label.to_owned() })
+ } else {
+ let path = self.path.as_ref().ok_or_else(|| {
+ Error::msg(format!("Partition {} doesn't set path/paths", &self.label))
+ })?;
+ Ok(AidlPartition {
+ images: vec![open_parcel_file(&path, self.writable)?],
+ writable: self.writable,
+ label: self.label.to_owned(),
+ })
}
- let images = partition
- .paths
- .iter()
- .map(|path| open_parcel_file(&path, partition.writable))
- .collect::<Result<Vec<_>, _>>()?;
- Ok(AidlPartition {
- images,
- writable: partition.writable,
- label: partition.label.to_owned(),
- })
- } else {
- let path = partition.path.as_ref().ok_or_else(|| {
- Error::msg(format!("Partition {} doesn't set path/paths", &partition.label))
- })?;
- Ok(AidlPartition {
- images: vec![open_parcel_file(&path, partition.writable)?],
- writable: partition.writable,
- label: partition.label.to_owned(),
- })
}
}
/// Try to open the given file and wrap it in a [`ParcelFileDescriptor`].
-fn open_parcel_file(filename: &Path, writable: bool) -> Result<ParcelFileDescriptor, Error> {
+fn open_parcel_file(filename: &Path, writable: bool) -> Result<ParcelFileDescriptor> {
Ok(ParcelFileDescriptor::new(
OpenOptions::new()
.read(true)
@@ -156,6 +170,6 @@
fn maybe_open_parcel_file(
filename: &Option<PathBuf>,
writable: bool,
-) -> Result<Option<ParcelFileDescriptor>, Error> {
+) -> Result<Option<ParcelFileDescriptor>> {
filename.as_deref().map(|filename| open_parcel_file(filename, writable)).transpose()
}