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
| | // SPDX-License-Identifier: EUPL-1.2+
// SPDX-FileCopyrightText: 2022-2024 Alyssa Ross <hi@alyssa.is>
mod ch;
mod net;
mod s6;
use std::borrow::Cow;
use std::convert::TryInto;
use std::env::args_os;
use std::ffi::OsStr;
use std::fs::File;
use std::io::{self, ErrorKind};
use std::path::Path;
use ch::{
ConsoleConfig, DiskConfig, FsConfig, GpuConfig, LandlockConfig, MemoryConfig, PayloadConfig,
VmConfig, VsockConfig,
};
use net::net_setup;
pub fn prog_name() -> String {
args_os()
.next()
.as_ref()
.map(Path::new)
.and_then(Path::file_name)
.map_or(Cow::Borrowed("start-vmm"), OsStr::to_string_lossy)
.into_owned()
}
pub fn vm_config(vm_dir: &Path) -> Result<VmConfig, String> {
let Some(vm_name) = vm_dir.file_name().unwrap().to_str() else {
return Err(format!("VM dir {vm_dir:?} is not valid UTF-8"));
};
// A colon is used for namespacing vhost-user backends, so while
// we have the VM name we enforce that it doesn't contain one.
if vm_name.contains(':') {
return Err(format!("VM name may not contain a colon: {vm_name:?}"));
}
let name_bytes = vm_name.as_bytes();
let config_dir = vm_dir.join("config");
let blk_dir = config_dir.join("blk");
let kernel_path = config_dir.join("vmlinux");
let net_providers_dir = config_dir.join("providers/net");
Ok(VmConfig {
console: ConsoleConfig {
mode: "Pty",
file: None,
},
disks: match blk_dir.read_dir() {
Ok(entries) => entries
.into_iter()
.map(|result| {
Ok(result
.map_err(|e| format!("examining directory entry: {e}"))?
.path())
})
.filter(|result| {
result
.as_ref()
.map(|entry| entry.extension() == Some(OsStr::new("img")))
.unwrap_or(true)
})
.map(|result: Result<_, String>| {
let entry = result?.to_str().unwrap().to_string();
if entry.contains(',') {
return Err(format!("illegal ',' character in path {entry:?}"));
}
Ok(DiskConfig {
path: entry,
readonly: true,
})
})
.collect::<Result<_, _>>()?,
Err(e) => return Err(format!("reading directory {blk_dir:?}: {e}")),
},
fs: [FsConfig {
tag: "virtiofs0",
socket: format!(
"/run/service/vm-services/instance/{vm_name}/data/service/vhost-user-fs/env/virtiofsd.sock"
),
}],
gpu: vec![GpuConfig {
socket: format!(
"/run/service/vm-services/instance/{vm_name}/data/service/vhost-user-gpu/env/crosvm.sock"
),
}],
memory: MemoryConfig {
size: 1 << 30,
shared: true,
},
net: match net_providers_dir.read_dir() {
Ok(_) => {
// SAFETY: we check the result.
let net = unsafe {
net_setup(
name_bytes.as_ptr().cast(),
name_bytes
.len()
.try_into()
.map_err(|e| format!("VM name too long: {e}"))?,
)
};
if net.fd == -1 {
let e = io::Error::last_os_error();
return Err(format!("setting up networking failed: {e}"));
}
vec![net.try_into().unwrap()]
}
Err(e) if e.kind() == ErrorKind::NotFound => Default::default(),
Err(e) => return Err(format!("reading directory {net_providers_dir:?}: {e}")),
},
payload: PayloadConfig {
kernel: kernel_path.to_str().unwrap().to_string(),
#[cfg(target_arch = "x86_64")]
cmdline: "console=ttyS0 root=PARTLABEL=root",
#[cfg(not(target_arch = "x86_64"))]
cmdline: "root=PARTLABEL=root",
},
serial: ConsoleConfig {
mode: "File",
file: Some(format!("/run/{vm_name}.log")),
},
vsock: VsockConfig {
cid: 3,
socket: vm_dir.join("vsock").into_os_string().into_string().unwrap(),
},
landlock_enable: true,
landlock_rules: vec![
LandlockConfig {
path: "/sys/devices",
access: "rw",
},
LandlockConfig {
path: "/dev/vfio",
access: "rw",
},
],
})
}
pub fn create_vm(vm_dir: &Path, ready_fd: File) -> Result<(), String> {
let config = vm_config(vm_dir)?;
ch::create_vm(vm_dir, ready_fd, config).map_err(|e| format!("creating VM: {e}"))
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs::OpenOptions;
#[test]
fn test_vm_name_colon() {
let ready_fd = OpenOptions::new().write(true).open("/dev/null").unwrap();
let e = create_vm(Path::new("/:vm"), ready_fd).unwrap_err();
assert!(e.contains("colon"), "unexpected error: {:?}", e);
}
}
|