def connect(image):
    '''
    Activate nbd for an image file.

    CLI Example:

    .. code-block:: bash

        salt '*' qemu_nbd.connect /tmp/image.raw
    '''
    if not os.path.isfile(image):
        log.warning('Could not connect image: %s does not exist', image)
        return ''

    if salt.utils.path.which('sfdisk'):
        fdisk = 'sfdisk -d'
    else:
        fdisk = 'fdisk -l'
    __salt__['cmd.run']('modprobe nbd max_part=63')
    for nbd in glob.glob('/dev/nbd?'):
        if __salt__['cmd.retcode']('{0} {1}'.format(fdisk, nbd)):
            while True:
                # Sometimes nbd does not "take hold", loop until we can verify
                __salt__['cmd.run'](
                        'qemu-nbd -c {0} {1}'.format(nbd, image),
                        python_shell=False,
                        )
                if not __salt__['cmd.retcode']('{0} {1}'.format(fdisk, nbd)):
                    break
            return nbd
    log.warning('Could not connect image: %s', image)
    return ''
def connect ( image ) : if not os . path . isfile ( image ) : log . warning ( 'Could not connect image: %s does not exist' , image ) return '' if salt . utils . path . which ( 'sfdisk' ) : fdisk = 'sfdisk -d' else : fdisk = 'fdisk -l' __salt__ [ 'cmd.run' ] ( 'modprobe nbd max_part=63' ) for nbd in glob . glob ( '/dev/nbd?' ) : if __salt__ [ 'cmd.retcode' ] ( '{0} {1}' . format ( fdisk , nbd ) ) : while True : __salt__ [ 'cmd.run' ] ( 'qemu-nbd -c {0} {1}' . format ( nbd , image ) , python_shell = False , ) if not __salt__ [ 'cmd.retcode' ] ( '{0} {1}' . format ( fdisk , nbd ) ) : break return nbd log . warning ( 'Could not connect image: %s' , image ) return ''
Activate nbd for an image file.

    CLI Example:

    .. code-block:: bash

        salt '*' qemu_nbd.connect /tmp/image.raw