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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
//! An abstraction on top of the libvirt bindings.
use log::debug;
use std::path::Path;
use std::thread;
use std::time::Duration;
use virt::connect::Connect;
use virt::domain::Domain;
/// Errors from this module.
#[derive(Debug, thiserror::Error)]
pub enum VirtError {
/// Error creating virtual machine.
#[error(transparent)]
VirtError(#[from] virt::error::Error),
/// Error doing I/O.
#[error(transparent)]
IoError(#[from] std::io::Error),
}
/// Access libvirt for all the things this program needs.
pub struct Libvirt {
conn: Connect,
}
impl Libvirt {
pub fn connect(url: &str) -> Result<Self, VirtError> {
debug!("connecting to libvirtd {}", url);
let conn = Connect::open(url)?;
Ok(Self { conn })
}
fn get_domains(&self) -> Result<Vec<Domain>, VirtError> {
debug!("listing all domains");
Ok(self.conn.list_all_domains(0)?)
}
fn get_domain(&self, name: &str) -> Result<Option<Domain>, VirtError> {
for domain in self.get_domains()? {
if domain.get_name()? == name {
return Ok(Some(domain));
}
}
Ok(None)
}
pub fn names(&self) -> Result<Vec<String>, VirtError> {
let mut ret = vec![];
for domain in self.get_domains()? {
ret.push(domain.get_name()?);
}
Ok(ret)
}
pub fn is_active(&self, name: &str) -> Result<bool, VirtError> {
if let Some(domain) = self.get_domain(name)? {
Ok(domain.is_active()?)
} else {
Ok(false)
}
}
pub fn start(&self, name: &str) -> Result<(), VirtError> {
if let Some(domain) = self.get_domain(name)? {
domain.create()?;
}
Ok(())
}
pub fn shutdown(&self, name: &str) -> Result<(), VirtError> {
if let Some(domain) = self.get_domain(name)? {
domain.shutdown()?;
}
Ok(())
}
pub fn delete(&self, name: &str, image: &Path) -> Result<(), VirtError> {
if let Some(domain) = self.get_domain(name)? {
debug!("shutting down {}", name);
domain.shutdown()?;
wait_until_inactive(&domain, name)?;
debug!("undefine {}", name);
domain.undefine()?;
debug!("removing image file {}", image.display());
std::fs::remove_file(image)?;
}
Ok(())
}
}
fn wait_until_inactive(domain: &Domain, name: &str) -> Result<(), VirtError> {
debug!("waiting for domain {} to become inactive", name);
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!("domain {} is still running", name);
}
Ok(())
}
|