Package rtslib :: Module utils
[hide private]
[frames] | no frames]

Module utils

source code


Provides various utility functions.

This file is part of RTSLib.
Copyright (c) 2011-2013 by Datera, Inc

Licensed under the Apache License, Version 2.0 (the "License"); you may
not use this file except in compliance with the License. You may obtain
a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations
under the License.

Classes [hide private]
  RTSLibError
Generic rtslib error.
  RTSLibBrokenLink
Broken link in configfs, i.e.
  RTSLibNotInCFS
The underlying configfs object does not exist.
Functions [hide private]
 
fwrite(path, string)
This function writes a string to a file, and takes care of opening it and closing it.
source code
 
fread(path)
This function reads the contents of a file.
source code
 
is_dev_in_use(path)
This function will check if the device or file referenced by path is already mounted or used as a storage object backend.
source code
 
get_blockdev_size(path)
Returns the size in logical blocks of a disk-type block device.
source code
 
get_block_size(path)
Returns the size in logical blocks of a disk-type block device.
source code
 
get_blockdev_type(path)
This function returns a block device's type.
source code
 
get_block_type(path)
This function returns a block device's type.
source code
 
convert_scsi_path_to_hctl(path)
This function returns the SCSI ID in H:C:T:L form for the block device being mapped to the udev path specified.
source code
 
convert_scsi_hctl_to_path(host, controller, target, lun)
This function returns a udev path pointing to the block device being mapped to the SCSI device that has the provided H:C:T:L.
source code
 
generate_wwn(wwn_type)
Generates a random WWN of the specified type:
source code
 
colonize(str)
helper function to add colons every 2 chars
source code
 
_cleanse_wwn(wwn_type, wwn)
Some wwns may have alternate text representations.
source code
 
normalize_wwn(wwn_types, wwn)
Take a WWN as given by the user and convert it to a standard text representation.
source code
 
list_loaded_kernel_modules()
List all currently loaded kernel modules
source code
 
modprobe(module)
Load the specified kernel module if needed.
source code
 
mount_configfs() source code
 
dict_remove(d, items) source code
 
ignored(*args, **kwds) source code
 
_get_auth_attr(self, attribute, ignore=False) source code
 
_set_auth_attr(self, value, attribute, ignore=False) source code
 
set_attributes(obj, attr_dict, err_func) source code
 
set_parameters(obj, param_dict, err_func) source code
 
_test()
Run the doctests
source code
Variables [hide private]
  __package__ = 'rtslib'
Function Details [hide private]

fwrite(path, string)

source code 

This function writes a string to a file, and takes care of opening it and closing it. If the file does not exist, it will be created.

>>> from rtslib.utils import *
>>> fwrite("/tmp/test", "hello")
>>> fread("/tmp/test")
'hello'
Parameters:
  • path (string) - The file to write to.
  • string (string) - The string to write to the file.

fread(path)

source code 

This function reads the contents of a file. It takes care of opening and closing it.

>>> from rtslib.utils import *
>>> fwrite("/tmp/test", "hello")
>>> fread("/tmp/test")
'hello'
>>> fread("/tmp/notexistingfile") # doctest: +ELLIPSIS
Traceback (most recent call last):
    ...
IOError: [Errno 2] No such file or directory: '/tmp/notexistingfile'
Parameters:
  • path (string) - The path to the file to read from.
Returns:
A string containing the file's contents.

is_dev_in_use(path)

source code 

This function will check if the device or file referenced by path is already mounted or used as a storage object backend. It works by trying to open the path with O_EXCL flag, which will fail if someone else already did. Note that the file is closed before the function returns, so this does not guaranteed the device will still be available after the check.

Parameters:
  • path (string) - path to the file of device to check
Returns:
A boolean, True is we cannot get exclusive descriptor on the path, False if we can.

get_blockdev_type(path)

source code 

This function returns a block device's type. Example: 0 is TYPE_DISK If no match is found, None is returned.

>>> from rtslib.utils import *
>>> get_blockdev_type("/dev/sda")
0
>>> get_blockdev_type("/dev/sr0")
5
>>> get_blockdev_type("/dev/scd0")
5
>>> get_blockdev_type("/dev/nodevicehere") is None
True
Parameters:
  • path (string) - path to the block device
Returns:
An int for the block device type, or None if not a block device.

get_block_type(path)

source code 

This function returns a block device's type. Example: 0 is TYPE_DISK If no match is found, None is returned.

>>> from rtslib.utils import *
>>> get_blockdev_type("/dev/sda")
0
>>> get_blockdev_type("/dev/sr0")
5
>>> get_blockdev_type("/dev/scd0")
5
>>> get_blockdev_type("/dev/nodevicehere") is None
True
Parameters:
  • path (string) - path to the block device
Returns:
An int for the block device type, or None if not a block device.

convert_scsi_path_to_hctl(path)

source code 

This function returns the SCSI ID in H:C:T:L form for the block device being mapped to the udev path specified. If no match is found, None is returned.

>>> import rtslib.utils as utils
>>> utils.convert_scsi_path_to_hctl('/dev/scd0')
(2, 0, 0, 0)
>>> utils.convert_scsi_path_to_hctl('/dev/sr0')
(2, 0, 0, 0)
>>> utils.convert_scsi_path_to_hctl('/dev/sda')
(3, 0, 0, 0)
>>> utils.convert_scsi_path_to_hctl('/dev/sda1')
>>> utils.convert_scsi_path_to_hctl('/dev/sdb')
(3, 0, 1, 0)
>>> utils.convert_scsi_path_to_hctl('/dev/sdc')
(3, 0, 2, 0)
Parameters:
  • path (string) - The udev path to the SCSI block device.
Returns:
An (host, controller, target, lun) tuple of integer values representing the SCSI ID of the device, or None if no match is found.

convert_scsi_hctl_to_path(host, controller, target, lun)

source code 

This function returns a udev path pointing to the block device being mapped to the SCSI device that has the provided H:C:T:L.

>>> import rtslib.utils as utils
>>> utils.convert_scsi_hctl_to_path(0,0,0,0)
''
>>> utils.convert_scsi_hctl_to_path(2,0,0,0) # doctest: +ELLIPSIS
'/dev/s...0'
>>> utils.convert_scsi_hctl_to_path(3,0,2,0)
'/dev/sdc'
Parameters:
  • host (int) - The SCSI host id.
  • controller (int) - The SCSI controller id.
  • target (int) - The SCSI target id.
  • lun (int) - The SCSI Logical Unit Number.
Returns:
A string for the canonical path to the device, or empty string.

generate_wwn(wwn_type)

source code 

Generates a random WWN of the specified type:

  • unit_serial: T10 WWN Unit Serial.
  • iqn: iSCSI IQN
  • naa: SAS NAA address
Parameters:
  • wwn_type (str) - The WWN address type.
Returns:
A string containing the WWN.

_cleanse_wwn(wwn_type, wwn)

source code 

Some wwns may have alternate text representations. Adjust to our preferred representation.

normalize_wwn(wwn_types, wwn)

source code 

Take a WWN as given by the user and convert it to a standard text representation. If possible_wwns is not None, verify that the given WWN is on that list.

Returns (normalized_wwn, wwn_type), or exception if invalid wwn.

modprobe(module)

source code 

Load the specified kernel module if needed.

Parameters:
  • module (str) - The name of the kernel module to be loaded.

ignored(*args, **kwds)

source code 
Decorators:
  • @contextmanager