guest_xml.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from models import Config, Disk
  4. from models import Guest
  5. from models import status
  6. __author__ = 'James Iter'
  7. __date__ = '2017/3/25'
  8. __contact__ = 'james.iter.cn@gmail.com'
  9. __copyright__ = '(c) 2017 by James Iter.'
  10. class GuestXML(object):
  11. def __init__(self, guest=None, disk=None, config=None, os_type=None):
  12. assert isinstance(guest, Guest)
  13. assert isinstance(disk, Disk)
  14. assert isinstance(config, Config)
  15. self.guest = guest
  16. self.disk = disk
  17. self.config = config
  18. self.os_type = os_type
  19. def get_domain(self):
  20. return """<?xml version="1.0" encoding="utf-8"?>
  21. <domain type="kvm">
  22. {0}
  23. {1}
  24. {2}
  25. {3}
  26. {4}
  27. {5}
  28. {6}
  29. {7}
  30. {8}
  31. </domain>
  32. """.format(self.get_features(), self.get_cpu_mode(), self.get_clock(), self.get_name(), self.get_uuid(),
  33. self.get_vcpu(), self.get_memory(), self.get_os(), self.get_devices())
  34. @staticmethod
  35. def get_features():
  36. return """
  37. <features>
  38. <acpi/>
  39. <apic/>
  40. </features>
  41. """
  42. @staticmethod
  43. def get_cpu_mode():
  44. return """<cpu mode='host-passthrough'/>"""
  45. def get_clock(self):
  46. # clock 参考链接:https://libvirt.org/formatdomain.html#elementsTime
  47. # Windows Guest 设为 localtime,非 Windows Guest 都设为 utc
  48. offset = 'utc'
  49. if str(self.os_type).lower().find('windows') >= 0:
  50. offset = 'localtime'
  51. return """<clock offset='{0}'/>""".format(offset)
  52. def get_name(self):
  53. return """<name>{0}</name>""".format(self.guest.label)
  54. def get_uuid(self):
  55. return """<uuid>{0}</uuid>""".format(self.guest.uuid)
  56. def get_vcpu(self):
  57. return """<vcpu>{0}</vcpu>""".format(self.guest.cpu.__str__())
  58. def get_memory(self):
  59. return """<memory unit="GiB">{0}</memory>""".format(self.guest.memory.__str__())
  60. @staticmethod
  61. def get_os():
  62. return """
  63. <os>
  64. <boot dev="hd"/>
  65. <type arch="x86_64">hvm</type>
  66. <bootmenu timeout="3000" enable="yes"/>
  67. </os>
  68. """
  69. def get_devices(self):
  70. return """
  71. <devices>
  72. {0}
  73. {1}
  74. {2}
  75. {3}
  76. {4}
  77. </devices>
  78. """.format(self.get_interface(), self.get_disk(), self.get_graphics(), self.get_console(), self.get_channel())
  79. def get_interface(self):
  80. return """
  81. <interface type='network'>
  82. <source network='{0}'/>
  83. <model type='virtio'/>
  84. <bandwidth>
  85. <inbound average='{1}'/>
  86. <outbound average='{1}'/>
  87. </bandwidth>
  88. </interface>
  89. """.format(self.guest.network, self.guest.bandwidth / 1000 / 8)
  90. def get_disk(self):
  91. from initialize import dev_table
  92. if self.config.storage_mode in [status.StorageMode.local.value, status.StorageMode.shared_mount.value]:
  93. disk_xml = """
  94. <disk type='file' device='disk'>
  95. <driver name='qemu' type='{0}' cache='none'/>
  96. <source file='{1}'/>
  97. <target dev='{2}' bus='virtio'/>
  98. </disk>
  99. """.format(self.disk.format, self.disk.path, dev_table[self.disk.sequence])
  100. elif self.config.storage_mode in [status.StorageMode.ceph.value, status.StorageMode.glusterfs.value]:
  101. dfs_protocol = 'ceph'
  102. if self.config.storage_mode == status.StorageMode.glusterfs.value:
  103. dfs_protocol = 'gluster'
  104. disk_xml = """
  105. <disk type='network' device='disk'>
  106. <driver name='qemu' type='{0}' cache='none'/>
  107. <source protocol='{1}' name='{2}/{3}'>
  108. <host name='127.0.0.1' port='24007'/>
  109. </source>
  110. <target dev='{4}' bus='virtio'/>
  111. </disk>
  112. """.format(self.disk.format, dfs_protocol, self.config.dfs_volume, self.disk.path,
  113. dev_table[self.disk.sequence])
  114. else:
  115. disk_xml = ''
  116. return disk_xml
  117. def get_graphics(self):
  118. return """
  119. <graphics passwd="{0}" keymap="en-us" port="{1}" type="vnc">
  120. <listen network="{2}" type="network"/>
  121. </graphics>
  122. """.format(self.guest.vnc_password, self.guest.vnc_port, self.guest.manage_network)
  123. @staticmethod
  124. def get_console():
  125. return """
  126. <serial type='pty'>
  127. <target port='0'/>
  128. </serial>
  129. <console type='pty'>
  130. <target type='serial' port='0'/>
  131. </console>
  132. """
  133. @staticmethod
  134. def get_channel():
  135. return """
  136. <channel type='unix'>
  137. <source mode='bind'/>
  138. <target type='virtio' name='org.qemu.guest_agent.0'/>
  139. </channel>
  140. """