def _link_device(image, nbd):
    """Link image to device using loopback or nbd"""

    if nbd:
        device = _allocate_device()
        utils.execute('qemu-nbd', '-c', device, image, run_as_root=True)
        # NOTE(vish): this forks into another process, so give it a chance
        #             to set up before continuuing
        for i in xrange(FLAGS.timeout_nbd):
            if os.path.exists("/sys/block/%s/pid" % os.path.basename(device)):
                return device
            time.sleep(1)
        raise exception.Error(_('nbd device %s did not show up') % device)
    else:
        out, err = utils.execute('losetup', '--find', '--show', image,
                                 run_as_root=True)
        if err:
            raise exception.Error(_('Could not attach image to loopback: %s')
                                  % err)
        return out.strip()
dataset/ETHPy150Open nii-cloud/dodai-compute/nova/virt/disk.py/_link_device/original