summaryrefslogtreecommitdiff
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
parente6455df900d71826b04d33fba358f0c91e54eec3 (diff)
downloadvmadm-32e73732e2f350818c077846596122f99bd6813b.tar.gz
start on image
-rw-r--r--Cargo.toml1
-rw-r--r--src/errors.rs4
-rw-r--r--src/image.rs47
-rw-r--r--src/lib.rs1
4 files changed, 53 insertions, 0 deletions
diff --git a/Cargo.toml b/Cargo.toml
index fac79e6..77bb5c4 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -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(())
+ }
+}
diff --git a/src/lib.rs b/src/lib.rs
index 3766a04..16a21b3 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1 +1,2 @@
pub mod cloudinit;
+pub mod image;