예제 #1
0
def mount_imgfn(imgname):
    """
    Link vm image with an nbd device.

    Parameters
    ----------
    imgname: str
        Full path of the image file.

    Returns
    -------
        str: Device name on success, raises an exception otherwise.
    """
    #
    # create nbd devices
    _logger.debug('__ Running mount image file %s.' % imgname)
    result_msg(msg='Load nbd')
    if not system_tools.create_nbd():
        raise OciMigrateException('Failed ot load nbd module')
    else:
        _logger.debug('nbd module loaded')
    #
    # find free nbd device
    result_msg(msg='Find free nbd device')
    devpath = system_tools.get_free_nbd()
    _logger.debug('Device %s is free.' % devpath)
    #
    # link img with first free nbd device
    result_msg(msg='Mount image %s' % imgname, result=True)
    _, nbcols = terminal_dimension()
    try:
        mountwait = ProgressBar(nbcols, 0.2, progress_chars=['mounting image'])
        mountwait.start()
        qemucmd = ['-c', devpath, imgname]
        pause_msg(qemucmd)
        qemunbd_ret = system_tools.exec_qemunbd(qemucmd)
        if qemunbd_ret == 0:
            time.sleep(5)
            _logger.debug('qemu-nbd %s succeeded' % qemucmd)
            return devpath
        else:
            _logger.critical('\n   Failed to create nbd devices: %d' %
                             qemunbd_ret)
            raise Exception('Failed to create nbd devices: %d' % qemunbd_ret)
    except Exception as e:
        _logger.critical('\n   Something wrong with creating nbd devices: %s' %
                         str(e))
        raise OciMigrateException('Unable to create nbd devices: %s' % str(e))
    finally:
        if system_tools.is_thread_running(mountwait):
            mountwait.stop()
예제 #2
0
def unmount_imgfn(devname):
    """
    Unlink a device.

    Parameters
    ----------
    devname: str
        The device name

    Returns
    -------
        bool: True on succes, raise an exception otherwise.
    """
    _logger.debug('__ Unmount %s' % devname)
    try:
        #
        # release device
        qemucmd = ['-d', devname]
        pause_msg(qemucmd)
        qemunbd_ret = system_tools.exec_qemunbd(qemucmd)
        if qemunbd_ret == 0:
            _logger.debug('qemu-nbd %s succeeded: %d' % (qemucmd, qemunbd_ret))
        else:
            raise Exception('%s returned %d' % (qemucmd, qemunbd_ret))
        #
        # clear lvm cache, if necessary.
        if system_tools.exec_pvscan(['--cache']):
            _logger.debug('lvm cache updated')
        else:
            _logger.error('  Failed to clear LVM cache.')
            raise OciMigrateException('Failed to clear LVM cache.')
        #
        # remove nbd module
        if not system_tools.rm_nbd():
            raise OciMigrateException('Failed to remove nbd module.')
        else:
            _logger.debug('Successfully removed nbd module.')
    except Exception as e:
        _logger.critical('   Something wrong with removing nbd devices: %s' %
                         str(e))
        raise OciMigrateException('\nSomething wrong with removing nbd '
                                  'devices: %s' % str(e))
    return True