ia64/xen-unstable

view tools/python/xen/xm/xenapi_create.py @ 17641:9da75b150015

XM/XenAPI: Add cdrom support when creating domains by xm through Xen API.

Signed-off-by: Yosuke Iwamatsu <y-iwamatsu@ab.jp.nec.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue May 13 09:48:55 2008 +0100 (2008-05-13)
parents 900228550847
children 0be5d3510305
line source
1 #!/usr/bin/python
2 #============================================================================
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of version 2.1 of the GNU Lesser General Public
5 # License as published by the Free Software Foundation.
6 #
7 # This library is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 # Lesser General Public License for more details.
11 #
12 # You should have received a copy of the GNU Lesser General Public
13 # License along with this library; if not, write to the Free Software
14 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 #============================================================================
16 # Copyright (C) 2007 Tom Wilkie <tom.wilkie@gmail.com>
17 #============================================================================
18 """Domain creation using new XenAPI
19 """
21 from xen.xm.main import server, get_default_SR
22 from xml.dom.minidom import parse, getDOMImplementation
23 from xml.parsers.xmlproc import xmlproc, xmlval, xmldtd
24 from xen.xend import sxp
25 from xen.xend.XendAPIConstants import XEN_API_ON_NORMAL_EXIT, \
26 XEN_API_ON_CRASH_BEHAVIOUR
27 from xen.xm.opts import OptionError
28 from xen.util import xsconstants
29 import xen.util.xsm.xsm as security
31 import sys
32 import os
33 import traceback
34 import re
36 def log(_, msg):
37 #print "> " + msg
38 pass
40 DEBUG = 0
42 def get_name_label(node):
43 name_node = node.getElementsByTagName("name")[0]
44 label_node = name_node.getElementsByTagName("label")[0]
45 return " ".join([child.nodeValue for child in label_node.childNodes])
47 def get_name_description(node):
48 name_node = node.getElementsByTagName("name")[0]
49 description_node = name_node.getElementsByTagName("description")[0]
50 return " ".join([child.nodeValue for child in description_node.childNodes])
52 def get_text_in_child_node(node, child):
53 tag_node = node.getElementsByTagName(child)[0]
54 return " ".join([child.nodeValue for child in tag_node.childNodes])
56 def get_child_node_attribute(node, child, attribute):
57 tag_node = node.getElementsByTagName(child)[0]
58 return tag_node.attributes[attribute].value
60 def get_child_nodes_as_dict(node, child_name,
61 key_attribute_name,
62 value_attribute_name):
63 return dict([(child.attributes[key_attribute_name].value,
64 child.attributes[value_attribute_name].value)
65 for child in node.getElementsByTagName(child_name)])
67 def try_quietly(fn, *args):
68 try:
69 return fn(*args)
70 except:
71 return None
73 class xenapi_create:
75 def __init__(self):
76 self.DEFAULT_STORAGE_REPOSITORY = get_default_SR()
78 self.dtd = "/usr/share/xen/create.dtd"
80 def create(self, filename=None, document=None, skipdtd=False):
81 """
82 Create a domain from an XML file or DOM tree
83 """
84 if skipdtd:
85 print "Skipping DTD checks. Dangerous!"
87 if filename is not None:
88 if not skipdtd:
89 self.check_dtd(filename)
90 document = parse(filename)
91 elif document is not None:
92 if not skipdtd:
93 self.check_dom_against_dtd(document)
95 self.check_doc(document)
97 vdis = document.getElementsByTagName("vdi")
98 vdi_refs_dict = self.create_vdis(vdis)
100 networks = document.getElementsByTagName("network")
101 network_refs_dict = self.create_networks(networks)
103 try:
104 vms = document.getElementsByTagName("vm")
105 return self.create_vms(vms, vdi_refs_dict, network_refs_dict)
106 except Exception, exn:
107 try_quietly(self.cleanup_vdis(vdi_refs_dict))
108 raise exn
110 # Methods to check xml file
111 # try to use dtd to check where possible
112 def check_dtd(self, file):
113 """
114 Check file against DTD.
115 Use this if possible as it gives nice
116 error messages
117 """
118 dtd = xmldtd.load_dtd(self.dtd)
119 parser = xmlproc.XMLProcessor()
120 parser.set_application(xmlval.ValidatingApp(dtd, parser))
121 parser.dtd = dtd
122 parser.ent = dtd
123 parser.parse_resource(file)
125 def check_dom_against_dtd(self, dom):
126 """
127 Check DOM again DTD.
128 Doesn't give as nice error messages.
129 (no location info)
130 """
131 dtd = xmldtd.load_dtd(self.dtd)
132 app = xmlval.ValidatingApp(dtd, self)
133 app.set_locator(self)
134 self.dom2sax(dom, app)
136 # Get errors back from ValidatingApp
137 def report_error(self, number, args=None):
138 self.errors = xmlproc.errors.english
139 try:
140 msg = self.errors[number]
141 if args != None:
142 msg = msg % args
143 except KeyError:
144 msg = self.errors[4002] % number # Unknown err msg :-)
145 print msg
146 sys.exit(-1)
148 # Here for compatibility with ValidatingApp
149 def get_line(self):
150 return -1
152 def get_column(self):
153 return -1
155 def dom2sax(self, dom, app):
156 """
157 Take a dom tree and tarverse it,
158 issuing SAX calls to app.
159 """
160 for child in dom.childNodes:
161 if child.nodeType == child.TEXT_NODE:
162 data = child.nodeValue
163 app.handle_data(data, 0, len(data))
164 else:
165 app.handle_start_tag(
166 child.nodeName,
167 self.attrs_to_dict(child.attributes))
168 self.dom2sax(child, app)
169 app.handle_end_tag(child.nodeName)
171 def attrs_to_dict(self, attrs):
172 return dict(attrs.items())
174 #
175 # Checks which cannot be done with dtd
176 #
177 def check_doc(self, doc):
178 vms = doc.getElementsByTagName("vm")
179 self.check_vms(vms)
181 def check_vms(self, vms):
182 map(self.check_vm, vms)
184 def check_vm(self, vm):
185 vifs = vm.getElementsByTagName("vif")
186 self.check_vifs(vifs)
188 def check_vifs(self, vifs):
189 map(self.check_vif, vifs)
191 def check_vif(self, vif):
192 pass
194 # Cleanup methods here
195 def cleanup_vdis(self, vdi_refs_dict):
196 map(self.cleanup_vdi, vdi_refs_dict.values())
198 def cleanup_vdi(self, vdi_ref):
199 server.xenapi.VDI.destroy(vdi_ref)
201 def cleanup_vms(self, vm_refs):
202 map(self.cleanup_vm, vm_refs)
204 def cleanup_vm(self, vm_ref):
205 server.xenapi.VM.destroy(vm_ref)
207 # Create methods here
208 def create_vdis(self, vdis):
209 log(DEBUG, "create_vdis")
210 return dict(map(self.create_vdi, vdis))
212 def create_vdi(self, vdi):
213 log(DEBUG, "create_vdi")
215 vdi_record = {
216 "name_label": get_name_label(vdi),
217 "name_description": get_name_description(vdi),
218 "SR": self.DEFAULT_STORAGE_REPOSITORY,
219 "virtual_size": vdi.attributes["size"].value,
220 "type": vdi.attributes["type"].value,
221 "sharable": bool(vdi.attributes["sharable"].value),
222 "read_only": bool(vdi.attributes["read_only"].value),
223 "other_config": {"location":
224 vdi.attributes["src"].value}
225 }
227 key = vdi.attributes["name"].value
228 value = server.xenapi.VDI.create(vdi_record)
230 return (key, value)
232 def create_networks(self, networks):
233 log(DEBUG, "create_networks")
234 return dict(map(self.create_network, networks))
236 def create_network(self, network):
237 log(DEBUG, "create_network")
239 network_record = {
240 "name_label": get_name_label(network),
241 "name_description": get_name_description(network),
242 "other_config":
243 get_child_nodes_as_dict(network, "other_config",
244 "key", "value"),
245 "default_netmask": network.attributes["default_netmask"].value,
246 "default_gateway": network.attributes["default_gateway"].value
247 }
249 key = network.attributes["name"].value
250 value = server.xenapi.network.create(network_record)
252 return (key, value)
254 def create_vms(self, vms, vdis, networks):
255 log(DEBUG, "create_vms")
256 return map(lambda vm: self.create_vm(vm, vdis, networks), vms)
258 def create_vm(self, vm, vdis, networks):
259 log(DEBUG, "create_vm")
261 vm_record = {
262 "name_label":
263 get_name_label(vm),
264 "name_description":
265 get_name_description(vm),
266 "user_version":
267 get_text_in_child_node(vm, "version"),
268 "is_a_template":
269 vm.attributes["is_a_template"].value == 'true',
270 "auto_power_on":
271 vm.attributes["auto_power_on"].value == 'true',
272 "memory_static_max":
273 get_child_node_attribute(vm, "memory", "static_max"),
274 "memory_static_min":
275 get_child_node_attribute(vm, "memory", "static_min"),
276 "memory_dynamic_max":
277 get_child_node_attribute(vm, "memory", "dynamic_max"),
278 "memory_dynamic_min":
279 get_child_node_attribute(vm, "memory", "dynamic_min"),
280 "VCPUs_params":
281 get_child_nodes_as_dict(vm, "vcpu_param", "key", "value"),
282 "VCPUs_max":
283 vm.attributes["vcpus_max"].value,
284 "VCPUs_at_startup":
285 vm.attributes["vcpus_at_startup"].value,
286 "actions_after_shutdown":
287 vm.attributes["actions_after_shutdown"].value,
288 "actions_after_reboot":
289 vm.attributes["actions_after_reboot"].value,
290 "actions_after_crash":
291 vm.attributes["actions_after_crash"].value,
292 "platform":
293 get_child_nodes_as_dict(vm, "platform", "key", "value"),
294 "other_config":
295 get_child_nodes_as_dict(vm, "other_config", "key", "value"),
296 "PV_bootloader":
297 "",
298 "PV_kernel":
299 "",
300 "PV_ramdisk":
301 "",
302 "PV_args":
303 "",
304 "PV_bootloader_args":
305 "",
306 "HVM_boot_policy":
307 "",
308 "HVM_boot_params":
309 {},
310 "PCI_bus":
311 ""
312 }
314 if vm.attributes.has_key("security_label"):
315 vm_record.update({
316 "security_label":
317 vm.attributes["security_label"].value
318 })
320 if len(vm.getElementsByTagName("pv")) > 0:
321 vm_record.update({
322 "PV_bootloader":
323 get_child_node_attribute(vm, "pv", "bootloader"),
324 "PV_kernel":
325 get_child_node_attribute(vm, "pv", "kernel"),
326 "PV_ramdisk":
327 get_child_node_attribute(vm, "pv", "ramdisk"),
328 "PV_args":
329 get_child_node_attribute(vm, "pv", "args"),
330 "PV_bootloader_args":
331 get_child_node_attribute(vm, "pv", "bootloader_args")
332 })
333 else:
334 hvm = vm.getElementsByTagName("hvm")[0]
335 vm_record.update({
336 "HVM_boot_policy":
337 get_child_node_attribute(vm, "hvm", "boot_policy"),
338 "HVM_boot_params":
339 get_child_nodes_as_dict(hvm, "boot_param", "key", "value")
340 })
341 try:
342 vm_ref = server.xenapi.VM.create(vm_record)
343 except:
344 traceback.print_exc()
345 sys.exit(-1)
347 try:
348 # Now create vbds
350 vbds = vm.getElementsByTagName("vbd")
352 self.create_vbds(vm_ref, vbds, vdis)
354 # Now create vifs
356 vifs = vm.getElementsByTagName("vif")
358 self.create_vifs(vm_ref, vifs, networks)
360 # Now create vtpms
362 vtpms = vm.getElementsByTagName("vtpm")
364 self.create_vtpms(vm_ref, vtpms)
366 # Now create consoles
368 consoles = vm.getElementsByTagName("console")
370 self.create_consoles(vm_ref, consoles)
372 return vm_ref
373 except:
374 server.xenapi.VM.destroy(vm_ref)
375 raise
377 def create_vbds(self, vm_ref, vbds, vdis):
378 log(DEBUG, "create_vbds")
379 return map(lambda vbd: self.create_vbd(vm_ref, vbd, vdis), vbds)
381 def create_vbd(self, vm_ref, vbd, vdis):
382 log(DEBUG, "create_vbd")
384 vbd_record = {
385 "VM":
386 vm_ref,
387 "VDI":
388 vdis[vbd.attributes["vdi"].value],
389 "device":
390 vbd.attributes["device"].value,
391 "bootable":
392 vbd.attributes["bootable"].value == "True",
393 "mode":
394 vbd.attributes["mode"].value,
395 "type":
396 vbd.attributes["type"].value,
397 "qos_algorithm_type":
398 vbd.attributes["qos_algorithm_type"].value,
399 "qos_algorithm_params":
400 get_child_nodes_as_dict(vbd,
401 "qos_algorithm_param", "key", "value")
402 }
404 return server.xenapi.VBD.create(vbd_record)
406 def create_vifs(self, vm_ref, vifs, networks):
407 log(DEBUG, "create_vifs")
408 return map(lambda vif: self.create_vif(vm_ref, vif, networks), vifs)
410 def create_vif(self, vm_ref, vif, networks):
411 log(DEBUG, "create_vif")
413 if 'network' in vif.attributes.keys():
414 network_name = vif.attributes['network'].value
416 if network_name in networks.keys():
417 network_uuid = networks[network_name]
418 else:
419 networks = dict([(record['name_label'], ref)
420 for ref, record in
421 server.xenapi.network.get_all_records().items()])
422 if network_name in networks.keys():
423 network_uuid = networks[network_name]
424 else:
425 raise OptionError("Network %s doesn't exist"
426 % vif.attributes["network"].value)
427 else:
428 network_uuid = self._get_network_ref()
430 vif_record = {
431 "device":
432 vif.attributes["device"].value,
433 "network":
434 network_uuid,
435 "VM":
436 vm_ref,
437 "MAC":
438 vif.attributes["mac"].value,
439 "MTU":
440 vif.attributes["mtu"].value,
441 "qos_algorithm_type":
442 vif.attributes["qos_algorithm_type"].value,
443 "qos_algorithm_params":
444 get_child_nodes_as_dict(vif,
445 "qos_algorithm_param", "key", "value"),
446 "security_label":
447 vif.attributes["security_label"].value
448 }
450 return server.xenapi.VIF.create(vif_record)
452 _network_refs = []
454 def _get_network_ref(self):
455 try:
456 return self._network_refs.pop(0)
457 except IndexError:
458 self._network_refs = server.xenapi.network.get_all()
459 return self._network_refs.pop(0)
461 def create_vtpms(self, vm_ref, vtpms):
462 if len(vtpms) > 1:
463 vtpms = [ vtpms[0] ]
464 log(DEBUG, "create_vtpms")
465 return map(lambda vtpm: self.create_vtpm(vm_ref, vtpm), vtpms)
467 def create_vtpm(self, vm_ref, vtpm):
468 vtpm_record = {
469 "VM":
470 vm_ref,
471 "backend":
472 vtpm.attributes["backend"].value
473 }
474 return server.xenapi.VTPM.create(vtpm_record)
476 def create_consoles(self, vm_ref, consoles):
477 log(DEBUG, "create_consoles")
478 return map(lambda console: self.create_console(vm_ref, console),
479 consoles)
481 def create_console(self, vm_ref, console):
482 log(DEBUG, "create_consoles")
484 console_record = {
485 "VM":
486 vm_ref,
487 "protocol":
488 console.attributes["protocol"].value,
489 "other_config":
490 get_child_nodes_as_dict(console,
491 "other_config", "key", "value")
492 }
494 return server.xenapi.console.create(console_record)
496 def get_child_by_name(exp, childname, default = None):
497 try:
498 return [child for child in sxp.children(exp)
499 if child[0] == childname][0][1]
500 except:
501 return default
503 # Convert old sxp into new xml
505 class sxp2xml:
507 def convert_sxp_to_xml(self, config, transient=False):
509 devices = [child for child in sxp.children(config)
510 if len(child) > 0 and child[0] == "device"]
512 vbds_sxp = map(lambda x: x[1], [device for device in devices
513 if device[1][0] in ("vbd", "tap")])
515 vifs_sxp = map(lambda x: x[1], [device for device in devices
516 if device[1][0] == "vif"])
518 vtpms_sxp = map(lambda x: x[1], [device for device in devices
519 if device[1][0] == "vtpm"])
521 # Create XML Document
523 impl = getDOMImplementation()
525 document = impl.createDocument(None, "xm", None)
527 # Lets make the VM tag..
529 vm = document.createElement("vm")
531 # Some string compatibility
533 actions_after_shutdown \
534 = get_child_by_name(config, "on_poweroff", "destroy")
535 actions_after_reboot \
536 = get_child_by_name(config, "on_reboot", "restart")
537 actions_after_crash \
538 = get_child_by_name(config, "on_crash", "restart")
540 def conv_chk(val, vals):
541 val.replace("-", "_")
542 if val not in vals:
543 raise "Invalid value: " + val
544 else:
545 return val
547 actions_after_shutdown = conv_chk(actions_after_shutdown,\
548 XEN_API_ON_NORMAL_EXIT)
549 actions_after_reboot = conv_chk(actions_after_reboot, \
550 XEN_API_ON_NORMAL_EXIT)
551 actions_after_crash = conv_chk(actions_after_crash, \
552 XEN_API_ON_CRASH_BEHAVIOUR)
553 # Flesh out tag attributes
555 vm.attributes["is_a_template"] = "false"
556 vm.attributes["auto_power_on"] = "false"
557 vm.attributes["actions_after_shutdown"] \
558 = actions_after_shutdown
559 vm.attributes["actions_after_reboot"] \
560 = actions_after_reboot
561 vm.attributes["actions_after_crash"] \
562 = actions_after_crash
563 vm.attributes["PCI_bus"] = ""
565 vm.attributes["vcpus_max"] \
566 = str(get_child_by_name(config, "vcpus", 1))
567 vm.attributes["vcpus_at_startup"] \
568 = str(get_child_by_name(config, "vcpus", 1))
570 sec_data = get_child_by_name(config, "security")
571 if sec_data:
572 try :
573 vm.attributes['security_label'] = \
574 security.set_security_label(sec_data[0][1][1],sec_data[0][2][1])
575 except Exception, e:
576 raise "Invalid security data format: %s" % str(sec_data)
578 # Make the name tag
580 vm.appendChild(self.make_name_tag(
581 get_child_by_name(config, "name"), document))
583 # Make version tag
585 version = document.createElement("version")
586 version.appendChild(document.createTextNode("0"))
587 vm.appendChild(version)
589 # Make pv or hvm tag
591 image = get_child_by_name(config, "image")
593 if image[0] == "linux":
594 pv = document.createElement("pv")
595 pv.attributes["kernel"] \
596 = get_child_by_name(image, "kernel", "")
597 pv.attributes["bootloader"] = ""
598 pv.attributes["ramdisk"] \
599 = get_child_by_name(image, "ramdisk", "")
600 pv.attributes["args"] \
601 = "root=" + get_child_by_name(image, "root", "") \
602 + " " + get_child_by_name(image, "args", "")
603 pv.attributes["bootloader_args"] = ""
605 vm.appendChild(pv)
606 elif image[0] == "hvm":
607 hvm = document.createElement("hvm")
608 hvm.attributes["boot_policy"] = "BIOS order"
610 boot_order = document.createElement("boot_param")
611 boot_order.attributes["key"] = "order"
612 boot_order.attributes["value"] \
613 = get_child_by_name(image, "boot", "abcd")
614 hvm.appendChild
616 vm.appendChild(hvm)
618 # Make memory tag
620 memory = document.createElement("memory")
622 memory_str = str(int(
623 get_child_by_name(config, "memory"))*1024*1024)
625 memory.attributes["static_min"] = str(0)
626 memory.attributes["static_max"] = memory_str
627 memory.attributes["dynamic_min"] = memory_str
628 memory.attributes["dynamic_max"] = memory_str
630 if get_child_by_name(config, "maxmem"):
631 memory.attributes["static_max"] = \
632 str(int(get_child_by_name(config, "maxmem")*1024*1024))
634 vm.appendChild(memory)
636 # And now the vbds
638 vbds = map(lambda vbd: self.extract_vbd(vbd, document), vbds_sxp)
640 map(vm.appendChild, vbds)
642 # And now the vifs
644 vifs = map(lambda vif: self.extract_vif(vif, document), vifs_sxp)
646 map(vm.appendChild, vifs)
648 # And now the vTPMs
650 vtpms = map(lambda vtpm: self.extract_vtpm(vtpm, document), vtpms_sxp)
652 map(vm.appendChild, vtpms)
654 # Last but not least the consoles...
656 consoles = self.extract_consoles(image, document)
658 map(vm.appendChild, consoles)
660 # Platform variables...
662 platform = self.extract_platform(image, document)
664 map(vm.appendChild, platform)
666 # transient?
668 if transient:
669 other_config = document.createElement("other_config")
670 other_config.attributes["key"] = "transient"
671 other_config.attributes["value"] = "True"
672 vm.appendChild(other_config)
674 # Add it to doc_root
676 document.documentElement.appendChild(vm)
678 # We want to pull out vdis
680 vdis = map(lambda vdb: self.extract_vdi(vdb, document), vbds_sxp)
682 map(document.documentElement.appendChild, vdis)
684 return document
686 def make_name_tag(self, label_text, document):
687 name = document.createElement("name")
689 label = document.createElement("label")
690 label.appendChild(document.createTextNode(str(label_text)))
691 name.appendChild(label)
693 description = document.createElement("description")
694 description.appendChild(document.createTextNode(" "))
695 name.appendChild(description)
697 return name
699 def extract_vbd(self, vbd_sxp, document):
700 src = get_child_by_name(vbd_sxp, "uname")
701 name = str(src.__hash__())
703 vbd = document.createElement("vbd")
705 vbd.attributes["name"] = "vdb" + name
706 vbd.attributes["vdi"] = "vdi" + name
707 vbd.attributes["mode"] \
708 = get_child_by_name(vbd_sxp, "mode") != "w" \
709 and "RO" or "RW"
710 vbd.attributes["device"] \
711 = re.sub(":cdrom$", "", get_child_by_name(vbd_sxp, "dev"))
712 vbd.attributes["bootable"] = "1"
713 vbd.attributes["type"] \
714 = re.search(":cdrom$", get_child_by_name(vbd_sxp, "dev")) \
715 and "CD" or "disk"
716 vbd.attributes["qos_algorithm_type"] = ""
718 return vbd
720 def extract_vdi(self, vbd_sxp, document):
721 src = get_child_by_name(vbd_sxp, "uname")
722 name = "vdi" + str(src.__hash__())
724 vdi = document.createElement("vdi")
726 vdi.attributes["src"] = src
727 vdi.attributes["read_only"] \
728 = (get_child_by_name(vbd_sxp, "mode") != "w") \
729 and "True" or "False"
730 vdi.attributes["size"] = '-1'
731 vdi.attributes["type"] = "system"
732 vdi.attributes["sharable"] = "False"
733 vdi.attributes["name"] = name
735 vdi.appendChild(self.make_name_tag(name, document))
737 return vdi
739 def extract_vif(self, vif_sxp, document):
741 vif = document.createElement("vif")
743 dev = get_child_by_name(vif_sxp, "vifname", None)
745 if dev is None:
746 dev = self.getFreshEthDevice()
748 vif.attributes["name"] \
749 = "vif" + str(dev.__hash__())
750 vif.attributes["mac"] \
751 = get_child_by_name(vif_sxp, "mac", "")
752 vif.attributes["mtu"] \
753 = get_child_by_name(vif_sxp, "mtu", "")
754 vif.attributes["device"] = dev
755 vif.attributes["qos_algorithm_type"] = ""
757 policy = get_child_by_name(vif_sxp, "policy")
758 label = get_child_by_name(vif_sxp, "label")
760 vif.attributes["security_label"] = security.set_security_label(policy, label)
762 if get_child_by_name(vif_sxp, "bridge") is not None:
763 vif.attributes["network"] \
764 = get_child_by_name(vif_sxp, "bridge")
766 return vif
768 def extract_vtpm(self, vtpm_sxp, document):
770 vtpm = document.createElement("vtpm")
772 vtpm.attributes["backend"] \
773 = get_child_by_name(vtpm_sxp, "backend", "0")
775 return vtpm
777 _eths = -1
779 def mk_other_config(self, key, value, document):
780 other_config = document.createElement("other_config")
781 other_config.attributes["key"] = key
782 other_config.attributes["value"] = value
783 return other_config
785 def extract_consoles(self, image, document):
786 consoles = []
788 if int(get_child_by_name(image, "nographic", "1")) == 1:
789 return consoles
791 if int(get_child_by_name(image, "vnc", "0")) == 1:
792 console = document.createElement("console")
793 console.attributes["protocol"] = "rfb"
794 console.appendChild(self.mk_other_config(
795 "vncunused", str(get_child_by_name(image, "vncunused", "0")),
796 document))
797 console.appendChild(self.mk_other_config(
798 "vnclisten",
799 get_child_by_name(image, "vnclisten", "127.0.0.1"),
800 document))
801 console.appendChild(self.mk_other_config(
802 "vncpasswd", get_child_by_name(image, "vncpasswd", ""),
803 document))
804 consoles.append(console)
805 if int(get_child_by_name(image, "sdl", "0")) == 1:
806 console = document.createElement("console")
807 console.attributes["protocol"] = "sdl"
808 console.appendChild(self.mk_other_config(
809 "display", get_child_by_name(image, "display", ""),
810 document))
811 console.appendChild(self.mk_other_config(
812 "xauthority",
813 get_child_by_name(image, "vxauthority", "127.0.0.1"),
814 document))
815 console.appendChild(self.mk_other_config(
816 "opengl", get_child_by_name(image, "opengl", "1"),
817 document))
818 consoles.append(console)
820 return consoles
823 def extract_platform(self, image, document):
824 platform_keys = ['acpi', 'apic', 'pae', 'vhpt', 'timer_mode',
825 'hap', 'hpet']
827 def extract_platform_key(key):
828 platform = document.createElement("platform")
829 platform.attributes["key"] = key
830 platform.attributes["value"] \
831 = str(get_child_by_name(image, key, "1"))
832 return platform
834 return map(extract_platform_key, platform_keys)
836 def getFreshEthDevice(self):
837 self._eths += 1
838 return "eth%i" % self._eths