-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsimplevm.rs
166 lines (150 loc) · 5.49 KB
/
simplevm.rs
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
extern crate virtualization_rs;
use block::{Block, ConcreteBlock};
use libc::sleep;
use objc::rc::StrongPtr;
use std::fs::canonicalize;
use std::sync::{Arc, RwLock};
use virtualization_rs::{
base::{dispatch_async, dispatch_queue_create, Id, NSError, NSFileHandle, NIL},
virtualization::{
boot_loader::VZLinuxBootLoaderBuilder,
entropy_device::VZVirtioEntropyDeviceConfiguration,
memory_device::VZVirtioTraditionalMemoryBalloonDeviceConfiguration,
network_device::{
VZMACAddress, VZNATNetworkDeviceAttachment, VZVirtioNetworkDeviceConfiguration,
},
serial_port::{
VZFileHandleSerialPortAttachmentBuilder, VZVirtioConsoleDeviceSerialPortConfiguration,
},
storage_device::{
VZDiskImageStorageDeviceAttachmentBuilder, VZVirtioBlockDeviceConfiguration,
},
virtual_machine::{VZVirtualMachine, VZVirtualMachineConfigurationBuilder},
},
};
use std::path::PathBuf;
use structopt::StructOpt;
#[derive(StructOpt, Debug)]
#[structopt(name = "simplevm")]
struct Opt {
#[structopt(long, parse(from_os_str))]
kernel: PathBuf,
#[structopt(short, long, parse(from_os_str))]
initrd: PathBuf,
#[structopt(short, long, default_value = "console=hvc0")]
command_line: String,
#[structopt(short, long, parse(from_os_str))]
disk: Vec<PathBuf>,
#[structopt(short, long, default_value = "4")]
cpu: usize,
#[structopt(short, long, default_value = "2147483648")]
memory_size: usize,
}
fn main() {
let opt = Opt::from_args();
let cpu_count = opt.cpu;
let memory_size = opt.memory_size;
let command_line = opt.command_line;
let kernel = opt.kernel;
let disks: Vec<PathBuf> = opt.disk;
let initrd = opt.initrd;
if !VZVirtualMachine::supported() {
println!("not supported");
return;
}
let boot_loader = VZLinuxBootLoaderBuilder::new()
.kernel_url(
canonicalize(&kernel)
.unwrap()
.into_os_string()
.into_string()
.unwrap(),
)
.initial_ramdisk_url(
canonicalize(&initrd)
.unwrap()
.into_os_string()
.into_string()
.unwrap(),
)
.command_line(command_line)
.build();
let file_handle_for_reading = NSFileHandle::file_handle_with_standard_input();
let file_handle_for_writing = NSFileHandle::file_handle_with_standard_output();
let attachement = VZFileHandleSerialPortAttachmentBuilder::new()
.file_handle_for_reading(file_handle_for_reading)
.file_handle_for_writing(file_handle_for_writing)
.build();
let serial = VZVirtioConsoleDeviceSerialPortConfiguration::new(attachement);
let entropy = VZVirtioEntropyDeviceConfiguration::new();
let memory_balloon = VZVirtioTraditionalMemoryBalloonDeviceConfiguration::new();
let mut block_devices = Vec::with_capacity(disks.len());
for disk in &disks {
let block_attachment = match VZDiskImageStorageDeviceAttachmentBuilder::new()
.path(
canonicalize(disk)
.unwrap()
.into_os_string()
.into_string()
.unwrap(),
)
.read_only(false)
.build()
{
Ok(x) => x,
Err(err) => {
err.dump();
return;
}
};
let block_device = VZVirtioBlockDeviceConfiguration::new(block_attachment);
block_devices.push(block_device);
}
let network_attachment = VZNATNetworkDeviceAttachment::new();
let mut network_device = VZVirtioNetworkDeviceConfiguration::new(network_attachment);
network_device.set_mac_address(VZMACAddress::random_locally_administered_address());
let conf = VZVirtualMachineConfigurationBuilder::new()
.boot_loader(boot_loader)
.cpu_count(cpu_count)
.memory_size(memory_size)
.entropy_devices(vec![entropy])
.memory_balloon_devices(vec![memory_balloon])
.network_devices(vec![network_device])
.serial_ports(vec![serial])
.storage_devices(block_devices)
.build();
match conf.validate_with_error() {
Ok(_) => {
let label = std::ffi::CString::new("second").unwrap();
let queue = unsafe { dispatch_queue_create(label.as_ptr(), NIL) };
let vm = Arc::new(RwLock::new(VZVirtualMachine::new(conf, queue)));
let dispatch_block = ConcreteBlock::new(move || {
let completion_handler = ConcreteBlock::new(|err: Id| {
if err != NIL {
let error = unsafe { NSError(StrongPtr::new(err)) };
error.dump();
}
});
let completion_handler = completion_handler.copy();
let completion_handler: &Block<(Id,), ()> = &completion_handler;
vm.write()
.unwrap()
.start_with_completion_handler(completion_handler);
});
let dispatch_block = dispatch_block.copy();
let dispatch_block: &Block<(), ()> = &dispatch_block;
unsafe {
dispatch_async(queue, dispatch_block);
}
loop {
unsafe {
sleep(100);
}
}
}
Err(e) => {
e.dump();
return;
}
}
}