summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormulhern <amulhern@redhat.com>2016-01-11 10:09:41 -0500
committermulhern <amulhern@redhat.com>2016-01-11 13:15:52 -0500
commit2393c1fd4fdda71c2b594212a6f5471852536e3d (patch)
treef2e4387243b6c2c652a308d3080f202093aae192
parent598af739d2918412aa51b3b635f5d1e90c805350 (diff)
downloadrtslib-fb-2393c1fd4fdda71c2b594212a6f5471852536e3d.tar.gz
Make sure to properly avoid treating partitions the same as disk.
Return None if device path is a partition in convert_scsi_path_to_hctl. In convert_scsi_hctl_to_path ensure that device is not a partition in the most correct and efficient way. Get exactly the set of devices that are descendants of the scsi device and are block devices and have type disk. This should be a singleton, or the empty set. Signed-off-by: mulhern <amulhern@redhat.com>
-rw-r--r--rtslib/utils.py43
1 files changed, 21 insertions, 22 deletions
diff --git a/rtslib/utils.py b/rtslib/utils.py
index a763bcc..acf8aa5 100644
--- a/rtslib/utils.py
+++ b/rtslib/utils.py
@@ -224,22 +224,6 @@ def get_blockdev_type(path):
get_block_type = get_blockdev_type
-def _hctl_from_dev(device):
- '''
- @param device: the device
- @type device: pyudev.Device
- @returns: H:C:T:L specifier or None if not found
- @rtype: list of int * 4 or NoneType
- '''
- parent = device.find_parent(subsystem='scsi')
- if parent is None:
- return None
-
- try:
- return [int(data) for data in parent.sys_name.split(':')]
- except (ValueError, TypeError):
- return None
-
def convert_scsi_path_to_hctl(path):
'''
This function returns the SCSI ID in H:C:T:L form for the block
@@ -266,10 +250,22 @@ def convert_scsi_path_to_hctl(path):
match is found.
'''
try:
- device = pyudev.Device.from_device_file(context, os.path.realpath(path))
+ path = os.path.realpath(path)
+ device = pyudev.Device.from_device_file(_CONTEXT, path)
except (pyudev.DeviceNotFoundError, EnvironmentError, ValueError):
return None
- return _hctl_from_dev(device)
+
+ if device.device_type != u'disk':
+ return None
+
+ parent = device.find_parent(subsystem='scsi')
+ if parent is None:
+ return None
+
+ try:
+ return [int(data) for data in parent.sys_name.split(':')]
+ except (ValueError, TypeError):
+ return None
def convert_scsi_hctl_to_path(host, controller, target, lun):
'''
@@ -309,10 +305,13 @@ def convert_scsi_hctl_to_path(host, controller, target, lun):
except DeviceNotFoundError:
return ''
- for device in context.list_devices(subsystem='block'):
- if device.parent == scsi_device:
- return device.device_node
- return ''
+ devices = _CONTEXT.list_devices(
+ subsystem='block',
+ DEVTYPE='disk',
+ parent=scsi_device
+ )
+
+ return next((dev.device_node for dev in devices), '')
def generate_wwn(wwn_type):
'''