guest_xml.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from models import Config, Disk, OSTemplate, OSType
  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. </domain>
  31. """.format(self.get_features(), self.get_clock(), self.get_name(), self.get_uuid(), self.get_vcpu(),
  32. self.get_memory(), self.get_os(), self.get_devices())
  33. @staticmethod
  34. def get_features():
  35. return """
  36. <features>
  37. <acpi/>
  38. <apic/>
  39. </features>
  40. """
  41. def get_clock(self):
  42. # clock 参考链接:https://libvirt.org/formatdomain.html#elementsTime
  43. # Windows Guest 设为 localtime,非 Windows Guest 都设为 utc
  44. offset = 'utc'
  45. if self.os_type == OSType.windows.value:
  46. offset = 'localtime'
  47. return """<clock offset='{0}'/>""".format(offset)
  48. def get_name(self):
  49. return """<name>{0}</name>""".format(self.guest.label)
  50. def get_uuid(self):
  51. return """<uuid>{0}</uuid>""".format(self.guest.uuid)
  52. def get_vcpu(self):
  53. return """<vcpu>{0}</vcpu>""".format(self.guest.cpu.__str__())
  54. def get_memory(self):
  55. return """<memory unit="GiB">{0}</memory>""".format(self.guest.memory.__str__())
  56. @staticmethod
  57. def get_os():
  58. return """
  59. <os>
  60. <boot dev="hd"/>
  61. <type arch="x86_64">hvm</type>
  62. <bootmenu timeout="3000" enable="yes"/>
  63. </os>
  64. """
  65. def get_devices(self):
  66. return """
  67. <devices>
  68. {0}
  69. {1}
  70. {2}
  71. {3}
  72. {4}
  73. </devices>
  74. """.format(self.get_interface(), self.get_disk(), self.get_graphics(), self.get_console(), self.get_channel())
  75. def get_interface(self):
  76. return """
  77. <interface type='network'>
  78. <source network='{0}'/>
  79. <model type='virtio'/>
  80. </interface>
  81. """.format(self.guest.network)
  82. def get_disk(self):
  83. from initialize import dev_table
  84. if self.config.storage_mode in [status.StorageMode.local.value, status.StorageMode.shared_mount.value]:
  85. disk_xml = """
  86. <disk type='file' device='disk'>
  87. <driver name='qemu' type='{0}' cache='none'/>
  88. <source file='{1}'/>
  89. <target dev='{2}' bus='virtio'/>
  90. </disk>
  91. """.format(self.disk.format, self.disk.path, dev_table[self.disk.sequence])
  92. elif self.config.storage_mode in [status.StorageMode.ceph.value, status.StorageMode.glusterfs.value]:
  93. dfs_protocol = 'ceph'
  94. if self.config.storage_mode == status.StorageMode.glusterfs.value:
  95. dfs_protocol = 'gluster'
  96. disk_xml = """
  97. <disk type='network' device='disk'>
  98. <driver name='qemu' type='{0}' cache='none'/>
  99. <source protocol='{1}' name='{2}/{3}'>
  100. <host name='127.0.0.1' port='24007'/>
  101. </source>
  102. <target dev='{4}' bus='virtio'/>
  103. </disk>
  104. """.format(self.disk.format, dfs_protocol, self.config.dfs_volume, self.disk.path,
  105. dev_table[self.disk.sequence])
  106. else:
  107. disk_xml = ''
  108. return disk_xml
  109. def get_graphics(self):
  110. return """
  111. <graphics passwd="{0}" keymap="en-us" port="{1}" type="vnc">
  112. <listen network="{2}" type="network"/>
  113. </graphics>
  114. """.format(self.guest.vnc_password, self.guest.vnc_port, self.guest.manage_network)
  115. @staticmethod
  116. def get_console():
  117. return """
  118. <serial type='pty'>
  119. <target port='0'/>
  120. </serial>
  121. <console type='pty'>
  122. <target type='serial' port='0'/>
  123. </console>
  124. """
  125. @staticmethod
  126. def get_channel():
  127. return """
  128. <channel type='unix'>
  129. <source mode='bind'/>
  130. <target type='virtio' name='org.qemu.guest_agent.0'/>
  131. </channel>
  132. """