%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /lib/python2.7/site-packages/salt/modules/
Upload File :
Create Path :
Current File : //lib/python2.7/site-packages/salt/modules/bcache.pyc

�
���^c@@sydZddlmZmZmZddlZddlZddlZddlZddl	m
Z
ddlZej
e�Ziejd6ejd6ejd6ejd6ejd	6ejd
6Zidd6d
d6dd6Zejjjd�dk	Zd�Zdd�Zdd�Zdd�Z d�Z!dd�Z"de#e$dd�Z%de#dde$d�Z&dd�Z'e#e#e#e#e#d�Z(e#e#e#e#d�Z)d�Z*d�Z+d �Z,d!�Z-dd"�Z.d#�Z/d$�Z0dddd%�Z1dddd&�Z2dddd'�Z3de#e#e#e#d(�Z4d)�Z5d*�Z6d+�Z7ddd,d-�Z8dddd.�Z9d/�Z:dS(0u�
Module for managing BCache sets

BCache is a block-level caching mechanism similar to ZFS L2ARC/ZIL, dm-cache and fscache.
It works by formatting one block device as a cache set, then adding backend devices
(which need to be formatted as such) to the set and activating them.

It's available in Linux mainline kernel since 3.10

https://www.kernel.org/doc/Documentation/bcache.txt

This module needs the bcache userspace tools to function.

.. versionadded: 2016.3.0

i(tabsolute_importtprint_functiontunicode_literalsN(tsixutraceudebuguinfouwarnuerrorucrituattachuattach_uconfiguconfig_usuperusuper_u
blkdiscardcC@stjjjd�dk	S(u1
    Only work when make-bcache is installed
    umake-bcacheN(tsalttutilstpathtwhichtNone(((s7/usr/lib/python2.7/site-packages/salt/modules/bcache.pyt__virtual__2scC@sgyN|dkr4ttjjjd��dddStjjt|d��SWnt	k
rbt
SXdS(u
    Return the bcache UUID of a block device.
    If no device is given, the Cache UUID is returned.

    CLI example:

    .. code-block:: bash

        salt '*' bcache.uuid
        salt '*' bcache.uuid /dev/sda
        salt '*' bcache.uuid bcache0

    u/sys/fs/bcache/iiucacheN(RtlistRRRtos_walktostbasenamet_bcsyst	ExceptiontFalse(tdev((s7/usr/lib/python2.7/site-packages/salt/modules/bcache.pytuuid9s%
c@s<t���s#tjd��tS�dkr�i}xBtdt�j�D]+\�}d|krKt��|�<qKqKWt	|�r�|SdSnt��}|r�|�kr�tj
d���dSt��s�tSntjd���t
�d�ddj����stSt��fd	�dd
j����S(u
    Attach a backing devices to a cache set
    If no dev is given, all backing devices will be attached.

    CLI example:

    .. code-block:: bash

        salt '*' bcache.attach sdc
        salt '*' bcache.attach /dev/bcache1


    :return: bool or None if nuttin' happened
    uNo cache to attach %s totalldevsucacheu2%s is already attached to bcache %s, doing nothinguAttaching %s to bcache %suattachuerroru!Error attaching {0} to bcache {1}c@st���kS(N(R((tcacheR(s7/usr/lib/python2.7/site-packages/salt/modules/bcache.pyt<lambda>su5{0} received attach to bcache {1}, but did not complyN(RtlogterrorRRtstatustTruetitemstattach_tlentinfotdetachtdebugRtformatt_wait(Rtrestdatatbcache((RRs7/usr/lib/python2.7/site-packages/salt/modules/bcache.pyRRs2	"c@s��dkrni}xBtdt�j�D]+\�}d|kr(t��|�<q(q(Wt|�rg|SdSntjd��t�ddddj	���s�t
St�fd�dd	j	��d
�S(u�
    Detach a backing device(s) from a cache set
    If no dev is given, all backing devices will be attached.

    Detaching a backing device will flush it's write cache.
    This should leave the underlying device in a consistent state, but might take a while.

    CLI example:

    .. code-block:: bash

        salt '*' bcache.detach sdc
        salt '*' bcache.detach bcache1

    RucacheuDetaching %sudetachugoawayuerroruError detaching {0}c@st��tkS(N(RR((R(s7/usr/lib/python2.7/site-packages/salt/modules/bcache.pyR�su'{0} received detach, but did not complyi,N(RRRRRRRRRR RR!(RR"R#((Rs7/usr/lib/python2.7/site-packages/salt/modules/bcache.pyR�s"!cC@s3tddd�stStd�dd�s/tStS(u�
    Trigger a start of the full bcache system through udev.

    CLI example:

    .. code-block:: bash

        salt '*' bcache.start

    uudevadm triggeruerroruError starting bcache: %scS@s
t�tk	S(N(RR(((s7/usr/lib/python2.7/site-packages/salt/modules/bcache.pyR�suwarnu5Bcache system started, but no active cache set found.(t_run_allRR!R(((s7/usr/lib/python2.7/site-packages/salt/modules/bcache.pytstart�s
c@s��d
k	rftjd��t�ddddj���sAtSt�fd�ddj��d�St�}|s�tjd	�d
Stt	��s�tSt
dddd
�s�tStd�ddd�Sd
S(u�
    Stop a bcache device
    If no device is given, all backing devices will be detached from the cache, which will subsequently be stopped.

    .. warning::
        'Stop' on an individual backing device means hard-stop;
        no attempt at flushing will be done and the bcache device will seemingly 'disappear' from the device lists

    CLI example:

    .. code-block:: bash

        salt '*' bcache.stop

    u;Stopping %s, device will only reappear after reregistering!ustopugoawayuerroruError stopping {0}c@stt���tkS(N(t_sysfs_attrt_bcpathR((R(s7/usr/lib/python2.7/site-packages/salt/modules/bcache.pyR�suDevice {0} did not stopi,ubcache already stopped?uError stopping cachecS@s
t�tkS(N(RR(((s7/usr/lib/python2.7/site-packages/salt/modules/bcache.pyR�suCache did not stopN(RRtwarningRR RR!Rt_alltrueRt_fssys(RR((Rs7/usr/lib/python2.7/site-packages/salt/modules/bcache.pytstop�s!%	
u	writebackc@sit�}|s tjd�tStt���r||sLtjd��tSt��rit��ritSt��s|tSnt���t	t
d��}|d
kr�t	t
d��}ndj|||��}|r�|d7}nt
|ddj���stStd	t��dd
j���s-tSt�fd�ddj���sUtS|ret��StS(u�
    Create a backing device for attachment to a set.
    Because the block size must be the same, a cache set already needs to exist.

    CLI example:

    .. code-block:: bash

        salt '*' bcache.back_make sdc cache_mode=writeback attach=True


    :param cache_mode: writethrough, writeback, writearound or none.
    :param force: Overwrite existing bcaches
    :param attach: Immediately attach the backing device to the set
    :param bucket_size: Size of a bucket (see kernel doc)
    uNo bcache set foundu;%s already contains a bcache. Wipe it manually or use forceu
block_sizeubucket_sizeu5make-bcache --block {0} --bucket {1} --{2} --bdev {3}u --wipe-bcacheuerroru%Error creating backing device {0}: %sufs/bcache/registeru$Error registering backing device {0}c@stt���tk	S(N(R'R(R((R(s7/usr/lib/python2.7/site-packages/salt/modules/bcache.pyRsu#Backing device {0} did not registerN(RRRRR'R(RR,t_devpatht	_size_mapR+RR R%R!RR(Rt
cache_modetforcetattachtbucket_sizeRt
block_sizetcmd((Rs7/usr/lib/python2.7/site-packages/salt/modules/bcache.pyt	back_make�s<	


c	C@st�}|r5|s)tjd|�tSt�}nt|�}td|�}d|ks�|jdd�dkr�d|kr�|r�tjd|�tS|dk	r�|jdd�dkr�tjd	�tSt	|�\}}	}
|dkr|
}t}n|dkr|	}n|r<t
�s)tSt|�s<tSnt|�sLtS|r�d
j||�}t
|ddj|��s�tSd
j|�}ndj||�}|r�|dj|�7}nt
|ddj|��s�tStd�ddj|��stS|rtt��StSdS(u�
    Create BCache cache on a block device.
    If blkdiscard is available the entire device will be properly cleared in advance.

    CLI example:

    .. code-block:: bash

        salt '*' bcache.cache_make sdb reserved=10% block_size=4096


    :param reserved: if dev is a full device, create a partition table with this size empty.

        .. note::
              this increases the amount of reserved space available to SSD garbage collectors,
              potentially (vastly) increasing performance
    :param block_size: Block size of the cache; defaults to devices' logical block size
    :param force: Overwrite existing BCache sets
    :param attach: Attach all existing backend devices immediately
    u(BCache cache %s is already on the systemuudev.envu
ID_FS_TYPEuDEVTYPEu	partitionuID_PART_TABLE_TYPEu-%s already contains data, wipe first or forceudisku2Need a partitionable blockdev for reserved to workudparted -m -s -a optimal -- /dev/{0} mklabel gpt mkpart bcache-reserved 1M {1} mkpart bcache {1} 100%uerroru+Error creating bcache partitions on {0}: %su{0}2u6make-bcache --cache /dev/{0} --block {1} --wipe-bcacheu
 --bucket {0}uError creating cache {0}: %scS@s
t�tk	S(N(RR(((s7/usr/lib/python2.7/site-packages/salt/modules/bcache.pyRdsu7Cache {0} seemingly created OK, but FS did not activateN(RRRRt_bdevt_devbaset__salt__tgetRt_sizesR,t_wipeR R%R!R*RR(RtreservedR0R3R2R1Rtudevt_tblocktbucketR4((s7/usr/lib/python2.7/site-packages/salt/modules/bcache.pyt
cache_makesZ	0$
				
c	
K@sF|d
krt�}nt|�}tg|j�D]'\}}|jd�s4||f^q4�}|r�d}xT|j�D]F\}}|t||g|ddjtj	j
||�|��7}q}W|dkSi}t|dtdtdt�}x$dD]}||kr�||=q�q�Wx|D]}|j
||�q#W|Sd
S(u�
    Show or update config of a bcache device.

    If no device is given, operate on the cache set itself.

    CLI example:

    .. code-block:: bash

        salt '*' bcache.config
        salt '*' bcache.config bcache1
        salt '*' bcache.config errors=panic journal_delay_ms=150
        salt '*' bcache.config bcache1 cache_mode=writeback writeback_percent=15

    :return: config or True/False
    u__iuwarnuFailed to update {0} with {1}tconfigt	internalstoptionsuother_rouinter_roN(uother_rouinter_ro(Rt_fspathR(tdictRt
startswithR'R RRtjoint_sysfs_parseRtupdate(	RtkwargstspathtkeytvaltupdatestendrestresultR#((s7/usr/lib/python2.7/site-packages/salt/modules/bcache.pytconfig_ns$@,


cC@slg}x�tjjjd�D]�\}}}x�|D]x}d|krJq2nx]tjjjdj|�dt�D]7\}	}
}d|
kro|jtjj|	��qoqoWq2WqWi}x*|D]"}t	|||||�||<q�Wt
�}
t�}|rhd}x>|D]6}||kr
||d|
kr@|d7}q@q
q
W|||d<|sh||}qhn|S(	u�
    Show the full status of the BCache system and optionally all it's involved devices

    CLI example:

    .. code-block:: bash

        salt '*' bcache.status
        salt '*' bcache.status stats=True
        salt '*' bcache.status internals=True alldevs=True

    :param stats: include statistics
    :param config: include settings
    :param internals: include internals
    :param superblock: include superblock
    u/sys/block/ubcacheu/sys/block/{0}tfollowlinksiucacheiuattached_backing_devices(RRRRR RtappendRR
tdeviceRR6(tstatsRBRCt
superblockRtbdevsR>tlinksR?RLtsdirststatiiR$tcuuidtcdevtcountR((s7/usr/lib/python2.7/site-packages/salt/modules/bcache.pyR�s.%
4(
 		
cC@s�i}tt|�d$ddj|��s.tSt|d�r�t�|d<ddddd	g}|jtt|�||||��|jtt	�||||��|j|j
d
��n=t|�}|d$k	r�||d<ny#tjj
t|d��|d<Wntk
rnXt|�|d
<ddddg}t|�}|jt|||||��|j|j
d
��|dg}	|j
d�r�|	jd�n
|	jd�d|kr�|j
d�r�|	jd�q�|	jd�n|	|d<d|kr�d}
|d}x�|dD]�}d|kr`tj|
d|�}
|j
|�||
<q#|jdd�\}}
tj|
d|�}tj|
d|
�}
||kr�i||<n|j
|�|||
<q#W||d<n|r�|j
di�}|j|j
di��t|�r�x�|D]�}
|
jd�rmtjdd|
�}|j
|
�||<|}
n|
jd%�r'tjd|
d d�\}}||kr�i||<n|j
|
�|||<q'q'W||d!<q�n|ro|d"}xn|D]f}
|
jd�r�tjd|
d d�\}}||krDi||<n|j
|
�|||<q�q�W||d"<n|r�t|�|d#<n|S(&ue
    Check the state of a single bcache device

    CLI example:

    .. code-block:: bash

        salt '*' bcache.device bcache0
        salt '*' bcache.device /dev/sdc stats=True

    :param stats: include statistics
    :param settings: include all settings
    :param internals: include all internals
    :param superblock: include superblock info
    uerroru{0} is not a bcache fo any kindusetuuuidu
block_sizeubucket_sizeucache_available_percentucache_replacement_policyu	congestedubaseucacheudevubdevu
cache_modeurunningustateuwriteback_runningustoppeduwriteback_stoppedustatsu(stats|cache)_u/uiuinter_rouinter_rwuinternalu
internal[s/]*ubtreeu	writebacku_tmaxsplitu	internalsuconfigu
superblockN(ubtreeu	writeback(R'R(RR RRRRJRIREtpopRRR
RR6RTtretsubtsplitRRGtsuper_(RRVRBRCRWRQt	base_attrt	back_uuidt	base_pathtstatetreplretstatrestattrRMtstattinterrestnkeytmkeytskeyt	configres((s7/usr/lib/python2.7/site-packages/salt/modules/bcache.pyRU�s�$
%"
#







	




c	C@s�t|�}i}tdj|�ddj|��}|s@tSx�|j�D]�}|j�}|skqMngtjd|dd�D]}|j�^q�\}}|o�|s�qMnd}d|kr
gtjd|dd�D]}|j�^q�\}}|dd!}n|}yt	|�}Wn_t
k
r�yt|�}Wq�t
k
r�|d	krht}q�|d
kr�t}q�q�XnXtjd|dd�\}}||kr�i||<n|dk	r�||f|||<qM||||<qMW|S(
u�
    Read out BCache SuperBlock

    CLI example:

    .. code-block:: bash

        salt '*' bcache.device bcache0
        salt '*' bcache.device /dev/sdc

    ubcache-super-show {0}uerroru#Error reading superblock on {0}: %su[\s]+R_iu i����uyesunou\.N(
R-R%R Rt
splitlineststripRaRcRtintRtfloatR(	RtretR"tlineRNRMtmvaltrvaltpkey((s7/usr/lib/python2.7/site-packages/salt/modules/bcache.pyRd:sD$77

	
cC@s4tjjtjj|��}tjj|�}|S(u(
    Basename of just about any dev
    (RRtrealpatht
expandvarsR
(R((s7/usr/lib/python2.7/site-packages/salt/modules/bcache.pyR7vscC@stjjdt|��S(uM
    Return /dev name of just about any dev
    :return: /dev/devicename
    u/dev(RRRHR7(R((s7/usr/lib/python2.7/site-packages/salt/modules/bcache.pyR-scC@s4t|�}tjdd|�}tjjd|�S(u%
    Full SysFS path of a device
    u^([vhs][a-z]+)([0-9]+)u\1/\1\2u/sys/block/(R7RaRbRRRH(R((s7/usr/lib/python2.7/site-packages/salt/modules/bcache.pyt_syspath�scC@sZ|dkrtd�}nt|�}|s1tSttjjtjj|d���SdS(uV
    Resolve a bcacheX or cache to a real dev
    :return: basename of bcache dev
    ucache0u../N(	RR+R(RR7RRR{RH(R((s7/usr/lib/python2.7/site-packages/salt/modules/bcache.pyR6�scC@stjjt|�d�S(u,
    Full SysFS path of a bcache device
    ubcache(RRRHR}(R((s7/usr/lib/python2.7/site-packages/salt/modules/bcache.pyR(�scC@s*t�}|stStjjd|�SdS(u(
    :return: path of active bcache
    u/sys/fs/bcache/N(RRRRRH(R\((s7/usr/lib/python2.7/site-packages/salt/modules/bcache.pyRE�s	cC@s0t�}|stSt||g|||�SdS(u7
    Simple wrapper to interface with bcache SysFS
    N(RERR'(tnametvaluetlog_lvltlog_msgtfspath((s7/usr/lib/python2.7/site-packages/salt/modules/bcache.pyR+�s	cC@stt|�|g|||�S(u=
    Simple wrapper to interface with backing devs SysFS
    (R'R((RR~RR�R�((s7/usr/lib/python2.7/site-packages/salt/modules/bcache.pyR�scC@swt|tj�r|g}ntdtjj|�|�}|rs|dk	rs|dk	rstjt	||�n|S(u7
    Simple wrapper with logging around sysfs.attr
    u
sysfs.attrN(
t
isinstanceRtstring_typesR8RRRHRRtLOG(R~RR�R�R"((s7/usr/lib/python2.7/site-packages/salt/modules/bcache.pyR'�scC@s`i}td|�}|d=g}|r?|jddg�n|rU|jd�n|rk|jd�ni}	x|D]}
g|	|
<qxWxe|dD]Y}|jd�r�d}
nd|kr�d}
nd}
|
|	kr�|	|
j|�q�q�WxP|d	D]D}|jd�rd}
nd}
|
|	kr�|	|
j|�q�q�W|dk	r�x;|	D]3}g|	|D]}||krk|^qk|	|<qZW||	d
<niddd
ddddgd6}
x�|
j�D]�\}}g}||	kr�q�nxN|D]F}x=|	j�D]/}||kr|j|�|j|�qqWq�W|	|c|7<q�Wg|	j�D]}|D]}|^qmqc}|jtd||��x�d D]�}||kr�i}x�||j	d�D]�}|j	dd�\}
}|j
�}yt|�}Wn5tk
r@yt
|�}WqAtk
r<qAXnX|||
j
�<q�W|||<q�q�W|s�xDd!D]9}||krstjd||�j�d||<qsqsWnx1d"D])}||kr�t||�||<q�q�Wi}xl|	j�D]^\}}i}x0|D](}||kr|j|�||<qqWt|�r�|||<q�q�W|S(#u>
    Helper function for parsing BCache's SysFS interface
    usysfs.interfacesuwuinter_rouinter_rwuconfigustatsuruinternalurwubaseuinternal/bset_tree_statsuwriteback_rate_debugumetadata_writtenunbucketsuwrittenuaverage_key_sizeubtree_cache_sizeu
sysfs.readupriority_statsu
u:iu
cache_modeucache_replacement_policyuerrorsu\[(.+)\]iurunninguwriteback_runningu	congestedN(uwriteback_rate_debuguinternal/bset_tree_statsupriority_stats(u
cache_modeucache_replacement_policyuerrors(urunninguwriteback_runningu	congested(R8textendRTRGRRtvaluestremoveRJRcRsRtRRuRatsearchtgroupstboolR`R(RReRVRBRCRDRQtintfstbinkeystbintfRMtintftsintftmodstmodttmodlisttfoundtmodtintflisttiflistt	bintflisttstrlisttlistresRwRNtsellisttboolkeytbresulttiftypetifres((s7/usr/lib/python2.7/site-packages/salt/modules/bcache.pyRI�s�
			
1


,



.

cC@s�y�t|t�s�tjd|�rFdttjdd|��}n4tjd|�rzdttjdd|��}nt|�}n|SWntk
r�dSXdS(u1
    Map Bcache's size strings to real bytes
    u[Kk]iuu[Mm]iNi(R�RtRaR�RuRbRR(tsize((s7/usr/lib/python2.7/site-packages/salt/modules/bcache.pyR.Cs""
cC@s�t|�}d}d}tddd
t|��}|jd|jdd��}|jd	|jd
d��}d|d||fS(u�
    Return neigh useless sizing info about a blockdev
    :return: (total size in blocks, blocksize, maximum discard size in bytes)
    uhw_sector_sizeuminimum_io_sizeuphysical_block_sizeulogical_block_sizeudiscard_max_bytesudiscard_max_hw_bytesu
sysfs.readusizeuqueue/hw_sector_sizeu../queue/hw_sector_sizeuqueue/discard_max_bytesu../queue/discard_max_bytestrooti(uhw_sector_sizeuminimum_io_sizeuphysical_block_sizeulogical_block_size(udiscard_max_bytesudiscard_max_hw_bytes(usizeuqueue/hw_sector_sizeu../queue/hw_sector_sizeuqueue/discard_max_bytesu../queue/discard_max_bytesN(R7R8R}R9R(Rtblock_sizest
discard_sizestsysfstdiscardR?((s7/usr/lib/python2.7/site-packages/salt/modules/bcache.pyR:Us
c	C@sId}t|�}t|�\}}}|dkrGtjd|�dS|sftjd|�d}n"ts�tjd�d}nd}dj|�}|dkrd}d	j||�}|t|d
|�7}|dj|d|�7}|t|d
|�7}n:|dkr?dj|�}|t|d
|�7}d}n|dkS(u.
    REALLY DESTRUCTIVE STUFF RIGHT AHEAD
    iu!Unable to read SysFS props for %su%s seems unable to discardudduSblkdiscard binary not available, properly wipe the dev manually for optimal resultsu
blkdiscarduError wiping {0}: %siu+dd if=/dev/zero of=/dev/{0} bs=1M count={1}uwarnu	 seek={0}iiublkdiscard /dev/{0}iNi(	R7R:RRRR)tHAS_BLKDISCARDR R%(	RRPR�R?R�twipertwipe_failmsgtblocksR4((s7/usr/lib/python2.7/site-packages/salt/modules/bcache.pyR;ws2	
		i
cC@sdd}x4||kr<tjd�|�r/tS|d7}q	W|dk	r`tjt||�ntS(ui
    Wait for lfunc to be True
    :return: True if lfunc succeeded within tries, False if it didn't
    iiN(ttimetsleepRRRR�R(tlfuncR�R�ttriesti((s7/usr/lib/python2.7/site-packages/salt/modules/bcache.pyR!�s
	cC@sgtd|�}|d|kr9|dr2|dStSn|dk	rctjt|||d�ntS(u�
    Simple wrapper around cmd.run_all
    log_msg can contain {0} for stderr
    :return: True or stdout, False if retcode wasn't exitcode
    ucmd.run_alluretcodeustdoutustderrN(R8RRRR�R(R4R�R�texitcodeR"((s7/usr/lib/python2.7/site-packages/salt/modules/bcache.pyR%�s
cC@s?|dkrtStg|j�D]}|r |^q �dkS(Ni(RRRR�(tresdictRN((s7/usr/lib/python2.7/site-packages/salt/modules/bcache.pyR*�s(;t__doc__t
__future__RRRtloggingRR�Ratsalt.extRtsalt.utils.pathRt	getLoggert__name__RtTRACEtDEBUGtINFOtWARNINGtERRORtCRITICALR�t__func_alias__RRRRR�R	RRRR&R,RRR5RARRRRURdR7R-R}R6R(RER+RR'RIR.R:R;R!R%R*(((s7/usr/lib/python2.7/site-packages/salt/modules/bcache.pyt<module>s`






	1!	#8],/q	<						q		"	(

Zerion Mini Shell 1.0