summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy Grover <agrover@redhat.com>2016-08-12 10:19:14 -0700
committerAndy Grover <agrover@redhat.com>2016-08-12 10:19:14 -0700
commitba87a191edffe01235f86cfb145aa6834f5f0057 (patch)
treef702f9429098bf40d4c1b9adf7704990b793f9a5
parent8277306b4d30e93b6985302c25910550d4172fff (diff)
downloadrtslib-fb-ba87a191edffe01235f86cfb145aa6834f5f0057.tar.gz
Rework convert_scsi_path_to_hctl and convert_scsi_hctl_to_path
Do not check for type=disk, this is not a requirement. Raise an RTSLibError if not found, rather than returning None. Rework PSCSIStorageObject._configure to use the new semantics, and avoid else clauses in try blocks, since they are confusing and not strictly needed. Properly qualify DeviceNotFoundError as pyudev.DeviceNotFoundError. Signed-off-by: Andy Grover <agrover@redhat.com>
-rw-r--r--rtslib/tcm.py23
-rw-r--r--rtslib/utils.py31
2 files changed, 23 insertions, 31 deletions
diff --git a/rtslib/tcm.py b/rtslib/tcm.py
index bdeafd2..2ea5d0f 100644
--- a/rtslib/tcm.py
+++ b/rtslib/tcm.py
@@ -313,11 +313,14 @@ class PSCSIStorageObject(StorageObject):
def _configure(self, dev):
self._check_self()
- # Use H:C:T:L format or preserve the path given by the user.
+ # Use H:C:T:L format or use the path given by the user.
try:
+ # assume 'dev' is the path, try to get h:c:t:l values
(hostid, channelid, targetid, lunid) = \
convert_scsi_path_to_hctl(dev)
- except TypeError:
+ udev_path = dev.strip()
+ except:
+ # Oops, maybe 'dev' is in h:c:t:l format, try to get udev_path
try:
(hostid, channelid, targetid, lunid) = dev.split(':')
hostid = int(hostid)
@@ -329,15 +332,13 @@ class PSCSIStorageObject(StorageObject):
+ "path, and dev "
+ "parameter not in H:C:T:L "
+ "format: %s" % dev)
- else:
- udev_path = convert_scsi_hctl_to_path(hostid,
- channelid,
- targetid,
- lunid)
- if not udev_path:
- raise RTSLibError("SCSI device does not exist")
- else:
- udev_path = dev.strip()
+
+ udev_path = convert_scsi_hctl_to_path(hostid,
+ channelid,
+ targetid,
+ lunid)
+
+ # -- now have all 5 values or have errored out --
if is_dev_in_use(udev_path):
raise RTSLibError("Cannot configure StorageObject because "
diff --git a/rtslib/utils.py b/rtslib/utils.py
index c9f8213..969d2d7 100644
--- a/rtslib/utils.py
+++ b/rtslib/utils.py
@@ -246,26 +246,15 @@ def convert_scsi_path_to_hctl(path):
@param path: The udev path to the SCSI block device.
@type path: string
@return: An (host, controller, target, lun) tuple of integer
- values representing the SCSI ID of the device, or None if no
- match is found.
+ values representing the SCSI ID of the device, or raise RTSLibError.
'''
try:
path = os.path.realpath(path)
device = pyudev.Device.from_device_file(_CONTEXT, path)
- except (pyudev.DeviceNotFoundError, EnvironmentError, ValueError):
- return None
-
- if device.device_type != u'disk':
- return None
-
- parent = device.find_parent(subsystem='scsi')
- if parent is None:
- return None
-
- try:
+ parent = device.find_parent(subsystem='scsi')
return [int(data) for data in parent.sys_name.split(':')]
- except (ValueError, TypeError):
- return None
+ except:
+ raise RTSLibError("Could not convert scsi path to hctl")
def convert_scsi_hctl_to_path(host, controller, target, lun):
'''
@@ -288,7 +277,7 @@ def convert_scsi_hctl_to_path(host, controller, target, lun):
@type target: int
@param lun: The SCSI Logical Unit Number.
@type lun: int
- @return: A string for the canonical path to the device, or empty string.
+ @return: A string for the canonical path to the device, or raise RTSLibError.
'''
try:
host = int(host)
@@ -302,16 +291,18 @@ def convert_scsi_hctl_to_path(host, controller, target, lun):
hctl = [host, controller, target, lun]
try:
scsi_device = pyudev.Device.from_name(_CONTEXT, 'scsi', ':'.join(hctl))
- except DeviceNotFoundError:
- return ''
+ except pyudev.DeviceNotFoundError:
+ raise RTSLibError("Could not find path for SCSI hctl")
devices = _CONTEXT.list_devices(
subsystem='block',
- DEVTYPE='disk',
parent=scsi_device
)
- return next((dev.device_node for dev in devices), '')
+ path = next((dev.device_node for dev in devices), '')
+ if path == None:
+ raise RTSLibError("Could not find path for SCSI hctl")
+ return path
def generate_wwn(wwn_type):
'''