Skip to content

Commit c338612

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "Move libvirt calculation of machine type to utils.py"
2 parents c9977f2 + d2f8995 commit c338612

File tree

4 files changed

+50
-10
lines changed

4 files changed

+50
-10
lines changed

nova/tests/unit/virt/libvirt/fake_libvirt_utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ def cpu_features_to_traits(features):
179179
return libvirt_utils.cpu_features_to_traits(features)
180180

181181

182+
def get_default_machine_type(arch):
183+
return libvirt_utils.get_default_machine_type(arch)
184+
185+
182186
def mdev_name2uuid(mdev_name):
183187
return libvirt_utils.mdev_name2uuid(mdev_name)
184188

nova/tests/unit/virt/libvirt/test_utils.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from nova.objects import fields as obj_fields
3333
import nova.privsep.fs
3434
from nova import test
35+
from nova.tests import fixtures as nova_fixtures
3536
from nova.tests.unit import fake_instance
3637
from nova.tests.unit.virt.libvirt import fakelibvirt
3738
from nova.virt.disk import api as disk
@@ -969,3 +970,29 @@ def test_find_disk_parallels(self):
969970
disk_path, format = libvirt_utils.find_disk(guest)
970971
self.assertEqual('/test/disk', disk_path)
971972
self.assertEqual('ploop', format)
973+
974+
def test_machine_type_mappings(self):
975+
self.useFixture(nova_fixtures.ConfPatcher(
976+
group="libvirt", hw_machine_type=['x86_64=q35', 'i686=legacy']))
977+
self.assertDictEqual({'x86_64': 'q35', 'i686': 'legacy'},
978+
libvirt_utils.machine_type_mappings())
979+
980+
def test_invalid_machine_type_mappings(self):
981+
self.useFixture(nova_fixtures.ConfPatcher(
982+
group="libvirt", hw_machine_type=['x86_64=q35', 'foo']))
983+
self.assertDictEqual({'x86_64': 'q35'},
984+
libvirt_utils.machine_type_mappings())
985+
986+
def test_get_default_machine_type(self):
987+
self.useFixture(nova_fixtures.ConfPatcher(
988+
group="libvirt", hw_machine_type=['x86_64=q35', 'i686=legacy']))
989+
self.assertEqual('q35',
990+
libvirt_utils.get_default_machine_type('x86_64'))
991+
992+
def test_get_default_machine_type_empty(self):
993+
self.assertIsNone(libvirt_utils.get_default_machine_type('sparc'))
994+
995+
def test_get_default_machine_type_missing(self):
996+
self.useFixture(nova_fixtures.ConfPatcher(
997+
group="libvirt", hw_machine_type=['x86_64=q35', 'i686=legacy']))
998+
self.assertIsNone(libvirt_utils.get_default_machine_type('sparc'))

nova/virt/libvirt/driver.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4227,13 +4227,6 @@ def _get_guest_config_meta(self, instance):
42274227

42284228
return meta
42294229

4230-
def _machine_type_mappings(self):
4231-
mappings = {}
4232-
for mapping in CONF.libvirt.hw_machine_type:
4233-
host_arch, _, machine_type = mapping.partition('=')
4234-
mappings[host_arch] = machine_type
4235-
return mappings
4236-
42374230
def _get_machine_type(self, image_meta, caps):
42384231
# The guest machine type can be set as an image metadata
42394232
# property, or otherwise based on architecture-specific
@@ -4257,9 +4250,10 @@ def _get_machine_type(self, image_meta, caps):
42574250
mach_type = 's390-ccw-virtio'
42584251

42594252
# If set in the config, use that as the default.
4260-
if CONF.libvirt.hw_machine_type:
4261-
mappings = self._machine_type_mappings()
4262-
mach_type = mappings.get(caps.host.cpu.arch)
4253+
mach_type = (
4254+
libvirt_utils.get_default_machine_type(caps.host.cpu.arch)
4255+
or mach_type
4256+
)
42634257

42644258
return mach_type
42654259

nova/virt/libvirt/utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,21 @@ def get_cpu_model_from_arch(arch):
541541
return mode
542542

543543

544+
def machine_type_mappings():
545+
mappings = {}
546+
for mapping in CONF.libvirt.hw_machine_type or {}:
547+
host_arch, _, machine_type = mapping.partition('=')
548+
if machine_type == '':
549+
LOG.warning("Invalid hw_machine_type config value %s", mapping)
550+
else:
551+
mappings[host_arch] = machine_type
552+
return mappings
553+
554+
555+
def get_default_machine_type(arch):
556+
return machine_type_mappings().get(arch)
557+
558+
544559
def mdev_name2uuid(mdev_name):
545560
"""Convert an mdev name (of the form mdev_<uuid_with_underscores>) to a
546561
uuid (of the form 8-4-4-4-12).

0 commit comments

Comments
 (0)