HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.104.1.lve.el8.x86_64 #1 SMP Tue Feb 10 20:07:30 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.29
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/__pycache__/bcache.cpython-310.pyc
o

�N�g�r�@szdZddlZddlZddlZddlZddlZe�e�Z	ej
ejejej
ejejd�Zdddd�Zejj�d�duZd	d
�ZdEdd�ZdEd
d�ZdEdd�Zdd�ZdEdd�ZdFdd�Z	dGdd�ZdEdd�ZdHdd�ZdId d!�Z d"d#�Z!d$d%�Z"d&d'�Z#d(d)�Z$dEd*d+�Z%d,d-�Z&d.d/�Z'dJd0d1�Z(dJd2d3�Z)dJd4d5�Z*	dKd6d7�Z+d8d9�Z,d:d;�Z-d<d=�Z.dLd?d@�Z/dMdAdB�Z0dCdD�Z1dS)Na�
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

�N)Ztrace�debug�info�warn�errorZcrit�attach�config�super)�attach_�config_�super_�
blkdiscardcCstjj�d�duS)z1
    Only work when make-bcache is installed
    zmake-bcacheN)�salt�utils�path�which�rr�G/opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/bcache.py�__virtual__-�rcCsRz|durttjj�d��dddWStj�t|d��WSty(YdSw)a
    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

    N�/sys/fs/bcache/r��cacheF)	�listr
rr�os_walk�os�basename�_bcsys�	Exception��devrrr�uuid4s �r c	s�t���s
t�d��dS�dur/i}tdd���D]\�}d|vr(t��|�<q|r-|SdSt��}|rH|�krBt�d���dSt��sHdSt�d���t	�d	�d
d��d����s_dSt
��fd
d�d
��d��d��S)a
    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
    zNo cache to attach %s toFNT��alldevsrz2%s is already attached to bcache %s, doing nothingzAttaching %s to bcache %srrzError attaching z to bcache cst���kS�N�r r�rrrr�<lambda>}�zattach_.<locals>.<lambda>z received attach to bcache z, but did not comply)r �logr�status�itemsr	r�detachrr�_wait)r�res�data�bcacherr%rr	Ms@���r	cs��dur"i}tdd���D]\�}d|vrt��|�<q
|r |SdSt�d��t�dddd	����s5d
St�fdd�d��d
�d�S)a�
    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 its 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

    NTr!rzDetaching %sr+�goawayrzError detaching Fcst��duS�NFr$rrrrr&�r'zdetach.<locals>.<lambda>z$ received detach, but did not comply�,)r)r*r+r(rrr,)rr-r.rrrr+�s �
�r+cCs(tddd�sdStdd�dd�sdSd	S)
z�
    Trigger a start of the full bcache system through udev.

    CLI Example:

    .. code-block:: bash

        salt '*' bcache.start

    zudevadm triggerrzError starting bcache: %sFcS�
t�duSr1r$rrrrr&��
zstart.<locals>.<lambda>rz5Bcache system started, but no active cache set found.T)�_run_allr,rrrr�start�s�r6cs��dur&t�d��t�dddd����sdSt�fdd	�dd
��d�d�St�}|s2t�d
�dStt��s9dStdddd�sBdStdd	�ddd�S)a�
    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

    Nz;Stopping %s, device will only reappear after reregistering!�stopr0rzError stopping Fcstt���duSr1��_sysfs_attr�_bcpathrrrrr&��zstop.<locals>.<lambda>zDevice z
 did not stopr2zbcache already stopped?zError stopping cachecSs
t�duSr1r$rrrrr&�r4zCache did not stop)r(�warningrr,r �_alltruer+�_fssys)rrrrrr7�s&

�

r7�	writebackFTcst�}|st�d�dStt���r,|st�d��dSt��r&t��s&dSt��s,dSt���tt	d��}|dur@tt	d��}d�
|||��}|rN|d7}t|d	d
��d��sZdStdt��d	d
����shdSt�fdd�d	d��d��sxdS|r~t
��SdS)a�
    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)
    zNo bcache set foundFz;%s already contains a bcache. Wipe it manually or use force�
block_sizeN�bucket_sizez1make-bcache --block {} --bucket {} --{} --bdev {}� --wipe-bcacherzError creating backing device �: %szfs/bcache/registerz!Error registering backing device cstt���duSr1r8rrrrr&r;zback_make.<locals>.<lambda>zBacking device z did not registerT)r r(rr9r:r+r7�_devpath�	_size_mapr>�formatr5r,r	)r�
cache_mode�forcerrArr@�cmdrrr�	back_make�sR
���

�rJcCs~t�}|r|st�d|�dSt�}t|�}td|�}d|vs,|�dd�dkr6d|vr6|s6t�d	|�dS|durI|�dd�d
krIt�d�dSt|�\}}	}
|durX|
}d}|dur^|	}|rkt�sedSt	|�skdSt	|�sqdS|r�d�
||�}t|d
d|�d��s�dS|�d�}d|�d|�d�}|r�|d|��7}t|d
d|�d��s�dStdd�d
d|�d��s�dS|r�t
t��SdS)a�
    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
    z(BCache cache %s is already on the systemFzudev.envZ
ID_FS_TYPEZDEVTYPEN�	partitionZID_PART_TABLE_TYPEz-%s already contains data, wipe first or forceZdiskz2Need a partitionable blockdev for reserved to workzdparted -m -s -a optimal -- /dev/{0} mklabel gpt mkpart bcache-reserved 1M {1} mkpart bcache {1} 100%rz$Error creating bcache partitions on rC�2zmake-bcache --cache /dev/z	 --block rBz
 --bucket zError creating cache cSr3r1r$rrrrr&�r4zcache_make.<locals>.<lambda>zCache z. seemingly created OK, but FS did not activateT)r r(r�_bdev�_devbase�__salt__�get�_sizesr7�_wiperFr5r,r=r	)rZreservedrHr@rArrZudev�_�blockZbucketrIrrr�
cache_make'sj�
��

�
rUc	Ks�|durt�}nt|�}dd�|��D�}|r<d}|��D]\}}|t||g|ddtj�||��d|���7}q|dkSi}t|dddd	�}d
D]	}||vrQ||=qH|D]	}|�||�qT|S)a�
    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
    NcSs i|]\}}|�d�s||�qS)�__)�
startswith)�.0�key�valrrr�
<dictcomp>�s zconfig_.<locals>.<dictcomp>rrzFailed to update z with T)r�	internals�options)Zother_ro�inter_ro)	�_fspathr:r*r9rr�join�_sysfs_parse�update)	r�kwargs�spathZupdates�endresrYrZ�resultr.rrrr
�s.��r
cCs�g}tjj�d�D].\}}}|D]&}d|vrqtjjjd|��dd�D]\}	}
}d|
vr5|�tj�|	��q#qq	i}|D]}t|||||�||<q<t�}
t	�}|rtd}|D]}||krg||d|
krg|d7}qU|||d<|st||}|S)	a�
    Show the full status of the BCache system and optionally all its 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
    �/sys/block/r/F)�followlinksrrrZattached_backing_devices)
r
rrr�appendrr�devicer rM)�statsrr\�
superblockr"ZbdevsrSZlinksrTrdZsdirsZstatiir/�cuuidZcdev�countrrrrr)�s:
����	�r)cCs,i}tt|�dd|�d��sdSt|d�r@t�|d<gd�}|�tt|�||||��|�tt�||||��|�|�d��nqt|�}|durL||d	<z
tj	�
t|d
��|d
<Wn	tybYnwt|�|d<gd�}t|�}|�t|||||��|�|�d��|d
g}	|�d�r�|	�
d�n|	�
d�d|vr�|�d�r�|	�
d�n|	�
d�|	|d
<d|v�rd}
|d}|dD]<}d|vr�t�|
d|�}
|�|�||
<q�|�dd�\}}
t�|
d|�}t�|
d|
�}
||vr�i||<|�|�|||
<q�||d<|�rX|�di�}|�|�di��|�rX|D];}
|
�d��r0t�dd|
�}|�|
�||<|}
|
�d��rRtjd|
dd�\}}||v�rIi||<|�|
�|||<�q||d<|�r�|d}|D]%}
|
�d ��r�tjd|
dd�\}}||v�r|i||<|�|
�|||<�qa||d<|�r�t|�|d!<|S)"ae
    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
    Nrz is not a bcache fo any kindF�setr )r@rAZcache_available_percent�cache_replacement_policy�	congested�baserrZbdev)rG�running�state�writeback_runningrtrs�stoppedruZwriteback_stoppedrkz(stats|cache)_�/�rr^�inter_rw�internalz
internal[s/]*)Zbtreer?rS��maxsplitr\rr?rl)r9r:rr rbrar_�poprrrrrMri�re�sub�splitrWr)rrkrr\rlrf�	base_attrZ	back_uuid�	base_pathrtZreplre�statres�attrrY�statZinterresZnkeyZmkeyZskeyZ	configresrrrrj�s��

	�






�
�rjc	CsVt|�}i}td|��dd|�d��}|sdS|��D]�}|��}|s$qdd�tjd|d	d
�D�\}}|r7|s8qd}d|vrTd
d�tjd|d	d
�D�\}}|d	d�}n|}zt|�}Wn&ty�zt|�}Wnty|dkrwd}n|dkr}d}YnwYnwtjd|d	d
�\}}||vr�i||<|dur�||f|||<q||||<q|S)z�
    Read out BCache SuperBlock

    CLI Example:

    .. code-block:: bash

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

    zbcache-super-show rzError reading superblock on rCFcs��|]}|��VqdSr#��strip�rXrZrrr�	<genexpr>���zsuper_.<locals>.<genexpr>z[\s]+rr{N� csr�r#r�r�rrrr��r����ZyesT�noz\.)	rDr5�
splitlinesr�r~r��intr�float)	r�retr-�linerYrZZmvalZrvalZpkeyrrrrisT
�����	rcCs$tj�tj�|��}tj�|�}|S)z(
    Basename of just about any dev
    )rr�realpath�
expandvarsrrrrrrN�srNcCstj�dt|��S)zM
    Return /dev name of just about any dev
    :return: /dev/devicename
    z/dev)rrr`rNrrrrrD�srDcCs$t|�}t�dd|�}tj�d|�S)z%
    Full SysFS path of a device
    z^([vhs][a-z]+)([0-9]+)z\1/\1\2rg)rNr~rrrr`rrrr�_syspath�sr�cCs2|dur	td�}nt|�}|sdSttj�|��S)zV
    Resolve a bcacheX or cache to a real dev
    :return: basename of bcache dev
    NZcache0F)r>r:rNrr�dirnamerrrrrM�s
rMcCstj�t|�d�S)z,
    Full SysFS path of a bcache device
    r/)rrr`r�rrrrr:�rr:cCst�}|sdStj�d|�S)z(
    :return: path of active bcache
    Fr)r rrr`)rmrrrr_�sr_cCs t�}|sdSt||g|||�S)z7
    Simple wrapper to interface with bcache SysFS
    F)r_r9)�name�value�log_lvl�log_msg�fspathrrrr>�sr>cCstt|�|g|||�S)z=
    Simple wrapper to interface with backing devs SysFS
    r8)rr�r�r�r�rrrr�srcCsNt|t�r|g}tdtjj|�|�}|s%|dur%|dur%t�t||�|S)z7
    Simple wrapper with logging around sysfs.attr
    z
sysfs.attrN)�
isinstance�strrOrrr`r(�LOG)r�r�r�r�r-rrrr9�s
r9c
s�i}td|�}|d=g}|r|�ddg�|r|�d�|r$|�d�i}	|D]}
g|	|
<q(|dD]}|�d�r=d}
n	d|vrDd}
nd}
|
|	vrQ|	|
�|�q3|d	D]}|�d�r`d}
nd}
|
|	vrm|	|
�|�qV�d
ur�|	D]}�fdd�|	|D�|	|<qt�|	d
<dgd�i}|��D]-\}
}g}|
|	vr�q�|D]}|	��D]}||vr�|�|�|�|�q�q�|	|
|7<q�dd�|	��D�}|�td||��dD]K}||v�r i}||�d�D]6}|�dd�\}
}|�	�}zt
|�}Wnt�yzt|�}Wn
t�yYnwYnw|||
�	�<q�|||<q�|�s=dD]}||v�r;t
�d||���d||<�q&dD]}||v�rNt||�||<�q?i}|	��D] \}}i}|D]}||v�rl|�|�||<�q^|�ru|||<�qV|S)z>
    Helper function for parsing BCache's SysFS interface
    zsysfs.interfaces�wr^ryrrk�rrz�rwNcsg|]}|�vr|�qSrr)rXZsintf�r�rr�
<listcomp>9sz _sysfs_parse.<locals>.<listcomp>rr)�internal/bset_tree_stats�writeback_rate_debugZmetadata_writtenZnbucketsZwrittenZaverage_key_sizeZbtree_cache_sizecSsg|]	}|D]}|�qqSrr)rXZiflist�intfrrrr�Ts�
sysfs.read)r�r�Zpriority_stats�
�:r)rGrp�errorsz\[(.+)\]r)rsrurq)rO�extendrirWr*�values�removerbr�r�r�rr�r~�search�groups�boolr})rr�rkrr\r]rfZintfsZbinkeysZbintfrYr��modsZmodtZmodlist�found�modZintflistZ	bintflistZstrlistZlistresr�rZZsellistZboolkeyZbresultZiftypeZifresrr�rras�



�
��

��
����
�
�
��racCstz/t|t�s-t�d|�rdtt�dd|��}nt�d|�r)dtt�dd|��}t|�}|WSty9YdSw)z1
    Map Bcache's size strings to real bytes
    z[Kk]irxz[Mm]�N)r�r�r~r�r�rr)�sizerrrrE�s
�rEcCs^t|�}d}d}tddt|�d�}|�d|�dd��}|�d	|�d
d��}d|d||fS)
z�
    Return neigh useless sizing info about a blockdev
    :return: (total size in blocks, blocksize, maximum discard size in bytes)
    )Zhw_sector_sizeZminimum_io_sizeZphysical_block_sizeZlogical_block_size)Zdiscard_max_bytesZdiscard_max_hw_bytesr�)r��queue/hw_sector_size�../queue/hw_sector_size�queue/discard_max_bytes�../queue/discard_max_bytes)�rootr�r�Nr�r�ir�)rNrOr�rP)rZblock_sizesZ
discard_sizesZsysfs�discardrTrrrrQ�s���rQc	Cs�d}t|�}t|�\}}}|durt�d|�dS|s$t�d|�d}nts.t�d�d}nd}d|�d	�}|dkrcd
}d|�d|��}|t|d
|�7}|d|d|��7}|t|d
|�7}|dkS|dkrvd|��}|t|d
|�7}d}|dkS)z.
    REALLY DESTRUCTIVE STUFF RIGHT AHEAD
    rNz!Unable to read SysFS props for %sz%s seems unable to discard�ddzSblkdiscard binary not available, properly wipe the dev manually for optimal resultsrz
Error wiping rC�zdd if=/dev/zero of=/dev/z
 bs=1M count=rz seek=r�zblkdiscard /dev/r)rNrQr(rr<�HAS_BLKDISCARDr5)	rrer�rTr�ZwiperZwipe_failmsg�blocksrIrrrrR�s8��
rR�
cCsLd}||krt�d�|�rdS|d7}||ks|dur$t�t||�dS)zi
    Wait for lfunc to be True
    :return: True if lfunc succeeded within tries, False if it didn't
    rrTNF)�time�sleepr(r�)Zlfuncr�r�Ztries�irrrr,�s
�r,cCsNtd|�}|d|kr|dr|dSdS|dur%t�t|||d�dS)z�
    Simple wrapper around cmd.run_all
    log_msg can contain {0} for stderr
    :return: True or stdout, False if retcode wasn't exitcode
    zcmd.run_all�retcode�stdoutTN�stderrF)rOr(r�)rIr�r��exitcoder-rrrr5sr5cCs&|durdStdd�|��D��dkS)NTcSsg|]}|r|�qSrrr�rrrr�sz_alltrue.<locals>.<listcomp>r)�lenr�)Zresdictrrrr=sr=r#)r?FTN)NFNNT)FFFFF)FFFF)NNN)NFFFF)NNr�)NNr)2�__doc__�loggingrr~r�Zsalt.utils.pathr
�	getLogger�__name__r(ZTRACE�DEBUG�INFO�WARNING�ERROR�CRITICALr�Z__func_alias__rrrr�rr r	r+r6r7rJrUr
r)rjrrNrDr�rMr:r_r>rr9rarErQrRr,r5r=rrrr�<module>sb
�
�


6#

(D
�
h
0
1yA	




�~3
+