Exemplo n.º 1
0
def mount_partition(devname, mountpoint=None):
    """
    Create the mountpoint /mnt/<last part of device specification> and mount a
    partition on on this mountpoint.

    Parameters
    ----------
    devname: str
        The full path of the device.
    mountpoint: str
        The mountpoint, will be generated if not provided.

    Returns
    -------
        str: The mounted partition on Success, None otherwise.
    """
    _logger.debug('__ Mount partition %s.', devname)
    #
    # create mountpoint /mnt/<devname> if not specified.
    if mountpoint is None:
        mntpoint = migrate_data.loopback_root + '/' + devname.rsplit('/')[-1]
        _logger.debug('Loopback mountpoint: %s', mntpoint)
        try:
            if system_tools.exec_mkdir(mntpoint):
                _logger.debug('Mountpoint: %s created.', mntpoint)
        except Exception as e:
            _logger.critical('   Failed to create mountpoint %s: %s', mntpoint, str(e))
            raise OciMigrateException('Failed to create mountpoint %s:' % mntpoint) from e
    else:
        mntpoint = mountpoint
    #
    # actual mount
    cmd = ['mount', devname, mntpoint]
    pause_msg(cmd, pause_flag='_OCI_MOUNT')
    _, nbcols = terminal_dimension()
    try:
        mountpart = ProgressBar(nbcols, 0.2,
                                progress_chars=['mount %s' % devname])
        mountpart.start()
        _logger.debug('command: %s', cmd)
        cmdret = system_tools.run_call_cmd(cmd)
        if cmdret == 0:
            _logger.debug('%s mounted on %s.', devname, mntpoint)
            return mntpoint

        raise Exception('Mount %s failed: %d' % (devname, cmdret))
    except Exception as e:
        #
        # mount failed, need to remove mountpoint.
        _logger.critical('   Failed to mount %s, missing driver, filesystem corruption...: %s', devname, str(e))
        if mountpoint is None:
            if system_tools.exec_rmdir(mntpoint):
                _logger.debug('%s removed', mntpoint)
            else:
                _logger.critical('   Failed to remove mountpoint %s', mntpoint)
    finally:
        if system_tools.is_thread_running(mountpart):
            mountpart.stop()

    return None
Exemplo n.º 2
0
def unmount_part(devname):
    """
    Unmount a partition from mountpoint from /mnt/<last part of device
    specification> and remove the mountpoint.

    Parameters
    ----------
    devname: str
        The full path of the device.

    Returns
    -------
        bool
            True on success, False otherwise.
    """
    mntpoint = migrate_data.loopback_root + '/' + devname.rsplit('/')[-1]
    cmd = ['umount', mntpoint]
    _logger.debug('__ Running %s' % cmd)
    pause_msg(cmd)
    while True:
        try:
            _logger.debug('command: %s' % cmd)
            cmdret = system_tools.run_call_cmd(cmd)
            if cmdret == 0:
                _logger.debug('%s unmounted from %s' % (devname, mntpoint))
                #
                # remove mountpoint
                if system_tools.exec_rmdir(mntpoint):
                    _logger.debug('%s removed' % mntpoint)
                    return True
                else:
                    _logger.critical('   Failed to remove mountpoint %s' %
                                     mntpoint)
                    raise OciMigrateException(
                        'Failed to remove mountpoint %s' % mntpoint)
            else:
                _logger.critical('   Failed to unmount %s: %d' %
                                 (devname, cmdret))
                console_msg('Failed to unmount %s, error code %d.\n '
                            'Please verify before continuing.' %
                            (devname, cmdret))
                retry = read_yn(
                    'Something prevented to complete %s, please '
                    'verify and correct if possible. '
                    'Press Y to retry, N to ignore.',
                    waitenter=True)
                if not retry:
                    break
        except Exception as e:
            _logger.critical('   Failed to unmount %s: %s' % (devname, str(e)))
    return False