diff options
author | Lars Wirzenius <liw@liw.fi> | 2021-01-24 14:46:06 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2021-01-24 14:46:06 +0200 |
commit | 32e73732e2f350818c077846596122f99bd6813b (patch) | |
tree | 271721195cc8597009b2c11931faa8f498ad6ee1 | |
parent | e6455df900d71826b04d33fba358f0c91e54eec3 (diff) | |
download | vmadm-32e73732e2f350818c077846596122f99bd6813b.tar.gz |
start on image
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/errors.rs | 4 | ||||
-rw-r--r-- | src/image.rs | 47 | ||||
-rw-r--r-- | src/lib.rs | 1 |
4 files changed, 53 insertions, 0 deletions
@@ -7,3 +7,4 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +thiserror = "1" diff --git a/src/errors.rs b/src/errors.rs new file mode 100644 index 0000000..ba7875a --- /dev/null +++ b/src/errors.rs @@ -0,0 +1,4 @@ +use thiserror::Error; + +#[derive(Debug, Error)] +pub enum VmadminError {} diff --git a/src/image.rs b/src/image.rs new file mode 100644 index 0000000..e0b5a98 --- /dev/null +++ b/src/image.rs @@ -0,0 +1,47 @@ +use std::fs::copy; +use std::path::{Path, PathBuf}; +use std::process::Command; +use std::result::Result; + +#[derive(Debug, thiserror::Error)] +pub enum ImageError { + #[error("qemu-img resize failed: {0}")] + ResizeError(String), + + #[error(transparent)] + IoError(#[from] std::io::Error), + + #[error(transparent)] + StringError(#[from] std::string::FromUtf8Error), +} + +pub struct VirtualMachineImage { + filename: PathBuf, +} + +impl VirtualMachineImage { + pub fn new_from_base(base_image: &Path, image: &Path) -> Result<Self, ImageError> { + copy(base_image, image)?; + Ok(Self { + filename: image.to_path_buf(), + }) + } + + pub fn filename(&self) -> &Path { + &self.filename + } + + pub fn resize(&self, new_size: usize) -> Result<(), ImageError> { + let r = Command::new("qemu-img") + .arg("resize") + .arg("-f") + .arg(self.filename()) + .arg(format!("{}", new_size)) + .output()?; + if !r.status.success() { + let stderr = String::from_utf8(r.stderr)?; + return Err(ImageError::ResizeError(stderr)); + } + Ok(()) + } +} @@ -1 +1,2 @@ pub mod cloudinit; +pub mod image; |