Merge VM and CDISK config into libvmconfig

so that virtualizationservice and vm use it.

Bug: 190503456
Test: MicrodroidHostTestCases
Change-Id: I63eaf0ac1c6d0e1e17d19f4863c5f4f14525aa32
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/config.rs b/vm/src/config.rs
deleted file mode 100644
index c36c714..0000000
--- a/vm/src/config.rs
+++ /dev/null
@@ -1,161 +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.
-
-//! Struct for VM configuration.
-
-use android_system_virtualizationservice::{
-    aidl::android::system::virtualizationservice::DiskImage::DiskImage as AidlDiskImage,
-    aidl::android::system::virtualizationservice::Partition::Partition as AidlPartition,
-    aidl::android::system::virtualizationservice::VirtualMachineConfig::VirtualMachineConfig,
-    binder::ParcelFileDescriptor,
-};
-use anyhow::{bail, Context, Error};
-use compositediskconfig::Partition;
-use serde::{Deserialize, Serialize};
-use std::fs::{File, OpenOptions};
-use std::io::BufReader;
-use std::path::{Path, PathBuf};
-
-/// Configuration for a particular VM to be started.
-#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
-pub struct VmConfig {
-    /// The filename of the kernel image, if any.
-    pub kernel: Option<PathBuf>,
-    /// The filename of the initial ramdisk for the kernel, if any.
-    pub initrd: Option<PathBuf>,
-    /// Parameters to pass to the kernel. As far as the VMM and boot protocol are concerned this is
-    /// just a string, but typically it will contain multiple parameters separated by spaces.
-    pub params: Option<String>,
-    /// The bootloader to use. If this is supplied then the kernel and initrd must not be supplied;
-    /// the bootloader is instead responsibly for loading the kernel from one of the disks.
-    pub bootloader: Option<PathBuf>,
-    /// Disk images to be made available to the VM.
-    #[serde(default)]
-    pub disks: Vec<DiskImage>,
-    /// Whether the VM should be a protected VM.
-    #[serde(default)]
-    pub protected: bool,
-}
-
-impl VmConfig {
-    /// Ensure that the configuration has a valid combination of fields set, or return an error if
-    /// not.
-    pub fn validate(&self) -> Result<(), Error> {
-        if self.bootloader.is_none() && self.kernel.is_none() {
-            bail!("VM must have either a bootloader or a kernel image.");
-        }
-        if self.bootloader.is_some() && (self.kernel.is_some() || self.initrd.is_some()) {
-            bail!("Can't have both bootloader and kernel/initrd image.");
-        }
-        for disk in &self.disks {
-            if disk.image.is_none() == disk.partitions.is_empty() {
-                bail!("Exactly one of image and partitions must be specified. (Was {:?}.)", disk);
-            }
-        }
-        Ok(())
-    }
-
-    /// Load the configuration for a VM from the given JSON file, and check that it is valid.
-    pub fn load(file: &File) -> Result<VmConfig, Error> {
-        let buffered = BufReader::new(file);
-        let config: VmConfig = serde_json::from_reader(buffered)?;
-        config.validate()?;
-        Ok(config)
-    }
-
-    /// Convert the `VmConfig` to a [`VirtualMachineConfig`] which can be passed to the Virt
-    /// Manager.
-    pub fn to_parcelable(&self) -> Result<VirtualMachineConfig, Error> {
-        Ok(VirtualMachineConfig {
-            kernel: maybe_open_parcel_file(&self.kernel, false)?,
-            initrd: maybe_open_parcel_file(&self.initrd, false)?,
-            params: self.params.clone(),
-            bootloader: maybe_open_parcel_file(&self.bootloader, false)?,
-            disks: self.disks.iter().map(DiskImage::to_parcelable).collect::<Result<_, Error>>()?,
-            protected_vm: self.protected,
-        })
-    }
-}
-
-/// A disk image to be made available to the VM.
-#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
-pub struct DiskImage {
-    /// The filename of the disk image, if it already exists. Exactly one of this and `partitions`
-    /// must be specified.
-    #[serde(default)]
-    pub image: Option<PathBuf>,
-    /// A set of partitions to be assembled into a composite image.
-    #[serde(default)]
-    pub partitions: Vec<Partition>,
-    /// Whether this disk should be writable by the VM.
-    pub writable: bool,
-}
-
-impl DiskImage {
-    fn to_parcelable(&self) -> Result<AidlDiskImage, Error> {
-        let partitions =
-            self.partitions.iter().map(partition_to_parcelable).collect::<Result<_, Error>>()?;
-        Ok(AidlDiskImage {
-            image: maybe_open_parcel_file(&self.image, self.writable)?,
-            writable: self.writable,
-            partitions,
-        })
-    }
-}
-
-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);
-        }
-        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> {
-    Ok(ParcelFileDescriptor::new(
-        OpenOptions::new()
-            .read(true)
-            .write(writable)
-            .open(filename)
-            .with_context(|| format!("Failed to open {:?}", filename))?,
-    ))
-}
-
-/// If the given filename is `Some`, try to open it and wrap it in a [`ParcelFileDescriptor`].
-fn maybe_open_parcel_file(
-    filename: &Option<PathBuf>,
-    writable: bool,
-) -> Result<Option<ParcelFileDescriptor>, Error> {
-    filename.as_deref().map(|filename| open_parcel_file(filename, writable)).transpose()
-}
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(