blob: ee8f0e996f694bcd887d3752511ccf619aa510fb (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
use crate::spec::Specification;
use log::{debug, info};
use std::thread;
use std::time::Duration;
use virt::connect::Connect;
pub fn delete(spec: &Specification) -> anyhow::Result<()> {
info!("deleting virtual machine {}", spec.name);
debug!("connecting to libvirtd");
let conn = Connect::open("qemu:///system")?;
debug!("listing all domains");
let domains = conn.list_all_domains(0)?;
for domain in domains {
debug!("considering {}", domain.get_name()?);
if domain.get_name()? == spec.name {
debug!("shutdown {}", spec.name);
domain.shutdown().ok();
let briefly = Duration::from_millis(1000);
loop {
thread::sleep(briefly);
match domain.is_active() {
Ok(true) => (),
Ok(false) => break,
Err(err) => {
debug!("is_active: {}", err);
}
}
debug!("{} is still running", spec.name);
}
debug!("undefine {}", spec.name);
domain.undefine()?;
debug!("removing image file {}", spec.image.display());
std::fs::remove_file(&spec.image)?;
}
}
Ok(())
}
|