From 7374ba0e53d8e6af4abbb02bd60f35ed541b94f5 Mon Sep 17 00:00:00 2001 From: David Disseldorp Date: Tue, 10 Apr 2018 16:22:54 +0200 Patch-mainline: never (SUSE-specific) Subject: Split out blockdev readonly state detection helper So that it can be reused for RBD backstores. Note: not accepted upstream, but still needed here for our rbd stuff. (lduncan@suse.com) Signed-off-by: David Disseldorp --- targetcli/ui_backstore.py | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) --- a/targetcli/ui_backstore.py +++ b/targetcli/ui_backstore.py @@ -119,6 +119,25 @@ def complete_path(path, stat_fn): return sorted(filtered, key=lambda s: '~'+s if s.endswith('/') else s) +def blk_dev_ro_check(dev): + BLKROGET=0x0000125E + try: + f = os.open(dev, os.O_RDONLY) + except (OSError, IOError): + raise ExecutionError("Could not open %s" % dev) + # ioctl returns an int. Provision a buffer for it + buf = array.array('b', [0] * 4) + try: + fcntl.ioctl(f, BLKROGET, buf) + except (OSError, IOError): + os.close(f) + return False + + os.close(f) + if struct.unpack('I', buf)[0] == 0: + return False + return True + class UIALUATargetPortGroup(UIRTSLibNode): ''' @@ -536,25 +555,6 @@ class UIBlockBackstore(UIBackstore): self.so_cls = UIBlockStorageObject UIBackstore.__init__(self, 'block', parent) - def _ui_block_ro_check(self, dev): - BLKROGET=0x0000125E - try: - f = os.open(dev, os.O_RDONLY) - except (OSError, IOError): - raise ExecutionError("Could not open %s" % dev) - # ioctl returns an int. Provision a buffer for it - buf = array.array('b', [0] * 4) - try: - fcntl.ioctl(f, BLKROGET, buf) - except (OSError, IOError): - os.close(f) - return False - - os.close(f) - if struct.unpack('I', buf)[0] == 0: - return False - return True - def ui_command_create(self, name, dev, readonly=None, wwn=None): ''' Creates an Block Storage object. "dev" is the path to the TYPE_DISK @@ -565,7 +565,7 @@ class UIBlockBackstore(UIBackstore): ro_string = self.ui_eval_param(readonly, 'string', None) if ro_string == None: # attempt to detect block device readonly state via ioctl - readonly = self._ui_block_ro_check(dev) + readonly = blk_dev_ro_check(dev) else: readonly = self.ui_eval_param(readonly, 'bool', False)