2015-02-27 08:53:51 +00:00
|
|
|
import string
|
|
|
|
from vrtManager import util
|
|
|
|
from vrtManager.connection import wvmConnect
|
2015-02-27 12:44:44 +00:00
|
|
|
from webvirtcloud.settings import QEMU_CONSOLE_DEFAULT_TYPE
|
2018-09-14 13:23:25 +00:00
|
|
|
from webvirtcloud.settings import QEMU_CONSOLE_LISTEN_ADDRESSES
|
2018-10-05 12:49:02 +00:00
|
|
|
from webvirtcloud.settings import INSTANCE_VOLUME_DEFAULT_OWNER as default_owner
|
2018-07-20 10:40:49 +00:00
|
|
|
from webvirtcloud.settings import INSTANCE_VOLUME_DEFAULT_FORMAT
|
2015-02-27 08:53:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_rbd_storage_data(stg):
|
|
|
|
xml = stg.XMLDesc(0)
|
|
|
|
ceph_user = util.get_xml_path(xml, "/pool/source/auth/@username")
|
2016-04-16 13:04:28 +00:00
|
|
|
|
2018-07-20 12:43:29 +00:00
|
|
|
def get_ceph_hosts(doc):
|
2016-04-16 13:04:28 +00:00
|
|
|
hosts = []
|
2018-07-20 12:43:29 +00:00
|
|
|
for host in doc.xpath("/pool/source/host"):
|
2016-04-16 13:04:28 +00:00
|
|
|
name = host.prop("name")
|
|
|
|
if name:
|
|
|
|
hosts.append({'name': name, 'port': host.prop("port")})
|
|
|
|
return hosts
|
|
|
|
ceph_hosts = util.get_xml_path(xml, func=get_ceph_hosts)
|
|
|
|
secret_uuid = util.get_xml_path(xml, "/pool/source/auth/secret/@uuid")
|
|
|
|
return ceph_user, secret_uuid, ceph_hosts
|
2015-02-27 08:53:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
class wvmCreate(wvmConnect):
|
2018-07-20 10:40:49 +00:00
|
|
|
image_format = INSTANCE_VOLUME_DEFAULT_FORMAT
|
|
|
|
|
2015-02-27 08:53:51 +00:00
|
|
|
def get_storages_images(self):
|
|
|
|
"""
|
|
|
|
Function return all images on all storages
|
|
|
|
"""
|
|
|
|
images = []
|
2018-07-20 11:04:13 +00:00
|
|
|
storages = self.get_storages(only_actives=True)
|
2015-02-27 08:53:51 +00:00
|
|
|
for storage in storages:
|
|
|
|
stg = self.get_storage(storage)
|
|
|
|
try:
|
|
|
|
stg.refresh(0)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
for img in stg.listVolumes():
|
|
|
|
if img.endswith('.iso'):
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
images.append(img)
|
|
|
|
return images
|
|
|
|
|
|
|
|
def get_os_type(self):
|
|
|
|
"""Get guest capabilities"""
|
|
|
|
return util.get_xml_path(self.get_cap_xml(), "/capabilities/guest/os_type")
|
|
|
|
|
|
|
|
def get_host_arch(self):
|
|
|
|
"""Get guest capabilities"""
|
|
|
|
return util.get_xml_path(self.get_cap_xml(), "/capabilities/host/cpu/arch")
|
|
|
|
|
2018-10-05 12:49:02 +00:00
|
|
|
def create_volume(self, storage, name, size, image_format=image_format, metadata=False, owner=default_owner):
|
2015-02-27 08:53:51 +00:00
|
|
|
size = int(size) * 1073741824
|
|
|
|
stg = self.get_storage(storage)
|
|
|
|
storage_type = util.get_xml_path(stg.XMLDesc(0), "/pool/@type")
|
|
|
|
if storage_type == 'dir':
|
2019-03-20 11:30:41 +00:00
|
|
|
if image_format in ('qcow', 'qcow2'):
|
|
|
|
name += '.' + image_format
|
|
|
|
else:
|
|
|
|
name += '.img'
|
2015-02-27 08:53:51 +00:00
|
|
|
alloc = 0
|
|
|
|
else:
|
|
|
|
alloc = size
|
|
|
|
metadata = False
|
|
|
|
xml = """
|
|
|
|
<volume>
|
|
|
|
<name>%s</name>
|
|
|
|
<capacity>%s</capacity>
|
|
|
|
<allocation>%s</allocation>
|
|
|
|
<target>
|
|
|
|
<format type='%s'/>
|
2018-07-25 08:16:03 +00:00
|
|
|
<permissions>
|
2018-10-05 12:49:02 +00:00
|
|
|
<owner>%s</owner>
|
|
|
|
<group>%s</group>
|
2018-07-25 08:16:03 +00:00
|
|
|
<mode>0644</mode>
|
|
|
|
<label>virt_image_t</label>
|
|
|
|
</permissions>
|
2018-10-05 14:07:25 +00:00
|
|
|
<compat>1.1</compat>
|
|
|
|
<features>
|
|
|
|
<lazy_refcounts/>
|
|
|
|
</features>
|
2015-02-27 08:53:51 +00:00
|
|
|
</target>
|
2018-10-05 12:49:02 +00:00
|
|
|
</volume>""" % (name, size, alloc, image_format, owner['uid'], owner['guid'])
|
2015-02-27 08:53:51 +00:00
|
|
|
stg.createXML(xml, metadata)
|
|
|
|
try:
|
|
|
|
stg.refresh(0)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
vol = stg.storageVolLookupByName(name)
|
|
|
|
return vol.path()
|
|
|
|
|
|
|
|
def get_volume_type(self, path):
|
|
|
|
vol = self.get_volume_by_path(path)
|
|
|
|
vol_type = util.get_xml_path(vol.XMLDesc(0), "/volume/target/format/@type")
|
2018-11-23 12:18:32 +00:00
|
|
|
if vol_type == 'unknown' or vol_type == 'iso':
|
2015-02-27 08:53:51 +00:00
|
|
|
return 'raw'
|
|
|
|
if vol_type:
|
|
|
|
return vol_type
|
|
|
|
else:
|
|
|
|
return 'raw'
|
|
|
|
|
2018-12-18 07:46:23 +00:00
|
|
|
def get_volume_path(self, volume, pool=None):
|
|
|
|
if not pool:
|
|
|
|
storages = self.get_storages(only_actives=True)
|
|
|
|
else:
|
|
|
|
storages = [pool,]
|
2015-02-27 08:53:51 +00:00
|
|
|
for storage in storages:
|
|
|
|
stg = self.get_storage(storage)
|
|
|
|
if stg.info()[0] != 0:
|
|
|
|
stg.refresh(0)
|
|
|
|
for img in stg.listVolumes():
|
|
|
|
if img == volume:
|
|
|
|
vol = stg.storageVolLookupByName(img)
|
|
|
|
return vol.path()
|
|
|
|
|
|
|
|
def get_storage_by_vol_path(self, vol_path):
|
|
|
|
vol = self.get_volume_by_path(vol_path)
|
|
|
|
return vol.storagePoolLookupByVolume()
|
|
|
|
|
2019-01-09 11:16:51 +00:00
|
|
|
def clone_from_template(self, clone, template, storage=None, metadata=False, owner=default_owner):
|
2015-02-27 08:53:51 +00:00
|
|
|
vol = self.get_volume_by_path(template)
|
2019-01-09 11:16:51 +00:00
|
|
|
if not storage:
|
|
|
|
stg = vol.storagePoolLookupByVolume()
|
|
|
|
else:
|
|
|
|
stg = self.get_storage(storage)
|
|
|
|
|
2015-02-27 08:53:51 +00:00
|
|
|
storage_type = util.get_xml_path(stg.XMLDesc(0), "/pool/@type")
|
|
|
|
format = util.get_xml_path(vol.XMLDesc(0), "/volume/target/format/@type")
|
|
|
|
if storage_type == 'dir':
|
|
|
|
clone += '.img'
|
|
|
|
else:
|
|
|
|
metadata = False
|
|
|
|
xml = """
|
|
|
|
<volume>
|
|
|
|
<name>%s</name>
|
|
|
|
<capacity>0</capacity>
|
|
|
|
<allocation>0</allocation>
|
|
|
|
<target>
|
|
|
|
<format type='%s'/>
|
2018-07-25 08:16:03 +00:00
|
|
|
<permissions>
|
2018-10-05 12:49:02 +00:00
|
|
|
<owner>%s</owner>
|
|
|
|
<group>%s</group>
|
2018-07-25 08:16:03 +00:00
|
|
|
<mode>0644</mode>
|
|
|
|
<label>virt_image_t</label>
|
|
|
|
</permissions>
|
2018-07-31 13:26:28 +00:00
|
|
|
<compat>1.1</compat>
|
|
|
|
<features>
|
|
|
|
<lazy_refcounts/>
|
|
|
|
</features>
|
2015-02-27 08:53:51 +00:00
|
|
|
</target>
|
2018-10-05 12:49:02 +00:00
|
|
|
</volume>""" % (clone, format, owner['uid'], owner['guid'])
|
2015-02-27 08:53:51 +00:00
|
|
|
stg.createXMLFrom(xml, vol, metadata)
|
|
|
|
clone_vol = stg.storageVolLookupByName(clone)
|
|
|
|
return clone_vol.path()
|
|
|
|
|
|
|
|
def _defineXML(self, xml):
|
|
|
|
self.wvm.defineXML(xml)
|
|
|
|
|
|
|
|
def delete_volume(self, path):
|
|
|
|
vol = self.get_volume_by_path(path)
|
|
|
|
vol.delete()
|
|
|
|
|
2018-11-26 12:40:02 +00:00
|
|
|
def create_instance(self, name, memory, vcpu, host_model, uuid, images, cache_mode, networks, virtio, listen_addr, nwfilter=None, video="cirrus", console_pass="random", mac=None, qemu_ga=False):
|
2015-02-27 08:53:51 +00:00
|
|
|
"""
|
|
|
|
Create VM function
|
|
|
|
"""
|
|
|
|
memory = int(memory) * 1024
|
|
|
|
|
|
|
|
if self.is_kvm_supported():
|
|
|
|
hypervisor_type = 'kvm'
|
|
|
|
else:
|
|
|
|
hypervisor_type = 'qemu'
|
|
|
|
|
|
|
|
xml = """
|
|
|
|
<domain type='%s'>
|
|
|
|
<name>%s</name>
|
|
|
|
<description>None</description>
|
|
|
|
<uuid>%s</uuid>
|
|
|
|
<memory unit='KiB'>%s</memory>
|
|
|
|
<vcpu>%s</vcpu>""" % (hypervisor_type, name, uuid, memory, vcpu)
|
|
|
|
if host_model:
|
|
|
|
xml += """<cpu mode='host-model'/>"""
|
|
|
|
xml += """<os>
|
|
|
|
<type arch='%s'>%s</type>
|
|
|
|
<boot dev='hd'/>
|
|
|
|
<boot dev='cdrom'/>
|
|
|
|
<bootmenu enable='yes'/>
|
|
|
|
</os>""" % (self.get_host_arch(), self.get_os_type())
|
|
|
|
xml += """<features>
|
|
|
|
<acpi/><apic/><pae/>
|
|
|
|
</features>
|
|
|
|
<clock offset="utc"/>
|
|
|
|
<on_poweroff>destroy</on_poweroff>
|
|
|
|
<on_reboot>restart</on_reboot>
|
|
|
|
<on_crash>restart</on_crash>
|
|
|
|
<devices>"""
|
|
|
|
|
2018-11-23 12:18:32 +00:00
|
|
|
vd_disk_letters = list(string.lowercase)
|
|
|
|
fd_disk_letters = list(string.lowercase)
|
|
|
|
hd_disk_letters = list(string.lowercase)
|
|
|
|
sd_disk_letters = list(string.lowercase)
|
|
|
|
add_cd = True
|
|
|
|
#for image, img_type in images.items():
|
|
|
|
for volume in images:
|
|
|
|
stg = self.get_storage_by_vol_path(volume['path'])
|
2015-02-27 08:53:51 +00:00
|
|
|
stg_type = util.get_xml_path(stg.XMLDesc(0), "/pool/@type")
|
|
|
|
|
2018-11-23 12:18:32 +00:00
|
|
|
if volume['device'] == 'cdrom': add_cd = False
|
|
|
|
|
2015-02-27 08:53:51 +00:00
|
|
|
if stg_type == 'rbd':
|
2016-04-16 13:04:28 +00:00
|
|
|
ceph_user, secret_uuid, ceph_hosts = get_rbd_storage_data(stg)
|
2015-02-27 08:53:51 +00:00
|
|
|
xml += """<disk type='network' device='disk'>
|
2016-04-17 21:41:40 +00:00
|
|
|
<driver name='qemu' type='%s' cache='%s'/>
|
2015-02-27 08:53:51 +00:00
|
|
|
<auth username='%s'>
|
|
|
|
<secret type='ceph' uuid='%s'/>
|
|
|
|
</auth>
|
2018-11-23 12:18:32 +00:00
|
|
|
<source protocol='rbd' name='%s'>""" % (volume['type'], cache_mode, ceph_user, secret_uuid, volume['path'])
|
2016-04-16 13:04:28 +00:00
|
|
|
if isinstance(ceph_hosts, list):
|
|
|
|
for host in ceph_hosts:
|
|
|
|
if host.get('port'):
|
|
|
|
xml += """
|
|
|
|
<host name='%s' port='%s'/>""" % (host.get('name'), host.get('port'))
|
|
|
|
else:
|
|
|
|
xml += """
|
|
|
|
<host name='%s'/>""" % host.get('name')
|
|
|
|
xml += """
|
|
|
|
</source>"""
|
2015-02-27 08:53:51 +00:00
|
|
|
else:
|
2018-11-23 12:18:32 +00:00
|
|
|
xml += """<disk type='file' device='%s'>
|
2016-04-17 21:41:40 +00:00
|
|
|
<driver name='qemu' type='%s' cache='%s'/>
|
2018-11-23 12:18:32 +00:00
|
|
|
<source file='%s'/>""" % (volume['device'], volume['type'], cache_mode, volume['path'])
|
|
|
|
|
|
|
|
if volume['bus'] == 'virtio':
|
|
|
|
xml += """<target dev='vd%s' bus='%s'/>""" % (vd_disk_letters.pop(0), volume['bus'])
|
|
|
|
elif volume['bus'] == 'ide':
|
|
|
|
xml += """<target dev='hd%s' bus='%s'/>""" % (hd_disk_letters.pop(0), volume['bus'])
|
|
|
|
elif volume['bus'] == 'fdc':
|
|
|
|
xml += """<target dev='fd%s' bus='%s'/>""" % (fd_disk_letters.pop(0), volume['bus'])
|
2015-02-27 08:53:51 +00:00
|
|
|
else:
|
2018-11-23 12:18:32 +00:00
|
|
|
xml += """<target dev='sd%s' bus='%s'/>""" % (sd_disk_letters.pop(0), volume['bus'])
|
2015-02-27 08:53:51 +00:00
|
|
|
xml += """</disk>"""
|
2018-11-23 12:18:32 +00:00
|
|
|
if add_cd:
|
|
|
|
xml += """ <disk type='file' device='cdrom'>
|
|
|
|
<driver name='qemu' type='raw'/>
|
|
|
|
<source file=''/>
|
|
|
|
<target dev='hd%s' bus='ide'/>
|
|
|
|
<readonly/>
|
|
|
|
</disk>""" % (hd_disk_letters.pop(0),)
|
2015-02-27 08:53:51 +00:00
|
|
|
for net in networks.split(','):
|
|
|
|
xml += """<interface type='network'>"""
|
|
|
|
if mac:
|
|
|
|
xml += """<mac address='%s'/>""" % mac
|
2018-09-14 13:23:25 +00:00
|
|
|
xml += """<source network='%s'/>""" % net
|
|
|
|
if nwfilter:
|
|
|
|
xml += """<filterref filter='%s'/>""" % nwfilter
|
2015-02-27 08:53:51 +00:00
|
|
|
if virtio:
|
|
|
|
xml += """<model type='virtio'/>"""
|
|
|
|
xml += """</interface>"""
|
|
|
|
|
2018-09-14 20:02:07 +00:00
|
|
|
if console_pass == "random":
|
|
|
|
console_pass = "passwd='" + util.randomPasswd() + "'"
|
|
|
|
else:
|
|
|
|
if not console_pass == "":
|
|
|
|
console_pass = "passwd='" + console_pass + "'"
|
2018-09-14 13:23:25 +00:00
|
|
|
|
2015-02-27 08:53:51 +00:00
|
|
|
xml += """ <input type='mouse' bus='ps2'/>
|
|
|
|
<input type='tablet' bus='usb'/>
|
2018-09-14 13:23:25 +00:00
|
|
|
<graphics type='%s' port='-1' autoport='yes' %s listen='%s'/>
|
2018-11-26 12:40:02 +00:00
|
|
|
<console type='pty'/> """ % (QEMU_CONSOLE_DEFAULT_TYPE, console_pass, listen_addr)
|
|
|
|
|
|
|
|
if qemu_ga:
|
|
|
|
xml += """ <channel type='unix'>
|
|
|
|
<target type='virtio' name='org.qemu.guest_agent.0'/>
|
|
|
|
</channel>"""
|
|
|
|
|
|
|
|
xml += """ <video>
|
2018-09-14 13:23:25 +00:00
|
|
|
<model type='%s'/>
|
2018-11-26 12:40:02 +00:00
|
|
|
</video>
|
|
|
|
<memballoon model='virtio'/>
|
|
|
|
</devices>
|
|
|
|
</domain>""" % video
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-02-27 08:53:51 +00:00
|
|
|
self._defineXML(xml)
|