From e95fa37f9aef7bb038d9490e697cd1d71dd1f84f Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sun, 21 Mar 2021 11:29:19 +0200 Subject: feat: optionally mark new VM as autostarted --- src/cmd/new.rs | 1 + src/config.rs | 3 +++ src/libvirt.rs | 7 +++++++ src/spec.rs | 15 +++++++++++++++ vmadm.md | 1 + 5 files changed, 27 insertions(+) diff --git a/src/cmd/new.rs b/src/cmd/new.rs index 5484c08..39f54c9 100644 --- a/src/cmd/new.rs +++ b/src/cmd/new.rs @@ -69,6 +69,7 @@ pub fn new(specs: &[Specification]) -> Result<(), NewError> { wait_for_ssh(&spec.name); libvirt.detach_cloud_init_iso(&spec.name)?; + libvirt.set_autostart(&spec.name, spec.autostart)?; } Ok(()) diff --git a/src/config.rs b/src/config.rs index 9894f45..88c186b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -24,6 +24,9 @@ pub struct Configuration { /// Should host certificates be generated for new VMs? pub default_generate_host_certificate: Option, + /// Should new VM be started automatically on host boot? + pub default_autostart: Option, + /// Directory where new VM images should be created, if given. pub image_directory: Option, diff --git a/src/libvirt.rs b/src/libvirt.rs index f0ad2d0..c93bd9f 100644 --- a/src/libvirt.rs +++ b/src/libvirt.rs @@ -119,6 +119,13 @@ impl Libvirt { Ok(()) } + + pub fn set_autostart(&self, name: &str, autostart: bool) -> Result<(), VirtError> { + if let Some(domain) = self.get_domain(name)? { + domain.set_autostart(autostart)?; + } + Ok(()) + } } fn wait_until_inactive(domain: &Domain, name: &str) { diff --git a/src/spec.rs b/src/spec.rs index 4bd0bb9..b9828e9 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -29,6 +29,7 @@ struct OneVmInputSpecification { pub memory_mib: Option, pub cpus: Option, pub generate_host_certificate: Option, + pub autostart: Option, pub ca_key: Option, } @@ -98,6 +99,16 @@ impl OneVmInputSpecification { SpecificationError::NoBaseImage(name.to_string()), ) } + + fn autostart(&self, config: &Configuration) -> bool { + if let Some(x) = self.autostart { + x + } else if let Some(x) = config.default_autostart { + x + } else { + false + } + } } fn get<'a, T>( @@ -171,6 +182,9 @@ pub struct Specification { /// Should a new host key and certificate be created for new VM? pub generate_host_certificate: bool, + /// Should the VM be started automatically when host starts? + pub autostart: bool, + /// Path to CA key for creating host certificate. pub ca_key: Option, } @@ -284,6 +298,7 @@ impl Specification { memory_mib: input.memory_mib(config, name)?, cpus: input.cpus(config, name)?, generate_host_certificate: gen_cert, + autostart: input.autostart(config), ca_key, }; diff --git a/vmadm.md b/vmadm.md index a3e62d7..aa3a704 100644 --- a/vmadm.md +++ b/vmadm.md @@ -43,6 +43,7 @@ default_image_gib: 5 default_memory_mib: 2048 default_cpus: 1 default_generate_host_certificate: true +default_autostart: true ca_key: ca_key authorized_keys: - .ssh/id_rsa.pub -- cgit v1.2.1