summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2021-03-21 09:38:13 +0000
committerLars Wirzenius <liw@liw.fi>2021-03-21 09:38:13 +0000
commiteb682fe9a29c1176ddf691b2829fb9dab5b7622e (patch)
tree3ad51d9f1de7a6cedfada18964a5869ce44d7c4e
parentb905e6d9ccc69781fc34d3b31a627cb5eacdabcf (diff)
parente95fa37f9aef7bb038d9490e697cd1d71dd1f84f (diff)
downloadvmadm-eb682fe9a29c1176ddf691b2829fb9dab5b7622e.tar.gz
Merge branch 'autostart' into 'main'
feat: optionally mark new VM as autostarted See merge request larswirzenius/vmadm!25
-rw-r--r--src/cmd/new.rs1
-rw-r--r--src/config.rs3
-rw-r--r--src/libvirt.rs7
-rw-r--r--src/spec.rs15
-rw-r--r--vmadm.md1
5 files changed, 27 insertions, 0 deletions
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<bool>,
+ /// Should new VM be started automatically on host boot?
+ pub default_autostart: Option<bool>,
+
/// Directory where new VM images should be created, if given.
pub image_directory: Option<PathBuf>,
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<u64>,
pub cpus: Option<u64>,
pub generate_host_certificate: Option<bool>,
+ pub autostart: Option<bool>,
pub ca_key: Option<PathBuf>,
}
@@ -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<PathBuf>,
}
@@ -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