HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.109.1.lve.el8.x86_64 #1 SMP Thu Mar 5 20:23:46 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.30
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/utils/__pycache__/zfs.cpython-310.pyc
o

�N�gtL�@s�dZddlZddlZddlZddlZddlmZddlZddl	m
Zddlm
Z
ddlmZe�d�Zgd�Ze�e�Zd	d
�Zdd�ZdRdd�Zdd�Zdd�Zdd�ZdSdd�ZdTdd�Zedd��Zedd��Z 							dUd d!�Z!d"d#�Z"ed$d%��Z#ed&d'��Z$ed(d)��Z%d*d+�Z&d,d-�Z'd.d/�Z(d0d1�Z)d2d3�Z*d4d5�Z+d6d7�Z,dVd8d9�Z-d:d;�Z.d<d=�Z/dWd>d?�Z0dTd@dA�Z1dWdBdC�Z2dTdDdE�Z3dFdG�Z4dHdI�Z5dJdK�Z6						dXdLdM�Z7							dUdNdO�Z8dSdPdQ�Z9dS)Ya7
Utility functions for zfs

These functions are for dealing with type conversion and basic execution

:maintainer:    Jorge Schrauwen <sjorge@blackdot.be>
:maturity:      new
:depends:       salt.utils.stringutils, salt.ext, salt.module.cmdmod
:platform:      illumos,freebsd,linux

.. versionadded:: 2018.3.1

�N)�Number)�memoize)�OrderedDict)�to_numz,^(\d+|\d+(?=\d*)\.\d+)([KkMmGgTtPpEe][Bb]?)$)�K�M�G�T�P�EcCstjjj|ddd�dkS)z4
    Simple internal wrapper for cmdmod.retcode
    �quietT)�output_loglevel�ignore_retcoder)�salt�modules�cmdmod�retcode��cmd�r�B/opt/saltstack/salt/lib/python3.10/site-packages/salt/utils/zfs.py�_check_retcode!s��rcKs4d|vrd|d<d|vrd|d<tjjjdi|��S)z0
    Simple internal wrapper for cmdmod.run
    rTr
rNr)rrrZrun_all)�kwargsrrr�_exec+s
r� cCs8t|�|kr|d|d�|�||dd��g}|S)z=
    Merge values all values after X into the last value
    r�N)�len�join)�valuesZmerge_afterZ
merge_withrrr�_merge_last6s
�rcCs"d|vr|d|�d�d�}|S)z#
    Normalizes property names
    �@Nr)�index��namerrr�_property_normalize_nameBsr$cCshd}|�d�rd}|S|�d�rd}|S|dvrd}|S|dvr$d	}|S|d
vr,d}|S|dvr2d	}|S)z+
    Detect the datatype of a property
    �strzon | off�boolzyes | noZbool_alt)�<size>z
<size> | none�size)�<count>z<count> | nonez<guid>�numeric)ZsharenfsZsharesmbZcanmount)�versionZcopies)�
startswith)r#r�
value_typerrr�_property_detect_typeKs&

�	����r.cCsptt|t|t|����}t|d�|d<t|d|d�|d<t|d�|d<d|vr3t|d�|d<|d=|S)z 
    Create a property dict
    �propertyr#r�type�editZinherit)�dict�ziprrr$r.�	from_bool)�header�data�proprrr�_property_create_dict_sr8cCs�|si}i}|dd�dkr|d7}g}t|d�d�d�D]9}|����}|s)q|dd	kr2|}q|r:|d
dvr;qt||�}|||d<|d|vrT||||d<|d=q|S)
z/
    Parse output of zpool/zfs get command
    ���N�getz getr�stderr�
rr/r)�no�yesr#)r�split�lowerr8)r�aliasZ
propertiesZprop_hdrZ	prop_datar7rrr�_property_parse_cmdms*
rB�autoTcCs�|dvr|St�}|dkrt�}n
|dkr|�t��||vr%||dnd}|dkr<|dkr<t�|�d|��||�St�|�d|��|�S)	z2
    Internal magic for from_auto and to_auto
    )�to�from�zfsrCr0r%r(rD�_)�property_data_zpool�property_data_zfs�update�globals)�	directionr#�value�source�convert_to_humanZpropsr-rrr�_auto�srPcC�tjj�d�S)z6
    Return the path of the zfs binary if present
    rF�r�utils�path�whichrrrr�_zfs_cmd��rVcCrQ)z8
    Return the path of the zpool binary if present
    �zpoolrRrrrr�
_zpool_cmd�rWrYc	Cs�|dkrt�nt�|g}	|durg}|D]}
|	�|
�q|dur"i}t|�D]#}t||t�s6||g||<||D]}|	�|�|	�t|��q:q&|durPi}t|�D]}
|	�|dkr^dnd�|	�dj|
t|
||
ddd�d	��qT|durzi}t|�D]}
|	�d�|	�dj|
t|
||
ddd�d	��q~|dur�|dur�t|t�s�|g}t|t�s�|g}t	||�D]\}}|	�dj|t|||dd�d	��q�n|	�|�|dur�t|t�s�|g}|D]}|dur�q�|	�t|��q�d
�
|	�S)��
    Build and properly escape a zfs command

    .. note::

        Input is not considered safe and will be passed through
        to_auto(from_auto('input_here')), you do not need to do so
        your self first.

    rXNz-Oz-oz{key}={val}rFF)rNrO)�key�valr)rYrV�append�sorted�
isinstance�list�to_str�format�to_autor3r)rN�command�flags�opts�
property_name�property_value�filesystem_properties�pool_properties�targetr�flag�optr\Zfsoptr[Ztgtrrr�_command�s�
����
���

���


rncCs�d}tjj��rd}n@tjj��rtd�rd}n3tjj��r'tj�	d�r'd}n$tjj��r7tjj�
d�r7d}ntjj��rKtj�	d�rKtj�	d�rKd}tjj�
d�oS|duS)	z*
    Check the system for ZFS support
    FTzkldstat -q -m zfsz/sys/module/zfszzfs-fusez/Library/Extensions/zfs.kextz/dev/zfsrX)rrS�platformZis_sunosZ
is_freebsdrZis_linux�osrT�existsrUZ	is_darwin)Zon_supported_platformrrr�is_supported/s"
�
�
�rrcCs$tjj�d�}|rt|�d��SdS)z.
    Check if zpool-features is available
    �manz zpool-featuresF)rrSrTrUr)rsrrr�has_feature_flagsJsrtcCs`tt�dddddd��}gd�}dd	g}|D]
}d
ddd
�||<q|D]
}d
ddd
�||<q#|S)a$
    Return a dict of zpool properties

    .. note::

        Each property will have an entry with the following info:
            - edit : boolean - is this property editable after pool creation
            - type : str - either bool, bool_alt, size, numeric, or string
            - values : str - list of possible values

    .. warning::

        This data is probed from the output of 'zpool get' with some supplemental
        data that is hardcoded. There is no better way to get this information aside
        from reading the code.

    Zalloc�expand�replaceZ	listsnaps�frag)Z	allocatedZ
autoexpandZautoreplaceZ
listsnapshotsZ
fragmentation)zcapacity-allocz
capacity-freezoperations-readzoperations-writezbandwidth-readzbandwidth-write�read�writeZcksumZcapFr(r')r1r0rr*r))rBrY)Z
property_dataZzpool_size_extraZzpool_numeric_extrar7rrrrHTs0�����rHc
Cs"tt�ddddddddd	d
d�
�S)a_
    Return a dict of zfs properties

    .. note::

        Each property will have an entry with the following info:
            - edit : boolean - is this property editable after pool creation
            - inherit : boolean - is this property inheritable
            - type : str - either bool, bool_alt, size, numeric, or string
            - values : str - list of possible values

    .. warning::

        This data is probed from the output of 'zfs get' with some supplemental
        data that is hardcoded. There is no better way to get this information aside
        from reading the code.

    Zavailzlrefer.zlused.ZreferZvolblock�compressZrdonlyZrecsizeZ	refreservZreserv)
Z	availableZlogicalreferencedZlogicalusedZ
referencedZvolblocksize�compression�readonlyZ
recordsizeZrefreservationZreservation)rBrVrrrrrI�s��rIcCs |dkrd}|S|rt|�}|S)z+
    Convert zfs numeric to python int
    �noneN)�
str_to_num�rMrrr�from_numeric�s�r�cCst|�}|dur
d}|S)z+
    Convert python int to zfs numeric
    Nr})r�rrrr�
to_numeric�sr�cCs0|dvrd}|S|dvrd}|S|dkrd}|S)z)
    Convert zfs bool to python bool
    )�onr>T)�offr=Fr}Nrrrrrr4�s��r4cCst|�S)z-
    Convert zfs bool_alt to python bool
    )r4rrrr�
from_bool_alt�sr�cC�6t|�}t|t�r|rd}|Sd}|S|durd}|S)z0
    Convert python bool to zfs on/off bool
    r�r�Nr})r4r_r&rrrr�to_bool��
��r�cCr�)z,
    Convert python to zfs yes/no value
    r>r=Nr})r�r_r&rrrr�to_bool_alt�r�r�cCs�t�t|��}|r6|�d���d}t|�d��}t�dt�	|�d�}||}t
|�|kr2t
|�}t|�S|dur>t|�}t|�S)zA
    Convert zfs size (human readable) to python int (bytes)
    �rr�N)�re_zfs_size�matchr%�group�upper�float�math�pow�zfs_sizer!�intr�)rMZ
match_sizeZv_unit�v_size�v_multiplierrrr�	from_size�s�r�cCs�t|�}|dur
d}t|t�rX|dkrX|rXtt�t�|d���}t�d|�}t|�|}|t|�kr>d�	|t
|d�}|SdD]}|�	|t
|d�}t|�dkrW|}|Sq@|S)z�
    Convert python int (bytes) to zfs size

    NOTE: http://src.illumos.org/source/xref/illumos-gate/usr/src/lib/pyzfs/common/util.py#114
    Nr}r��{:.0f}{}r)z{:.2f}{}z{:.1f}{}r��)r�r_rr�r��floor�logr�r�rbr�r)rMrOZv_powerr�Zv_size_floatZv_precisionr�rrr�to_sizes0
��
��r�cCsH|dkrd}|r"t|�}|�d�r|�d�r|dd�}|�dd�}|S)z;
    Decode zfs safe string (used for name, path, ...)
    r}N�"r����\")r%r,�endswithrvrrrr�from_str2sr�cCs@t|�}|r|�dd�}d|vrd|d}|S|durd}|S)z;
    Encode zfs safe string (used for name, path, ...)
    r�r�rNr})r�rvrrrrraAs�racCstd|||�S)z+
    Convert zfs value to python value
    rE�rP)r#rMrNrrr�	from_autoQsr�cCstd||||�S)z+
    Convert python value to zfs value
    rDr�)r#rMrNrOrrrrcXsrccCs&|��D]\}}t|||�||<q|S)zk
    Pass an entire dictionary to from_auto

    .. note::
        The key will be passed as the name

    )�itemsr�)rrNr#rMrrr�from_auto_dict_sr�cCs(|��D]
\}}t||||�||<q|S)zh
    Pass an entire dictionary to to_auto

    .. note::
        The key will be passed as the name
    )r�rc)rrNrOr#rMrrr�to_auto_dictmsr�cC�t|��d�dkS)z0
    Check if name is a valid snapshot name
    r r�r��countr"rrr�is_snapshotz�r�cCr�)z0
    Check if name is a valid bookmark name
    �#rr�r"rrr�is_bookmark�r�r�cCst|�o	t|�S)z<
    Check if name is a valid filesystem or volume name
    )r�r�r"rrr�
is_dataset�sr�cCstd||||||d|d�	S)rZrFN�rdrerfrgrhrirjrk�rn)rdrerfrgrhrirkrrr�zfs_command�s�r�cCstd||||||||d�	S)z�
    Build and properly escape a zpool command

    .. note::

        Input is not considered safe and will be passed through
        to_auto(from_auto('input_here')), you do not need to do so
        your self first.

    rXr�r�r�rrr�
zpool_command�s�r�cCs�t�}|r
|ddk||<|ddkr\g|d<|d��D],}|���d�r(n"|���d�r5|�dd�}|���d	�rB|�d
d�}|d�|�q|drYd�|d�|d<|S|d=|S)
af
    Parse the result of a zpool/zfs command

    .. note::

        Output on failure is rather predictable.
        - retcode > 0
        - each 'error' is a line on stderr
        - optional 'Usage:' block under those with hits

        We simple check those and return a OrderedDict were
        we set label = True|False and error = error_messages

    rr�errorr;zusage:zuse '-f'z-fz
force=Truezuse '-r'z-rzrecursive=Truer<)r�
splitlinesr@r,rvr]r)�resZlabel�retr�rrr�parse_command_result�s$�r�)r)N)rCT)NNNNNNN)T)rC)NNNNNN):�__doc__�loggingr�rp�reZnumbersrZsalt.modules.cmdmodrZsalt.utils.decoratorsrZreal_memoizeZsalt.utils.odictrZsalt.utils.stringutilsrr~�compiler�r��	getLogger�__name__r�rrrr$r.r8rBrPrVrYrnrrrtrHrIr�r�r4r�r�r�r�r�r�rar�rcr�r�r�r�r�r�r�r�rrrr�<module>s�



	

*


�m
	
?
$


#




	
�"
�!