guest_xml.py 4.6 KB

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