Add a get_root_bdm utility function
Getting the root block device out of a list of block devices is a common idiom and we use it in several places in our code base. This patch factors it out in a handy utilty method and adds tests for it. Change-Id: I4a2cb3c689fed9fbc0bc51ddba095e1269cacb2d blueprint: clean-up-legacy-block-device-mapping
This commit is contained in:
@@ -388,6 +388,13 @@ def new_format_is_ephemeral(bdm):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def get_root_bdm(bdms):
|
||||||
|
try:
|
||||||
|
return (bdm for bdm in bdms if bdm.get('boot_index', -1) == 0).next()
|
||||||
|
except StopIteration:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def mappings_prepend_dev(mappings):
|
def mappings_prepend_dev(mappings):
|
||||||
"""Prepend '/dev/' to 'device' entry of swap/ephemeral virtual type."""
|
"""Prepend '/dev/' to 'device' entry of swap/ephemeral virtual type."""
|
||||||
for m in mappings:
|
for m in mappings:
|
||||||
|
@@ -1297,10 +1297,8 @@ class ComputeManager(manager.SchedulerDependentManager):
|
|||||||
It also ensures that there is a root_device_name and is set to the
|
It also ensures that there is a root_device_name and is set to the
|
||||||
first block device in the boot sequence (boot_index=0).
|
first block device in the boot sequence (boot_index=0).
|
||||||
"""
|
"""
|
||||||
try:
|
root_bdm = block_device.get_root_bdm(block_devices)
|
||||||
root_bdm = (bdm for bdm in block_devices
|
if not root_bdm:
|
||||||
if bdm['boot_index'] == 0).next()
|
|
||||||
except StopIteration:
|
|
||||||
return
|
return
|
||||||
|
|
||||||
# Get the root_device_name from the root BDM or the instance
|
# Get the root_device_name from the root BDM or the instance
|
||||||
|
@@ -129,6 +129,19 @@ class BlockDeviceTestCase(test.NoDBTestCase):
|
|||||||
_assert_volume_in_mapping('sdg', False)
|
_assert_volume_in_mapping('sdg', False)
|
||||||
_assert_volume_in_mapping('sdh1', False)
|
_assert_volume_in_mapping('sdh1', False)
|
||||||
|
|
||||||
|
def test_get_root_bdm(self):
|
||||||
|
root_bdm = {'device_name': 'vda', 'boot_index': 0}
|
||||||
|
bdms = [root_bdm,
|
||||||
|
{'device_name': 'vdb', 'boot_index': 1},
|
||||||
|
{'device_name': 'vdc', 'boot_index': -1},
|
||||||
|
{'device_name': 'vdd'}]
|
||||||
|
self.assertEqual(root_bdm, block_device.get_root_bdm(bdms))
|
||||||
|
self.assertEqual(root_bdm, block_device.get_root_bdm([bdms[0]]))
|
||||||
|
self.assertEqual(None, block_device.get_root_bdm(bdms[1:]))
|
||||||
|
self.assertEqual(None, block_device.get_root_bdm(bdms[2:]))
|
||||||
|
self.assertEqual(None, block_device.get_root_bdm(bdms[3:]))
|
||||||
|
self.assertEqual(None, block_device.get_root_bdm([]))
|
||||||
|
|
||||||
|
|
||||||
class TestBlockDeviceDict(test.NoDBTestCase):
|
class TestBlockDeviceDict(test.NoDBTestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
@@ -517,15 +517,11 @@ def get_disk_mapping(virt_type, instance,
|
|||||||
|
|
||||||
return mapping
|
return mapping
|
||||||
|
|
||||||
try:
|
# NOTE (ndipanov): root_bdm can be None when we boot from image
|
||||||
root_bdm = (bdm for bdm in
|
# as there is no driver represenation of local targeted images
|
||||||
driver.block_device_info_get_mapping(block_device_info)
|
# and they will not be in block_device_info list.
|
||||||
if bdm.get('boot_index') == 0).next()
|
root_bdm = block_device.get_root_bdm(
|
||||||
except StopIteration:
|
driver.block_device_info_get_mapping(block_device_info))
|
||||||
# NOTE (ndipanov): This happens when we boot from image as
|
|
||||||
# there is no driver representation of local targeted images
|
|
||||||
# and they will not be in block_device_info list.
|
|
||||||
root_bdm = None
|
|
||||||
|
|
||||||
root_device_name = block_device.strip_dev(
|
root_device_name = block_device.strip_dev(
|
||||||
driver.block_device_info_get_root(block_device_info))
|
driver.block_device_info_get_root(block_device_info))
|
||||||
|
Reference in New Issue
Block a user