guest_xml.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. </interface>
  85. """.format(self.guest.network)
  86. def get_disk(self):
  87. from initialize import dev_table
  88. if self.config.storage_mode in [status.StorageMode.local.value, status.StorageMode.shared_mount.value]:
  89. disk_xml = """
  90. <disk type='file' device='disk'>
  91. <driver name='qemu' type='{0}' cache='none'/>
  92. <source file='{1}'/>
  93. <target dev='{2}' bus='virtio'/>
  94. </disk>
  95. """.format(self.disk.format, self.disk.path, dev_table[self.disk.sequence])
  96. elif self.config.storage_mode in [status.StorageMode.ceph.value, status.StorageMode.glusterfs.value]:
  97. dfs_protocol = 'ceph'
  98. if self.config.storage_mode == status.StorageMode.glusterfs.value:
  99. dfs_protocol = 'gluster'
  100. disk_xml = """
  101. <disk type='network' device='disk'>
  102. <driver name='qemu' type='{0}' cache='none'/>
  103. <source protocol='{1}' name='{2}/{3}'>
  104. <host name='127.0.0.1' port='24007'/>
  105. </source>
  106. <target dev='{4}' bus='virtio'/>
  107. </disk>
  108. """.format(self.disk.format, dfs_protocol, self.config.dfs_volume, self.disk.path,
  109. dev_table[self.disk.sequence])
  110. else:
  111. disk_xml = ''
  112. return disk_xml
  113. def get_graphics(self):
  114. return """
  115. <graphics passwd="{0}" keymap="en-us" port="{1}" type="vnc">
  116. <listen network="{2}" type="network"/>
  117. </graphics>
  118. """.format(self.guest.vnc_password, self.guest.vnc_port, self.guest.manage_network)
  119. @staticmethod
  120. def get_console():
  121. return """
  122. <serial type='pty'>
  123. <target port='0'/>
  124. </serial>
  125. <console type='pty'>
  126. <target type='serial' port='0'/>
  127. </console>
  128. """
  129. @staticmethod
  130. def get_channel():
  131. return """
  132. <channel type='unix'>
  133. <source mode='bind'/>
  134. <target type='virtio' name='org.qemu.guest_agent.0'/>
  135. </channel>
  136. """