diff options
author | Lars Wirzenius <liw@liw.fi> | 2021-12-08 12:12:09 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2021-12-08 12:12:09 +0200 |
commit | 94bc8dc8b4537d228121f877513aafcae42cbb44 (patch) | |
tree | c5a8d68f5fc9e9814b8918d7a7f3ceabddaea685 /src/image.rs | |
parent | 31dc6df2d478e66eb39388dedd48080da0e72645 (diff) | |
download | vmadm-94bc8dc8b4537d228121f877513aafcae42cbb44.tar.gz |
feat: give a more useful error message when VM image already exists
Sponsored-by: author
Diffstat (limited to 'src/image.rs')
-rw-r--r-- | src/image.rs | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/src/image.rs b/src/image.rs index 7b7c2ce..ffe5b56 100644 --- a/src/image.rs +++ b/src/image.rs @@ -1,5 +1,6 @@ //! Virtual machine image handling. +use log::debug; use std::fs::copy; use std::path::{Path, PathBuf}; use std::process::Command; @@ -8,6 +9,10 @@ use std::result::Result; /// Errors from this module. #[derive(Debug, thiserror::Error)] pub enum ImageError { + /// The image file for a new VM already exists. + #[error("image file already exists: {0}")] + ImageExists(PathBuf), + /// Error resizing image. #[error("qemu-img resize failed: {0}")] ResizeError(String), @@ -34,9 +39,21 @@ pub struct VirtualMachineImage { impl VirtualMachineImage { /// Create new image from a base image. pub fn new_from_base(base_image: &Path, image: &Path) -> Result<Self, ImageError> { + debug!( + "creating new image {} from base image {}", + image.display(), + base_image.display() + ); + debug!("does {} exixt? {}", image.display(), image.exists()); + if image.exists() { + debug!("image already exists: {}", image.display()); + return Err(ImageError::ImageExists(image.to_path_buf())); + } + debug!("copying base image to new image"); copy(base_image, image).map_err(|err| { ImageError::BaseImageCopy(base_image.to_path_buf(), image.to_path_buf(), err) })?; + debug!("all good"); Ok(Self { filename: image.to_path_buf(), }) |