-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathexample
More file actions
executable file
·272 lines (230 loc) · 7.28 KB
/
example
File metadata and controls
executable file
·272 lines (230 loc) · 7.28 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/usr/bin/env python3 -u
# SPDX-FileCopyrightText: The vmnet-helper authors
# SPDX-License-Identifier: Apache-2.0
"""
Run a vm with vmnet-helper.
"""
import argparse
import logging
import os
import signal
import sys
import vmnet
VMNET_OFFLOAD_AUTO = {
"vfkit": "off",
"krunkit": "off",
"qemu": "off",
}
def main():
shared_interfaces = vmnet.shared_interfaces()
p = argparse.ArgumentParser(description="Run virtual machine using vmnet-helper")
# Main arguments
p.add_argument("vm_name", help="VM name")
p.add_argument(
"--connection",
choices=["fd", "socket", "runner"],
default="fd",
help="How to connect the helper and vm (fd)",
)
# Helper arguments
p.add_argument(
"--operation-mode",
choices=vmnet.OPERATION_MODES,
help="vmnet operation mode",
)
p.add_argument(
"--start-address",
help=(
"The starting IPv4 address (string) to use for the interface. This "
"address is used as the gateway address. The subsequent address up "
"to and including --end-address_key are placed in the DHCP pool. All "
"other addresses are available for static assignment. "
"The address must be in the private IP range (RFC 1918)."
),
)
p.add_argument(
"--end-address",
help=(
"The DHCP IPv4 range end address (string) to use for the interface. "
"The address must be in the private IP range (RFC 1918)."
),
)
p.add_argument(
"--subnet-mask", help="The IPv4 subnet mask (string) to use on the interface."
)
p.add_argument(
"--shared-interface",
choices=shared_interfaces,
default=shared_interfaces[0],
help=f"vmnet shared interface for --operation-mode=bridged ({shared_interfaces[0]})",
)
p.add_argument(
"--enable-isolation",
action="store_true",
help="Isolate the guest from other guests on the vmnet interface, requires --operation-mode=host",
)
p.add_argument(
"--enable-offloading",
action="store_true",
help="Enable vmnet tso and checksum-offload options (krunkit driver only)",
)
p.add_argument(
"--network",
dest="network_name",
metavar="NAME",
help="Join a network managed by vmnet-broker (macOS 26 only)",
)
# VM arguments
p.add_argument(
"--driver",
metavar="NAME",
choices=vmnet.DRIVERS,
default=vmnet.DRIVERS[0],
help=f"VM driver ({vmnet.DRIVERS[0]})",
)
p.add_argument(
"--driver-command",
metavar="COMMAND",
help=f"Use custom driver command",
)
p.add_argument(
"--cpus",
metavar="N",
type=cpus,
default=2,
help="Number of vpus (2)",
)
p.add_argument(
"--memory",
metavar="M",
type=int,
default=1024,
help="Memory size in MiB (1024)",
)
p.add_argument(
"--timeout",
metavar="SECONDS",
type=int,
default=60,
help="Time to wait for VM IP address (60 seconds)",
)
distros = list(vmnet.IMAGES.keys())
p.add_argument(
"--distro",
metavar="NAME",
choices=distros,
default=distros[0],
help=f"Linux distro ({distros[0]})",
)
p.add_argument(
"--dns-servers",
metavar="ADDR",
type=ip_list,
default=["8.8.8.8", "1.1.1.1"],
help="Comma-separated DNS servers for the VM (8.8.8.8,1.1.1.1)",
)
# Debugging
p.add_argument("-v", "--verbose", action="store_true", help="Be more verbose")
args = p.parse_args()
setup_logging(args.verbose)
if args.network_name:
if args.operation_mode:
p.error("--network cannot be used with --operation-mode")
if args.start_address:
p.error("--network cannot be used with --start-address")
if args.end_address:
p.error("--network cannot be used with --end-address")
if args.subnet_mask:
p.error("--network cannot be used with --subnet-mask")
if args.operation_mode == "bridged":
if not args.shared_interface:
p.error("--shared-interface required for --operation-mode=bridged")
if args.enable_isolation:
p.error("--enable-isolation not compatible with --operation-mode=bridged")
signal.signal(signal.SIGTERM, terminate)
signal.signal(signal.SIGINT, terminate)
if args.connection == "fd":
run_with_fd(args)
elif args.connection == "socket":
run_with_socket(args)
elif args.connection == "runner":
run_with_runner(args)
class SecondsFormatter(logging.Formatter):
def format(self, record):
record.seconds = record.relativeCreated / 1000.0
return super().format(record)
def setup_logging(verbose=False):
formatter = SecondsFormatter("[%(seconds)8.3f] %(levelname)s %(message)s")
handler = logging.StreamHandler(stream=sys.stdout)
handler.setFormatter(formatter)
level = logging.DEBUG if verbose else logging.INFO
logging.basicConfig(handlers=[handler], level=level)
def run_with_fd(args):
"""
Use file descriptor passing to connect the vm and the helper.
"""
# Create a socketpair for the child processes. The vm_sock will be used by
# the vm, and the host_sock will be used by the helper.
vm_sock, helper_sock = vmnet.socketpair()
# Start vmnet-helper first, since we need the MAC address before starting
# the VM.
helper = vmnet.Helper(args, fd=helper_sock.fileno())
helper.start()
try:
# Start the VM with the MAC address and the second socket.
vm = vmnet.VM(
args, helper.interface[vmnet.VMNET_MAC_ADDRESS], fd=vm_sock.fileno()
)
vm.start()
try:
vm.wait_until_ready(timeout=args.timeout)
os.wait()
finally:
vm.stop()
finally:
helper.stop()
def run_with_socket(args):
"""
Use unix socket to connect the vm and the helper.
"""
# The helper will create a unix datagram socket at this path and wait for
# client connection.
socket = vmnet.vm_path(args.vm_name, "vmnet.sock")
# Start vmnet-helper first, since we need the MAC address before starting
# the VM.
helper = vmnet.Helper(args, socket=socket)
helper.start()
try:
# Start the VM with the MAC address and the socket path.
vm = vmnet.VM(args, helper.interface[vmnet.VMNET_MAC_ADDRESS], socket=socket)
vm.start()
try:
vm.wait_until_ready(timeout=args.timeout)
os.wait()
finally:
vm.stop()
finally:
helper.stop()
def run_with_runner(args):
"""
Use vmnet-run to run a vm with fd.
"""
vm = vmnet.VM(args, vmnet.mac_address_from(args.vm_name), fd=4, runner=vmnet.RUNNER)
vm.start()
try:
vm.wait_until_ready(timeout=args.timeout)
os.wait()
finally:
vm.stop()
def ip_list(s):
# TODO: validate that values are IP addresses.
return s.split(",")
def cpus(s):
n = int(s)
if n < 1:
raise ValueError(f"Invalid number of cpus: '{s}'")
return n
def terminate(signo, frame):
sys.exit(1)
if __name__ == "__main__":
main()