Package rtslib :: Module utils

[hide private]
[frames] | no frames]

# Module utils

    Provides various utility functions.

    This file is part of RTSLib.
    Copyright (c) 2011-2013 by Datera, Inc
    Copyright (c) 2011-2014 by Red Hat, 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. |

fread(path) This function reads the contents of a file. |

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. |

get_size_for_blk_dev(path) Returns: The size in logical blocks of the device |

get_block_size(path) Returns: The size in logical blocks of the device |

get_size_for_disk_name(name) Returns: The size in logical blocks of a disk-type block device. |

get_blockdev_type(path) This function returns a block device's type. |

get_block_type(path) This function returns a block device's type. |

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. |

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. |

generate_wwn(wwn_type) Generates a random WWN of the specified type: |

colonize(str) helper function to add colons every 2 chars |

_cleanse_wwn(wwn_type, wwn) Some wwns may have alternate text representations. |

normalize_wwn(wwn_types, wwn) Take a WWN as given by the user and convert it to a standard text representation. |

list_loaded_kernel_modules() List all currently loaded kernel modules |

modprobe(module) Load the specified kernel module if needed. |

mount_configfs() |

dict_remove(d, items) |

ignored(*args, **kwds) |

_get_auth_attr(self, attribute, ignore=False) |

_set_auth_attr(self, value, attribute, ignore=False) |

set_attributes(obj, attr_dict, err_func) |

set_parameters(obj, param_dict, err_func) |

_test() Run the doctests |

Variables | [hide private]

| __package__ = ``'``rtslib``'``

Function Details | [hide private]

### fwrite(path, string)

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)

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)

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_size_for_blk_dev(path)

Parameters:
  - `path` (string) - The path to a block device
Returns:
  The size in logical blocks of the device

### get_block_size(path)

Parameters:
  - `path` (string) - The path to a block device
Returns:
  The size in logical blocks of the device

### get_size_for_disk_name(name)

Parameters:
  - `name` (string) - a kernel disk name, as found in /proc/partitions
Returns:
  The size in logical blocks of a disk-type block device.

### get_blockdev_type(path)

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)

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)

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)

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)

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)

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

### normalize_wwn(wwn_types, wwn)

Take a WWN as given by the user and convert it to a standard text representation.

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

### modprobe(module)

Load the specified kernel module if needed.

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

### ignored(*args, **kwds)

Decorators:
  - `@contextmanager`