Skip to content

Commit fec896b

Browse files
committed
ceph-volume: refactor device path handling for LVM lookups
This consolidates the conditional checks for device paths to reduce redundancy and improve readability and adds logic to handle both '/dev/mapper' and '/dev/dm-' paths uniformly by introducing a utility function `get_lvm_mapper_path_from_dm()`. Signed-off-by: Guillaume Abrioux <[email protected]>
1 parent 8883613 commit fec896b

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

src/ceph-volume/ceph_volume/tests/util/test_disk.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22
from ceph_volume.util import disk
3-
from mock.mock import patch, MagicMock
3+
from mock.mock import patch, Mock, MagicMock, mock_open
44

55

66
class TestFunctions:
@@ -38,6 +38,11 @@ def test_is_partition(self):
3838
assert disk.is_partition('sda1')
3939

4040

41+
@patch('os.path.exists', Mock(return_value=True))
42+
def test_get_lvm_mapper_path_from_dm(self):
43+
with patch('builtins.open', mock_open(read_data='test--foo--vg-test--foo--lv')):
44+
assert disk.get_lvm_mapper_path_from_dm('/dev/dm-123') == '/dev/mapper/test--foo--vg-test--foo--lv'
45+
4146
class TestLsblkParser(object):
4247

4348
def test_parses_whitespace_values(self):

src/ceph-volume/ceph_volume/util/arg_validators.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ def __call__(self, dev_path):
9292
super().get_device(dev_path)
9393
return self._format_device(self._is_valid_device())
9494

95+
def _format_device(self, device: Device) -> str:
96+
return device.path
97+
9598
def _is_valid_device(self, raise_sys_exit=True):
9699
out, err, rc = process.call([
97100
'ceph-bluestore-tool', 'show-label',

src/ceph-volume/ceph_volume/util/device.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,21 @@ def _parse(self):
211211
lv = _lv
212212
break
213213
else:
214+
filters = {}
214215
if self.path[0] == '/':
215-
lv = lvm.get_single_lv(filters={'lv_path': self.path})
216+
lv_mapper_path: str = self.path
217+
field: str = 'lv_path'
218+
219+
if self.path.startswith('/dev/mapper') or self.path.startswith('/dev/dm-'):
220+
path = os.path.realpath(self.path) if self.path.startswith('/dev/mapper') else self.path
221+
lv_mapper_path = disk.get_lvm_mapper_path_from_dm(path)
222+
field = 'lv_dm_path'
223+
224+
filters = {field: lv_mapper_path}
216225
else:
217226
vgname, lvname = self.path.split('/')
218-
lv = lvm.get_single_lv(filters={'lv_name': lvname,
219-
'vg_name': vgname})
227+
filters = {'lv_name': lvname, 'vg_name': vgname}
228+
lv = lvm.get_single_lv(filters=filters)
220229

221230
if lv:
222231
self.lv_api = lv

src/ceph-volume/ceph_volume/util/disk.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,3 +1104,28 @@ def get_parent_device_from_mapper(mapper: str, abspath: bool = True) -> str:
11041104
except KeyError:
11051105
pass
11061106
return result
1107+
1108+
1109+
def get_lvm_mapper_path_from_dm(path: str, sys_block: str = '/sys/block') -> str:
1110+
"""_summary_
1111+
Retrieve the logical volume path for a given device.
1112+
1113+
This function takes the path of a device and returns the corresponding
1114+
logical volume path by reading the 'dm/name' file within the sysfs
1115+
directory.
1116+
1117+
Args:
1118+
path (str): The device path for which to retrieve the logical volume path.
1119+
sys_block (str, optional): The base sysfs block directory. Defaults to '/sys/block'.
1120+
1121+
Returns:
1122+
str: The device mapper path in the form of '/dev/dm-X'.
1123+
"""
1124+
result: str = ''
1125+
dev: str = os.path.basename(path)
1126+
sys_block_path: str = os.path.join(sys_block, dev, 'dm/name')
1127+
if os.path.exists(sys_block_path):
1128+
with open(sys_block_path, 'r') as f:
1129+
content: str = f.read()
1130+
result = f'/dev/mapper/{content}'
1131+
return result

0 commit comments

Comments
 (0)