diff options
Diffstat (limited to 'src/install.rs')
-rw-r--r-- | src/install.rs | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/install.rs b/src/install.rs index 21b9f3f..7506e0d 100644 --- a/src/install.rs +++ b/src/install.rs @@ -1,24 +1,38 @@ +//! Create a new VM. +//! +//! This module runs the `virt-install` tool to create a new VM, using +//! an existing image file. It attaches the cloud-init ISO +//! configuration image to the VM. + use crate::cloudinit::{CloudInitConfig, CloudInitError}; use crate::image::VirtualMachineImage; use std::process::Command; use std::result::Result; use tempfile::tempdir; +/// Errors from this module #[derive(Debug, thiserror::Error)] pub enum VirtInstallError { + /// Failed to create VM. #[error("virt-install failed: {0}")] VirtInstallFailed(String), + /// I/O error. #[error(transparent)] IoError(#[from] std::io::Error), + /// Error parsing a string as UTF8. #[error(transparent)] StringError(#[from] std::string::FromUtf8Error), + /// Error from cloud-init configuration. #[error(transparent)] CloudInitError(#[from] CloudInitError), } +/// Arguments to virt-install. +/// +/// These are the arguments we can adjust, for running virt-install. #[derive(Debug)] pub struct VirtInstallArgs { name: String, @@ -29,6 +43,7 @@ pub struct VirtInstallArgs { } impl VirtInstallArgs { + /// Create new set of arguments for virt-install. pub fn new(name: &str, image: &VirtualMachineImage, init: &CloudInitConfig) -> Self { Self { name: name.to_string(), @@ -39,35 +54,43 @@ impl VirtInstallArgs { } } + /// Name for new VM. pub fn name(&self) -> &str { &self.name } + /// Memory for new VM, in MiB. pub fn memory(&self) -> u64 { self.memory } + /// Change memory to give to new VM, in MiB. pub fn set_memory(&mut self, memory: u64) { self.memory = memory } + /// Virtual CPUs for new VM. pub fn vcpus(&self) -> u64 { self.vcpus } + /// Change virtual CPUs for new VM. pub fn set_vcpus(&mut self, vcpus: u64) { self.vcpus = vcpus } + /// Image for new VM. pub fn image(&self) -> &VirtualMachineImage { &self.image } + /// cloud-init configuration for new VM. pub fn init(&self) -> &CloudInitConfig { &self.init } } +/// Create new VM with virt-install. pub fn virt_install(args: &VirtInstallArgs) -> Result<(), VirtInstallError> { let dir = tempdir()?; let iso = dir.path().join("cloudinit.iso"); |