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/modules/__pycache__/qemu_nbd.cpython-310.pyc
o

�N�g��@svdZddlZddlZddlZddlZddlZddlZddlZe�	e
�Zdd�Zdd�Z
d
dd�Zd
d	d
�Zdd�ZdS)z�
Qemu Command Wrapper

The qemu system comes with powerful tools, such as qemu-img and qemu-nbd which
are used here to build up kvm images.
�NcCstjj�d�r	dSdS)z:
    Only load if qemu-img and qemu-nbd are installed
    zqemu-nbdZqemu_nbd)FzWThe qemu_nbd execution module cannot be loaded: the qemu-nbd binary is not in the path.)�salt�utils�path�which�rr�I/opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/qemu_nbd.py�__virtual__srcCs�tj�|�st�d|�dStjj�d�rd}nd}tdd�t	�	d�D]-}td	|�d
|���rR	tdd|�d
|��d
d�td	|�d
|���sQ	|Sq3q%t�d|�dS)z�
    Activate nbd for an image file.

    CLI Example:

    .. code-block:: bash

        salt '*' qemu_nbd.connect /tmp/image.raw
    z*Could not connect image: %s does not exist�Zsfdiskz	sfdisk -dzfdisk -l�cmd.runzmodprobe nbd max_part=63z	/dev/nbd?zcmd.retcode� Tzqemu-nbd -c F�Zpython_shellzCould not connect image: %s)
�osr�isfile�log�warningrrr�__salt__�glob)�imageZfdisk�nbdrrr�connect"s*
���
rcCs�tdd|��dd�i}|durtj�t��dtj�|��}t�|�d��D]#}tj�|tj�|��}t�	d�td	||d
�}|d
urFq'|||<q'|S)z�
    Pass in the nbd connection device location, mount all partitions and return
    a dict of mount points

    CLI Example:

    .. code-block:: bash

        salt '*' qemu_nbd.mount /dev/nbd0
    r
z
partprobe FrNrzp*�zmount.mountT)
rr
r�join�tempfileZ
gettempdir�basenamer�time�sleep)r�root�ret�part�m_pt�mntrrr�mountDs�

r!cCst|�}|sdSt||�S)z�
    Mount the named image via qemu-nbd and return the mounted roots

    CLI Example:

    .. code-block:: bash

        salt '*' qemu_nbd.init /srv/image.qcow2
    r	)rr!)rrrrrr�init`s

r"cCs|i}t�}|��D]\}}td|�}|dur|||<|�|d|�d���q	|r,|S|D]
}tdd|��dd�q.|S)	aX
    Pass in the mnt dict returned from nbd_mount to unmount and disconnect
    the image from nbd. If all of the partitions are unmounted return an
    empty dict, otherwise return a dict containing the still mounted
    partitions

    CLI Example:

    .. code-block:: bash

        salt '*' qemu_nbd.clear '{"/mnt/foo": "/dev/nbd0p1"}'
    zmount.umountTN�pr
zqemu-nbd -d Fr)�set�itemsr�add�rindex)r rZnbdsr�devZmnt_retrrrr�clearps
r))N)�__doc__r�loggingr
rrZ
salt.cryptrZsalt.utils.path�	getLogger�__name__rrrr!r"r)rrrr�<module>s

"