�
/�P`c@s�ddlZddlZddlZddlZddlmZmZmZmZd�Z d�Z
d�Zd�Zd�Z
d�Zd �Zed
kr�e�ndS(i����N(tClienttLsmErrortErrorNumbert LocalDiskcCs$tjj|�tjjd�dS(Ns
(tsyststderrtwrite(tmsg((s%/usr/libexec/lsm.d/find_unused_lun.pytprint_stderrscCs�y*tj|tj�}tj|�tSWnStk
r}|jtjkrRtS|jtj kryt
tjd��q��nXdS(s
The manpage of open(2):
There is one exception: on Linux 2.6 and later, O_EXCL can be used
without O_CREAT if pathname refers to a block device. If the block
device is in use by the system (e.g., mounted), open() fails with the
error EBUSY.
Even when disk has partitions, when all partitions are not used, this
function still treats it as free disk.
When specific disk or its partition is used by LVM, if there is no data
stored in it, this function still treats it as free disk.
s(Permission deny, try sudo or run as rootN(
tostopentO_EXCLtclosetTruetOSErrorterrnotEBUSYtFalsetEACCESRRtPERMISSION_DENIED(tblk_pathtfdterr((s%/usr/libexec/lsm.d/find_unused_lun.pytis_free_disk s
cCs�|td�}d|}ytj|�}Wntk
rAdSXt|�dkrXdS|d}d|}d|}t|�� }|j�jd�s�dSWdQXt|��}|j�j�SWdQXdS( s�
Use `/sys/block/sdX/holders/dm-0/dm/name` to get mpath name,
use `/sys/block/sdf/holders/dm-0/dm/uuid` to check whether holder is
multipath.
s/dev/s/sys/block/%s/holdersiis/sys/block/%s/dm/names/sys/block/%s/dm/uuidsmpath-N( tlenR tlistdirtFileNotFoundErrortNoneR
treadt
startswithtstrip(Rtblk_nametsysfs_holder_foldertholderstholder_nametsysfs_dm_name_pathtsysfs_dm_uuid_pathtf((s%/usr/libexec/lsm.d/find_unused_lun.pyt get_mpath:s
cCs$t|�}|r td|�StS(Ns/dev/mapper/%s(R&RR(Rt
mpath_name((s%/usr/libexec/lsm.d/find_unused_lun.pytis_free_multipathSscCs�|j�}i}i}x'|D]}|jr|||j<qqWx�tj�D]�}t|�smt|�rOytj|�}Wn.tk
r�}|jt j
kr�qOn�nX||kr�td|�qOn||||<qOqOW|j�S(s>
Return a list of lsm.Volume objects.
Unused here means the LUN/volume meets one of following conditions:
* the block device representing LUN/Volume could be opened with O_EXCL,
which means its block is completely free.
* The block device is used as PV of LVM, but not been used.
s<Found free disk %s, but not managed by libstoragemgmt plugin(
tvolumestvpd83RtlistRR(t vpd83_getRtcodeRt
NO_SUPPORTRtvalues(tctvolstvpd83_to_vol_hasht
free_vol_dicttvolt disk_pathR*tlsm_err((s%/usr/libexec/lsm.d/find_unused_lun.pytfind_unused_lunZs(
cCs�i|jd6|jd6|jd6|jd6||jjd6}tj|j�}|r�||d<t|d�}|r�d||d <q�n|S(
Ntidtnametwwidt system_idtsystem_namet blk_pathsis/dev/mapper/%st mpath_blk(R8R9R*R;Rtvpd83_searchR&(R4tsys_dicttdR=R'((s%/usr/libexec/lsm.d/find_unused_lun.pyt
format_vol{s
cCs�tjd�}tjd�}|dkr=td�d}nt||�}t|�}|srtd�t�n|j�}i}x|D]}|||j<q�Wtdt |��x+|D]#}t
jt||�dd�GHq�WdS( Nt
LSMCLI_URItLSMCLI_PASSWORDs4LSMCLI_URI environment not defined, using 'local://'slocal://sDidn't find any free LUNs
Found %d free LUN(s):
tindenti(
R tgetenvRRRR7texittsystemsR8RtjsontdumpsRB(turitpasswordR0t free_volstsyssR@tlsm_sysR4((s%/usr/libexec/lsm.d/find_unused_lun.pytmain�s"
t__main__(R RRIRtlsmRRRRRRR&R(R7RBRPt__name__(((s%/usr/libexec/lsm.d/find_unused_lun.pyt<module>s" ! |