diff options
author | Lars Wirzenius <liw@liw.fi> | 2021-03-13 14:51:19 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2021-03-14 09:11:07 +0200 |
commit | b8119579a246727805a03c5a8e60fb44109410f6 (patch) | |
tree | b4e0698ae9f50b8757530f2332cb1c40872d4a5c /src/cmd | |
parent | d4cba41b5674e6ca1cbd5669aeb42d5f7d62e8bd (diff) | |
download | vmadm-b8119579a246727805a03c5a8e60fb44109410f6.tar.gz |
fix: VMs can be restarted
Previously, the temporary file for the cloud-init configuration ISO
was left attached to the VM. This meant the VM couldn't be turned off
and back on again: the temporary no longer existed.
Now we detach the ISO file after the VM has booted.
As a side effect, vmadm has gained start and shutdown subcommands, so
that the fix can be tested.
Diffstat (limited to 'src/cmd')
-rw-r--r-- | src/cmd/mod.rs | 6 | ||||
-rw-r--r-- | src/cmd/new.rs | 32 | ||||
-rw-r--r-- | src/cmd/shutdown.rs | 19 | ||||
-rw-r--r-- | src/cmd/start.rs | 31 |
4 files changed, 74 insertions, 14 deletions
diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index 606e326..8ae8f70 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -12,5 +12,11 @@ pub use list::list; pub mod delete; pub use delete::delete; +pub mod start; +pub use start::start; + +pub mod shutdown; +pub use shutdown::shutdown; + pub mod cloud_init; pub use cloud_init::cloud_init; diff --git a/src/cmd/new.rs b/src/cmd/new.rs index 57f69ae..5484c08 100644 --- a/src/cmd/new.rs +++ b/src/cmd/new.rs @@ -3,13 +3,13 @@ use crate::cloudinit::{CloudInitConfig, CloudInitError}; use crate::image::{ImageError, VirtualMachineImage}; use crate::install::{virt_install, VirtInstallArgs, VirtInstallError}; +use crate::libvirt::{Libvirt, VirtError}; use crate::spec::Specification; +use crate::util::wait_for_ssh; use bytesize::GIB; use log::info; -use std::net::TcpStream; - -const SSH_PORT: i32 = 22; +use tempfile::tempdir; /// Errors returned by this module. #[derive(Debug, thiserror::Error)] @@ -25,6 +25,14 @@ pub enum NewError { /// Problem with libvirt. #[error(transparent)] VirtInstallError(#[from] VirtInstallError), + + /// Problem with virsh. + #[error(transparent)] + VirshError(#[from] std::io::Error), + + /// Problem from libvirt server. + #[error(transparent)] + VirtError(#[from] VirtError), } /// The `new` sub-command. @@ -32,6 +40,7 @@ pub enum NewError { /// Create all the new virtual machines specified by the caller. Wait /// until each VM's SSH port listens for connections. pub fn new(specs: &[Specification]) -> Result<(), NewError> { + let libvirt = Libvirt::connect("qemu:///system")?; for spec in specs { info!("creating new VM {}", spec.name); @@ -49,23 +58,18 @@ pub fn new(specs: &[Specification]) -> Result<(), NewError> { image.resize(spec.image_size_gib * GIB)?; info!("creating VM"); + let dir = tempdir()?; + let iso = dir.path().join("cloudinit.iso"); let mut args = VirtInstallArgs::new(&spec.name, &image, &init); args.set_memory(spec.memory_mib); args.set_vcpus(spec.cpus); - virt_install(&args)?; + virt_install(&args, &iso)?; info!("waiting for {} to open its SSH port", spec.name); - wait_for_port(&spec.name, SSH_PORT)?; + wait_for_ssh(&spec.name); + + libvirt.detach_cloud_init_iso(&spec.name)?; } Ok(()) } - -fn wait_for_port(name: &str, port: i32) -> Result<(), NewError> { - let addr = format!("{}:{}", name, port); - loop { - if TcpStream::connect(&addr).is_ok() { - return Ok(()); - } - } -} diff --git a/src/cmd/shutdown.rs b/src/cmd/shutdown.rs new file mode 100644 index 0000000..b53ebd3 --- /dev/null +++ b/src/cmd/shutdown.rs @@ -0,0 +1,19 @@ +//! The `shutdown` sub-command. + +use crate::libvirt::{Libvirt, VirtError}; +use crate::spec::Specification; +use log::{debug, info}; + +/// Shut down VMs corresponding to specifications. +pub fn shutdown(specs: &[Specification]) -> Result<(), VirtError> { + let libvirt = Libvirt::connect("qemu:///system")?; + for spec in specs { + info!("shutting down virtual machine {}", spec.name); + libvirt.shutdown(&spec.name)?; + } + for spec in specs { + debug!("waiting for {} to become inactive", spec.name); + libvirt.wait_for_inactive(&spec.name)?; + } + Ok(()) +} diff --git a/src/cmd/start.rs b/src/cmd/start.rs new file mode 100644 index 0000000..a69f54c --- /dev/null +++ b/src/cmd/start.rs @@ -0,0 +1,31 @@ +//! The `start` sub-command. + +use crate::libvirt::{Libvirt, VirtError}; +use crate::spec::Specification; +use crate::util::wait_for_ssh; +use log::info; + +/// Errors from this module. +#[derive(Debug, thiserror::Error)] +pub enum StartError { + /// Error from libvirt. + #[error(transparent)] + VirtError(#[from] VirtError), + + /// Error doing I/O. + #[error(transparent)] + IoError(#[from] std::io::Error), +} + +/// Start existing VMs corresponding to specifications. +pub fn start(specs: &[Specification]) -> Result<(), StartError> { + let libvirt = Libvirt::connect("qemu:///system")?; + for spec in specs { + info!("starting virtual machine {}", spec.name); + libvirt.start(&spec.name)?; + } + for spec in specs { + wait_for_ssh(&spec.name); + } + Ok(()) +} |