-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaunch.py
executable file
·473 lines (429 loc) · 13.3 KB
/
launch.py
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#!/usr/bin/python
#
# LEGACY SCRIPT -- use run.py instead
#
import sys
import subprocess, os
from subprocess import Popen, call
dnsmasq = None
smbd = None
def hostnet_up():
# NOTE: if we're going to support spawning multiple vms at the
# same time, we'd only need to create br0 and tap0 once. This
# would imply some sort of tracking mechanism. But that felt
# less like simple scripts and more like bloated virt-manager,
# so it's not happening.
# oh btw, the taps need to be promiscuous for samba to work.
global dnsmasq
global smbd
#global nmbd
# setup bridge
call(['sudo', 'brctl','addbr','br0'])
call(['sudo', 'ip', 'addr', 'add', '192.168.101.1/24',
'broadcast', '192.168.101.255', 'dev', 'br0'])
call(['sudo', 'ip', 'link', 'set', 'dev', 'br0', 'address',
'42:48:12:34:56:78'])
call(['sudo', 'ip', 'link', 'set', 'br0', 'up'])
# setup host's adapter
call(['sudo', 'ip', 'tuntap', 'add', 'dev', 'tap0', 'mode', 'tap'])
call(['sudo', 'ip', 'addr', 'add', '192.168.101.2',
'broadcast', '192.168.101.255', 'dev', 'tap0'])
call(['sudo', 'ip', 'link', 'set', 'tap0', 'up', 'promisc', 'on'])
call(['sudo', 'brctl', 'addif', 'br0', 'tap0'])
# setup guest's adapter
call(['sudo', 'ip', 'tuntap', 'add', 'dev', 'tap1', 'mode', 'tap'])
call(['sudo', 'ip', 'link', 'set', 'tap1', 'up', 'promisc', 'on'])
call(['sudo', 'brctl', 'addif', 'br0', 'tap1'])
# fire up the servers
dnsmasq = Popen(['sudo', 'dnsmasq', '-k',
'--interface=br0', '--bind-interfaces',
'--dhcp-range=192.168.101.10,192.168.101.254'])
smbd = Popen(['sudo', 'smbd', '-i', '-s', 'conf/smb.conf', '--piddir=.'])
def hostnet_down():
# kill daemons politely
call(['sudo', 'kill', str(-15), str(dnsmasq.pid)])
call(['sudo', 'kill', str(-15), str(smbd.pid)])
# remove taps from bridge
call(['sudo', 'brctl', 'delif', 'br0', 'tap0'])
call(['sudo', 'brctl', 'delif', 'br0', 'tap1'])
# delete interfaces
call(['sudo', 'ip', 'link', 'delete', 'tap1'])
call(['sudo', 'ip', 'link', 'delete', 'tap0'])
call(['sudo', 'ip', 'link', 'delete', 'br0'])
qemu_parts = {
'emu': {
'enable-kvm': '',
'ctrl-grab': ''
},
'emu-nohead': {
'display': 'none',
'enable-kvm': ''
},
'cpu1': {
'cpu': 'host,hv_relaxed,hv_spinlocks=0x1fff,hv_vapic,hv_time'
},
'cpu2': {
'smp': '4,cores=4'
},
'memory': {
'm': '8G'
},
'mobo35': {
'M': 'q35',
# 'bios': '/usr/share/qemu/sgabios.bin'
},
'mobopc': {
'M': 'pc',
# 'bios': '/usr/share/qemu/sgabios.bin',
},
'moboisa': {
'm': '64M',
'M': 'isapc',
'bios': '/usr/share/qemu/bios.bin'
},
'hostnet': {
'netdev': 'tap,id=hostnet,ifname=tap1,script=no,downscript=no',
'net': 'nic,model=virtio,macaddr=52:54:ea:d6:1b:ae,netdev=hostnet'
},
'usernet1': {
'netdev': 'user,id=usernet',
'net': 'nic,model=e3000,macaddr=52:54:ad:47:98:03,netdev=usernet'
},
'usernet2': {
'netdev': 'user,id=usernet',
'net': 'nic,model=virtio,macaddr=52:54:ad:47:98:03,netdev=usernet'
},
'usb': {
'usb': '',
'usbdevice': [
]
},
'vga1': {
'vga': 'vmware'
},
'vga2': {
'vga': 'none'
},
'vga3': {
'device': [
'ioh3420,bus=pcie.0,addr=1c,multifunction=on,port=1,chassis=1,id=root.1',
'vfio-pci,host=07:00.0,bus=root.1,addr=00.0,multifunction=on,x-vga=on'
]
},
'vgahack': {
'device': 'isa-cirrus-vga'
},
'audio': {
'device': [
'ich9-intel-hda',
'hda-micro'
]
},
'drive': {
'drive': [
]
},
'splash': {
'boot': 'menu=on,splash=splash/boot.bmp,splash-time=5000'
},
'name': {
'name': ''
}
}
qemu_drives = {
'ide1': {
'cdrom': 'ide',
'disks': 'ide',
'drive': [
'if=ide,file=vm/{}/disk.img,media=disk'
]
},
'ide2': {
'cdrom': 'ide',
'disks': 'ide',
'drive': [
'if=ide,file=ide,vm/{}/disk.img,media=disk'
'if=virtio,file=img/temp.qcow2,media=disk'
]
},
'virtio': {
'cdrom': 'scsi',
'disks': 'virtio',
'drive': [
'if=virtio,file=vm/{}/disk.img,media=disk'
]
}
}
# note: the weird format is because we need to keep it in order
qemu_model = [
['archaic', {
'parts': ['emu', 'cpu1', 'cpu2', 'memory', 'moboisa', 'vga1', 'drive',
'splash', 'name' ],
'drives': 'ide1',
'desc': 'ISA-only system with no ethernet',
'purpose': 'To run really ancient systems',
'memory': '16M'
}],
['basic', {
'parts': ['emu', 'cpu1', 'cpu2', 'memory', 'mobopc', 'vga1', 'usernet1',
'usb', 'drive', 'splash', 'name'],
'drives': 'ide1',
'desc': 'PCI-based system with 1G of ram and a PIIX chipset',
'purpose': 'You should install your OS and get your virtio drivers'
'ready for later systems.',
'memory': '2G'
}],
['simple', {
'parts': ['emu', 'cpu1', 'cpu2', 'memory', 'mobopc', 'vga1', 'usernet1',
'usb', 'drive', 'splash', 'name'],
'drives': 'ide2',
'desc': 'As above with a dummy virtio disk',
'purpose': 'Teach your OS about virtio disks.',
'memory': '2G'
}],
['virtio', {
'parts': ['emu', 'cpu1', 'cpu2', 'memory', 'mobopc', 'vga1', 'usernet2',
'hostnet', 'usb', 'drive', 'splash', 'name'],
'drives': 'virtio',
'desc': 'As above with a virtio host-only network and virtio disks',
'purpose': 'Teach your OS about virtio network adapters and '
'install synergy.\n Your shared folder is at \\\\192.168.101.2.',
'pre': hostnet_up,
'post': hostnet_down,
'memory': '2G'
}],
['modern', {
'parts': ['emu', 'cpu1', 'cpu2', 'memory', 'mobo35', 'vga1', 'hostnet',
'usernet2', 'usb', 'audio', 'drive', 'splash', 'name'],
'drives': 'virtio',
'desc': 'q35/virtio system, 8G of ram, host-only networking, and audio',
'purpose': 'Teach your system about the q35 architechture.\n'
' Meanwhile, single-card users drown in remorse.',
'pre': hostnet_up,
'post': hostnet_down,
'memory': '2G'
}],
['complex', {
'parts': ['emu', 'cpu1', 'cpu2', 'memory', 'mobo35', 'vga2', 'hostnet',
'usernet2', 'usb', 'vgahack', 'vga3', 'audio', 'drive',
'splash', 'name'],
'drives': 'virtio',
'desc': 'As above with pcie-passthrough',
'purpose': 'Play some games and blurays from the comfort of X/Wayland\n'
'Test out distros with realish hardware.',
'pre': hostnet_up,
'post': hostnet_down,
'xrandr': True,
'memory': '8G'
}],
['nohead', {
'parts': ['emu-nohead', 'cpu1', 'cpu2', 'memory', 'mobo35', 'vga2',
'hostnet', 'usernet2', 'usb', 'vgahack', 'vga3', 'audio',
'drive', 'splash', 'name'],
'drives': 'virtio',
'desc': 'As above but without a qemu window. You are on your own',
'purpose': 'Enjoy your fully functional guest operating system.',
'pre': hostnet_up,
'post': hostnet_down,
'xrandr': True,
'memory': '8G'
}]
]
qemu_model_drive = {}
def build_opts_single(opts):
out = []
for o in opts:
out.append('-'+o);
return out
def build_opts_multi(opts,fill=''):
out = []
for k,v in opts.iteritems():
if type(v) == str:
if v:
out.append('-{} {}'.format(k,v))
else:
out.append('-{}'.format(k))
else:
for u in v:
out.append('-{} {}'.format(k,u.format(fill)))
return out
def qemu_build(name):
global qemu_model_drive
model = qemu_model_drive['model']
out = []
for part in model['parts']:
out.extend(build_opts_multi(qemu_parts[part]))
return out
def check_for_model(make):
global qemu_model_drive
qemu_model_dict = dict((k,v) for k,v in qemu_model)
if make in qemu_model_dict:
model = qemu_model_dict[make]
qemu_model_drive = qemu_drives[model['drives']]
qemu_model_drive['model'] = model
else:
print 'We don\'t make that build :('
exit(1)
def print_usage():
usage=[
'No build and name specified.',
'',
'Usage: sudo ' + sys.argv[0] + ' VMNAME MODEL [OPTIONS]',
'',
'VMNAME The directory name where the disk.img is',
'',
'MODEL What sort of devices the vm will see. Models available are:'
]
for make,model in qemu_model:
usage.append('\t{}\t {}'.format(make, model['desc']))
usage+=[
' You will step through these systems, typically from basic to modern.',
' (going backwards may harm your system, and is not recommended.)',
' Members of the multi-gpu master race can proceed on to nohead.',
' Linux vms can start at modern or complex.'
'',
'OPTIONS Further options tweaking the layout of the system.',
' cpu:smt Use two threads per core',
' vga:hack Add a dummy isa vga to kickstart some pcie cards',
' m:ram Set the amount of ram given to the OS (e.g. m:32G)',
' cd:img mount image from the _img directory as a cdrom',
' dd:img mount image from the _img directory as a disk',
' cdv:img mount image from the vm directory as a cdrom',
' ddv:img mount image from the vm directory as a disk',
' cdg:img mount image from the filesystem as a cdrom',
' ddg:img mount image from the filesystem as a disk',
' usb:x:y pass usbdevice xxxx:yyyy where available',
' c:cores Set the number of emulated cores',
' barf Your vm barfs at acpi tables',
'',
'Examples:',
'\t' + sys.argv[0] + ' archaic dos',
'\t' + sys.argv[0] + ' virtio winxp barf',
'\t' + sys.argv[0] + ' modern win7 cpu:smt c:2',
'\t' + sys.argv[0] + ' complex win7 vga:hack',
'\t' + sys.argv[0] + ' nohead coinminer',
''
]
print '\n'.join(usage)
## TODO: Every arg in this function should come from a conf file
## BUT we should allow manual overrides from the command line
def process_args():
# check for too few arguments
if len(sys.argv) < 3:
print_usage()
exit(1)
# parse first two options
vm_model = sys.argv[1]
vm_name = sys.argv[2]
check_for_model(vm_model)
# parse extra options if any -- these effect parsing of first
isos = []
imgs = []
usbs = []
vgahack = 0
cores = 4
smt = 0
memory = qemu_model_drive['model']['memory']
for arg in sys.argv[3:]:
head,sep,tail = arg.partition(':')
if tail:
if arg == 'vga:hack':
vgahack = 1
elif arg == 'cpu:fake':
smt = 1
elif head == 'm':
memory = tail
elif head == 'cd':
isos.append('_img/'+tail)
elif head == 'dd':
imgs.append('_img/'+tail)
elif head == 'cdv':
isos.append('{}/{}'.format(vm_name,tail))
elif head == 'ddv':
imgs.append('{}/{}'.format(vm_name,tail))
elif head == 'cdg':
isos.append(tail)
elif head == 'ddg':
imgs.append(tail)
elif head == 'usb':
usbs.append(tail)
elif head == 'c':
cores = int(tail)
elif arg == 'barf':
qemu_parts['emu']['no-acpi'] = ''
if not vgahack:
qemu_parts['vgahack'] = {}
if not smt:
qemu_parts['cpu2']['smp'] = 'cores={}'.format(cores)
else: #inb4 idiots confuse 'hyper'threads with actual cores
qemu_parts['cpu2']['smp'] = 'cores={},threads=2'.format(cores)
qemu_parts['memory']['m'] = memory
# build a list of drives for the vm
drives = qemu_model_drive['drive']
drives[0] = drives[0].format(vm_name)
disk_mode = qemu_model_drive['disks']
cdrom_mode = qemu_model_drive['cdrom']
for img in imgs:
if img:
drives.append('if={},file={},media=disk'.
format(disk_mode,img))
for iso in isos:
if iso:
drives.append('if={},file={},media=cdrom'.
format(cdrom_mode,iso))
# build a list usbdevices for the vm
usbdevices = []
for usb in usbs:
if usb:
usbdevices.append('host:{}'.format(usb))
# add accumulated option detail to the parts list
qemu_parts['drive']['drive'] = drives
qemu_parts['usb']['usbdevice'] = usbdevices
qemu_parts['name']['name'] = vm_name
# return a freshly baked parameter list
return qemu_build(vm_name)
# ====================== #
# == MAIN ENTRY POINT == #
# ====================== #
# TODO:
# - convert cmdline params to guest config
# - better integration with caller script
#def do_launch(host_conf, guest):
print 'LEGACY SCRIPT -- use run.py instead\n'
# check for boot splash -- remove detail entry in parts list if not there
if not os.path.exists('_splash/boot.jpg'):
qemu_parts['boot'] = {}
# build desired model details
qemu_args = process_args()
model = qemu_model_drive['model']
# perform pre scripts
if 'pre' in model:
model['pre']()
# setup environment vars
my_env = os.environ
if 'DISPLAY' not in my_env:
my_env['DISPLAY'] = ':0' # spoof running X in case we're in a tty
my_env['QEMU_AUDIO_DRV'] = 'alsa'
# turn off the display we're using
# TODO: logic for this should come for a conf file
# I've looked at XLib.ext.randr and it seems very verbose to do
# it in python code
if 'xrandr' in model:
Popen(['xrandr', '--output', 'DVI-I-1', '--off'],
env=my_env).wait()
# run the vm
qemu_binary = 'qemu-system-x86_64'
qemu_command = ' '.join([qemu_binary] + qemu_args).split()
print 'Launching:\n\t' + qemu_binary + ' \\\n\t '.join([""] + qemu_args) + '\n'
# believe it or not, the sudo here is important for getting decent sound
vm = Popen(['sudo'] + qemu_command, env=my_env)
print 'FOR WHAT PURPOSE: ' + model['purpose']
vm.wait()
# turn the display back on
# TODO: Restore xrandr state from save state rather than trying
# to fudge it
if 'xrandr' in model:
Popen(['xrandr', '--output', 'DVI-I-1', '--auto', '--left-of', 'DVI-I-2'], env=my_env).wait()
# perform post scripts
if 'post' in model:
model['post']()