Merge VM and CDISK config into libvmconfig

so that virtualizationservice and vm use it.

Bug: 190503456
Test: MicrodroidHostTestCases
Change-Id: I63eaf0ac1c6d0e1e17d19f4863c5f4f14525aa32
This commit is contained in:
Jooyung Han 2021-06-26 02:54:20 +09:00
parent 9713bd4664
commit fc732f56f1
6 changed files with 49 additions and 76 deletions

View File

@ -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,
}

View File

@ -10,13 +10,13 @@ rust_binary {
rustlibs: [
"android.system.virtualizationservice-rust",
"libanyhow",
"libcompositediskconfig",
"libenv_logger",
"liblibc",
"liblog_rust",
"libserde_json",
"libserde",
"libstructopt",
"libvmconfig",
],
apex_available: [
"com.android.virt",

View File

@ -14,7 +14,6 @@
//! Android VM control tool.
mod config;
mod run;
mod sync;

View File

@ -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::fs::File;
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(

View File

@ -3,12 +3,13 @@ package {
}
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",
],

View File

@ -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 @@ use android_system_virtualizationservice::{
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 @@ pub struct DiskImage {
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 @@ impl DiskImage {
}
}
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 open_parcel_file(filename: &Path, writable: bool) -> Result<ParcelFileDescrip
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()
}