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
|
use crate::cloudinit::{CloudInitConfig, CloudInitError};
use crate::image::VirtualMachineImage;
use std::process::Command;
use std::result::Result;
use tempfile::tempdir;
#[derive(Debug, thiserror::Error)]
pub enum VirtInstallError {
#[error("virt-install failed: {0}")]
VirtInstallFailed(String),
#[error(transparent)]
IoError(#[from] std::io::Error),
#[error(transparent)]
StringError(#[from] std::string::FromUtf8Error),
#[error(transparent)]
CloudInitError(#[from] CloudInitError),
}
pub struct VirtInstallArgs {
name: String,
memory: usize,
vcpus: usize,
image: VirtualMachineImage,
init: CloudInitConfig,
}
impl VirtInstallArgs {
pub fn new(name: &str, image: &VirtualMachineImage, init: &CloudInitConfig) -> Self {
Self {
name: name.to_string(),
memory: 1024,
vcpus: 1,
image: image.clone(),
init: init.clone(),
}
}
pub fn name(&self) -> &str {
&self.name
}
pub fn memory(&self) -> usize {
self.memory
}
pub fn set_memory(&mut self, memory: usize) {
self.memory = memory
}
pub fn vcpus(&self) -> usize {
self.vcpus
}
pub fn set_vcpus(&mut self, vcpus: usize) {
self.vcpus = vcpus
}
pub fn image(&self) -> &VirtualMachineImage {
&self.image
}
pub fn init(&self) -> &CloudInitConfig {
&self.init
}
}
pub fn virt_install(args: &VirtInstallArgs) -> Result<(), VirtInstallError> {
let dir = tempdir()?;
let iso = dir.path().join("cloudinit.iso");
args.init().create_iso(&iso)?;
let r = Command::new("virt-install")
.arg("--name")
.arg(args.name())
.arg("--memory")
.arg(format!("{}", args.memory()))
.arg("--vcpus")
.arg(format!("{}", args.vcpus()))
.arg(format!(
"--disk=path={},cache=none",
args.image().filename().display()
))
.arg(format!("--disk=path={},readconly=on", iso.display()))
.output()?;
if !r.status.success() {
let stderr = String::from_utf8(r.stderr)?;
return Err(VirtInstallError::VirtInstallFailed(stderr));
}
Ok(())
}
|