summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2017-06-18 07:10:37 +0000
committerGerrit Code Review <review@openstack.org>2017-06-18 07:10:38 +0000
commit84b5f71933bd10cc8897a3329577aca7e56bd118 (patch)
tree0de84dc9352b8887ebb12be2c983cc356596d905
parentf67d46c5383b3c454f365b653a36d3cd043a7814 (diff)
parenta760c549b335b077172f744105ceda9c90cf81e1 (diff)
Merge "Prevent rbd map again if it's already mapped"
-rw-r--r--os_brick/initiator/connectors/rbd.py20
-rw-r--r--os_brick/tests/initiator/connectors/test_rbd.py25
2 files changed, 39 insertions, 6 deletions
diff --git a/os_brick/initiator/connectors/rbd.py b/os_brick/initiator/connectors/rbd.py
index 9bca351..e6bdc59 100644
--- a/os_brick/initiator/connectors/rbd.py
+++ b/os_brick/initiator/connectors/rbd.py
@@ -180,12 +180,20 @@ class RBDConnector(base.BaseLinuxConnector):
180 # NOTE(e0ne): map volume to a block device 180 # NOTE(e0ne): map volume to a block device
181 # via the rbd kernel module. 181 # via the rbd kernel module.
182 pool, volume = connection_properties['name'].split('/') 182 pool, volume = connection_properties['name'].split('/')
183 cmd = ['rbd', 'map', volume, '--pool', pool] 183 rbd_dev_path = RBDConnector.get_rbd_device_name(pool, volume)
184 cmd += self._get_rbd_args(connection_properties) 184 if (not os.path.islink(rbd_dev_path) or
185 self._execute(*cmd, root_helper=self._root_helper, 185 not os.path.exists(os.path.realpath(rbd_dev_path))):
186 run_as_root=True) 186 cmd = ['rbd', 'map', volume, '--pool', pool]
187 187 cmd += self._get_rbd_args(connection_properties)
188 return {'path': RBDConnector.get_rbd_device_name(pool, volume), 188 self._execute(*cmd, root_helper=self._root_helper,
189 run_as_root=True)
190 else:
191 LOG.debug('volume %(vol)s is already mapped to local'
192 ' device %(dev)s',
193 {'vol': volume,
194 'dev': os.path.realpath(rbd_dev_path)})
195
196 return {'path': rbd_dev_path,
189 'type': 'block'} 197 'type': 'block'}
190 198
191 rbd_handle = self._get_rbd_handle(connection_properties) 199 rbd_handle = self._get_rbd_handle(connection_properties)
diff --git a/os_brick/tests/initiator/connectors/test_rbd.py b/os_brick/tests/initiator/connectors/test_rbd.py
index 1f11ca6..69b1b8f 100644
--- a/os_brick/tests/initiator/connectors/test_rbd.py
+++ b/os_brick/tests/initiator/connectors/test_rbd.py
@@ -187,6 +187,31 @@ class RBDConnectorTestCase(test_connector.ConnectorTestCase):
187 self.assertEqual(expected_info, device_info) 187 self.assertEqual(expected_info, device_info)
188 188
189 @mock.patch.object(priv_rootwrap, 'execute', return_value=None) 189 @mock.patch.object(priv_rootwrap, 'execute', return_value=None)
190 @mock.patch('os.path.exists')
191 @mock.patch('os.path.islink')
192 @mock.patch('os.path.realpath')
193 def test_connect_local_volume_dev_exist(self, mock_realpath, mock_islink,
194 mock_exists, mock_execute):
195 rbd_connector = rbd.RBDConnector(None, do_local_attach=True)
196 conn = {'name': 'pool/image',
197 'auth_username': 'fake_user',
198 'hosts': ['192.168.10.2'],
199 'ports': ['6789']}
200 mock_realpath.return_value = '/dev/rbd0'
201 mock_islink.return_value = True
202 mock_exists.return_value = True
203 device_info = rbd_connector.connect_volume(conn)
204 execute_call1 = mock.call('which', 'rbd')
205 cmd = ['rbd', 'map', 'image', '--pool', 'pool', '--id', 'fake_user',
206 '--mon_host', '192.168.10.2:6789']
207 execute_call2 = mock.call(*cmd, root_helper=None, run_as_root=True)
208 mock_execute.assert_has_calls([execute_call1])
209 self.assertFalse(execute_call2 in mock_execute.mock_calls)
210 expected_info = {'path': '/dev/rbd/pool/image',
211 'type': 'block'}
212 self.assertEqual(expected_info, device_info)
213
214 @mock.patch.object(priv_rootwrap, 'execute', return_value=None)
190 def test_connect_local_volume_without_mons(self, mock_execute): 215 def test_connect_local_volume_without_mons(self, mock_execute):
191 rbd_connector = rbd.RBDConnector(None, do_local_attach=True) 216 rbd_connector = rbd.RBDConnector(None, do_local_attach=True)
192 conn = {'name': 'pool/image', 217 conn = {'name': 'pool/image',