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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
pub(crate) mod nat;
pub(crate) mod vm;
#[cfg(feature = "zfs")]
use crate::{imgroot_register, CLONE_MARK};
use crate::{ImagePath, OsName, Vm, VmId, VmKind};
use myutil::{err::*, *};
use nix::sched::{clone, CloneFlags};
use std::collections::HashMap;
use std::{
fs,
path::{Path, PathBuf},
};
#[cfg(feature = "zfs")]
#[inline(always)]
pub fn exec(
imgpath: &str,
cb: fn() -> Result<()>,
serv_ip: &str,
) -> Result<()> {
imgroot_register(Some(imgpath));
do_exec(cb, serv_ip).c(d!())
}
#[cfg(not(feature = "zfs"))]
#[inline(always)]
pub fn exec(
_imgpath: &str,
cb: fn() -> Result<()>,
serv_ip: &str,
) -> Result<()> {
do_exec(cb, serv_ip).c(d!())
}
fn do_exec(cb: fn() -> Result<()>, serv_ip: &str) -> Result<()> {
const STACK_SIZ: usize = 1024 * 1024;
let mut stack = Vec::with_capacity(STACK_SIZ);
unsafe {
stack.set_len(STACK_SIZ);
}
let mut flags = CloneFlags::empty();
flags.insert(CloneFlags::CLONE_NEWNS);
flags.insert(CloneFlags::CLONE_NEWPID);
let ops = || -> isize {
info!(
vm::util::mount_make_rprivate()
.c(d!())
.and_then(|_| vm::util::mount_dynfs_proc().c(d!()))
.and_then(|_| vm::util::mount_tmp_tmpfs().c(d!()))
.and_then(|_| vm::engine::init().c(d!()))
.and_then(|_| vm::cgroup::init().c(d!()))
.and_then(|_| nat::init(serv_ip).c(d!()))
.and_then(|_| cb().c(d!()))
)
.and(Ok(0))
.or::<Result<i32>>(Ok(-1))
.unwrap()
};
clone(
Box::new(ops),
stack.as_mut_slice(),
flags,
Some(libc::SIGCHLD),
)
.c(d!())
.map(|_| ())
}
#[cfg(feature = "zfs")]
pub fn get_os_info(img_path: &str) -> Result<HashMap<OsName, ImagePath>> {
get_image_path(img_path).c(d!()).map(|path| {
path.iter()
.filter_map(|i| {
i.file_name()
.map(|j| j.to_str())
.flatten()
.map(|os| (os, i))
})
.filter(|(os, _)| {
vm_kind(os).is_ok()
&& !(os.contains('@')
|| os.contains("-part")
|| os.starts_with(CLONE_MARK))
})
.map(|(os, i)| {
(os.to_lowercase(), i.to_string_lossy().into_owned())
})
.collect()
})
}
#[cfg(not(feature = "zfs"))]
pub fn get_os_info(img_path: &str) -> Result<HashMap<OsName, ImagePath>> {
get_image_path(img_path).c(d!()).map(|path| {
path.iter()
.filter_map(|i| {
i.file_name()
.map(|j| j.to_str())
.flatten()
.map(|os| (os, i))
})
.filter(|(os, _)| vm_kind(os).is_ok())
.map(|(os, i)| {
(os.to_lowercase(), i.to_string_lossy().into_owned())
})
.collect()
})
}
#[cfg(feature = "zfs")]
fn get_image_path(img_path: &str) -> Result<Vec<PathBuf>> {
let mut res = vct![];
let dir = Path::new(img_path);
if dir.is_dir() {
for entry in fs::read_dir(dir).c(d!())? {
let entry = entry.c(d!())?;
let path = entry.path();
if let Some(p) = path.to_str() {
res.push(PathBuf::from(p));
}
}
}
Ok(res)
}
#[inline(always)]
#[cfg(not(feature = "zfs"))]
fn get_image_path(img_path: &str) -> Result<Vec<PathBuf>> {
walk_gen(&Path::new(img_path)).c(d!())
}
#[inline(always)]
#[cfg(not(feature = "zfs"))]
fn walk_gen(dir: &Path) -> Result<Vec<PathBuf>> {
let mut res = vct![];
if dir.is_dir() {
for entry in fs::read_dir(dir).c(d!())? {
let entry = entry.c(d!())?;
let path = entry.path();
if path.is_dir() {
res.append(&mut walk_gen(&path).c(d!())?);
} else if let Some(p) = path.to_str() {
res.push(PathBuf::from(p));
}
}
}
Ok(res)
}
#[inline(always)]
pub fn pause(id: VmId) -> Result<()> {
vm::cgroup::kill_vm(id).c(d!())
}
#[inline(always)]
pub fn resume(vm: &Vm) -> Result<()> {
vm::start(vm).c(d!())
}
pub fn vm_kind(os: &str) -> Result<VmKind> {
let os = os.to_lowercase();
for (prefix, kind) in
&[("qemu:", VmKind::Qemu), ("fire:", VmKind::FireCracker)]
{
if os.starts_with(prefix) {
return Ok(*kind);
}
}
Err(eg!(
"Invalid OS name, it should starts with one of [ qemu, fire[cracker] ]."
))
}