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 5, 2018
1 parent 57a8f48 commit 2463ebe
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 18 deletions.
40 changes: 29 additions & 11 deletions salt/modules/virt.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,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 @@ -557,6 +549,8 @@ def _gen_xml(name,
diskp,
nicp,
hypervisor,
os_type,
arch,
graphics=None,
loader=None,
**kwargs):
Expand Down Expand Up @@ -638,6 +632,9 @@ def _gen_xml(name,
context['disks'][disk['name']]['index'] = six.text_type(i)
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 @@ -1217,6 +1214,8 @@ def init(name,
enable_qcow=False,
graphics=None,
loader=None,
os_type=None,
arch=None,
**kwargs):
'''
Initialize a new vm
Expand Down Expand Up @@ -1281,6 +1280,16 @@ def init(name,
See :ref:`init-loader-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:: Neon
:param arch:
architecture of the virtual machine. The default value is taken from the host capabilities,
but ``x86_64`` is prefed over ``i686``.
.. versionadded:: Neon
:param enable_qcow:
``True`` to create a QCOW2 overlay image, rather than copying the image
(Default: ``False``).
Expand Down Expand Up @@ -1478,7 +1487,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 @@ -1610,7 +1622,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, loader, **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, loader, **kwargs)
conn = __get_conn(**kwargs)
try:
conn.defineXML(vm_xml)
Expand Down Expand Up @@ -1854,6 +1869,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 @@ -2564,7 +2581,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
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,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 @@ -332,6 +334,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 @@ -435,6 +448,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
Original file line number Diff line number Diff line change
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
58 changes: 53 additions & 5 deletions tests/unit/modules/test_virt.py
Original file line number Diff line number Diff line change
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 @@ -290,7 +310,9 @@ def test_gen_xml_noloader_default(self):
512,
diskp,
nicp,
'kvm'
'kvm',
'hvm',
'x86_64',
)
root = ET.fromstring(xml_data)
self.assertIsNone(root.find('os/loader'))
Expand All @@ -308,6 +330,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 @@ -333,6 +357,8 @@ def test_gen_xml_spice_default(self):
diskp,
nicp,
'kvm',
'hvm',
'x86_64',
graphics={'type': 'spice'},
)
root = ET.fromstring(xml_data)
Expand All @@ -355,6 +381,8 @@ def test_gen_xml_loader_default(self):
diskp,
nicp,
'kvm',
'hvm',
'x86_64',
loader={'path': '/foo/bar', 'readonly': 'yes', 'type': 'pflash', 'secure': 'yes'}
)
root = ET.fromstring(xml_data)
Expand All @@ -376,6 +404,8 @@ def test_gen_xml_loader_invalid(self):
diskp,
nicp,
'kvm',
'hvm',
'x86_64',
loader={'readonly': 'yes', 'type': 'pflash', 'secure': 'yes'}
)
root = ET.fromstring(xml_data)
Expand All @@ -394,6 +424,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 @@ -487,6 +519,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 @@ -529,6 +563,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 @@ -583,6 +619,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 @@ -619,6 +657,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 @@ -689,7 +729,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 @@ -709,7 +751,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 @@ -813,6 +857,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 @@ -858,6 +905,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
Original file line number Diff line number Diff line change
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, loader=None, hypervisor=None,
seed=True, install=True, pub_key=None, priv_key=None,
Expand Down Expand Up @@ -290,6 +291,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 @@ -307,6 +310,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 2463ebe

Please sign in to comment.