Mini Shell

Direktori : /lib/python3.6/site-packages/blivet/__pycache__/
Upload File :
Current File : //lib/python3.6/site-packages/blivet/__pycache__/zfcp.cpython-36.opt-1.pyc

3

{)c�Q�@sddlZddlZddlmZmZddlZddlmZddlmZddl	m
Z
ddlmZmZddl
Z
e
jdd	�dd
lmZddlZejd�Zdd
�ZdZdZdZdd�Zdd�Zdd�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd �d �Z e �Z!dS)!�N)�ABC�abstractmethod�)�udev)�util)�_)�	stringize�
unicodeize�BlockDevz2.0)r
ZblivetcCs2t|d�}tjd||�|jd|�|j�dS)N�wzecho %s > %sz%s
)�open�log�debug�write�close)�fn�value�f�r�/usr/lib/python3.6/zfcp.py�logged_write_line_to_file(s
rz/sys/bus/ccw/drivers/zfcpz/sys/bus/scsi/devicesz/etc/zfcp.confcCstjdd�}|dkS)z?Return True if automatic LUN scanning is enabled by the kernel.�zfcp�allow_lun_scan�Y)rZget_kernel_module_parameter)rrrr�_is_lun_scan_allowed4srcCs�d}dj|�}tj|�}y>x8|D]0}t|��}|j�}WdQRXtjd|�r d}q WWn8tk
r�}ztjd|t	|��d}WYdd}~XnX|S)z�Return True if the device ID is configured in NPIV mode. See
    https://www.ibm.com/docs/en/linux-on-systems?topic=devices-use-npiv
    Fz5/sys/bus/ccw/devices/{}/host*/fc_host/host*/port_typeNz(^|\s)NPIV(\s|$)Tz:Couldn't read the port_type attribute of the %s device: %s)
�format�globr�read�re�search�OSErrorr
�warning�str)�	device_idZport_in_npiv_modeZport_type_pathZport_type_paths�filenamerZ	port_type�errr�_is_port_in_npiv_mode;s



r&cCs0t�stjd�dSt|�s,tjd|�dSdS)z�Return True if the given zFCP device ID is configured in NPIV (N_Port ID Virtualization)
    mode and zFCP auto LUN scan is not disabled.

    :returns: True or False
    z=Automatic LUN scanning is disabled by the zfcp kernel module.Fz2The zFCP device %s is not configured in NPIV mode.T)rr
r!r&)r#rrr�has_auto_lun_scanPs
r'c@sdeZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
edd��Zdd�Z
dd�ZdS)�ZFCPDeviceBasez0An abstract base class for zFCP storage devices.cCs8tjj|�|_|js ttd���tjjt	|jd�|_
dS)Nz?You have not specified a device number or the number is invalidZonline)�blockdev�s390Zsanitize_dev_input�devnum�
ValueErrorr�os�path�join�	zfcpsysfs�_device_online_path)�selfr+rrr�__init__gszZFCPDeviceBase.__init__cCs
t|j�S)N)r"r+)r2rrr�
_to_stringoszZFCPDeviceBase._to_stringcCst|j��S)N)rr4)r2rrr�__str__rszZFCPDeviceBase.__str__cCst|j��S)N)r	r4)r2rrr�__unicode__uszZFCPDeviceBase.__unicode__cCsTtjj|j�s.tjd|j�tjdd|jg�tjj|j�sPt	t
d�|jf��dS)z�Remove the device from the I/O ignore list to make it visible to the system.

        :raises: ValueError if the device cannot be removed from the I/O ignore list
        zFreeing zFCP device %sZ
zfcp_cio_freez-dz9zFCP device %s not found, not even in device ignore list.N)r-r.�existsr1r
�infor+rZrun_programr,r)r2rrr�_free_devicexszZFCPDeviceBase._free_devicecCszy:t|j��}|j�j�}WdQRX|dkr8t|jd�Wn:tk
rt}zttd�|j|d���WYdd}~XnXdS)zdSet the zFCP device online.

        :raises: ValueError if the device cannot be set online
        N�1z4Could not set zFCP device %(devnum)s online (%(e)s).)r+r%)	rr1�readline�striprr r,rr+)r2rZ	devonliner%rrr�_set_zfcp_device_online�sz&ZFCPDeviceBase._set_zfcp_device_onlinecCsPyt|jd�Wn:tk
rJ}zttd�|j|d���WYdd}~XnXdS)zfSet the zFCP device offline.

        :raises: ValueError if the device cannot be set offline
        �0z5Could not set zFCP device %(devnum)s offline (%(e)s).)r+r%N)rr1r r,rr+)r2r%rrr�_set_zfcp_device_offline�s
z'ZFCPDeviceBase._set_zfcp_device_offlinecCsdS)z�Decide if the provided FCP addressing corresponds to the path stored in the zFCP device.

        :returns: True or False
        Nr)r2�fcphbasysfs�fcpwwpnsysfs�fcplunsysfsrrr�_is_associated_with_fcp�sz&ZFCPDeviceBase._is_associated_with_fcpcCs|j�|j�dS)z�Initialize the device and make its storage block device(s) ready to use.

        :returns: True if success
        :raises: ValueError if the device cannot be initialized
        T)r9r=)r2rrr�
online_device�szZFCPDeviceBase.online_devicec
Cs�dd�tjt�D�}d}x�|D]�}tjjt|�}ttjj|d���}|j�j�}WdQRXttjj|d���}|j�j�}WdQRXttjj|d���}|j�j�}WdQRX|j|||�rd}tjjt|d	�}	t	|	d
�t
j�qW|s�tj
d|�dS)zPFind SCSI devices associated to the zFCP device and remove them from the system.cSsg|]}tjd|�r|�qS)z^[0-9]+:[0-9]+:[0-9]+:[0-9]+$)rr)�.0rrrr�
<listcomp>�sz6ZFCPDeviceBase.offline_scsi_device.<locals>.<listcomp>FZhba_idN�wwpnZfcp_lunT�deleter:z*No scsi device found to delete for zfcp %s)r-�listdir�scsidevsysfsr.r/rr;r<rCrr�settler
r!)
r2Zscsi_devicesZscsi_device_foundZscsidevZfcpsysfsrr@rArBZscsidelrrr�offline_scsi_device�s"

z"ZFCPDeviceBase.offline_scsi_deviceN)�__name__�
__module__�__qualname__�__doc__r3r4r5r6r9r=r?rrCrDrLrrrrr(ds
r(csDeZdZdZ�fdd�Zdd�Zdd�Z�fdd	�Zd
d�Z�Z	S)�ZFCPDeviceFullPathz�A class for zFCP devices where zFCP auto LUN scan is not available. Such
    devices have to be specified by a device number, WWPN and LUN.
    csPt�j|�tjj|�|_|js,ttd���tjj|�|_	|j	sLttd���dS)NzDYou have not specified a worldwide port name or the name is invalid.z:You have not specified a FCP LUN or the number is invalid.)
�superr3r)r*Zzfcp_sanitize_wwpn_inputrGr,rZzfcp_sanitize_lun_input�fcplun)r2r+rGrS)�	__class__rrr3�szZFCPDeviceFullPath.__init__cCsdj|j|j|j�S)Nz{} {} {})rr+rGrS)r2rrrr4�szZFCPDeviceFullPath._to_stringcCs||jko||jko||jkS)z�Decide if the provided FCP addressing corresponds to the path stored in the zFCP device.

        :returns: True or False
        )r+rGrS)r2r@rArBrrrrC�s

z*ZFCPDeviceFullPath._is_associated_with_fcpc	0s>t�j�dt|jf}dt|j|jf}d|}d||jf}d|}t|j�r`tjd|j�t	j
j|�s�t	j
j|�r�yt||j�t
j�Wq�tk
r�}z"ttd�|j|j|d���WYd	d	}~Xq�Xnttd
�|j|jd���n$t	j
j|��rtjd|j|jd��t	j
j|��s�yt||j�t
j�WnDtk
�r|}z&ttd
�|j|j|j|d���WYd	d	}~XnXnttd�|j|j|jd���d}y"t|d�}|j�j�}|j�WnDtk
�r}z&ttd�|j|j|j|d���WYd	d	}~XnX|dk�r:|j�ttd�|j|j|jd���dS)z�Initialize the device and make its storage block device(s) ready to use.

        :returns: True if success
        :raises: ValueError if the device cannot be initialized
        z%s/%s/port_addz%s/%s/%sz%s/unit_addz%s/%sz	%s/failedz~zFCP device %s in NPIV mode brought online. All LUNs will be activated automatically although WWPN and LUN have been provided.z>Could not add WWPN %(wwpn)s to zFCP device %(devnum)s (%(e)s).)rGr+r%Nz2WWPN %(wwpn)s not found at zFCP device %(devnum)s.)rGr+z6WWPN %(wwpn)s at zFCP device %(devnum)s already there.zPCould not add LUN %(fcplun)s to WWPN %(wwpn)s on zFCP device %(devnum)s (%(e)s).)rSrGr+r%zMLUN %(fcplun)s at WWPN %(wwpn)s on zFCP device %(devnum)s already configured.)rSrGr+r>�rzeCould not read failed attribute of LUN %(fcplun)s at WWPN %(wwpn)s on zFCP device %(devnum)s (%(e)s).zOFailed LUN %(fcplun)s at WWPN %(wwpn)s on zFCP device %(devnum)s removed again.T)rRrDr0r+rGrSr'r
r!r-r.r7rrrKr r,rr8rr;r<r�offline_device)	r2�portadd�portdirZunitaddZunitdirZfailedr%Zfailr)rTrrrD�sp

 $

z ZFCPDeviceFullPath.online_devicec
/CsDdt|jf}dt|jf}dt|j|jf}dt|j|jf}dt|jf}y|j�WnBtk
r�}z&ttd�|j|j|j|d���WYdd}~XnXyt||j�WnBtk
r�}z&ttd	�|j|j|j|d
���WYdd}~XnXt	j
j|��r�xPt	j|�D]B}|j
d��rt	j
jt	j
j||���rtjd|j|j|�d
S�qWyt||j�Wn@tk
�r�}z"ttd�|j|j|d���WYdd}~XnXt	j
j|��rx�t	j|�D]>}|j
d��r�t	j
jt	j
j||���r�tjd|j|�d
S�q�Wn6tjd|f�}	t|	�dk�r8tjd|j|	d�d
S|j�d
S)z'Remove the zFCP device from the system.z%s/%s/port_addz%s/%s/port_removez%s/%s/%s/unit_removez%s/%s/%sz%s/%szVCould not correctly delete SCSI device of zFCP %(devnum)s %(wwpn)s %(fcplun)s (%(e)s).)r+rGrSr%NzSCould not remove LUN %(fcplun)s at WWPN %(wwpn)s on zFCP device %(devnum)s (%(e)s).)rSrGr+r%Z0xzPNot removing WWPN %s at zFCP device %s since port still has other LUNs, e.g. %s.TzACould not remove WWPN %(wwpn)s on zFCP device %(devnum)s (%(e)s).)rGr+r%zKNot setting zFCP device %s offline since it still has other ports, e.g. %s.z(%s/0x????????????????/0x????????????????rzJNot setting zFCP device %s offline since it still has other LUNs, e.g. %s.)r0r+rGrLr r,rrSrr-r.r7rI�
startswith�isdirr/r
r8r�lenr?)
r2rWZ
portremoveZ
unitremoverXZdevdirr%ZlunZportZlunsrrrrVGsZ""
"

z!ZFCPDeviceFullPath.offline_device)
rMrNrOrPr3r4rCrDrV�
__classcell__rr)rTrrQ�s
YrQc@seZdZdZdS)�
ZFCPDevicez�Class derived from ZFCPDeviceFullPath to reserve backward compatibility for applications
    using the ZFCPDevice class. ZFCPDeviceFullPath should be used instead in new code.
    N)rMrNrOrPrrrrr]�sr]cs0eZdZdZ�fdd�Zdd�Zdd�Z�ZS)�ZFCPDeviceAutoLunScanz�Class for zFCP devices configured in NPIV mode and zFCP auto LUN scan not disabled. Only
    a zFCP device number is needed for such devices.
    cs(t�j�t|j�s$ttd�|��dS)z�Initialize the device and make its storage block device(s) ready to use.

        :returns: True if success
        :raises: ValueError if the device cannot be initialized
        z(zFCP device %s cannot use auto LUN scan.T)rRrDr'r+r,r)r2)rTrrrD�s

z#ZFCPDeviceAutoLunScan.online_devicecCsRy|j�Wn8tk
rD}zttd�||d���WYdd}~XnX|j�dS)z�Remove the zFCP device from the system.

        :returns: True if success
        :raises: ValueError if the device cannot be brought offline
         zCCould not correctly delete SCSI device of zFCP %(zfcpdev)s (%(e)s).)Zzfcpdevr%NT)rLr r,rr?)r2r%rrrrV�s z$ZFCPDeviceAutoLunScan.offline_devicecCs
||jkS)zoDecide if the provided FCP addressing corresponds to the zFCP device.

        :returns: True or False
        )r+)r2r@Z
_fcpwwpnsysfsZ_fcplunsysfsrrrrC�sz-ZFCPDeviceAutoLunScan._is_associated_with_fcp)rMrNrOrPrDrVrCr\rr)rTrr^�sr^c@sReZdZdZdd�Zdd�Zdd�Zdd	�Zddd�Zd
d�Z	dd�Z
dd�Zd
S)�zFCPa� ZFCP utility class.

        This class will automatically online to ZFCP drives configured in
        /tmp/fcpconfig when the startup() method gets called. It can also be
        used to manually configure ZFCP devices through the add_fcp() method.

        As this class needs to make sure that /tmp/fcpconfig configured
        drives are only onlined once and as it keeps a global list of all ZFCP
        devices it is implemented as a Singleton.
    cCst�|_d|_d|_dS)NFT)�set�fcpdevs�has_read_config�down)r2rrrr3�sz
zFCP.__init__cCs|S)Nr)r2rrr�__call__�sz
zFCP.__call__cCs|S)Nr)r2Z	memo_dictrrr�__deepcopy__�szzFCP.__deepcopy__c	Cs:yttd�}Wn tk
r.tjdt�dSXdd�|j�D�}|j�x�|D]�}|jd�sP|dkrhqP|j�}t	|�dkr�|d}d}d}n\t	|�d	kr�|d}|d}|d
}n6t	|�dkr�|d}|d
}|d}ntj
d
t|�qPy|j|||�WqPtk
�r0}ztj
dt
|��WYdd}~XqPXqPWdS)NrUzno %s; not configuring zfcpcSsg|]}|j�j��qSr)r<�lower)rE�xrrrrF�sz$zFCP.read_config.<locals>.<listcomp>�#�rr����zInvalid line found in %s: %sz%s)r�zfcpconfr r
r8�	readlinesrrY�splitr[r!�add_fcpr,r")	r2r�lines�lineZfieldsr+rGrSr%rrr�read_config�s:


zzFCP.read_configNcCs6|r|rt|||�}nt|�}|j�r2|jj|�dS)N)rQr^rDra�add)r2r+rGrS�drrrrqs
zzFCP.add_fcpcCsv|jr
dSd|_t|j�dkr"dSxN|jD]D}y|j�Wq*tk
rl}ztjdt|��WYdd}~Xq*Xq*WdS)NTrz%s)rcr[rarVr,r
r!r")r2rvr%rrr�shutdownsz
zFCP.shutdowncCs�|js
dSd|_|js(|j�d|_dSt|j�dkr:dSxN|jD]D}y|j�WqBtk
r�}ztjdt	|��WYdd}~XqBXqBWdS)NFTrz%s)
rcrbrtr[rarDr,r
r!r")r2rvr%rrr�startupszzFCP.startupcCslt|j�dkrdSt|td�}x|jD]}|jd|f�q(W|j�t|dd�}|jd�|j�dS)Nrrz%s
z/etc/modprobe.conf�azalias scsi_hostadapter zfcp
)r[rarrnrr)r2�rootrrvrrrr*s
z
zFCP.write)NN)rMrNrOrPr3rdrertrqrwrxrrrrrr_�s(
	r_)"r-r�abcrrrrirrZi18nrrr	ZgiZrequire_versionZ
gi.repositoryr
r)ZloggingZ	getLoggerr
rr0rJrnrr&r'r(rQr]r^r_rrrrr�<module>s4
l>.w