diff --git a/cinder/brick/local_dev/lvm.py b/cinder/brick/local_dev/lvm.py index aecdb3c2bbf..50acfe3a0e1 100644 --- a/cinder/brick/local_dev/lvm.py +++ b/cinder/brick/local_dev/lvm.py @@ -25,9 +25,12 @@ import time from cinder.brick import exception from cinder.brick import executor from cinder.i18n import _ +from cinder.openstack.common import excutils +from cinder.openstack.common.gettextutils import _LW from cinder.openstack.common import log as logging from cinder.openstack.common import processutils as putils + LOG = logging.getLogger(__name__) @@ -284,10 +287,22 @@ class LVM(executor.Executor): :returns: dict representation of Logical Volume if exists """ - ref_list = self.get_volumes(name) - for r in ref_list: - if r['name'] == name: - return r + try: + ref_list = self.get_volumes(name) + for r in ref_list: + if r['name'] == name: + return r + except putils.ProcessExecutionError as err: + # NOTE(joachim): Catch the "notfound" case from the stderr + # in order to let the other errors through. + with excutils.save_and_reraise_exception(reraise=True) as ctx: + if "not found" in err.stderr: + ctx.reraise = False + LOG.warning( + _LW("Caught exception for lvs 'LV not found': %s") + % (name) + ) + return None @staticmethod def get_all_physical_volumes(root_helper, vg_name=None): diff --git a/cinder/tests/brick/test_brick_lvm.py b/cinder/tests/brick/test_brick_lvm.py index 95b37ae1203..11a317d9adb 100644 --- a/cinder/tests/brick/test_brick_lvm.py +++ b/cinder/tests/brick/test_brick_lvm.py @@ -89,6 +89,10 @@ class BrickLvmTestCase(test.TestCase): "mXzbuX-dKpG-Rz7E-xtKY-jeju-QsYU-SLG8Z3\n" elif ('env, LC_ALL=C, lvs, --noheadings, ' '--unit=g, -o, vg_name,name,size' in cmd_string): + if 'fake-unknown' in cmd_string: + raise processutils.ProcessExecutionError( + stderr="One of more volume(s) not found." + ) data = " fake-vg fake-1 1.00g\n" data += " fake-vg fake-2 1.00g\n" elif ('env, LC_ALL=C, lvdisplay, --noheading, -C, -o, Attr' in @@ -147,6 +151,9 @@ class BrickLvmTestCase(test.TestCase): def test_get_volume(self): self.assertEqual(self.vg.get_volume('fake-1')['name'], 'fake-1') + def test_get_volume_none(self): + self.assertEqual(self.vg.get_volume('fake-unknown'), None) + def test_get_all_physical_volumes(self): # Filtered VG version pvs = self.vg.get_all_physical_volumes('sudo', 'fake-vg')