summaryrefslogtreecommitdiff
path: root/src/image.rs
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2021-01-24 14:46:06 +0200
committerLars Wirzenius <liw@liw.fi>2021-01-24 14:46:06 +0200
commit32e73732e2f350818c077846596122f99bd6813b (patch)
tree271721195cc8597009b2c11931faa8f498ad6ee1 /src/image.rs
parente6455df900d71826b04d33fba358f0c91e54eec3 (diff)
downloadvmadm-32e73732e2f350818c077846596122f99bd6813b.tar.gz
start on image
Diffstat (limited to 'src/image.rs')
-rw-r--r--src/image.rs47
1 files changed, 47 insertions, 0 deletions
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(())
+ }
+}