-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathguest.py
391 lines (352 loc) · 9.86 KB
/
guest.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
# Authors: Antoine Ginies <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Create some XML section for the XML Guest definition
The template are filed with data from scenario and user
"""
import uuid
from string import Template
import virtscenario.template as template
def create_name(name_data):
"""
name and uuid
"""
xml_template = template.NAME_TEMPLATE
xml_name = {
'VM_name': name_data['VM_name'],
'VM_uuid': str(uuid.uuid4()),
}
xml = Template(xml_template).substitute(xml_name)
return xml
def create_metadata(): #metadata_data):
"""
metadata
"""
xml_template = template.METADATA_TEMPLATE
#xml_metadata = { }
#xml = Template(xml_template).substitute(xml_metadata)
return xml_template
def create_controller(data):
"""
controller
"""
if data['machine'].find('q35') >= 0:
xml_template = template.CONTROLLER_Q35_TEMPLATE
else:
xml_template = template.CONTROLLER_PC_TEMPLATE
return xml_template
def create_memory(memory_data):
"""
memory
"""
xml_template = template.MEMORY_TEMPLATE
xml_mem = {
'mem_unit': memory_data['mem_unit'],
'max_memory': memory_data['max_memory'],
'current_mem_unit': memory_data['current_mem_unit'],
'memory': memory_data['memory'],
}
xml = Template(xml_template).substitute(xml_mem)
if memory_data.get('pin') is True:
memory = int(memory_data['memory'])
if memory_data['mem_unit'] == 'Gib':
memory = memory * 1024
elif memory_data['mem_unit'] == 'Kib':
memory = memory / 1024
memory = memory + 256
memtune_template = template.MEMTUNE_TEMPLATE
xml_memtune = {'pinned': str(memory)}
xml = xml + Template(memtune_template).substitute(xml_memtune)
return xml
def create_memory_backing():
"""
memory backing
https://libvirt.org/formatdomain.html#memory-backing
"""
xml_template = template.MEMORY_BACKING
return xml_template
def create_cpu(cpu_data):
"""
cpu
"""
xml_template = template.CPU_TEMPLATE
xml_cpu = {
'vcpu': cpu_data['vcpu'],
}
xml = Template(xml_template).substitute(xml_cpu)
return xml
def create_osdef(osdef_data):
"""
os
"""
xml_template = template.OS_TEMPLATE
xml_os = {
'arch': osdef_data['arch'],
'machine': osdef_data['machine'],
'boot_dev': osdef_data['boot_dev'],
}
xml = Template(xml_template).substitute(xml_os)
return xml
def create_features(features_data):
"""
features
"""
xml_template = template.FEATURES_TEMPLATE
xml_features = {
'features': features_data['features'],
}
xml = Template(xml_template).substitute(xml_features)
return xml
def create_cpumode_pass(cpumode_data):
"""
cpumode
"""
xml_template = template.CPUMODE_PASS_TEMPLATE
xml_cpumode = {
'migratable': cpumode_data['migratable'],
'extra': cpumode_data['extra'],
}
xml = Template(xml_template).substitute(xml_cpumode)
return xml
def create_clock(clock_data):
"""
clock
"""
xml_template = template.CLOCK_TEMPLATE
xml_clock = {
'clock_offset': clock_data['clock_offset'],
'clock': clock_data['clock'],
}
xml = Template(xml_template).substitute(xml_clock)
return xml
def create_ondef(on_data):
"""
on power etc...
"""
xml_template = template.ON_TEMPLATE
xml_ondef = {
'on_poweroff': on_data['on_poweroff'],
'on_reboot': on_data['on_reboot'],
'on_crash': on_data['on_crash'],
}
xml = Template(xml_template).substitute(xml_ondef)
return xml
def create_power(power_data):
"""
power
"""
xml_template = template.POWER_TEMPLATE
xml_power = {
'suspend_to_mem': power_data['suspend_to_mem'],
'suspend_to_disk': power_data['suspend_to_disk'],
}
xml = Template(xml_template).substitute(xml_power)
return xml
def create_emulator(power_data):
"""
power
"""
xml_template = template.EMULATOR_TEMPLATE
xml_emulator = {
'emulator': power_data['emulator'],
}
xml = Template(xml_template).substitute(xml_emulator)
return xml
def create_xml_disk(disk_data):
"""
disk
"""
xml_template = template.DISK_TEMPLATE
xml_disk = {
'disk_type': disk_data['disk_type'],
'disk_cache': disk_data['disk_cache'],
'disk_target': disk_data['disk_target'],
'disk_bus': disk_data['disk_bus'],
'format': disk_data['format'],
'source_file': disk_data['source_file'],
}
xml = Template(xml_template).substitute(xml_disk)
return xml
def create_interface(interface_data):
"""
interface
"""
xml_template = template.INTERFACE_TEMPLATE
xml_interface = {
'mac_address': interface_data['mac_address'],
'source_network': interface_data['source_network'],
'type': interface_data['type'],
}
xml = Template(xml_template).substitute(xml_interface)
return xml
def create_channel(): #channel_data):
"""
channel
"""
xml_template = template.CHANNEL_TEMPLATE
#xml_channel = { }
#xml = Template(xml_template).substitute(xml_channel)
return xml_template
def create_hugepages():
"""
hugepages
"""
xml_template = template.HUGEPAGES_TEMPLATE
return xml_template
def create_console(): #console_data):
"""
console
"""
xml_template = template.CONSOLE_TEMPLATE
#xml_console = { }
#xml = Template(xml_template).substitute(xml_console)
return xml_template
def create_input(input_data):
"""
input
"""
xml_template = template.INPUT_TEMPLATE
xml_input = {
'type': input_data['type'],
'bus': input_data['bus'],
}
xml = Template(xml_template).substitute(xml_input)
return xml
def create_graphics(): #graphics_data):
"""
graphics
"""
xml_template = template.GRAPHICS_TEMPLATE
#xml_graphics = { }
#xml = Template(xml_template).substitute(xml_graphics)
return xml_template
def create_audio(audio_data):
"""
audio
"""
xml_template = template.AUDIO_TEMPLATE
xml_audio = {
'model': audio_data['model'],
}
xml = Template(xml_template).substitute(xml_audio)
return xml
def create_usb(usb_data):
"""
audio
"""
xml_template = template.USB_TEMPLATE
xml_usb = {
'model': usb_data['model'],
}
xml = Template(xml_template).substitute(xml_usb)
return xml
def create_cdrom(cdrom_data):
"""
cdrom
"""
xml_template = template.CDROM_TEMPLATE
xml_cdrom = {
'source_file': cdrom_data['source_file'],
}
xml = Template(xml_template).substitute(xml_cdrom)
return xml
def create_video(video_data):
"""
video
"""
if video_data['type'] != "virtio":
xml_template = template.VIDEO_TEMPLATE
xml_video = {
'type': video_data['type'],
}
xml = Template(xml_template).substitute(xml_video)
else:
xml = template.VIDEO_VIRTIO_TEMPLATE
return xml
def create_watchdog(watchdog_data):
"""
watchdog
"""
xml_template = template.WATCHDOG_TEMPLATE
xml_watchdog = {
'model': watchdog_data['model'],
'action': watchdog_data['action'],
}
xml = Template(xml_template).substitute(xml_watchdog)
return xml
def create_memballoon(): #memballoon_data):
"""
memballoon
"""
xml_template = template.MEMBALLOON_TEMPLATE
#xml_memballoon = { }
#xml = Template(xml_template).substitute(xml_memballoon)
return xml_template
def create_rng(): #rng_data):
"""
rng
"""
xml_template = template.RNG_TEMPLATE
#xml_rng = { }
#xml = Template(xml_template).substitute(xml_rng)
return xml_template
def create_tpm(tpm_data):
"""
tpm
"""
xml_tpm = {
'tpm_model': tpm_data['tpm_model'],
'tpm_type': tpm_data['tpm_type'],
}
if tpm_data['tpm_type'] == "emulator":
xml_template = template.TPM_TEMPLATE_EMULATED
xml_tpm['version'] = tpm_data['version']
else:
xml_template = template.TPM_TEMPLATE
xml_tpm['device_path'] = tpm_data['device_path']
xml = Template(xml_template).substitute(xml_tpm)
return xml
def create_iothreads(iothreads_data):
"""
iothreads
"""
xml_template = template.IOTHREADS_TEMPLATE
xml_iothreads = {
'iothreads': iothreads_data['iothreads'],
}
xml = Template(xml_template).substitute(xml_iothreads)
return xml
def create_security(security_data):
"""
security
"""
xml_template = template.SECURITY_TEMPLATE
xml_security = {
'sectype': security_data['sectype'],
'secdata': security_data['secdata'],
}
xml = Template(xml_template).substitute(xml_security)
return xml
def create_host_filesystem(host_filesystem_data):
"""
host filesystem sharing
"""
xml_template = template.HOST_FILESYSTEM_TEMPLATE
xml_host_filesystem = {
'fmode': host_filesystem_data['fmode'],
'dmode': host_filesystem_data['dmode'],
'source_dir': host_filesystem_data['source_dir'],
'target_dir': host_filesystem_data['target_dir'],
}
xml = Template(xml_template).substitute(xml_host_filesystem)
return xml