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

Module utils

Provides various utility functions.

This file is part of RTSLib Community Edition. Copyright (c) 2011 by RisingTide Systems LLC

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, version 3 (AGPLv3).

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

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]
 
flatten_nested_list(nested_list)
Function to flatten a nested list.
 
gen_list_item(nested_list)
The generator for flatten_nested_list().
 
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.
 
is_disk_partition(path)
Try to find out if path is a partition of a TYPE_DISK device.
 
get_disk_size(path)
This function returns the size in bytes of a disk-type block device, or None if path does not point to a disk- type device.
 
get_block_numbers(path)
This function returns a (major,minor) tuple for the block device found at path, or (None, None) if path is not a block device.
 
get_block_type(path)
This function returns a block device's type.
 
list_scsi_hbas()
This function returns the list of HBA indexes for existing SCSI HBAs.
 
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.
 
convert_human_to_bytes(hsize, kilo=1024)
This function converts human-readable amounts of bytes to bytes.
 
generate_wwn(wwn_type)
Generates a random WWN of the specified type:
 
is_valid_wwn(wwn_type, wwn, wwn_list=None)
Returns True if the wwn is a valid wwn of type wwn_type.
 
list_available_kernel_modules()
List all loadable kernel modules as registered by depmod
 
list_loaded_kernel_modules()
List all currently loaded kernel modules
 
modprobe(module)
Load the specified kernel module if needed.
 
exec_argv(argv, strip=True, shell=False)
Executes a command line given as an argv table and either:
 
list_eth_names(max_eth=1024)
List the max_eth first local ethernet interfaces names from SIOCGIFCONF struct.
 
list_eth_ips(ifnames=None)
List the IPv4 and IPv6 non-loopback, non link-local addresses (in the RFC3330 sense, not addresses attached to lo) of a list of ethernet interfaces from the SIOCGIFADDR struct.
 
is_ipv4_address(addr)
 
is_ipv6_address(addr)
 
get_main_ip()
Try to guess the local machine non-loopback IP.
 
_test()
Run the doctests
Variables [hide private]
  __package__ = 'rtslib'
Function Details [hide private]

flatten_nested_list(nested_list)

 

Function to flatten a nested list.

>>> import rtslib.utils as utils
>>> utils.flatten_nested_list([[1,2,3,[4,5,6]],[7,8],[[[9,10]],[11,]]])
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Parameters:
  • nested_list (list) - A nested list (list of lists of lists etc.)
Returns:
A list with only non-list elements

gen_list_item(nested_list)

 

The generator for flatten_nested_list(). It returns one by one items that are not a list, and recurses when he finds an item that is a list.

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

is_disk_partition(path)

 

Try to find out if path is a partition of a TYPE_DISK device. Handles both /dev/sdaX and /dev/disk/by-*/*-part? schemes.

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_block_type("/dev/sda")
0
>>> get_block_type("/dev/sr0")
5
>>> get_block_type("/dev/scd0")
5
>>> get_block_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.

convert_human_to_bytes(hsize, kilo=1024)

 

This function converts human-readable amounts of bytes to bytes. It understands the following units :

  • B or no unit present for Bytes
  • k, K, kB, KB for kB (kilobytes)
  • m, M, mB, MB for MB (megabytes)
  • g, G, gB, GB for GB (gigabytes)
  • t, T, tB, TB for TB (terabytes)

Note: The definition of kilo defaults to 1kB = 1024Bytes. Strictly speaking, those should not be called kB but kiB. You can override that with the optional kilo parameter.

Example:

>>> import rtslib.utils as utils
>>> utils.convert_human_to_bytes("1k")
1024
>>> utils.convert_human_to_bytes("1k", 1000)
1000
>>> utils.convert_human_to_bytes("1MB")
1048576
>>> utils.convert_human_to_bytes("12kB")
12288
Parameters:
  • hsize (string or int) - The human-readable version of the Bytes amount to convert
  • kilo (int) - Optionnal base for the kilo prefix
Returns:
An int representing the human-readable string converted to bytes

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.

is_valid_wwn(wwn_type, wwn, wwn_list=None)

 

Returns True if the wwn is a valid wwn of type wwn_type.

Parameters:
  • wwn_type (str) - The WWN address type.
  • wwn (str) - The WWN address to check.
  • wwn_list (list of str) - An optional list of wwns to check the wwn parameter from.
Returns:
bool.

modprobe(module)

 

Load the specified kernel module if needed.

Parameters:
  • module (str) - The name of the kernel module to be loaded.
Returns:
Whether of not we had to load the module.

exec_argv(argv, strip=True, shell=False)

 

Executes a command line given as an argv table and either:

  • raise an exception if return != 0
  • return the output

If strip is True, then output lines will be stripped. If shell is True, the argv must be a string that will be evaluated by the shell, instead of the argv list.

list_eth_ips(ifnames=None)

 

List the IPv4 and IPv6 non-loopback, non link-local addresses (in the RFC3330 sense, not addresses attached to lo) of a list of ethernet interfaces from the SIOCGIFADDR struct. If ifname is omitted, list all IPs of all ifaces excepted for lo.

get_main_ip()

 

Try to guess the local machine non-loopback IP. If available, local hostname resolution is used (if non-loopback), else try to find an other non-loopback IP on configured NICs. If no usable IP address is found, returns None.