-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample
executable file
·206 lines (170 loc) · 5.17 KB
/
example
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
#!/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 os
import signal
import sys
import vmnet
CONNECTION_AUTO = {
"vfkit": "fd",
"krunkit": "socket",
"qemu": "fd",
}
VMNET_OFFLOAD_AUTO = {
"vfkit": "off",
# libkrun enables offloading feautres by default, so we must enable vmnet
# offloading. See https://github.com/containers/libkrun/issues/264
"krunkit": "on",
"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=["auto", "fd", "socket"],
default="auto",
help="How to connect the helper and vm (auto)",
)
# Helper arguments
p.add_argument(
"--operation-mode",
choices=vmnet.OPERATION_MODES,
default=vmnet.OPERATION_MODES[0],
help=f"vmnet operation mode ({vmnet.OPERATION_MODES[0]})",
)
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(
"--vmnet-offload",
choices=["auto", "on", "off"],
default="auto",
help="Enable vmnet tso and checksum-offload options (auto)",
)
# 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(
"--krunkit-port",
metavar="PORT",
type=int,
default=8081,
help="krunkit restful-uri port (8081)",
)
p.add_argument(
"--cpus",
metavar="N",
type=cpus,
default=1,
help="Number of vpus (1)",
)
p.add_argument(
"--memory",
metavar="M",
type=int,
default=1024,
help="Memory size in MiB (1024)",
)
distros = list(vmnet.IMAGES.keys())
p.add_argument(
"--distro",
metavar="NAME",
choices=distros,
default=distros[0],
help=f"Linux distro ({distros[0]})",
)
# Debugging
p.add_argument("-v", "--verbose", action="store_true", help="Be more verbose")
args = p.parse_args()
if args.connection == "auto":
args.connection = CONNECTION_AUTO[args.driver]
if args.operation_mode == "bridged" and not args.shared_interface:
p.error("--shared-interface required for --operation-mode=bridged")
if args.enable_isolation and args.operation_mode != "host":
p.error("--enable-isolation requires --operation-mode=host")
if args.vmnet_offload == "auto":
args.vmnet_offload = VMNET_OFFLOAD_AUTO[args.driver]
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)
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_mac_address"], fd=vm_sock.fileno())
vm.start()
try:
vm.wait_for_ip_address()
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_mac_address"], socket=socket)
vm.start()
try:
vm.wait_for_ip_address()
os.wait()
finally:
vm.stop()
finally:
helper.stop()
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()