Skip to content

Commit

Permalink
virt: allow defining the VM type and arch when creating it
Browse files Browse the repository at this point in the history
Some hypervisors can handle several CPU architectures or have different
virtualization types. This is reflected in libvirt by the OS type (badly
named, indeed) and the arch value. Allow users to set them when creating
a VM using either virt.init or virt.running.

Signed-off-by: Cédric Bosdonnat <cbosdonnat@suse.com>
  • Loading branch information
cbosdo committed Sep 6, 2018
1 parent d53cd16 commit a4d67e9
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 17 deletions.
40 changes: 29 additions & 11 deletions salt/modules/virt.py
Expand Up @@ -243,14 +243,6 @@ def __get_conn(**kwargs):
return conn


def _get_domain_types(**kwargs):
'''
Return the list of possible values for the <domain> type attribute.
'''
caps = capabilities(**kwargs)
return sorted(set([x for y in [guest['arch']['domains'].keys() for guest in caps['guests']] for x in y]))


def _get_domain(conn, *vms, **kwargs):
'''
Return a domain object for the named VM or return domain object for all VMs.
Expand Down Expand Up @@ -538,6 +530,8 @@ def _gen_xml(name,
diskp,
nicp,
hypervisor,
os_type,
arch,
graphics=None,
**kwargs):
'''
Expand Down Expand Up @@ -607,6 +601,9 @@ def _gen_xml(name,

context['nics'] = nicp

context['os_type'] = os_type
context['arch'] = arch

fn_ = 'libvirt_domain.jinja'
try:
template = JINJA.get_template(fn_)
Expand Down Expand Up @@ -1107,6 +1104,8 @@ def init(name,
enable_vnc=False,
enable_qcow=False,
graphics=None,
os_type=None,
arch=None,
**kwargs):
'''
Initialize a new vm
Expand Down Expand Up @@ -1165,6 +1164,16 @@ def init(name,
Dictionary providing details on the graphics device to create. (Default: ``None``)
See :ref:`init-graphics-def` for more details on the possible values.
.. versionadded:: Fluorine
:param os_type:
type of virtualization as found in the ``//os/type`` element of the libvirt definition.
The default value is taken from the host capabilities, with a preference for ``hvm``.
.. versionadded:: Fluorine
:param arch:
architecture of the virtual machine. The default value is taken from the host capabilities,
but ``x86_64`` is prefed over ``i686``.
.. versionadded:: Fluorine
:param enable_qcow:
``True`` to create a QCOW2 overlay image, rather than copying the image
Expand Down Expand Up @@ -1327,7 +1336,10 @@ def init(name,
virt:
images: /data/my/vm/images/
'''
hypervisors = _get_domain_types(**kwargs)
caps = capabilities(**kwargs)
os_types = sorted(set([guest['os_type'] for guest in caps['guests']]))
arches = sorted(set([guest['arch']['name'] for guest in caps['guests']]))
hypervisors = sorted(set([x for y in [guest['arch']['domains'].keys() for guest in caps['guests']] for x in y]))
hypervisor = __salt__['config.get']('libvirt:hypervisor', hypervisor)
if hypervisor is not None:
salt.utils.versions.warn_until(
Expand Down Expand Up @@ -1448,7 +1460,10 @@ def init(name,
'\'enable_vnc\' will be removed in {version}. ')
graphics = {'type': 'vnc'}

vm_xml = _gen_xml(name, cpu, mem, diskp, nicp, hypervisor, graphics, **kwargs)
os_type = 'hvm' if 'hvm' in os_types else os_types[0]
arch = 'x86_64' if 'x86_64' in arches else arches[0]

vm_xml = _gen_xml(name, cpu, mem, diskp, nicp, hypervisor, os_type, arch, graphics, **kwargs)
conn = __get_conn(**kwargs)
try:
conn.defineXML(vm_xml)
Expand Down Expand Up @@ -1692,6 +1707,8 @@ def update(name,
_disk_profile(disk_profile, hypervisor, disks, name, **kwargs),
_get_merged_nics(hypervisor, nic_profile, interfaces),
hypervisor,
domain.OSType(),
desc.find('.//os/type').get('arch'),
graphics,
**kwargs))

Expand Down Expand Up @@ -2378,7 +2395,8 @@ def get_profiles(hypervisor=None, **kwargs):
'''
ret = {}

hypervisors = _get_domain_types(**kwargs)
caps = capabilities(**kwargs)
hypervisors = sorted(set([x for y in [guest['arch']['domains'].keys() for guest in caps['guests']] for x in y]))
default_hypervisor = 'kvm' if 'kvm' in hypervisors else hypervisors[0]

if not hypervisor:
Expand Down
17 changes: 16 additions & 1 deletion salt/states/virt.py
Expand Up @@ -253,7 +253,9 @@ def running(name,
update=False,
connection=None,
username=None,
password=None):
password=None,
os_type=None,
arch=None):
'''
Starts an existing guest, or defines and starts a new VM with specified arguments.
Expand Down Expand Up @@ -326,6 +328,17 @@ def running(name,
:param password: password to connect with, overriding defaults
.. versionadded:: Fluorine
:param os_type:
type of virtualization as found in the ``//os/type`` element of the libvirt definition.
The default value is taken from the host capabilities, with a preference for ``hvm``.
Only used when creating a new virtual machine.
.. versionadded:: Neon
:param arch:
architecture of the virtual machine. The default value is taken from the host capabilities,
but ``x86_64`` is prefed over ``i686``. Only used when creating a new virtual machine.
.. versionadded:: Neon
.. rubric:: Example States
Expand Down Expand Up @@ -429,6 +442,8 @@ def running(name,
__salt__['virt.init'](name,
cpu=cpu,
mem=mem,
os_type=os_type,
arch=arch,
image=image,
hypervisor=vm_type,
disk=disk_profile,
Expand Down
2 changes: 1 addition & 1 deletion salt/templates/virt/libvirt_domain.jinja
Expand Up @@ -3,7 +3,7 @@
<vcpu>{{ cpu }}</vcpu>
<memory unit='KiB'>{{ mem }}</memory>
<os>
<type>hvm</type>
<type arch='{{ arch }}'>{{ os_type }}</type>
{% for dev in boot_dev %}
<boot dev='{{ dev }}' />
{% endfor %}
Expand Down
50 changes: 46 additions & 4 deletions tests/unit/modules/test_virt.py
Expand Up @@ -116,10 +116,14 @@ def test_boot_default_dev(self):
512,
diskp,
nicp,
'kvm'
'kvm',
'hvm',
'x86_64'
)
root = ET.fromstring(xml_data)
self.assertEqual(root.find('os/boot').attrib['dev'], 'hd')
self.assertEqual(root.find('os/type').attrib['arch'], 'x86_64')
self.assertEqual(root.find('os/type').text, 'hvm')

def test_boot_custom_dev(self):
'''
Expand All @@ -134,6 +138,8 @@ def test_boot_custom_dev(self):
diskp,
nicp,
'kvm',
'hvm',
'x86_64',
boot_dev='cdrom'
)
root = ET.fromstring(xml_data)
Expand All @@ -152,6 +158,8 @@ def test_boot_multiple_devs(self):
diskp,
nicp,
'kvm',
'hvm',
'x86_64',
boot_dev='cdrom network'
)
root = ET.fromstring(xml_data)
Expand All @@ -171,6 +179,8 @@ def test_gen_xml_for_serial_console(self):
diskp,
nicp,
'kvm',
'hvm',
'x86_64',
serial_type='pty',
console=True
)
Expand All @@ -191,6 +201,8 @@ def test_gen_xml_for_telnet_console(self):
diskp,
nicp,
'kvm',
'hvm',
'x86_64',
serial_type='tcp',
console=True,
telnet_port=22223
Expand All @@ -213,6 +225,8 @@ def test_gen_xml_for_telnet_console_unspecified_port(self):
diskp,
nicp,
'kvm',
'hvm',
'x86_64',
serial_type='tcp',
console=True
)
Expand All @@ -234,6 +248,8 @@ def test_gen_xml_for_serial_no_console(self):
diskp,
nicp,
'kvm',
'hvm',
'x86_64',
serial_type='pty',
console=False
)
Expand All @@ -254,6 +270,8 @@ def test_gen_xml_for_telnet_no_console(self):
diskp,
nicp,
'kvm',
'hvm',
'x86_64',
serial_type='tcp',
console=False,
)
Expand All @@ -273,7 +291,9 @@ def test_gen_xml_nographics_default(self):
512,
diskp,
nicp,
'kvm'
'kvm',
'hvm',
'x86_64'
)
root = ET.fromstring(xml_data)
self.assertIsNone(root.find('devices/graphics'))
Expand All @@ -291,6 +311,8 @@ def test_gen_xml_vnc_default(self):
diskp,
nicp,
'kvm',
'hvm',
'x86_64',
graphics={'type': 'vnc', 'port': 1234, 'tlsPort': 5678,
'listen': {'type': 'address', 'address': 'myhost'}},
)
Expand All @@ -316,6 +338,8 @@ def test_gen_xml_spice_default(self):
diskp,
nicp,
'kvm',
'hvm',
'x86_64',
graphics={'type': 'spice'},
)
root = ET.fromstring(xml_data)
Expand All @@ -338,6 +362,8 @@ def test_gen_xml_spice(self):
diskp,
nicp,
'kvm',
'hvm',
'x86_64',
graphics={'type': 'spice', 'port': 1234, 'tls_port': 5678, 'listen': {'type': 'none'}},
)
root = ET.fromstring(xml_data)
Expand Down Expand Up @@ -431,6 +457,8 @@ def test_gen_xml_for_kvm_default_profile(self):
diskp,
nicp,
'kvm',
'hvm',
'x86_64',
)
root = ET.fromstring(xml_data)
self.assertEqual(root.attrib['type'], 'kvm')
Expand Down Expand Up @@ -473,6 +501,8 @@ def test_gen_xml_for_esxi_default_profile(self):
diskp,
nicp,
'vmware',
'hvm',
'x86_64',
)
root = ET.fromstring(xml_data)
self.assertEqual(root.attrib['type'], 'vmware')
Expand Down Expand Up @@ -527,6 +557,8 @@ def test_gen_xml_for_esxi_custom_profile(self):
diskp,
nicp,
'vmware',
'hvm',
'x86_64',
)
root = ET.fromstring(xml_data)
self.assertEqual(root.attrib['type'], 'vmware')
Expand Down Expand Up @@ -563,6 +595,8 @@ def test_gen_xml_for_kvm_custom_profile(self):
diskp,
nicp,
'kvm',
'hvm',
'x86_64',
)
root = ET.fromstring(xml_data)
self.assertEqual(root.attrib['type'], 'kvm')
Expand Down Expand Up @@ -633,7 +667,9 @@ def test_controller_for_esxi(self):
512,
diskp,
nicp,
'vmware'
'vmware',
'hvm',
'x86_64',
)
root = ET.fromstring(xml_data)
controllers = root.findall('.//devices/controller')
Expand All @@ -653,7 +689,9 @@ def test_controller_for_kvm(self):
512,
diskp,
nicp,
'kvm'
'kvm',
'hvm',
'x86_64',
)
root = ET.fromstring(xml_data)
controllers = root.findall('.//devices/controller')
Expand Down Expand Up @@ -757,6 +795,9 @@ def test_update(self):
<memory unit='KiB'>1048576</memory>
<currentMemory unit='KiB'>1048576</currentMemory>
<vcpu placement='auto'>1</vcpu>
<os>
<type arch='x86_64' machine='pc-i440fx-2.6'>hvm</type>
</os>
<devices>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
Expand Down Expand Up @@ -802,6 +843,7 @@ def test_update(self):
</domain>
'''
domain_mock = self.set_mock_vm('myvm', xml)
domain_mock.OSType = MagicMock(return_value='hvm')
define_mock = MagicMock(return_value=True)
self.mock_conn.defineXML = define_mock

Expand Down
5 changes: 5 additions & 0 deletions tests/unit/states/test_virt.py
Expand Up @@ -253,6 +253,7 @@ def test_running(self):
mem=2048,
image='/path/to/img.qcow2'), ret)
init_mock.assert_called_with('myvm', cpu=2, mem=2048, image='/path/to/img.qcow2',
os_type=None, arch=None,
disk=None, disks=None, nic=None, interfaces=None,
graphics=None, hypervisor=None,
seed=True, install=True, pub_key=None, priv_key=None,
Expand Down Expand Up @@ -289,6 +290,8 @@ def test_running(self):
self.assertDictEqual(virt.running('myvm',
cpu=2,
mem=2048,
os_type='linux',
arch='i686',
vm_type='qemu',
disk_profile='prod',
disks=disks,
Expand All @@ -305,6 +308,8 @@ def test_running(self):
init_mock.assert_called_with('myvm',
cpu=2,
mem=2048,
os_type='linux',
arch='i686',
image=None,
disk='prod',
disks=disks,
Expand Down

0 comments on commit a4d67e9

Please sign in to comment.