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/states/__pycache__/virt.cpython-310.pyc
o

�N�g6@�@s(dZddlZddlZddlZddlZddlZddlZddlZddl	m
Z
mZzddlZdZ
Wney9dZ
YnwdZe�e�Zdd�Zd-d
d�Z				d.dd
�Zd/dd�Zd/dd�Z																												d0dd�Z																											d1dd�Zd.dd�Zd/dd�Zdd�Zd2dd�Z	d3dd�Z															d4d d!�Z															d4d"d#�Z hd$�Z!									d5d%d&�Z"									d5d'd(�Z#d6d)d*�Z$									d7d+d,�Z%dS)8z�
Manage virt
===========

For the key certificate this state uses the external pillar in the master to call
for the generation and signing of certificates for systems running libvirt:

.. code-block:: yaml

    libvirt_keys:
      virt.keys
�N)�CommandExecutionError�SaltInvocationErrorTFZvirtcCsdtvrtSdS)z9
    Only if virt module is available.

    :return:
    zvirt.node_info)Fzvirt module could not be loaded)�__salt__�__virtualname__�rr�D/opt/saltstack/salt/lib/python3.10/site-packages/salt/states/virt.py�__virtual__%sr�/etc/pkic
Ks|iddd�}i}|��D]\}}||d|��<q
tdddi|�}tj�|ddd	�tj�|dd
�tj�|ddd�tj�|dd�tj�|d
d�d�}|D]`}d|�d�}	|	|vrYqLtj�tj�||��spt�tj�||��tj�||�r�t	j
j�||d��}
t	j
j
�|
���||	kr�d|d|<Wd�n1s�wYqLd|d|<qL|ds�d|d<|Stdr�d|d<d|d<i|d<|S|dD],}t	j
j�||d��}
|
�t	j
j
�|d|�d���Wd�n1s�wYq�d|d<|S)a�
    Manage libvirt keys.

    name
        The name variable used to track the execution

    basepath
        Defaults to ``/etc/pki``, this is the root location used for libvirt
        keys on the hypervisor

    The following parameters are optional:

        country
            The country that the certificate should use.  Defaults to US.

        .. versionadded:: 2018.3.0

        state
            The state that the certificate should use.  Defaults to Utah.

        .. versionadded:: 2018.3.0

        locality
            The locality that the certificate should use.
            Defaults to Salt Lake City.

        .. versionadded:: 2018.3.0

        organization
            The organization that the certificate should use.
            Defaults to Salted.

        .. versionadded:: 2018.3.0

        expiration_days
            The number of days that the certificate should be valid for.
            Defaults to 365 days (1 year)

        .. versionadded:: 2018.3.0

    T���name�changes�result�commentzext_pillar_virt.z
pillar.ext�libvirt�_Zprivatez
serverkey.pemzservercert.pemz
clientkey.pemzclientcert.pem�CAz
cacert.pem)Z	serverkeyZ
servercertZ	clientkeyZ
clientcertZcacertzlibvirt.z.pem�r�updater
N�newzAll keys are correctr�testrz"Libvirt keys are set to be updatedzw+zUpdated libvirt certs and keys)�itemsr�os�path�join�exists�dirname�makedirs�isfile�salt�utils�filesZfopenZstringutilsZ
to_unicode�read�__opts__�writeZto_str)rZbasepath�kwargs�retZ
pillar_kwargs�key�valueZpillar�pathsZp_keyZfp_rrr�keys0sP*������"��r*cKs\tdsdnd}	|i|	dd�}
t�td�|�}t�}t�}
t�}|D]e}zFd}|dur9td|�}|�|�|k}|red}tds[td|��|f|||d	�|��}t|t�r[|d
}|�d|||i�n|�|�Wq#t	j
y�}z|
�|t|�d��WYd}~q#d}~ww|s�|
o�t|�|
d
<d|
d<|
r�d|
i|
d<|
S||i|
d<||
d<|
S)a�
    Helper to call the virt functions. Wildcards supported.

    :param domain: the domain to apply the function on. Can contain wildcards.
    :param function: virt function to call
    :param section: key for the changed domains in the return changes dictionary
    :param comment: comment to return
    :param state: the expected final state of the VM. If None the VM state won't be checked.
    :return: the salt state return
    rTNr
r�virt.list_domains�
virt.vm_statezvirt.��
connection�username�passwordr�domain�r1ZissuerzNo changes had happenedr�ignoredr
)
r#�fnmatch�filterr�list�get�
isinstance�dict�appendr�libvirtError�str�bool)r1�function�sectionr�stater.r/r0r%rr&Ztargeted_domainsZchanged_domains�ignored_domainsZnoaction_domainsZtargeted_domainZ
action_needed�domain_state�response�errrrr�
_virt_call�sT���

� ���rEc
Cst|dddd|||d�S)a�
    Stops a VM by shutting it down nicely.

    .. versionadded:: 2016.3.0

    :param connection: libvirt connection URI, overriding defaults

        .. versionadded:: 2019.2.0
    :param username: username to connect with, overriding defaults

        .. versionadded:: 2019.2.0
    :param password: password to connect with, overriding defaults

        .. versionadded:: 2019.2.0

    .. code-block:: yaml

        domain_name:
          virt.stopped
    �shutdown�stoppedzMachine has been shut down�r@r.r/r0�rE�rr.r/r0rrrrG�s�rGc
Cst|dddd|||d�S)a�
    Stops a VM by power off.

    .. versionadded:: 2016.3.0

    :param connection: libvirt connection URI, overriding defaults

        .. versionadded:: 2019.2.0
    :param username: username to connect with, overriding defaults

        .. versionadded:: 2019.2.0
    :param password: password to connect with, overriding defaults

        .. versionadded:: 2019.2.0

    .. code-block:: yaml

        domain_name:
          virt.stopped
    �stop�	unpowered�Machine has been powered offrFrHrIrJrrr�powered_off�s�rNc 
Cs\|itdsdnddd�}�z|td|
||d�vr�td|fid	|�d
|�d|�d|�d
|�d|�d|�d|�d|
�d|�d|�d|�d|�d|�d|�dtd�d|�d|�d|�d|�d|�d|��}||d|<|�d�s�i|d<d |�d!�|d"<d|d#<W|S|�d$�r�d |�d%�|d"<W|Sd |�d&�|d"<W|Stds�td'|fid	|�d
|�d(|�d)|�d*|�d+|�d|�d,|�d|�d|�d-|	�d.|
�d/|�d0|�d|
�d|�d|�d|�d|�d|�d|�d1d2�d|�d|�d|�d|�d|��ddi|d|<d |�d3�|d"<W|Stj�y-}zt|�|d"<d2|d#<WYd}~|Sd}~ww)4a1
    Starts an existing guest, or defines and starts a new VM with specified arguments.

    .. versionadded:: 3001

    :param name: name of the virtual machine to run
    :param cpu:
        Number of virtual CPUs to assign to the virtual machine or a dictionary with detailed information to configure
        cpu model and topology, numa node tuning, cpu tuning and iothreads allocation. The structure of the dictionary is
        documented in :ref:`init-cpu-def`.

        .. code-block:: yaml

             cpu:
               placement: static
               cpuset: 0-11
               current: 5
               maximum: 12
               vcpus:
                 0:
                   enabled: 'yes'
                   hotpluggable: 'no'
                   order: 1
                 1:
                   enabled: 'no'
                   hotpluggable: 'yes'
               match: minimum
               mode: custom
               check: full
               vendor: Intel
               model:
                 name: core2duo
                 fallback: allow
                 vendor_id: GenuineIntel
               topology:
                 sockets: 1
                 cores: 12
                 threads: 1
               cache:
                 level: 3
                 mode: emulate
               feature:
                 policy: optional
                 name: lahf_lm
               numa:
                 0:
                    cpus: 0-3
                    memory: 1g
                    discard: 'yes'
                    distances:
                      0: 10     # sibling id : value
                      1: 21
                      2: 31
                      3: 41
                 1:
                    cpus: 4-6
                    memory: 1g
                    memAccess: shared
                    distances:
                      0: 21
                      1: 10
                      2: 21
                      3: 31
               tuning:
                    vcpupin:
                      0: 1-4,^2  # vcpuid : cpuset
                      1: 0,1
                      2: 2,3
                      3: 0,4
                    emulatorpin: 1-3
                    iothreadpin:
                      1: 5,6    # iothread id: cpuset
                      2: 7,8
                    shares: 2048
                    period: 1000000
                    quota: -1
                    global_period: 1000000
                    global_quota: -1
                    emulator_period: 1000000
                    emulator_quota: -1
                    iothread_period: 1000000
                    iothread_quota: -1
                    vcpusched:
                      - scheduler: fifo
                        priority: 1
                      - scheduler: fifo
                        priority: 2
                        vcpus: 1-3
                      - scheduler: rr
                        priority: 3
                        vcpus: 4
                    iothreadsched:
                      - scheduler: batch
                        iothreads: 2
                    emulatorsched:
                      scheduler: idle
                    cachetune:
                      0-3:      # vcpus set
                        0:      # cache id
                          level: 3
                          type: both
                          size: 4
                        1:
                          level: 3
                          type: both
                          size: 6
                        monitor:
                          1: 3
                          0-3: 3
                      4-5:
                        monitor:
                          4: 3  # vcpus: level
                          5: 3
                    memorytune:
                      0-3:      # vcpus set
                        0: 60   # node id: bandwidth
                      4-5:
                        0: 60
               iothreads: 4

        .. versionadded:: 3003

    :param mem: Amount of memory to allocate to the virtual machine in MiB. Since 3002, a dictionary can be used to
        contain detailed configuration which support memory allocation or tuning. Supported parameters are ``boot``,
        ``current``, ``max``, ``slots``, ``hard_limit``, ``soft_limit``, ``swap_hard_limit``, ``min_guarantee``,
        ``hugepages`` ,  ``nosharepages``, ``locked``, ``source``, ``access``, ``allocation`` and ``discard``. The structure
        of the dictionary is documented in  :ref:`init-mem-def`. Both decimal and binary base are supported. Detail unit
        specification is documented  in :ref:`virt-units`. Please note that the value for ``slots`` must be an integer.

        .. code-block:: yaml

            boot: 1g
            current: 1g
            max: 1g
            slots: 10
            hard_limit: 1024
            soft_limit: 512m
            swap_hard_limit: 1g
            min_guarantee: 512mib
            hugepages:
              - size: 2m
              - nodeset: 0-2
                size: 1g
              - nodeset: 3
                size: 2g
            nosharepages: True
            locked: True
            source: file
            access: shared
            allocation: immediate
            discard: True

        .. versionchanged:: 3002

    :param vm_type: force virtual machine type for the new VM. The default value is taken from
        the host capabilities. This could be useful for example to use ``'qemu'`` type instead
        of the ``'kvm'`` one.
    :param disk_profile:
        Name of the disk profile to use for the new virtual machine
    :param disks:
        List of disk to create for the new virtual machine.
        See :ref:`init-disk-def` for more details on the items on this list.
    :param nic_profile:
        Name of the network interfaces profile to use for the new virtual machine
    :param interfaces:
        List of network interfaces to create for the new virtual machine.
        See :ref:`init-nic-def` for more details on the items on this list.
    :param graphics:
        Graphics device to create for the new virtual machine.
        See :ref:`init-graphics-def` for more details on this dictionary
    :param saltenv:
        Fileserver environment (Default: ``'base'``).
        See :mod:`cp module for more details <salt.modules.cp>`
    :param seed: ``True`` to seed the disk image. Only used when the ``image`` parameter is provided.
                 (Default: ``True``)
    :param install: install salt minion if absent (Default: ``True``)
    :param pub_key: public key to seed with (Default: ``None``)
    :param priv_key: public key to seed with (Default: ``None``)
    :param seed_cmd: Salt command to execute to seed the image. (Default: ``'seed.apply'``)
    :param connection: libvirt connection URI, overriding defaults
    :param username: username to connect with, overriding defaults
    :param password: password to connect with, overriding defaults
    :param os_type:
        type of virtualization as found in the ``//os/type`` element of the libvirt definition.
        The default value is taken from the host capabilities, with a preference for ``hvm``.
        Only used when creating a new virtual machine.
    :param arch:
        architecture of the virtual machine. The default value is taken from the host capabilities,
        but ``x86_64`` is prefed over ``i686``. Only used when creating a new virtual machine.

    :param boot:
        Specifies kernel, initial ramdisk and kernel command line parameters for the virtual machine.
        This is an optional parameter, all of the keys are optional within the dictionary.

        Refer to :ref:`init-boot-def` for the complete boot parameters description.

        To update any boot parameters, specify the new path for each. To remove any boot parameters,
        pass a None object, for instance: 'kernel': ``None``.

        .. versionadded:: 3000

    :param boot_dev:
        Space separated list of devices to boot from sorted by decreasing priority.
        Values can be ``hd``, ``fd``, ``cdrom`` or ``network``.

        By default, the value will ``"hd"``.

        .. versionadded:: 3002

    :param numatune:
        The optional numatune element provides details of how to tune the performance of a NUMA host via controlling NUMA
        policy for domain process. The optional ``memory`` element specifies how to allocate memory for the domain process
        on a NUMA host. ``memnode`` elements can specify memory allocation policies per each guest NUMA node. The definition
        used in the dictionary can be found at :ref:`init-cpu-def`.

        .. versionadded:: 3003

        .. code-block:: python

            {
                'memory': {'mode': 'strict', 'nodeset': '0-11'},
                'memnodes': {0: {'mode': 'strict', 'nodeset': 1}, 1: {'mode': 'preferred', 'nodeset': 2}}
            }

    :param hypervisor_features:
        Enable or disable hypervisor-specific features on the virtual machine.

        .. versionadded:: 3003

        .. code-block:: yaml

            hypervisor_features:
              kvm-hint-dedicated: True

    :param clock:
        Configure the guest clock.
        The value is a dictionary with the following keys:

        adjustment
            time adjustment in seconds or ``reset``

        utc
            set to ``False`` to use the host local time as the guest clock. Defaults to ``True``.

        timezone
            synchronize the guest to the correspding timezone

        timers
            a dictionary associating the timer name with its configuration.
            This configuration is a dictionary with the properties ``track``, ``tickpolicy``,
            ``catchup``, ``frequency``, ``mode``, ``present``, ``slew``, ``threshold`` and ``limit``.
            See `libvirt time keeping documentation <https://libvirt.org/formatdomain.html#time-keeping>`_ for the possible values.

        .. versionadded:: 3003

        Set the clock to local time using an offset in seconds
        .. code-block:: yaml

            clock:
              adjustment: 3600
              utc: False

        Set the clock to a specific time zone:

        .. code-block:: yaml

            clock:
              timezone: CEST

    :param serials:
        Dictionary providing details on the serials connection to create. (Default: ``None``)
        See :ref:`init-chardevs-def` for more details on the possible values.

        .. versionadded:: 3003
    :param consoles:
        Dictionary providing details on the consoles device to create. (Default: ``None``)
        See :ref:`init-chardevs-def` for more details on the possible values.

        .. versionadded:: 3003

    :param stop_on_reboot:
        If set to ``True`` the guest will stop instead of rebooting.
        This is specially useful when creating a virtual machine with an installation cdrom or
        an autoinstallation needing a special first boot configuration.
        Defaults to ``False``

        .. versionadded:: 3003

    :param live:
        If set to ``False`` the changes will not be applied live to the running instance, but will
        only apply at the next start. Note that reboot will not take those changes.

        .. versionadded:: 3003

    :param host_devices:
        List of host devices to passthrough to the guest.
        The value is a list of device names as provided by the :py:func:`~salt.modules.virt.node_devices` function.
        (Default: ``None``)

        .. versionadded:: 3003

    :param autostart:
        If set to ``True`` the host will start the guest after boot.
        (Default: ``False``)

    .. rubric:: Example States

    Make sure a virtual machine called ``domain_name`` is defined:

    .. code-block:: yaml

        domain_name:
          virt.defined:
            - cpu: 2
            - mem: 2048
            - boot_dev: network hd
            - disk_profile: prod
            - disks:
              - name: system
                size: 8192
                overlay_image: True
                pool: default
                image: /path/to/image.qcow2
              - name: data
                size: 16834
            - nic_profile: prod
            - interfaces:
              - name: eth0
                mac: 01:23:45:67:89:AB
              - name: eth1
                type: network
                source: admin
            - graphics:
                type: spice
                listen:
                    type: address
                    address: 192.168.0.125

    rTNr
rr+r-zvirt.update�cpu�mem�disk_profile�disks�nic_profile�
interfaces�graphics�liver.r/r0�boot�numatune�serials�consoles�boot_dev�hypervisor_features�clock�stop_on_reboot�host_devices�	autostartr
�
definition�Domain �
 unchangedrr�errorsz% updated with live update(s) failures� updatedz	virt.init�os_type�archZ
hypervisor�diskZnic�seed�install�pub_key�priv_key�startF� defined)r#rr7rr;r<) rrOrP�vm_typerQrRrSrTrUrirjrkrlr.r/r0rfrgrWrXr[r\r]rYrZr^rVr_r`r&�statusrDrrr�definedsv����������	�
���
�����������

,
�*�(���������	�
���
�������������������rqc#
Cs�|}t|fid|�d|�d|�d|�d|�d|�d|�d|�d	|	�d
|
�d|�d|�d
|�d|�d|�d|�d|�d|�d|�d|�d|
�d|�d|�d|�d|�d|�d|��}tds_dnd}|ddusk|dr�|d �|i��d!d"�}zZtd#|�} | �|�d$kr�tds�td%||
||d&�d'|�d(�}!|d)�d*�s�d+�|d)�}!|!|d)<||d vr�i|d |<d|d |d,<W|S|s�d'|�d-�|d)<W|SW|Stjy�}"zt|"�|d)<d"|d<WYd}"~"|Sd}"~"ww|S).a�%
    Starts an existing guest, or defines and starts a new VM with specified arguments.

    .. versionadded:: 2016.3.0

    :param name: name of the virtual machine to run
    :param cpu:
        Number of virtual CPUs to assign to the virtual machine or a dictionary with detailed information to configure
        cpu model and topology, numa node tuning, cpu tuning and iothreads allocation. The structure of the dictionary is
        documented in :ref:`init-cpu-def`.

        To update any cpu parameters specify the new values to the corresponding tag. To remove any element or attribute,
        specify ``None`` object. Please note that ``None`` object is mapped to ``null`` in yaml, use ``null`` in sls file
        instead.
    :param mem: Amount of memory to allocate to the virtual machine in MiB. Since 3002, a dictionary can be used to
        contain detailed configuration which support memory allocation or tuning. Supported parameters are ``boot``,
        ``current``, ``max``, ``slots``, ``hard_limit``, ``soft_limit``, ``swap_hard_limit``, ``min_guarantee``,
        ``hugepages`` ,  ``nosharepages``, ``locked``, ``source``, ``access``, ``allocation`` and ``discard``. The structure
        of the dictionary is documented in  :ref:`init-mem-def`. Both decimal and binary base are supported. Detail unit
        specification is documented  in :ref:`virt-units`. Please note that the value for ``slots`` must be an integer.

        To remove any parameters, pass a None object, for instance: 'soft_limit': ``None``. Please note  that ``None``
        is mapped to ``null`` in sls file, pass ``null`` in sls file instead.

        .. code-block:: yaml

            - mem:
                hard_limit: null
                soft_limit: null

        .. versionchanged:: 3002
    :param vm_type: force virtual machine type for the new VM. The default value is taken from
        the host capabilities. This could be useful for example to use ``'qemu'`` type instead
        of the ``'kvm'`` one.

        .. versionadded:: 2019.2.0
    :param disk_profile:
        Name of the disk profile to use for the new virtual machine

        .. versionadded:: 2019.2.0
    :param disks:
        List of disk to create for the new virtual machine.
        See :ref:`init-disk-def` for more details on the items on this list.

        .. versionadded:: 2019.2.0
    :param nic_profile:
        Name of the network interfaces profile to use for the new virtual machine

        .. versionadded:: 2019.2.0
    :param interfaces:
        List of network interfaces to create for the new virtual machine.
        See :ref:`init-nic-def` for more details on the items on this list.

        .. versionadded:: 2019.2.0
    :param graphics:
        Graphics device to create for the new virtual machine.
        See :ref:`init-graphics-def` for more details on this dictionary

        .. versionadded:: 2019.2.0
    :param saltenv:
        Fileserver environment (Default: ``'base'``).
        See :mod:`cp module for more details <salt.modules.cp>`

        .. versionadded:: 2019.2.0
    :param seed: ``True`` to seed the disk image. Only used when the ``image`` parameter is provided.
                 (Default: ``True``)

        .. versionadded:: 2019.2.0
    :param install: install salt minion if absent (Default: ``True``)

        .. versionadded:: 2019.2.0
    :param pub_key: public key to seed with (Default: ``None``)

        .. versionadded:: 2019.2.0
    :param priv_key: public key to seed with (Default: ``None``)

        .. versionadded:: 2019.2.0
    :param seed_cmd: Salt command to execute to seed the image. (Default: ``'seed.apply'``)

        .. versionadded:: 2019.2.0
    :param connection: libvirt connection URI, overriding defaults

        .. versionadded:: 2019.2.0
    :param username: username to connect with, overriding defaults

        .. versionadded:: 2019.2.0
    :param password: password to connect with, overriding defaults

        .. versionadded:: 2019.2.0
    :param os_type:
        type of virtualization as found in the ``//os/type`` element of the libvirt definition.
        The default value is taken from the host capabilities, with a preference for ``hvm``.
        Only used when creating a new virtual machine.

        .. versionadded:: 3000
    :param arch:
        architecture of the virtual machine. The default value is taken from the host capabilities,
        but ``x86_64`` is prefed over ``i686``. Only used when creating a new virtual machine.

        .. versionadded:: 3000

    :param boot:
        Specifies kernel, initial ramdisk and kernel command line parameters for the virtual machine.
        This is an optional parameter, all of the keys are optional within the dictionary.

        Refer to :ref:`init-boot-def` for the complete boot parameters description.

        To update any boot parameters, specify the new path for each. To remove any boot parameters,
        pass a None object, for instance: 'kernel': ``None``.

        .. versionadded:: 3000
    :param serials:
        Dictionary providing details on the serials connection to create. (Default: ``None``)
        See :ref:`init-chardevs-def` for more details on the possible values.

        .. versionadded:: 3003
    :param consoles:
        Dictionary providing details on the consoles device to create. (Default: ``None``)
        See :ref:`init-chardevs-def` for more details on the possible values.

        .. versionadded:: 3003

    :param boot_dev:
        Space separated list of devices to boot from sorted by decreasing priority.
        Values can be ``hd``, ``fd``, ``cdrom`` or ``network``.

        By default, the value will ``"hd"``.

        .. versionadded:: 3002

    :param numatune:
        The optional numatune element provides details of how to tune the performance of a NUMA host via controlling NUMA
        policy for domain process. The optional ``memory`` element specifies how to allocate memory for the domain process
        on a NUMA host. ``memnode`` elements can specify memory allocation policies per each guest NUMA node. The definition
        used in the dictionary can be found at :ref:`init-cpu-def`.

        To update any numatune parameters, specify the new value. To remove any ``numatune`` parameters, pass a None object,
        for instance: 'numatune': ``None``. Please note that ``None`` is mapped to ``null`` in sls file, pass ``null`` in
        sls file instead.

        .. versionadded:: 3003

    :param stop_on_reboot:
        If set to ``True`` the guest will stop instead of rebooting.
        This is specially useful when creating a virtual machine with an installation cdrom or
        an autoinstallation needing a special first boot configuration.
        Defaults to ``False``

        .. versionadded:: 3003

    :param hypervisor_features:
        Enable or disable hypervisor-specific features on the virtual machine.

        .. versionadded:: 3003

        .. code-block:: yaml

            hypervisor_features:
              kvm-hint-dedicated: True

    :param clock:
        Configure the guest clock.
        The value is a dictionary with the following keys:

        adjustment
            time adjustment in seconds or ``reset``

        utc
            set to ``False`` to use the host local time as the guest clock. Defaults to ``True``.

        timezone
            synchronize the guest to the correspding timezone

        timers
            a dictionary associating the timer name with its configuration.
            This configuration is a dictionary with the properties ``track``, ``tickpolicy``,
            ``catchup``, ``frequency``, ``mode``, ``present``, ``slew``, ``threshold`` and ``limit``.
            See `libvirt time keeping documentation <https://libvirt.org/formatdomain.html#time-keeping>`_ for the possible values.

        .. versionadded:: 3003

        Set the clock to local time using an offset in seconds
        .. code-block:: yaml

            clock:
              adjustment: 3600
              utc: False

        Set the clock to a specific time zone:

        .. code-block:: yaml

            clock:
              timezone: CEST

    :param host_devices:
        List of host devices to passthrough to the guest.
        The value is a list of device names as provided by the :py:func:`~salt.modules.virt.node_devices` function.
        (Default: ``None``)

        .. versionadded:: 3003

    :param autostart:
        If set to ``True`` the host will start the guest after boot.
        (Default: ``False``)

    .. rubric:: Example States

    Make sure an already-defined virtual machine called ``domain_name`` is running:

    .. code-block:: yaml

        domain_name:
          virt.running

    Do the same, but define the virtual machine if needed:

    .. code-block:: yaml

        domain_name:
          virt.running:
            - cpu: 2
            - mem: 2048
            - disk_profile: prod
            - boot_dev: network hd
            - disks:
              - name: system
                size: 8192
                overlay_image: True
                pool: default
                image: /path/to/image.qcow2
              - name: data
                size: 16834
            - nic_profile: prod
            - interfaces:
              - name: eth0
                mac: 01:23:45:67:89:AB
              - name: eth1
                type: network
                source: admin
            - graphics:
                type: spice
                listen:
                    type: address
                    address: 192.168.0.125

    rOrProrQrRrSrTrUrirjrkrlrfrgrWr[rXr\r]r^r.r/r0rYrZr_r`rTNrr
raFr,�runningz
virt.startr-rbz startedrZ	unchanged�{} and started�startedz exists and is running)	rqr#r7r�endswith�formatrr;r<)#rrOrProrQrRrSrTrUrirjrkrlr.r/r0rfrgrWr[rXr\r]rYrZr^r_r`Zmerged_disksr&r�changedrBrrDrrrrr�s���������	�
���
�����������������	�����rrc
Cst|ddd||||d�S)aR
    Takes a snapshot of a particular VM or by a UNIX-style wildcard.

    .. versionadded:: 2016.3.0

    :param connection: libvirt connection URI, overriding defaults

        .. versionadded:: 2019.2.0
    :param username: username to connect with, overriding defaults

        .. versionadded:: 2019.2.0
    :param password: password to connect with, overriding defaults

        .. versionadded:: 2019.2.0

    .. code-block:: yaml

        domain_name:
          virt.snapshot:
            - suffix: periodic

        domain*:
          virt.snapshot:
            - suffix: periodic
    �snapshot�savedzSnapshot has been taken)�suffixr.r/r0rI)rrzr.r/r0rrrrx*s�rxc	Cst|ddd|||d�S)a}
    Reboots VMs

    .. versionadded:: 2016.3.0

    :param name:

    :param connection: libvirt connection URI, overriding defaults

        .. versionadded:: 2019.2.0
    :param username: username to connect with, overriding defaults

        .. versionadded:: 2019.2.0
    :param password: password to connect with, overriding defaults

        .. versionadded:: 2019.2.0
    Zreboot�rebootedzMachine has been rebootedr-rIrJrrrr{Rs�r{cCst|ddd�S)z�
    .. deprecated:: 2016.3.0
       Use :py:func:`~salt.modules.virt.powered_off` instead.

    Stops a VM by power off.

    .. versionadded:: 2016.3.0

    .. code-block:: yaml

        domain_name:
          virt.stopped
    rKrLrMrI)rrrrrLpsrLcCst|ddd|d�S)ao
    .. deprecated:: 2016.3.0
       Use :py:func:`~salt.modules.virt.snapshot` instead.

    Takes a snapshot of a particular VM or by a UNIX-style wildcard.

    .. versionadded:: 2016.3.0

    .. code-block:: yaml

        domain_name:
          virt.saved:
            - suffix: periodic

        domain*:
          virt.saved:
            - suffix: periodic
    rxryzSnapshots has been taken)rzrI)rrzrrrry�s
�ryc	Cs�|iddd�}z�t�td�|�}|sd|�d�|d<W|St�}t|�dkr.d	t�i|d
<|D]R}i}ztd|||d�}||d	|d
d�}Wn"tyk}zt|�dkra|�|t|�d��WYd}~nd}~wwt|�dkr~|r}|d
d	�|�q0||d
<t|�t|�k|d<|dr�d�t|�dkr�dp�d�|d<|r�||d
d<|d
d	s�|d
�	d	�W|SW|St
jy�}z
t|�|d<WYd}~|Sd}~wty�}z
t|�|d<WYd}~|Sd}~ww)aD
    .. deprecated:: 2016.3.0

    Reverts to the particular snapshot.

    .. versionadded:: 2016.3.0

    .. code-block:: yaml

        domain_name:
          virt.reverted:
            - cleanup: True

        domain_name_1:
          virt.reverted:
            - snapshot: snapshot_name
            - cleanup: False
    Fr
rr+zNo domains found for criteria "�"r��revertedr
zvirt.revert_snapshot)rx�cleanup�deleted)r1�currentr�r2NrzDomain{} has been reverted�sr3)r4r5rr6�lenrr:r<rv�poprr;)	rrxrr&ZdomainsrAr1rrDrrrr~�sf(��
�����������r~cCsb|itdsdnddd�}�ztd|||	|
d�}|r�||r�||dr(|p0||do0|}td	|||f|||||||
||||d||	|
d
��}|r�tdsktd	|||f|||||||
||||d||	|
d
��|rodnd}d
|��|d|<d|�d|��|d<nld|�d�|d<d|d<n_|}tds�td|||fid|�d|�d|�d|�d|�d|�d|
�d|�d|�d|�d|�dd�d d�d!|�d"|	�d#|
��|r�d$|d|<d|�d%�|d<nd&|d|<d|�d'�|d<|�rtd�std(||�rd)nd*||	|
d+�W|SW|SW|Stj�y0}zd|d<|��|d<WYd}~|Sd}~ww),a
    Defines a new network with specified arguments.

    :param name: Network name
    :param bridge: Bridge name
    :param forward: Forward mode(bridge, router, nat)

        .. versionchanged:: 3003
           a ``None`` value creates an isolated network with no forwarding at all

    :param vport: Virtualport type (Default: ``'None'``)
        The value can also be a dictionary with ``type`` and ``parameters`` keys.
        The ``parameters`` value is a dictionary of virtual port parameters.

        .. code-block:: yaml

          - vport:
              type: openvswitch
              parameters:
                interfaceid: 09b11c53-8b5c-4eeb-8f00-d84eaa0aaa4f

        .. versionchanged:: 3003
           possible dictionary value

    :param tag: Vlan tag (Default: ``'None'``)
        The value can also be a dictionary with the ``tags`` and optional ``trunk`` keys.
        ``trunk`` is a boolean value indicating whether to use VLAN trunking.
        ``tags`` is a list of dictionaries with keys ``id`` and ``nativeMode``.
        The ``nativeMode`` value can be one of ``tagged`` or ``untagged``.

        .. code-block:: yaml

          - tag:
              trunk: True
              tags:
                - id: 42
                  nativeMode: untagged
                - id: 47

        .. versionchanged:: 3003
           possible dictionary value

    :param ipv4_config:
        IPv4 network configuration. See the
        :py:func:`virt.network_define <salt.modules.virt.network_define>`
        function corresponding parameter documentation
        for more details on this dictionary.
        (Default: None).
    :param ipv6_config:
        IPv6 network configuration. See the :py:func:`virt.network_define
        <salt.modules.virt.network_define>` function corresponding parameter documentation
        for more details on this dictionary.
        (Default: None).
    :param autostart: Network autostart (default ``'True'``)
    :param connection: libvirt connection URI, overriding defaults
    :param username: username to connect with, overriding defaults
    :param password: password to connect with, overriding defaults
    :param mtu: size of the Maximum Transmission Unit (MTU) of the network.
        (default ``None``)

        .. versionadded:: 3003

    :param domain: DNS domain name of the DHCP server.
        The value is a dictionary with a mandatory ``name`` property and an optional ``localOnly`` boolean one.
        (default ``None``)

        .. code-block:: yaml

          - domain:
              name: lab.acme.org
              localOnly: True

        .. versionadded:: 3003

    :param nat: addresses and ports to route in NAT forward mode.
        The value is a dictionary with optional keys ``address`` and ``port``.
        Both values are a dictionary with ``start`` and ``end`` values.
        (default ``None``)

        .. code-block:: yaml

          - forward: nat
          - nat:
              address:
                start: 1.2.3.4
                end: 1.2.3.10
              port:
                start: 500
                end: 1000

        .. versionadded:: 3003

    :param interfaces: whitespace separated list of network interfaces devices that can be used for this network.
        (default ``None``)

        .. code-block:: yaml

          - forward: passthrough
          - interfaces: "eth10 eth11 eth12"

        .. versionadded:: 3003

    :param addresses: whitespace separated list of addresses of PCI devices that can be used for this network in `hostdev` forward mode.
        (default ``None``)

        .. code-block:: yaml

          - forward: hostdev
          - interfaces: "0000:04:00.1 0000:e3:01.2"

        .. versionadded:: 3003

    :param physical_function: device name of the physical interface to use in ``hostdev`` forward mode.
        (default ``None``)

        .. code-block:: yaml

          - forward: hostdev
          - physical_function: "eth0"

        .. versionadded:: 3003

    :param dns: virtual network DNS configuration
        The value is a dictionary described in :ref:`net-define-dns`.
        (default ``None``)

        .. code-block:: yaml

          - dns:
              forwarders:
                - domain: example.com
                  addr: 192.168.1.1
                - addr: 8.8.8.8
                - domain: www.example.com
              txt:
                example.com: "v=spf1 a -all"
                _http.tcp.example.com: "name=value,paper=A4"
              hosts:
                192.168.1.2:
                  - mirror.acme.lab
                  - test.acme.lab
              srvs:
                - name: ldap
                  protocol: tcp
                  domain: ldapserver.example.com
                  target: .
                  port: 389
                  priority: 1
                  weight: 10

        .. versionadded:: 3003

    .. versionadded:: 3001

    .. code-block:: yaml

        network_name:
          virt.network_defined

    .. code-block:: yaml

        network_name:
          virt.network_defined:
            - bridge: main
            - forward: bridge
            - vport: openvswitch
            - tag: 180
            - autostart: True

    .. code-block:: yaml

        network_name:
          virt.network_defined:
            - bridge: natted
            - forward: nat
            - ipv4_config:
                cidr: 192.168.42.0/24
                dhcp_ranges:
                  - start: 192.168.42.10
                    end: 192.168.42.25
                  - start: 192.168.42.100
                    end: 192.168.42.150
            - autostart: True

    rTNr
r�virt.network_infor-r`zvirt.network_update)�vport�tag�ipv4_config�ipv6_config�mtur1�natrT�	addresses�physical_function�dnsrr.r/r0F�, autostart flag changedzNetwork updatedr
zNetwork rerrcrzvirt.network_definer�r�r�r�r�r1r�rTr�r�r�rmr.r/r0z%Network defined, marked for autostart� defined, marked for autostart�Network definedrnzvirt.network_set_autostart�on�offrH)r#rrr;�get_error_message)r�bridge�forwardr�r�r�r�r`r.r/r0r�r1r�rTr�r�r�r&�info�needs_autostart�needs_update�actionrDrrr�network_defined�sO��������
������	�
���
�������
��
����r�cCsZt|||f|||||||
|||||||	|
d��}||dvo&|d|�d�}tds-dnd}|ddus9|dr�zStd|||	|
d	�}|�|i��d
d�rXd�|d
�|d
<n.tdsftd|||	|
d	�d}||dvrwd�|d|�}||d|<d�|d
�|d
<||d<W|Stjy�}zd|d<|��|d
<WYd}~|Sd}~ww|S)a�
    Defines and starts a new network with specified arguments.

    :param name: Network name
    :param bridge: Bridge name
    :param forward: Forward mode(bridge, router, nat)

        .. versionchanged:: 3003
           a ``None`` value creates an isolated network with no forwarding at all

    :param vport: Virtualport type (Default: ``'None'``)
        The value can also be a dictionary with ``type`` and ``parameters`` keys.
        The ``parameters`` value is a dictionary of virtual port parameters.

        .. code-block:: yaml

          - vport:
              type: openvswitch
              parameters:
                interfaceid: 09b11c53-8b5c-4eeb-8f00-d84eaa0aaa4f

        .. versionchanged:: 3003
           possible dictionary value

    :param tag: Vlan tag (Default: ``'None'``)
        The value can also be a dictionary with the ``tags`` and optional ``trunk`` keys.
        ``trunk`` is a boolean value indicating whether to use VLAN trunking.
        ``tags`` is a list of dictionaries with keys ``id`` and ``nativeMode``.
        The ``nativeMode`` value can be one of ``tagged`` or ``untagged``.

        .. code-block:: yaml

          - tag:
              trunk: True
              tags:
                - id: 42
                  nativeMode: untagged
                - id: 47

        .. versionchanged:: 3003
           possible dictionary value

    :param ipv4_config:
        IPv4 network configuration. See the :py:func`virt.network_define
        <salt.modules.virt.network_define>` function corresponding parameter documentation
        for more details on this dictionary.
        (Default: None).

        .. versionadded:: 3000
    :param ipv6_config:
        IPv6 network configuration. See the :py:func`virt.network_define
        <salt.modules.virt.network_define>` function corresponding parameter documentation
        for more details on this dictionary.
        (Default: None).

        .. versionadded:: 3000
    :param autostart: Network autostart (default ``'True'``)
    :param connection: libvirt connection URI, overriding defaults

        .. versionadded:: 2019.2.0
    :param username: username to connect with, overriding defaults

        .. versionadded:: 2019.2.0
    :param password: password to connect with, overriding defaults

        .. versionadded:: 2019.2.0
    :param mtu: size of the Maximum Transmission Unit (MTU) of the network.
        (default ``None``)

        .. versionadded:: 3003

    :param domain: DNS domain name of the DHCP server.
        The value is a dictionary with a mandatory ``name`` property and an optional ``localOnly`` boolean one.
        (default ``None``)

        .. code-block:: yaml

          - domain:
              name: lab.acme.org
              localOnly: True

        .. versionadded:: 3003

    :param nat: addresses and ports to route in NAT forward mode.
        The value is a dictionary with optional keys ``address`` and ``port``.
        Both values are a dictionary with ``start`` and ``end`` values.
        (default ``None``)

        .. code-block:: yaml

          - forward: nat
          - nat:
              address:
                start: 1.2.3.4
                end: 1.2.3.10
              port:
                start: 500
                end: 1000

        .. versionadded:: 3003

    :param interfaces: whitespace separated list of network interfaces devices that can be used for this network.
        (default ``None``)

        .. code-block:: yaml

          - forward: passthrough
          - interfaces: "eth10 eth11 eth12"

        .. versionadded:: 3003

    :param addresses: whitespace separated list of addresses of PCI devices that can be used for this network in `hostdev` forward mode.
        (default ``None``)

        .. code-block:: yaml

          - forward: hostdev
          - interfaces: "0000:04:00.1 0000:e3:01.2"

        .. versionadded:: 3003

    :param physical_function: device name of the physical interface to use in ``hostdev`` forward mode.
        (default ``None``)

        .. code-block:: yaml

          - forward: hostdev
          - physical_function: "eth0"

        .. versionadded:: 3003

    :param dns: virtual network DNS configuration
        The value is a dictionary described in :ref:`net-define-dns`.
        (default ``None``)

        .. code-block:: yaml

          - dns:
              forwarders:
                - domain: example.com
                  addr: 192.168.1.1
                - addr: 8.8.8.8
                - domain: www.example.com
              txt:
                host.widgets.com.: "printer=lpr5"
                example.com.: "This domain name is reserved for use in documentation"
              hosts:
                192.168.1.2:
                  - mirror.acme.lab
                  - test.acme.lab
              srvs:
                - name: ldap
                  protocol: tcp
                  domain: ldapserver.example.com
                  target: .
                  port: 389
                  priority: 1
                  weight: 10

        .. versionadded:: 3003

    .. code-block:: yaml

        network_name:
          virt.network_running

    .. code-block:: yaml

        network_name:
          virt.network_running:
            - bridge: main
            - forward: bridge
            - vport: openvswitch
            - tag: 180
            - autostart: True

    .. code-block:: yaml

        network_name:
          virt.network_running:
            - bridge: natted
            - forward: nat
            - ipv4_config:
                cidr: 192.168.42.0/24
                dhcp_ranges:
                  - start: 192.168.42.10
                    end: 192.168.42.25
                  - start: 192.168.42.100
                    end: 192.168.42.150
            - autostart: True

    )r�r�r�r�r�r1r�rTr�r�r�r`r.r/r0r
r�rTNrr�r-ZactiveFz{} and is runningrzvirt.network_startzNetwork startedrs)	r��
startswithr#rr7rvrr;r�)rr�r�r�r�r�r�r`r.r/r0r�r1r�rTr�r�r�r&rqrr��changerDrrr�network_runningslU�����
���r�>�logical�vstoragerh�dir�zfs�netfs�fsc
Cs.|itdsdnddd�}
�z�td||||	d�}d}|�r||d	r'|p/||d	o/|}td
|f||||p;i�d�|pAi�d�|pGi�d
�|pMi�d�|pSi�d�|pYi�d�|p_i�d�|pei�d�d|||	d��}
|
�r
tds�td
|||||p�i�d�|p�i�d�|p�i�d
�|p�i�d�|p�i�d�|p�i�d�|p�i�d�|p�i�d�|||	d�d}||ddkr�|tvr�tds�ztd||||	d�Wntjy�}zt�d|���WYd}~nd}~wwd}|r�|�d�n|}d|��|
d|<d|�d|��|
d<n�d|�d �|
d<d|
d!<n�|}td�s�td"|fid#|�d$|�d%|�d&|�p5i�d��d'|�p>i�d��d(|�pGi�d
��d)|�pPi�d��d*|�pYi�d��d+|�pbi�d��d,|�pki�d��d-|�pti�d��d.|�d/d�d0|�d1|�d2|	��|tv�r�ztd||||	d�Wntj�y�}zt�d|���WYd}~nd}~ww|�r�d3|
d|<d|�d4�|
d<nd5|
d|<d|�d6�|
d<|�r�td�s�td7||�r�d8nd9|||	d:�W|
SW|
SW|
Stj�y}z|��|
d<d|
d!<WYd}~|
Sd}~ww);a�
    Defines a new pool with specified arguments.

    .. versionadded:: 3001

    :param ptype: libvirt pool type
    :param target: full path to the target device or folder. (Default: ``None``)
    :param permissions:
        target permissions. See :ref:`pool-define-permissions` for more details on this structure.
    :param source:
        dictionary containing keys matching the ``source_*`` parameters in function
        :func:`salt.modules.virt.pool_define`.
    :param transient:
        when set to ``True``, the pool will be automatically undefined after being stopped. (Default: ``False``)
    :param autostart:
        Whether to start the pool when booting the host. (Default: ``True``)
    :param start:
        When ``True``, define and start the pool, otherwise the pool will be left stopped.
    :param connection: libvirt connection URI, overriding defaults
    :param username: username to connect with, overriding defaults
    :param password: password to connect with, overriding defaults

    .. code-block:: yaml

        pool_name:
          virt.pool_defined:
            - ptype: netfs
            - target: /mnt/cifs
            - permissions:
                - mode: 0770
                - owner: 1000
                - group: 100
            - source:
                dir: samba_share
                hosts:
                  - one.example.com
                  - two.example.com
                format: cifs
            - autostart: True

    rTNr
r�virt.pool_infor-Fr`zvirt.pool_updateZdevicesr�Z	initiatorZadapter�hostsZauthrrv)�ptype�target�permissions�source_devices�
source_dir�source_initiator�source_adapter�source_hosts�source_auth�source_name�
source_formatrr.r/r0)r�r�r�r�r�r�r�r�r�r�r�r.r/r0r@rr�virt.pool_buildz(Failed to build libvirt storage pool: %sz, builtr��Pool updatedr
�Pool rerrcrzvirt.pool_definer�r�r�r�r�r�r�r�r�r�r��	transientrmr.r/r0z"Pool defined, marked for autostartr��Pool definedrnzvirt.pool_set_autostartr�r�rH)	r#rr7�BUILDABLE_POOL_TYPESrr;�log�warningr�)rr�r�r��sourcer�r`r.r/r0r&r�r�r�r�rDrrr�pool_defined/s:6�������
�����

��������	�
���
�����

����
��
����r�c
Cs�t||||||||||	d�
}
||
dvo|
d|�d�}||
dvo+|
d|�d�}tds2dnd}
|
ddus>|
dr�z�td	||||	d
�}d}|�|i��dd
�dk}|r�|r�d}tdsktd||||	d
�|tddhvr�tds�td||||	d
�d|��}nd}d}
|r�|s�|r�tds�td||||	d
�d|��}d}||
dvr�d�|
d�}d�|
d|�}|dkr�|�d|��|
d|<|�d|��|
d<|
|
d<W|
Stjy�}z|�	�|
d<d|
d<WYd}~|
Sd}~ww|
S)a�
    Defines and starts a new pool with specified arguments.

    .. versionadded:: 2019.2.0

    :param ptype: libvirt pool type
    :param target: full path to the target device or folder. (Default: ``None``)
    :param permissions:
        target permissions. See :ref:`pool-define-permissions` for more details on this structure.
    :param source:
        dictionary containing keys matching the ``source_*`` parameters in function
        :func:`salt.modules.virt.pool_define`.
    :param transient:
        when set to ``True``, the pool will be automatically undefined after being stopped. (Default: ``False``)
    :param autostart:
        Whether to start the pool when booting the host. (Default: ``True``)
    :param connection: libvirt connection URI, overriding defaults
    :param username: username to connect with, overriding defaults
    :param password: password to connect with, overriding defaults

    .. code-block:: yaml

        pool_name:
          virt.pool_running

    .. code-block:: yaml

        pool_name:
          virt.pool_running:
            - ptype: netfs
            - target: /mnt/cifs
            - permissions:
                - mode: 0770
                - owner: 1000
                - group: 100
            - source:
                dir: samba_share
                hosts:
                  - one.example.com
                  - two.example.com
                format: cifs
            - autostart: True

    )	r�r�r�r�r�r`r.r/r0r
r�r�rTNrr�r-rtr@rGrrZ	restarted�virt.pool_stoprhr�r�zbuilt, zalready runningzvirt.pool_startr�ZPoolz{},r� F)
r�r�r#rr7r�rvrr;r�)rr�r�r�r�r�r`r.r/r0r&rq�updatedrr�r�Z
is_runningrr�rDrrr�pool_running�s�8����
��

���r�c
CsN|iddd�}z�td||||d�}|r�d|dd<d|dd	<d|dd
<g|dd<g}||dd
kr�|r�gd�}||d|vr|td||||d�td||||d�}	|	D] }
td}tdsotd||
|||d�}|rz|dd�|
�qZn|�d�tds�td||||d�|dd<nd|dd<|r�gd�}||d|vr�tds�td||||d�|dd	<nd|dd	<n|�d�tds�td||||d�|dd
<n
d|dd
<d|d<|r�d�||dd�|��|d<W|SW|Sd|��|d<W|Stj�y&}
zd |
����|d<d|d<WYd}
~
|Sd}
~
ww)!a�
    Deletes a virtual storage pool.

    :param name: the name of the pool to delete.
    :param purge:
        if ``True``, the volumes contained in the pool will be deleted as well as the pool itself.
        Note that these will be lost for ever. If ``False`` the pool will simply be undefined.
        (Default: ``False``)
    :param connection: libvirt connection URI, overriding defaults
    :param username: username to connect with, overriding defaults
    :param password: password to connect with, overriding defaults

    In order to be purged a storage pool needs to be running to get the list of volumes to delete.

    Some libvirt storage drivers may not implement deleting, those actions are implemented on a
    best effort idea. In any case check the result's comment property to see if any of the action
    was unsupported.

    .. code-block:: yaml

        pool_name:
          uyuni_virt.pool_deleted:
            - purge: True

    .. versionadded:: 3000
    Tr
rr�r-Fr
rGr�Z	undefinedZdeleted_volumesr@rr)Ziscsiziscsi-directZmpathZscsi�typezvirt.pool_refreshzvirt.pool_list_volumesrzvirt.volume_deletezdeleting volumer�)r�r�r�r�r�r�zvirt.pool_deletez
deleting poolzvirt.pool_undefineNrz-Unsupported actions for pool of type "{}": {}z, rz!Storage pool could not be found: zFailed deleting pool: )rr#r:rvrrr;r�)r�purger.r/r0r&r�ZunsupportedZunsupported_volume_deleteZvolumesZvolumer�Zsupported_pool_deleterDrrr�pool_deleted�s�������
��
�
�	�
����r�cCs�|iddd�}td|	|
|d�}
||
vrtd|�d���td|||	|
|d��|i��|�}|rd	|d
<|�d�p:i}|�d�|pBi�d�koR|�d
�|pNi�d
�k}|r`|�d
�|krj|durjd|d<d|d
<|St|�d��t|�ddkr}d|d
<|Stdr�dnd|d<d}z2tds�td||||||||||	|
|d�d}d�|||�|d
<|�d|��ddd�i|d<W|Stjy�}z|��|d
<d|d<WYd}~|Sd}~ww)aI
    Ensure a disk volume is existing.

    :param pool: name of the pool containing the volume
    :param name: name of the volume
    :param size: capacity of the volume to define in MiB
    :param allocation: allocated size of the volume in MiB. Defaults to 0.
    :param format:
        volume format. The allowed values are depending on the pool type.
        Check the virt.pool_capabilities output for the possible values and the default.
    :param type:
        type of the volume. One of file, block, dir, network, netdiri, ploop or None.
        By default, the type is guessed by libvirt from the pool type.
    :param permissions:
        Permissions to set on the target folder. This is mostly used for filesystem-based
        pool types. See :ref:`pool-define-permissions` for more details on this structure.
    :param backing_store:
        dictionary describing a backing file for the volume. It must contain a ``path``
        property pointing to the base volume and a ``format`` property defining the format
        of the base volume.

        The base volume format will not be guessed for security reasons and is thus mandatory.
    :param nocow: disable COW for the volume.
    :param connection: libvirt connection URI, overriding defaults
    :param username: username to connect with, overriding defaults
    :param password: password to connect with, overriding defaults

    .. rubric:: CLI Example:

    Volume on ESX:

    .. code-block:: yaml

        esx_volume:
          virt.volume_defined:
            - pool: "[local-storage]"
            - name: myvm/myvm.vmdk
            - size: 8192

    QCow2 volume with backing file:

    .. code-block:: bash

        myvolume:
          virt.volume_defined:
            - pool: default
            - name: myvm.qcow2
            - format: qcow2
            - size: 8192
            - permissions:
                mode: '0775'
                owner: '123'
                group: '345'
            - backing_store:
                path: /path/to/base.img
                format: raw
            - nocow: True

    .. versionadded:: 3001
    Tr
rzvirt.list_poolsr-z
Storage pool z
 not existingzvirt.volume_infoszvolume is existingr�
backing_storerrvNFrzMA volume with the same name but different backing store or format is existingZcapacityiz@The capacity of the volume is different, but no resize performedrz	would be zvirt.volume_define)	�
allocationrvr�r�r��nocowr.r/r0zVolume {} {}defined in pool {}�/rq)�oldrr
)	rrr7�intr#rvrr;r�)�poolr�sizer�rvr�r�r�r�r.r/r0r&ZpoolsZ	vol_infosZbacking_store_infoZsame_backing_storeZtest_commentrDrrr�volume_defined	s~J�
������������r�)r	)NNNN)NNN)NNNNNNNNTTNNNNNNNNNNNNNNFTNF)NNNNNNNNTTNNNNNNNNNNNNNNFNF)N)NF)NNNNTNNNNNNNNNN)	NNNNFTNNN)FNNN)	rNNNNFNNN)&�__doc__r4�loggingrZsalt.utils.argsrZsalt.utils.filesZsalt.utils.stringutilsZsalt.utils.versionsZsalt.exceptionsrrrZHAS_LIBVIRT�ImportErrorr�	getLogger�__name__r�rr*rErGrNrqrrrxr{rLryr~r�r�r�r�r�r�r�rrrr�<module>sJ
�

^
�
?
"#
�O
�
W
(

�I
�D
�
�O
�
�