diff options
author | Andy Grover <agrover@redhat.com> | 2015-04-28 12:46:34 -0700 |
---|---|---|
committer | Andy Grover <agrover@redhat.com> | 2015-04-28 12:46:34 -0700 |
commit | c369cacf781b9a6713d94ce6f257601b686fb998 (patch) | |
tree | acbf1acf056972441792ba4978dbeb0c72176d49 /rtslib/utils.py | |
parent | a5b4ac3b081393a1229f6f31ce6c082beb0c2f11 (diff) | |
download | rtslib-fb-c369cacf781b9a6713d94ce6f257601b686fb998.tar.gz |
Fix disk size code for disk name vs blkdev path differences
The existence of the cciss driver (see
https://bugzilla.redhat.com/show_bug.cgi?id=1215360 ) shows an error
introduced when we started looking up disk names for iblock devices instead
of udev_path. We need to replace '/' with '!' when looking up disk names
in /sys/block, but since get_blockdev_size handled both disk names and
paths to block devices, this would break looking up block devices.
Break the function into two parts: get_size_for_blk_dev looks up the disk
name for a block device by matching device numbers with what's in
/proc/partitions; and get_size_for_disk_name, which looks in /sys/block for
the given disk name and gets the size. The first calls the second.
Change tcm.py to use the now-split functions.
Signed-off-by: Andy Grover <agrover@redhat.com>
Diffstat (limited to 'rtslib/utils.py')
-rw-r--r-- | rtslib/utils.py | 33 |
1 files changed, 27 insertions, 6 deletions
diff --git a/rtslib/utils.py b/rtslib/utils.py index 713881e..5c97939 100644 --- a/rtslib/utils.py +++ b/rtslib/utils.py @@ -110,11 +110,30 @@ def is_dev_in_use(path): os.close(file_fd) return False -def get_blockdev_size(path): +def get_size_for_blk_dev(path): ''' - Returns the size in logical blocks of a disk-type block device. + @param path: The path to a block device + @type path: string + @return: The size in logical blocks of the device + ''' + rdev = os.lstat(path).st_rdev + maj, min = os.major(rdev), os.minor(rdev) + + for line in list(open("/proc/partitions"))[2:]: + xmaj, xmin, size, name = line.split() + if (maj, min) == (int(xmaj), int(xmin)): + return get_size_for_disk_name(name) + else: + return 0 + +get_block_size = get_size_for_blk_dev + +def get_size_for_disk_name(name): + ''' + @param name: a kernel disk name, as found in /proc/partitions + @type name: string + @return: The size in logical blocks of a disk-type block device. ''' - name = os.path.basename(os.path.realpath(path)) # size is in 512-byte sectors, we want to return number of logical blocks def get_size(path, is_partition=False): @@ -124,6 +143,10 @@ def get_blockdev_size(path): logical_block_size = int(fread("%s/queue/logical_block_size" % path)) return sect_size / (logical_block_size / 512) + # Disk names can include '/' (e.g. 'cciss/c0d0') but these are changed to + # '!' when listed in /sys/block. + name = name.replace("/", "!") + try: return get_size("/sys/block/%s" % name) except IOError: @@ -139,8 +162,6 @@ def get_blockdev_size(path): else: raise -get_block_size = get_blockdev_size - def get_blockdev_type(path): ''' This function returns a block device's type. @@ -212,7 +233,7 @@ def convert_scsi_path_to_hctl(path): % devname)[0].split(':') except: return None - + return [int(data) for data in hctl] def convert_scsi_hctl_to_path(host, controller, target, lun): |