summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2021-01-24 15:30:13 +0200
committerLars Wirzenius <liw@liw.fi>2021-01-24 15:30:13 +0200
commit1a5a6e5e8e356af082b11b549f3ddfa56f0046db (patch)
treea1f515317b010dcb4bfe046104b491f37cdc470d
parentd79286c5adb8b6960ff32afee4fb8d116f0f043b (diff)
downloadvmadm-1a5a6e5e8e356af082b11b549f3ddfa56f0046db.tar.gz
install
-rw-r--r--src/install.rs93
1 files changed, 93 insertions, 0 deletions
diff --git a/src/install.rs b/src/install.rs
new file mode 100644
index 0000000..5476277
--- /dev/null
+++ b/src/install.rs
@@ -0,0 +1,93 @@
+use crate::cloudinit::{CloudInitConfig, CloudInitError};
+use crate::image::VirtualMachineImage;
+use std::process::Command;
+use std::result::Result;
+use tempfile::tempdir;
+
+#[derive(Debug, thiserror::Error)]
+pub enum VirtInstallError {
+ #[error("virt-install failed: {0}")]
+ VirtInstallFailed(String),
+
+ #[error(transparent)]
+ IoError(#[from] std::io::Error),
+
+ #[error(transparent)]
+ StringError(#[from] std::string::FromUtf8Error),
+
+ #[error(transparent)]
+ CloudInitError(#[from] CloudInitError),
+}
+
+pub struct VirtInstallArgs {
+ name: String,
+ memory: usize,
+ vcpus: usize,
+ image: VirtualMachineImage,
+ init: CloudInitConfig,
+}
+
+impl VirtInstallArgs {
+ pub fn new(name: &str, image: &VirtualMachineImage, init: &CloudInitConfig) -> Self {
+ Self {
+ name: name.to_string(),
+ memory: 1024,
+ vcpus: 1,
+ image: image.clone(),
+ init: init.clone(),
+ }
+ }
+
+ pub fn name(&self) -> &str {
+ &self.name
+ }
+
+ pub fn memory(&self) -> usize {
+ self.memory
+ }
+
+ pub fn set_memory(&mut self, memory: usize) {
+ self.memory = memory
+ }
+
+ pub fn vcpus(&self) -> usize {
+ self.vcpus
+ }
+
+ pub fn set_vcpus(&mut self, vcpus: usize) {
+ self.vcpus = vcpus
+ }
+
+ pub fn image(&self) -> &VirtualMachineImage {
+ &self.image
+ }
+
+ pub fn init(&self) -> &CloudInitConfig {
+ &self.init
+ }
+}
+
+pub fn virt_install(args: &VirtInstallArgs) -> Result<(), VirtInstallError> {
+ let dir = tempdir()?;
+ let iso = dir.path().join("cloudinit.iso");
+ args.init().create_iso(&iso)?;
+
+ let r = Command::new("virt-install")
+ .arg("--name")
+ .arg(args.name())
+ .arg("--memory")
+ .arg(format!("{}", args.memory()))
+ .arg("--vcpus")
+ .arg(format!("{}", args.vcpus()))
+ .arg(format!(
+ "--disk=path={},cache=none",
+ args.image().filename().display()
+ ))
+ .arg(format!("--disk=path={},readconly=on", iso.display()))
+ .output()?;
+ if !r.status.success() {
+ let stderr = String::from_utf8(r.stderr)?;
+ return Err(VirtInstallError::VirtInstallFailed(stderr));
+ }
+ Ok(())
+}